Skip to content

Commit

Permalink
#9 fix Message List 查询,缺少参数 [run_id]
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambdua committed May 9, 2024
1 parent 486ded5 commit 6cd9f39
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.theokanning.openai.assistants.message;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.theokanning.openai.ListSearchParameters;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author LiangTao
* @date 2024年05月09 16:05
**/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MessageListSearchParameters {
/**
* A limit on the number of objects to be returned.
* Limit can range between 1 and 100, and the default is 20
*/

Integer limit;

/**
* Sort order by the 'created_at' timestamp of the objects.
* 'asc' for ascending order and 'desc' for descending order.
*/
ListSearchParameters.Order order;

/**
* A cursor for use in pagination. after is an object ID that defines your place in the list.
* For instance, if you make a list request and receive 100 objects, ending with obj_foo,
* your subsequent call can include after=obj_foo in order to fetch the next page of the list
*/
String after;

/**
* A cursor for use in pagination. before is an object ID that defines your place in the list.
* For instance, if you make a list request and receive 100 objects, ending with obj_foo,
* your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
*/
String before;
/**
* Filter messages by the run ID that generated them.
*/
@JsonProperty("run_id")
private String runId;

public enum Order {
@JsonProperty("asc")
ASCENDING,

@JsonProperty("desc")
DESCENDING
}
}
8 changes: 5 additions & 3 deletions example/src/main/java/example/AssistantExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.theokanning.openai.assistants.StreamEvent;
import com.theokanning.openai.assistants.assistant.*;
import com.theokanning.openai.assistants.message.Message;
import com.theokanning.openai.assistants.message.MessageListSearchParameters;
import com.theokanning.openai.assistants.message.MessageRequest;
import com.theokanning.openai.assistants.run.*;
import com.theokanning.openai.assistants.run_step.RunStep;
Expand Down Expand Up @@ -94,7 +95,8 @@ static void assistantToolCall() {

System.out.println(retrievedRun);

OpenAiResponse<Message> response = service.listMessages(threadId, new ListSearchParameters());
OpenAiResponse<Message> response = service.listMessages(threadId, MessageListSearchParameters.builder()
.runId(retrievedRun.getId()).build());
List<Message> messages = response.getData();
messages.forEach(message -> {
System.out.println(message.getContent());
Expand Down Expand Up @@ -238,7 +240,7 @@ static void fileSearchExample() throws UnsupportedEncodingException {
for (RunStep runStep : runSteps) {
System.out.println(runStep.getStepDetails());
}
service.listMessages(threadId, new ListSearchParameters()).getData().forEach(message -> {
service.listMessages(threadId, new MessageListSearchParameters()).getData().forEach(message -> {
System.out.println(message.getContent());
});
}
Expand Down Expand Up @@ -277,7 +279,7 @@ static void codeInterpreterExample() {
for (RunStep runStep : runSteps) {
System.out.println(runStep.getStepDetails());
}
service.listMessages(threadId, new ListSearchParameters()).getData().forEach(message -> {
service.listMessages(threadId, new MessageListSearchParameters()).getData().forEach(message -> {
System.out.println(message.getContent());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.theokanning.openai.assistants.assistant.ModifyAssistantRequest;
import com.theokanning.openai.assistants.assistant.VectorStoreFileRequest;
import com.theokanning.openai.assistants.message.Message;
import com.theokanning.openai.assistants.message.MessageListSearchParameters;
import com.theokanning.openai.assistants.message.MessageRequest;
import com.theokanning.openai.assistants.message.ModifyMessageRequest;
import com.theokanning.openai.assistants.run.*;
Expand Down Expand Up @@ -465,7 +466,7 @@ public Message modifyMessage(String threadId, String messageId, ModifyMessageReq
return execute(api.modifyMessage(threadId, messageId, request));
}

public OpenAiResponse<Message> listMessages(String threadId, ListSearchParameters params) {
public OpenAiResponse<Message> listMessages(String threadId, MessageListSearchParameters params) {
Map<String, Object> queryParameters = mapper.convertValue(params, new TypeReference<Map<String, Object>>() {
});
return execute(api.listMessages(threadId, queryParameters));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.theokanning.openai.service;

import com.theokanning.openai.DeleteResult;
import com.theokanning.openai.ListSearchParameters;
import com.theokanning.openai.assistants.assistant.CodeInterpreterTool;
import com.theokanning.openai.assistants.message.Message;
import com.theokanning.openai.assistants.message.MessageListSearchParameters;
import com.theokanning.openai.assistants.message.MessageRequest;
import com.theokanning.openai.assistants.message.ModifyMessageRequest;
import com.theokanning.openai.assistants.thread.Attachment;
Expand Down Expand Up @@ -102,7 +102,7 @@ void listMessages() {
createTestMessage(separateThreadId);
createTestMessage(separateThreadId);
createTestMessage(separateThreadId);
List<Message> messages = service.listMessages(separateThreadId, new ListSearchParameters()).getData();
List<Message> messages = service.listMessages(separateThreadId, new MessageListSearchParameters()).getData();
assertEquals(3, messages.size());
DeleteResult deleteResult = service.deleteThread(separateThreadId);
assertTrue(deleteResult.isDeleted());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.theokanning.openai.service.assistants;

import com.theokanning.openai.ListSearchParameters;
import com.theokanning.openai.OpenAiResponse;
import com.theokanning.openai.assistants.assistant.Assistant;
import com.theokanning.openai.assistants.assistant.AssistantRequest;
import com.theokanning.openai.assistants.assistant.FunctionTool;
import com.theokanning.openai.assistants.message.Message;
import com.theokanning.openai.assistants.message.MessageListSearchParameters;
import com.theokanning.openai.assistants.message.MessageRequest;
import com.theokanning.openai.assistants.run.*;
import com.theokanning.openai.assistants.thread.Thread;
Expand Down Expand Up @@ -114,10 +114,15 @@ void weatherFunctionCallTest() {
retrievedRun = service.retrieveRun(threadId, run.getId());
}
assertEquals("completed", retrievedRun.getStatus());
OpenAiResponse<Message> response = service.listMessages(threadId, new ListSearchParameters());
OpenAiResponse<Message> response = service.listMessages(threadId, MessageListSearchParameters.builder()
.runId(retrievedRun.getId())
.limit(1)
.build()
);
List<Message> messages = response.getData();
assertNotNull(messages);
assertEquals(2, messages.size());
//test message query parameters is correct
assertEquals(1, messages.size());
}

}

0 comments on commit 6cd9f39

Please sign in to comment.