Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for File Search Customizations #148

Merged
merged 2 commits into from
Jun 5, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ public class ConversationV2Demo {
.model("gpt-4o")
.instructions("You are a skilled tutor on geo-politic topics.")
.tools(functionExecutor.getToolFunctions())
.tool(AssistantTool.FILE_SEARCH)
.tool(AssistantTool.fileSearch())
.toolResources(ToolResourceFull.builder()
.fileSearch(FileSearch.builder()
.vectorStoreId(vectorStoreId)
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>simple-openai</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
<packaging>jar</packaging>

<name>simple-openai</name>
Expand Down Expand Up @@ -61,16 +61,16 @@
<mockito.version>5.12.0</mockito.version>
<!-- Plugins Versions -->
<compiler.version>3.13.0</compiler.version>
<enforcer.version>3.4.1</enforcer.version>
<enforcer.version>3.5.0</enforcer.version>
<surefire.version>3.2.5</surefire.version>
<exec.version>3.3.0</exec.version>
<jacoco.version>0.8.12</jacoco.version>
<helper.version>3.6.0</helper.version>
<versions.version>2.16.2</versions.version>
<source.version>3.3.1</source.version>
<javadoc.version>3.6.3</javadoc.version>
<javadoc.version>3.7.0</javadoc.version>
<gpg.version>3.1.0</gpg.version>
<sonatype.version>1.6.13</sonatype.version>
<sonatype.version>1.7.0</sonatype.version>
<spotless.version>2.43.0</spotless.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@
import io.github.sashirestela.openai.domain.assistant.AssistantModifyRequest;
import io.github.sashirestela.openai.domain.assistant.AssistantRequest;
import io.github.sashirestela.openai.domain.assistant.AssistantTool;
import io.github.sashirestela.openai.domain.assistant.ChunkingStrategy;
import io.github.sashirestela.openai.domain.assistant.ChunkingStrategy.StaticChunking;
import io.github.sashirestela.openai.domain.assistant.ToolResourceFull;
import io.github.sashirestela.openai.domain.assistant.ToolResourceFull.FileSearch;
import io.github.sashirestela.openai.domain.assistant.ToolResourceFull.FileSearch.VectorStore;
import io.github.sashirestela.openai.domain.file.FileRequest.PurposeType;

import java.util.Map;

public class AssistantV2Demo extends AbstractDemo {

private FileDemo fileDemo;
private String fileId;
private String assistantId;

public AssistantV2Demo() {
fileDemo = new FileDemo();
var file = fileDemo.createFile("src/demo/resources/mistral-ai.txt", PurposeType.ASSISTANTS);
fileId = file.getId();
}

public void createAssistant() {
var assistantRequest = AssistantRequest.builder()
.model("gpt-4-turbo")
Expand All @@ -19,7 +33,18 @@ public void createAssistant() {
.instructions("You are a very kind assistant. If you cannot find correct facts to answer the "
+ "questions, you have to refer to the attached files or use the functions provided. "
+ "Finally, if you receive math questions, you must write and run code to answer them.")
.tool(AssistantTool.FILE_SEARCH)
.tool(AssistantTool.fileSearch(10))
.toolResources(ToolResourceFull.builder()
.fileSearch(FileSearch.builder()
.vectorStore(VectorStore.builder()
.fileId(fileId)
.chunkingStrategy(ChunkingStrategy.staticType(StaticChunking.builder()
.maxChunkSizeTokens(100)
.chunkOverlapTokens(50)
.build()))
.build())
.build())
.build())
.metadata(Map.of("user", "tester"))
.temperature(0.2)
.responseFormat("auto")
Expand Down Expand Up @@ -50,6 +75,15 @@ public void listAssistants() {
}

public void deleteAssistant() {
var assistant = openAI.assistants().getOne(assistantId).join();
var vectorStoreId = assistant.getToolResources().getFileSearch().getVectorStoreIds().get(0);

var deletedFile = fileDemo.deleteFile(fileId);
System.out.println(deletedFile);

var deletedVectorStore = openAI.vectorStores().delete(vectorStoreId).join();
System.out.println(deletedVectorStore);

var deletedAssistant = openAI.assistants().delete(assistantId).join();
System.out.println(deletedAssistant);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void prepareConversation() {
.model("gpt-4o")
.instructions("You are a skilled tutor on geo-politic topics.")
.tools(functionExecutor.getToolFunctions())
.tool(AssistantTool.FILE_SEARCH)
.tool(AssistantTool.fileSearch())
.toolResources(ToolResourceFull.builder()
.fileSearch(FileSearch.builder()
.vectorStoreId(vectorStoreId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void prepareDemo() {
+ "questions, you have to refer to the attached files or use the functions provided. "
+ "Finally, if you receive math questions, you must write and run code to answer them.")
.tools(functionExecutor.getToolFunctions())
.tool(AssistantTool.FILE_SEARCH)
.tool(AssistantTool.fileSearch())
.toolResources(ToolResourceFull.builder()
.fileSearch(FileSearch.builder()
.vectorStoreId(vectorStoreId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.github.sashirestela.openai.domain.assistant;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.github.sashirestela.openai.common.tool.Tool;
import io.github.sashirestela.openai.common.tool.ToolType;
import io.github.sashirestela.slimvalidator.constraints.Range;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
Expand All @@ -11,13 +14,42 @@
@Getter
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class AssistantTool extends Tool {

public static final AssistantTool CODE_INTERPRETER = new AssistantTool(ToolType.CODE_INTERPRETER, null);
public static final AssistantTool FILE_SEARCH = new AssistantTool(ToolType.FILE_SEARCH, null);
private FileSearch fileSearch;

private AssistantTool(ToolType type, FileSearch fileSearch) {
super(type, null);
this.fileSearch = fileSearch;
}

public static AssistantTool codeInterpreter() {
return new AssistantTool(ToolType.CODE_INTERPRETER, null);
}

public static AssistantTool fileSearch() {
return new AssistantTool(ToolType.FILE_SEARCH, null);
}

public static AssistantTool fileSearch(Integer maxNumResults) {
return new AssistantTool(ToolType.FILE_SEARCH, new FileSearch(maxNumResults));
}

@NoArgsConstructor
@Getter
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class FileSearch {

@Range(min = 1, max = 50)
private Integer maxNumResults;

public FileSearch(Integer maxNumResults) {
this.maxNumResults = maxNumResults;
}

private AssistantTool(ToolType type, ToolFunctionDef function) {
super(type, function);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.github.sashirestela.openai.domain.assistant;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.github.sashirestela.slimvalidator.constraints.Range;
import io.github.sashirestela.slimvalidator.constraints.Required;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
@JsonInclude(Include.NON_EMPTY)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ChunkingStrategy {

private ChunkingStrategyType type;

@JsonProperty("static")
private StaticChunking staticChunking;

private ChunkingStrategy(ChunkingStrategyType type, StaticChunking staticChunking) {
this.type = type;
this.staticChunking = staticChunking;
}

public static ChunkingStrategy autoType() {
return new ChunkingStrategy(ChunkingStrategyType.AUTO, null);
}

public static ChunkingStrategy staticType(StaticChunking staticChunking) {
return new ChunkingStrategy(ChunkingStrategyType.STATIC, staticChunking);
}

public enum ChunkingStrategyType {

@JsonProperty("auto")
AUTO,

@JsonProperty("static")
STATIC,

@JsonProperty("other")
OTHER;
}

@Getter
@Builder
@JsonInclude(Include.NON_EMPTY)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class StaticChunking {

@Required
@Range(min = 100, max = 4_096)
private Integer maxChunkSizeTokens;

@Required
@Range(max = 2_048)
private Integer chunkOverlapTokens;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.NoArgsConstructor;
import lombok.Singular;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

import java.util.List;

Expand Down Expand Up @@ -45,7 +46,7 @@ public static class CodeInterpreter {
@NoArgsConstructor
@Getter
@ToString
@Builder
@SuperBuilder
@JsonInclude(Include.NON_EMPTY)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class FileSearch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Singular;
import lombok.experimental.SuperBuilder;

import java.util.List;
import java.util.Map;
Expand All @@ -18,30 +19,14 @@
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ToolResourceFull {

private CodeInterpreter codeInterpreter;
private ToolResource.CodeInterpreter codeInterpreter;
private FileSearch fileSearch;

@Getter
@Builder
@SuperBuilder
@JsonInclude(Include.NON_EMPTY)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class CodeInterpreter {

@Singular
@Size(max = 20)
private List<String> fileIds;

}

@Getter
@Builder
@JsonInclude(Include.NON_EMPTY)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class FileSearch {

@Singular
@Size(max = 1)
private List<String> vectorStoreIds;
public static class FileSearch extends ToolResource.FileSearch {

@Singular
@Size(max = 1)
Expand All @@ -57,6 +42,8 @@ public static class VectorStore {
@Size(max = 10_000)
private List<String> fileIds;

private ChunkingStrategy chunkingStrategy;

@Size(max = 16)
private Map<String, String> metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class VectorStoreFile {
private String vectorStoreId;
private FileStatus status;
private LastError lastError;
private ChunkingStrategy chunkingStrategy;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public class VectorStoreFileBatchRequest {
@Size(min = 1, max = 500)
private List<String> fileIds;

private ChunkingStrategy chunkingStrategy;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public class VectorStoreFileRequest {
@Required
private String fileId;

private ChunkingStrategy chunkingStrategy;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class VectorStoreRequest {

private ExpiresAfter expiresAfter;

private ChunkingStrategy chunkingStrategy;

@Size(max = 16)
private Map<String, String> metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import io.github.sashirestela.openai.SimpleOpenAI;
import io.github.sashirestela.openai.common.ResponseFormat;
import io.github.sashirestela.openai.domain.DomainTestingHelper;
import io.github.sashirestela.openai.domain.assistant.ChunkingStrategy.StaticChunking;
import io.github.sashirestela.openai.domain.assistant.ToolResourceFull.FileSearch;
import io.github.sashirestela.openai.domain.assistant.ToolResourceFull.FileSearch.VectorStore;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -37,7 +40,18 @@ void testCreateAssistant() throws IOException {
.instructions("You are a very kind assistant. If you cannot find correct facts to answer the "
+ "questions, you have to refer to the attached files or use the functions provided. "
+ "Finally, if you receive math questions, you must write and run code to answer them.")
.tool(AssistantTool.FILE_SEARCH)
.tool(AssistantTool.fileSearch(10))
.toolResources(ToolResourceFull.builder()
.fileSearch(FileSearch.builder()
.vectorStore(VectorStore.builder()
.fileId("fileId")
.chunkingStrategy(ChunkingStrategy.staticType(StaticChunking.builder()
.maxChunkSizeTokens(100)
.chunkOverlapTokens(50)
.build()))
.build())
.build())
.build())
.metadata(Map.of("user", "tester"))
.temperature(0.2)
.responseFormat("auto")
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/assistants_create.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"id":"asst_aCEgMriCKzNDqvkhl1TmrJaK","object":"assistant","created_at":1714499627,"name":"Demo Assistant","description":"This is an assistant for demonstration purposes.","model":"gpt-4-turbo","instructions":"You are a very kind assistant. If you cannot find correct facts to answer the questions, you have to refer to the attached files or use the functions provided. Finally, if you receive math questions, you must write and run code to answer them.","tools":[{"type":"file_search"}],"top_p":1.0,"temperature":0.2,"tool_resources":{"file_search":{"vector_store_ids":[]}},"metadata":{"user":"tester"},"response_format":"auto"}
{"id":"asst_pTVcc7eVjgnf347eVIoEJHqG","object":"assistant","created_at":1717562128,"name":"Demo Assistant","description":"This is an assistant for demonstration purposes.","model":"gpt-4-turbo","instructions":"You are a very kind assistant. If you cannot find correct facts to answer the questions, you have to refer to the attached files or use the functions provided. Finally, if you receive math questions, you must write and run code to answer them.","tools":[{"type":"file_search","file_search":{"max_num_results":10}}],"top_p":1.0,"temperature":0.2,"tool_resources":{"file_search":{"vector_store_ids":["vs_LdYNXS4dMnllTRG5t5Kp1OSg"]}},"metadata":{"user":"tester"},"response_format":"auto"}