From 99162e09d7d1f779639632a61eadc46ae4b4035a Mon Sep 17 00:00:00 2001 From: vacuityv Date: Wed, 15 Nov 2023 07:18:44 +0800 Subject: [PATCH] feat(threads): add support for Threads in the Assistants (#400) --- .../theokanning/openai/client/OpenAiApi.java | 13 ++++++++ .../openai/service/OpenAiService.java | 13 +++++++- .../openai/service/ThreadTest.java | 30 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java index c7bf2e10..cf09ff90 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -233,6 +233,19 @@ public interface OpenAiApi { @POST("/v1/threads") Single createThread(@Body ThreadRequest request); + @Headers({"OpenAI-Beta: assistants=v1"}) + @GET("/v1/threads/{thread_id}") + Single retrieveThread(@Path("thread_id") String threadId); + + @Headers({"OpenAI-Beta: assistants=v1"}) + @POST("/v1/threads/{thread_id}") + Single modifyThread(@Path("thread_id") String threadId, @Body ThreadRequest request); + + @Headers({"OpenAI-Beta: assistants=v1"}) + @DELETE("/v1/threads/{thread_id}") + Single deleteThread(@Path("thread_id") String threadId); + + @Headers({"OpenAI-Beta: assistants=v1"}) @POST("/v1/threads/{thread_id}/messages") Single createMessage(@Path("thread_id") String threadId, @Body MessageRequest request); diff --git a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java index a6bb9bc3..9c15522b 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -6,7 +6,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.node.TextNode; -import com.sun.org.apache.xpath.internal.operations.Mod; import com.theokanning.openai.*; import com.theokanning.openai.assistants.*; import com.theokanning.openai.audio.CreateSpeechRequest; @@ -408,6 +407,18 @@ public Thread createThread(ThreadRequest request) { return execute(api.createThread(request)); } + public Thread retrieveThread(String threadId) { + return execute(api.retrieveThread(threadId)); + } + + public Thread modifyThread(String threadId, ThreadRequest request) { + return execute(api.modifyThread(threadId, request)); + } + + public DeleteResult deleteThread(String threadId) { + return execute(api.deleteThread(threadId)); + } + public Message createMessage(String threadId, MessageRequest request) { return execute(api.createMessage(threadId, request)); } diff --git a/service/src/test/java/com/theokanning/openai/service/ThreadTest.java b/service/src/test/java/com/theokanning/openai/service/ThreadTest.java index 24ff2bb0..2c203790 100644 --- a/service/src/test/java/com/theokanning/openai/service/ThreadTest.java +++ b/service/src/test/java/com/theokanning/openai/service/ThreadTest.java @@ -1,5 +1,6 @@ package com.theokanning.openai.service; +import com.theokanning.openai.DeleteResult; import com.theokanning.openai.messages.MessageRequest; import com.theokanning.openai.threads.Thread; import com.theokanning.openai.threads.ThreadRequest; @@ -26,6 +27,35 @@ void createThread() { .build(); Thread thread = service.createThread(threadRequest); + System.out.println(thread.getId()); assertEquals("thread", thread.getObject()); } + + @Test + void retrieveThread() { + String threadId = "thread_K82pTg9kmhxpplGqalW6IHlc"; + + Thread thread = service.retrieveThread(threadId); + System.out.println(thread.getMetadata()); + assertEquals("thread", thread.getObject()); + } + + @Test + void modifyThread() { + String threadId = "thread_K82pTg9kmhxpplGqalW6IHlc"; + Map metadata = new HashMap<>(); + metadata.put("action", "modify"); + ThreadRequest threadRequest = ThreadRequest.builder() + .metadata(metadata) + .build(); + Thread thread = service.modifyThread(threadId, threadRequest); + assertEquals("thread", thread.getObject()); + } + + @Test + void deleteThread() { + String threadId = "thread_K82pTg9kmhxpplGqalW6IHlc"; + DeleteResult deleteResult = service.deleteThread(threadId); + assertEquals("thread.deleted", deleteResult.getObject()); + } } \ No newline at end of file