Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

feat(threads): add support for Threads in the Assistants #400

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions client/src/main/java/com/theokanning/openai/client/OpenAiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ public interface OpenAiApi {
@POST("/v1/threads")
Single<Thread> createThread(@Body ThreadRequest request);

@Headers({"OpenAI-Beta: assistants=v1"})
@GET("/v1/threads/{thread_id}")
Single<Thread> retrieveThread(@Path("thread_id") String threadId);

@Headers({"OpenAI-Beta: assistants=v1"})
@POST("/v1/threads/{thread_id}")
Single<Thread> modifyThread(@Path("thread_id") String threadId, @Body ThreadRequest request);

@Headers({"OpenAI-Beta: assistants=v1"})
@DELETE("/v1/threads/{thread_id}")
Single<DeleteResult> deleteThread(@Path("thread_id") String threadId);


@Headers({"OpenAI-Beta: assistants=v1"})
@POST("/v1/threads/{thread_id}/messages")
Single<Message> createMessage(@Path("thread_id") String threadId, @Body MessageRequest request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, String> 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());
}
}