Skip to content

Commit fead893

Browse files
committed
feat: custom tool
1 parent f7c9781 commit fead893

File tree

6 files changed

+58
-7
lines changed

6 files changed

+58
-7
lines changed

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.61</version>
9+
<version>0.23.62</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212
<artifactId>openai-api</artifactId>

api/src/main/java/com/theokanning/openai/assistants/assistant/Tool.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import com.fasterxml.jackson.annotation.JsonSubTypes;
77
import com.fasterxml.jackson.annotation.JsonTypeInfo;
8+
import com.theokanning.openai.response.tool.definition.CustomTool;
89
import com.theokanning.openai.response.tool.definition.ImageGenerationTool;
910
import com.theokanning.openai.response.tool.definition.LocalShellTool;
1011
import com.theokanning.openai.response.tool.definition.ToolDefinition;
1112
import com.theokanning.openai.response.tool.definition.WebSearchTool;
13+
import lombok.AllArgsConstructor;
1214
import lombok.Data;
15+
import lombok.NoArgsConstructor;
1316

1417
import javax.validation.constraints.NotBlank;
1518
import java.util.ArrayList;
@@ -23,6 +26,7 @@
2326
**/
2427
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
2528
@JsonSubTypes({
29+
@JsonSubTypes.Type(value = Tool.Custom.class, name = "custom_tool"),
2630
@JsonSubTypes.Type(value = Tool.LocalShell.class, name = "local_shell"),
2731
@JsonSubTypes.Type(value = Tool.Function.class, name = "function"),
2832
@JsonSubTypes.Type(value = CodeInterpreterTool.class, name = "code_interpreter"),
@@ -61,6 +65,24 @@ default ToolDefinition definition() {
6165
return null;
6266
}
6367

68+
@Data
69+
@AllArgsConstructor
70+
@NoArgsConstructor
71+
class Custom implements Tool {
72+
73+
private CustomTool definition;
74+
75+
@Override
76+
public String getType() {
77+
return "custom_tool";
78+
}
79+
80+
@Override
81+
public ToolDefinition definition() {
82+
return definition;
83+
}
84+
}
85+
6486
class LocalShell implements Tool {
6587

6688
@Override

api/src/main/java/com/theokanning/openai/response/tool/definition/CustomTool.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.theokanning.openai.response.tool.definition;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonSubTypes;
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import com.theokanning.openai.assistants.assistant.Tool;
37
import lombok.AllArgsConstructor;
48
import lombok.Builder;
59
import lombok.Data;
@@ -34,13 +38,33 @@ public class CustomTool implements ToolDefinition {
3438
/**
3539
* Input format specification.
3640
*/
37-
private Object format; // TextFormat or GrammarFormat
41+
private Format format; // TextFormat or GrammarFormat
42+
43+
44+
@JsonTypeInfo(
45+
use = JsonTypeInfo.Id.NAME,
46+
include = JsonTypeInfo.As.EXISTING_PROPERTY,
47+
property = "type"
48+
)
49+
@JsonSubTypes({
50+
@JsonSubTypes.Type(value = TextFormat.class, name = "text"),
51+
@JsonSubTypes.Type(value = GrammarFormat.class, name = "grammar"),
52+
})
53+
@JsonInclude(JsonInclude.Include.NON_NULL)
54+
public interface Format {
55+
56+
/**
57+
* Get the tool type.
58+
*/
59+
String getType();
60+
}
61+
3862

3963
@Data
4064
@Builder
4165
@NoArgsConstructor
4266
@AllArgsConstructor
43-
public static class TextFormat {
67+
public static class TextFormat implements Format {
4468

4569
/**
4670
* Format type, always "text".
@@ -52,7 +76,7 @@ public static class TextFormat {
5276
@Builder
5377
@NoArgsConstructor
5478
@AllArgsConstructor
55-
public static class GrammarFormat {
79+
public static class GrammarFormat implements Format {
5680

5781
/**
5882
* Format type, always "grammar".
@@ -69,4 +93,9 @@ public static class GrammarFormat {
6993
*/
7094
private String definition;
7195
}
96+
97+
@Override
98+
public Tool getRealTool() {
99+
return new Tool.Custom(this);
100+
}
72101
}

client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.61</version>
9+
<version>0.23.62</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>top.bella</groupId>
77
<artifactId>openai-java</artifactId>
8-
<version>0.23.61</version>
8+
<version>0.23.62</version>
99
<packaging>pom</packaging>
1010
<description>openai java 版本</description>
1111
<name>openai-java</name>

service/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.61</version>
9+
<version>0.23.62</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

0 commit comments

Comments
 (0)