26 | * See {@link Function} 27 | */ 28 | @JSONField(name = "function") 29 | @JsonProperty("function") 30 | private Function function; 31 | 32 | @Getter 33 | public enum Type { 34 | 35 | CODE_INTERPRETER("code_interpreter"), 36 | FILE_SEARCH("file_search"), 37 | FUNCTION("function"); 38 | 39 | Type(final String value) { 40 | this.value = value; 41 | } 42 | 43 | private final String value; 44 | 45 | @JsonValue 46 | public String value() { 47 | return value; 48 | } 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /core/src/main/java/xyz/felh/openai/assistant/CreateAssistantRequest.java: -------------------------------------------------------------------------------- 1 | package xyz.felh.openai.assistant; 2 | 3 | import com.alibaba.fastjson2.annotation.JSONField; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | import lombok.*; 6 | import xyz.felh.openai.IOpenAiApiRequest; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | @Data 12 | @ToString 13 | @Builder 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | public class CreateAssistantRequest implements IOpenAiApiRequest { 17 | 18 | /** 19 | * ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them. 20 | */ 21 | @NonNull 22 | @JSONField(name = "model") 23 | @JsonProperty("model") 24 | private String model; 25 | 26 | /** 27 | * The name of the assistant. The maximum length is 256 characters. 28 | */ 29 | @JSONField(name = "name") 30 | @JsonProperty("name") 31 | private String name; 32 | 33 | /** 34 | * The description of the assistant. The maximum length is 512 characters. 35 | */ 36 | @JSONField(name = "description") 37 | @JsonProperty("description") 38 | private String description; 39 | 40 | /** 41 | * The system instructions that the assistant uses. The maximum length is 32768 characters. 42 | */ 43 | @JSONField(name = "instructions") 44 | @JsonProperty("instructions") 45 | private String instructions; 46 | 47 | /** 48 | * A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types code_interpreter, file_search, or function. 49 | *
50 | * Defaults to [] 51 | *
52 | * See {@link AssistantTool}
53 | */
54 | @JSONField(name = "tools")
55 | @JsonProperty("tools")
56 | private List
82 | * We generally recommend altering this or temperature but not both.
83 | */
84 | @JSONField(name = "top_p")
85 | @JsonProperty("top_p")
86 | private Double topP;
87 |
88 | /**
89 | * Specifies the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models since gpt-3.5-turbo-1106.
90 | *
91 | * Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.
92 | *
93 | * Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.
94 | */
95 | @JSONField(name = "response_format")
96 | @JsonProperty("response_format")
97 | private Object responseFormat;
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/IncompleteDetails.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.AllArgsConstructor;
6 | import lombok.Builder;
7 | import lombok.Data;
8 | import lombok.NoArgsConstructor;
9 | import xyz.felh.openai.IOpenAiBean;
10 |
11 | @Data
12 | @Builder
13 | @AllArgsConstructor
14 | @NoArgsConstructor
15 | public class IncompleteDetails implements IOpenAiBean {
16 |
17 | /**
18 | * The reason the message is incomplete.
19 | */
20 | @JSONField(name = "reason")
21 | @JsonProperty("reason")
22 | private String reason;
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/ModifyAssistantRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiApiRequest;
7 |
8 | import java.util.List;
9 | import java.util.Map;
10 |
11 | @Data
12 | @ToString
13 | @Builder
14 | @AllArgsConstructor
15 | @NoArgsConstructor
16 | public class ModifyAssistantRequest implements IOpenAiApiRequest {
17 |
18 | /**
19 | * ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.
20 | */
21 | @JSONField(name = "model")
22 | @JsonProperty("model")
23 | private String model;
24 |
25 | /**
26 | * The name of the assistant. The maximum length is 256 characters.
27 | */
28 | @JSONField(name = "name")
29 | @JsonProperty("name")
30 | private String name;
31 |
32 | /**
33 | * The description of the assistant. The maximum length is 512 characters.
34 | */
35 | @JSONField(name = "description")
36 | @JsonProperty("description")
37 | private String description;
38 |
39 | /**
40 | * The system instructions that the assistant uses. The maximum length is 32768 characters.
41 | */
42 | @JSONField(name = "instructions")
43 | @JsonProperty("instructions")
44 | private String instructions;
45 |
46 | /**
47 | * A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types code_interpreter, retrieval, or function.
48 | *
49 | * Defaults to []
50 | *
51 | * See {@link AssistantTool}
52 | */
53 | @JSONField(name = "tools")
54 | @JsonProperty("tools")
55 | private List
81 | * We generally recommend altering this or temperature but not both.
82 | */
83 | @JSONField(name = "top_p")
84 | @JsonProperty("top_p")
85 | private Double topP;
86 |
87 | /**
88 | * Specifies the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models since gpt-3.5-turbo-1106.
89 | *
90 | * Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.
91 | *
92 | * Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.
93 | */
94 | @JSONField(name = "response_format")
95 | @JsonProperty("response_format")
96 | private Object responseFormat;
97 |
98 | }
99 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/message/CreateMessageRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant.message;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiApiRequest;
7 |
8 | import java.util.List;
9 | import java.util.Map;
10 |
11 |
12 | @Data
13 | @Builder
14 | @AllArgsConstructor
15 | @NoArgsConstructor(force = true)
16 | public class CreateMessageRequest implements IOpenAiApiRequest {
17 |
18 | /**
19 | * The role of the entity that is creating the message. Currently only user is supported.
20 | */
21 | @NonNull
22 | @JSONField(name = "role")
23 | @JsonProperty("role")
24 | private Message.Role role;
25 |
26 | /**
27 | * The content of the message.
28 | */
29 | @NonNull
30 | @JSONField(name = "content")
31 | @JsonProperty("content")
32 | private String content;
33 |
34 | /**
35 | * A list of files attached to the message, and the tools they were added to.
36 | */
37 | @JSONField(name = "attachments")
38 | @JsonProperty("attachments")
39 | private List
27 | * See {@link ToolOutput}
28 | */
29 | @JSONField(name = "submit_tool_outputs")
30 | @JsonProperty("submit_tool_outputs")
31 | private ToolOutput submitToolOutputs;
32 |
33 | @Data
34 | public static class ToolOutput {
35 | /**
36 | * A list of the relevant tool calls.
37 | *
38 | * See {@link ToolCall}
39 | */
40 | @JSONField(name = "tool_calls")
41 | @JsonProperty("tool_calls")
42 | private List
19 | * See {@link ToolOutput}
20 | */
21 | @NonNull
22 | @JSONField(name = "tool_outputs")
23 | @JsonProperty("tool_outputs")
24 | private List
19 | * See {@link Message}
20 | *
21 | * role and content is required
22 | */
23 | @JSONField(name = "messages")
24 | @JsonProperty("messages")
25 | private List
29 | * See {@link Function}
30 | */
31 | @JSONField(name = "function")
32 | @JsonProperty("function")
33 | private Function function;
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/run/ToolOutput.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant.run;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.Data;
6 | import xyz.felh.openai.IOpenAiBean;
7 |
8 | @Data
9 | public class ToolOutput implements IOpenAiBean {
10 |
11 | /**
12 | * The ID of the tool call in the required_action object within the run object the output is being submitted for.
13 | */
14 | @JSONField(name = "tool_call_id")
15 | @JsonProperty("tool_call_id")
16 | private String toolCallId;
17 |
18 | /**
19 | * The output of the tool call to be submitted to continue the run.
20 | */
21 | @JSONField(name = "output")
22 | @JsonProperty("output")
23 | private String output;
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/run/TruncationStrategy.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant.run;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import com.fasterxml.jackson.annotation.JsonValue;
6 | import lombok.*;
7 | import xyz.felh.openai.IOpenAiBean;
8 |
9 | @Builder
10 | @Data
11 | @NoArgsConstructor
12 | @AllArgsConstructor
13 | public class TruncationStrategy implements IOpenAiBean {
14 |
15 | /**
16 | * The truncation strategy to use for the thread. The default is auto. If set to last_messages, the thread will be truncated to the n most recent messages in the thread. When set to auto, messages in the middle of the thread will be dropped to fit the context length of the model, max_prompt_tokens.
17 | */
18 | @JSONField(name = "type")
19 | @JsonProperty("type")
20 | private Type type;
21 |
22 | /**
23 | * The number of most recent messages from the thread when constructing the context for the run.
24 | */
25 | @JSONField(name = "last_messages")
26 | @JsonProperty("last_messages")
27 | private Integer lastMessages;
28 |
29 | @Getter
30 | public enum Type {
31 |
32 | AUTO("auto"),
33 | LAST_MESSAGES("last_messages"),
34 | MAX_PROMPT_TOKENS("max_prompt_tokens");
35 |
36 | Type(final String value) {
37 | this.value = value;
38 | }
39 |
40 | private final String value;
41 |
42 | @JsonValue
43 | public String value() {
44 | return value;
45 | }
46 |
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/runstep/StepDetails.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant.runstep;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import com.fasterxml.jackson.annotation.JsonValue;
6 | import lombok.Data;
7 | import lombok.Getter;
8 | import xyz.felh.openai.IOpenAiBean;
9 | import xyz.felh.openai.assistant.message.MessageContent;
10 |
11 | import java.util.Arrays;
12 | import java.util.List;
13 |
14 | @Data
15 | public class StepDetails implements IOpenAiBean {
16 |
17 | /**
18 | * message_creation, tool_calls
19 | */
20 | @JSONField(name = "type")
21 | @JsonProperty("type")
22 | private Type type;
23 |
24 | /**
25 | * Details of the message creation by the run step.
26 | */
27 | @JSONField(name = "message_creation")
28 | @JsonProperty("message_creation")
29 | private MessageCreation messageCreation;
30 |
31 | /**
32 | * An array of tool calls the run step was involved in. These can be associated with one of three types of tools: code_interpreter, retrieval, or function.
33 | *
34 | * See {@link StepToolCall}
35 | */
36 | @JSONField(name = "tool_calls")
37 | @JsonProperty("tool_calls")
38 | private List
12 | * Occurs when a new thread is created.
13 | *
14 | * thread.run.created
15 | * data is a run
16 | *
17 | * Occurs when a new run is created.
18 | *
19 | * thread.run.queued
20 | * data is a run
21 | *
22 | * Occurs when a run moves to a queued status.
23 | *
24 | * thread.run.in_progress
25 | * data is a run
26 | *
27 | * Occurs when a run moves to an in_progress status.
28 | *
29 | * thread.run.requires_action
30 | * data is a run
31 | *
32 | * Occurs when a run moves to a requires_action status.
33 | *
34 | * thread.run.completed
35 | * data is a run
36 | *
37 | * Occurs when a run is completed.
38 | *
39 | * thread.run.failed
40 | * data is a run
41 | *
42 | * Occurs when a run fails.
43 | *
44 | * thread.run.cancelling
45 | * data is a run
46 | *
47 | * Occurs when a run moves to a cancelling status.
48 | *
49 | * thread.run.cancelled
50 | * data is a run
51 | *
52 | * Occurs when a run is cancelled.
53 | *
54 | * thread.run.expired
55 | * data is a run
56 | *
57 | * Occurs when a run expires.
58 | *
59 | * thread.run.step.created
60 | * data is a run step
61 | *
62 | * Occurs when a run step is created.
63 | *
64 | * thread.run.step.in_progress
65 | * data is a run step
66 | *
67 | * Occurs when a run step moves to an in_progress state.
68 | *
69 | * thread.run.step.delta
70 | * data is a run step delta
71 | *
72 | * Occurs when parts of a run step are being streamed.
73 | *
74 | * thread.run.step.completed
75 | * data is a run step
76 | *
77 | * Occurs when a run step is completed.
78 | *
79 | * thread.run.step.failed
80 | * data is a run step
81 | *
82 | * Occurs when a run step fails.
83 | *
84 | * thread.run.step.cancelled
85 | * data is a run step
86 | *
87 | * Occurs when a run step is cancelled.
88 | *
89 | * thread.run.step.expired
90 | * data is a run step
91 | *
92 | * Occurs when a run step expires.
93 | *
94 | * thread.message.created
95 | * data is a message
96 | *
97 | * Occurs when a message is created.
98 | *
99 | * thread.message.in_progress
100 | * data is a message
101 | *
102 | * Occurs when a message moves to an in_progress state.
103 | *
104 | * thread.message.delta
105 | * data is a message delta
106 | *
107 | * Occurs when parts of a Message are being streamed.
108 | *
109 | * thread.message.completed
110 | * data is a message
111 | *
112 | * Occurs when a message is completed.
113 | *
114 | * thread.message.incomplete
115 | * data is a message
116 | *
117 | * Occurs when a message ends before it is completed.
118 | *
119 | * error
120 | * data is an error
121 | *
122 | * Occurs when an error occurs. This can happen due to an internal server error or a timeout.
123 | *
124 | * done
125 | * data is [DONE]
126 | *
127 | * Occurs when a stream ends.
128 | */
129 | @Data
130 | public class StreamEvent implements IOpenAiApiObject {
131 |
132 | @JSONField(name = "event")
133 | @JsonProperty("event")
134 | private String event;
135 |
136 | @JSONField(name = "data")
137 | @JsonProperty("data")
138 | private IOpenAiApiObject data;
139 |
140 | }
141 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/stream/message/MessageDelta.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant.stream.message;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.Data;
6 | import lombok.EqualsAndHashCode;
7 | import xyz.felh.openai.OpenAiApiObjectWithId;
8 |
9 |
10 | @EqualsAndHashCode(callSuper = true)
11 | @Data
12 | public class MessageDelta extends OpenAiApiObjectWithId {
13 |
14 | public static String OBJECT = "thread.message.delta";
15 |
16 | /**
17 | * The delta containing the fields that have changed on the Message.
18 | */
19 | @JSONField(name = "delta")
20 | @JsonProperty("delta")
21 | private MessageDeltaContent delta;
22 |
23 | }
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/stream/message/MessageDeltaContent.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant.stream.message;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.Data;
6 | import xyz.felh.openai.IOpenAiBean;
7 | import xyz.felh.openai.assistant.message.Message;
8 | import xyz.felh.openai.assistant.message.MessageContent;
9 |
10 | import java.util.List;
11 |
12 | @Data
13 | public class MessageDeltaContent implements IOpenAiBean {
14 |
15 | @JSONField(name = "role")
16 | @JsonProperty("role")
17 | private Message.Role role;
18 |
19 | /**
20 | * The content of the message in array of text and/or images.
21 | */
22 | @JSONField(name = "content")
23 | @JsonProperty("content")
24 | private List
15 | * See {@link StepDetails}
16 | */
17 | @JSONField(name = "step_details")
18 | @JsonProperty("step_details")
19 | private StepDetails stepDetails;
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/assistant/thread/CreateThreadRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.assistant.thread;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.AllArgsConstructor;
6 | import lombok.Builder;
7 | import lombok.Data;
8 | import lombok.NoArgsConstructor;
9 | import xyz.felh.openai.IOpenAiApiRequest;
10 | import xyz.felh.openai.assistant.AssistToolResources;
11 | import xyz.felh.openai.assistant.message.Message;
12 |
13 | import java.util.List;
14 | import java.util.Map;
15 |
16 | @Data
17 | @Builder
18 | @AllArgsConstructor
19 | @NoArgsConstructor(force = true)
20 | public class CreateThreadRequest implements IOpenAiApiRequest {
21 |
22 | /**
23 | * A list of {@link Message} to start the thread with.
24 | *
25 | * required fields: role, content
26 | */
27 | @JSONField(name = "messages")
28 | @JsonProperty("messages")
29 | private List
31 | * response_format must be set verbose_json to use timestamp granularities.
32 | * Either or both of these options are supported: word, or segment. Note:
33 | * There is no additional latency for segment timestamps, but generating word timestamps incurs additional latency.
34 | */
35 | @JSONField(name = "timestamp_granularities")
36 | @JsonProperty("timestamp_granularities")
37 | private List
22 | * The audio file object (not file name) translate, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
23 | */
24 | @JSONField(name = "file_path")
25 | @JsonProperty("file_path")
26 | private String filePath;
27 |
28 | @JSONField(name = "file")
29 | @JsonProperty("file")
30 | private byte[] file;
31 |
32 | /**
33 | * The name must contain extension in order to let API know how the file's type
34 | */
35 | @NonNull
36 | @JSONField(name = "file_name")
37 | @JsonProperty("file_name")
38 | private String fileName;
39 |
40 | /**
41 | * Required
42 | * ID of the model to use. Only whisper-1 is currently available.
43 | */
44 | @NonNull
45 | @JSONField(name = "model")
46 | @JsonProperty("model")
47 | private String model;
48 |
49 | /**
50 | * Optional
51 | * An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.
52 | */
53 | @JSONField(name = "prompt")
54 | @JsonProperty("prompt")
55 | private String prompt;
56 |
57 | /**
58 | * Optional Defaults to json
59 | *
60 | * The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
61 | *
62 | * See {@link ResponseFormat}
63 | */
64 | @JSONField(name = "response_format")
65 | @JsonProperty("response_format")
66 | private ResponseFormat responseFormat;
67 |
68 | /**
69 | * Optional
70 | * Defaults to 0
71 | *
72 | * The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.
73 | */
74 | @JSONField(name = "temperature")
75 | @JsonProperty("temperature")
76 | private Double temperature;
77 |
78 | @Getter
79 | public enum ResponseFormat {
80 |
81 | JSON("json"),
82 | TEXT("text"),
83 | SRT("srt"),
84 | VERBOSE_JSON("verbose_json"),
85 | VTT("vtt");
86 |
87 | private final String value;
88 |
89 | ResponseFormat(final String value) {
90 | this.value = value;
91 | }
92 |
93 | @JsonValue
94 | public String value() {
95 | return value;
96 | }
97 |
98 | public static ResponseFormat findByValue(String value) {
99 | return Arrays.stream(values()).filter(it ->
100 | it.value.equals(value)).findFirst().orElse(null);
101 | }
102 | }
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/audio/CreateSpeechRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.audio;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import com.fasterxml.jackson.annotation.JsonValue;
6 | import lombok.*;
7 | import lombok.experimental.SuperBuilder;
8 | import xyz.felh.openai.IOpenAiApiRequest;
9 |
10 | import java.util.Arrays;
11 |
12 | @Data
13 | @SuperBuilder(toBuilder = true)
14 | @AllArgsConstructor
15 | @NoArgsConstructor(force = true)
16 | public class CreateSpeechRequest implements IOpenAiApiRequest {
17 |
18 | /**
19 | * One of the available TTS models: tts-1 or tts-1-hd
20 | */
21 | @NonNull
22 | @JSONField(name = "model")
23 | @JsonProperty("model")
24 | private String model;
25 |
26 | /**
27 | * The text to generate audio for. The maximum length is 4096 characters.
28 | */
29 | @NonNull
30 | @JSONField(name = "input")
31 | @JsonProperty("input")
32 | private String input;
33 |
34 | /**
35 | * The voice to use when generating the audio. Supported voices are alloy, echo, fable, onyx, nova, and shimmer. Previews of the voices are available in the Text to speech guide.
36 | *
37 | * See {@link Voice}
38 | */
39 | @NonNull
40 | @JSONField(name = "voice")
41 | @JsonProperty("voice")
42 | private Voice voice;
43 |
44 | /**
45 | * string Optional Defaults to mp3
46 | *
47 | * The format to audio in. Supported formats are mp3, opus, aac, and flac.
48 | *
49 | * See {@link ResponseFormat}
50 | */
51 | @JSONField(name = "response_format")
52 | @JsonProperty("response_format")
53 | private ResponseFormat responseFormat;
54 |
55 | /**
56 | * number Optional Defaults to 1
57 | *
58 | * The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
59 | */
60 | @JSONField(name = "speed")
61 | @JsonProperty("speed")
62 | private Double speed;
63 |
64 | @Getter
65 | public enum Voice {
66 |
67 | ALLOY("alloy"),
68 | ECHO("echo"),
69 | FABLE("fable"),
70 | ONYX("onyx"),
71 | NOVA("nova"),
72 | SHIMMER("shimmer"),
73 | ;
74 |
75 | private final String value;
76 |
77 | Voice(final String value) {
78 | this.value = value;
79 | }
80 |
81 | @JsonValue
82 | public String value() {
83 | return value;
84 | }
85 |
86 | public static Voice findByValue(String value) {
87 | return Arrays.stream(values()).filter(it ->
88 | it.value.equals(value)).findFirst().orElse(null);
89 | }
90 | }
91 |
92 | @Getter
93 | public enum ResponseFormat {
94 |
95 | MP3("mp3"),
96 | OPUS("opus"),
97 | AAC("aac"),
98 | FLAC("flac"),
99 | WAV("wav");
100 |
101 | private final String value;
102 |
103 | ResponseFormat(final String value) {
104 | this.value = value;
105 | }
106 |
107 | @JsonValue
108 | public String value() {
109 | return value;
110 | }
111 |
112 | public static ResponseFormat findByValue(String value) {
113 | return Arrays.stream(values()).filter(it ->
114 | it.value.equals(value)).findFirst().orElse(null);
115 | }
116 | }
117 |
118 | }
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/batch/BatchInputLineObject.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.batch;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.AllArgsConstructor;
6 | import lombok.Builder;
7 | import lombok.Data;
8 | import lombok.NoArgsConstructor;
9 | import xyz.felh.openai.IOpenAiBean;
10 |
11 | import java.util.Map;
12 |
13 | @Builder
14 | @NoArgsConstructor
15 | @AllArgsConstructor
16 | @Data
17 | public class BatchInputLineObject implements IOpenAiBean {
18 |
19 | /**
20 | * A developer-provided per-request id that will be used to match outputs to inputs. Must be unique for each request in a batch.
21 | */
22 | @JSONField(name = "custom_id")
23 | @JsonProperty("custom_id")
24 | private String customId;
25 |
26 | /**
27 | * The HTTP method to be used for the request. Currently only POST is supported.
28 | */
29 | @JSONField(name = "method")
30 | @JsonProperty("method")
31 | private String method;
32 |
33 | /**
34 | * The OpenAI API relative URL to be used for the request. Currently only /v1/chat/completions is supported.
35 | */
36 | @JSONField(name = "url")
37 | @JsonProperty("url")
38 | private String url;
39 |
40 | @JSONField(name = "body")
41 | @JsonProperty("body")
42 | private Map, ?> body;
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/batch/BatchOutputLineObject.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.batch;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.AllArgsConstructor;
6 | import lombok.Builder;
7 | import lombok.Data;
8 | import lombok.NoArgsConstructor;
9 | import xyz.felh.openai.IOpenAiBean;
10 |
11 | import java.util.Map;
12 |
13 | @Builder
14 | @NoArgsConstructor
15 | @AllArgsConstructor
16 | @Data
17 | public class BatchOutputLineObject implements IOpenAiBean {
18 |
19 | @JSONField(name = "id")
20 | @JsonProperty("id")
21 | private String id;
22 |
23 | /**
24 | * A developer-provided per-request id that will be used to match outputs to inputs. Must be unique for each request in a batch.
25 | */
26 | @JSONField(name = "custom_id")
27 | @JsonProperty("custom_id")
28 | private String customId;
29 |
30 | @JSONField(name = "response")
31 | @JsonProperty("response")
32 | private Response response;
33 |
34 | /**
35 | * For requests that failed with a non-HTTP error, this will contain more information on the cause of the failure.
36 | */
37 | @JSONField(name = "error")
38 | @JsonProperty("error")
39 | private Error error;
40 |
41 | @Data
42 | @Builder
43 | @NoArgsConstructor
44 | @AllArgsConstructor
45 | public static class Response implements IOpenAiBean {
46 |
47 | /**
48 | * The HTTP status code of the response
49 | */
50 | @JSONField(name = "status_code")
51 | @JsonProperty("status_code")
52 | private Integer statusCode;
53 |
54 | /**
55 | * An unique identifier for the OpenAI API request. Please include this request ID when contacting support.
56 | */
57 | @JSONField(name = "request_id")
58 | @JsonProperty("request_id")
59 | private String requestId;
60 |
61 | /**
62 | * The JSON body of the response
63 | */
64 | @JSONField(name = "body")
65 | @JsonProperty("body")
66 | private Map, ?> body;
67 |
68 | }
69 |
70 | @Data
71 | @Builder
72 | @NoArgsConstructor
73 | @AllArgsConstructor
74 | public static class Error implements IOpenAiBean {
75 |
76 | /**
77 | * A machine-readable error code.
78 | */
79 | @JSONField(name = "code")
80 | @JsonProperty("code")
81 | private String code;
82 |
83 | /**
84 | * A human-readable error message.
85 | */
86 | @JSONField(name = "message")
87 | @JsonProperty("message")
88 | private String message;
89 | }
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/batch/CreateBatchRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.batch;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiApiRequest;
7 |
8 | @Data
9 | @Builder
10 | @AllArgsConstructor
11 | @NoArgsConstructor(force = true)
12 | public class CreateBatchRequest implements IOpenAiApiRequest {
13 |
14 | /**
15 | * The ID of an uploaded file that contains requests for the new batch.
16 | *
17 | * See upload file for how to upload a file.
18 | *
19 | * Your input file must be formatted as a JSONL file, and must be uploaded with the purpose batch.
20 | */
21 | @NonNull
22 | @JSONField(name = "input_file_id")
23 | @JsonProperty("input_file_id")
24 | private String inputFileId;
25 |
26 | /**
27 | * The endpoint to be used for all requests in the batch. Currently only /v1/chat/completions is supported.
28 | */
29 | @NonNull
30 | @JSONField(name = "endpoint")
31 | @JsonProperty("endpoint")
32 | private String endpoint;
33 |
34 | /**
35 | * The time frame within which the batch should be processed. Currently only '24h' is supported.
36 | */
37 | @NonNull
38 | @JSONField(name = "completion_window")
39 | @JsonProperty("completion_window")
40 | private String completionWindow;
41 |
42 | /**
43 | * Optional custom metadata for the batch.
44 | */
45 | @JSONField(name = "metadata")
46 | @JsonProperty("metadata")
47 | private Object metadata;
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/ChatAudio.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.AllArgsConstructor;
6 | import lombok.Builder;
7 | import lombok.Data;
8 | import lombok.NoArgsConstructor;
9 | import xyz.felh.openai.IOpenAiBean;
10 |
11 | /**
12 | * @author Forest Wang
13 | * @package xyz.felh.openai.chat
14 | * @class ChatAudio
15 | * @email forestwanglin@gmail.cn
16 | * @date 2024/10/24
17 | */
18 | @Data
19 | @Builder
20 | @NoArgsConstructor
21 | @AllArgsConstructor
22 | public class ChatAudio implements IOpenAiBean {
23 |
24 | /**
25 | * Specifies the voice type. Supported voices are alloy, echo, fable, onyx, nova, and shimmer.
26 | */
27 | @JSONField(name = "voice")
28 | @JsonProperty("voice")
29 | private ChatAudioVoice voice;
30 |
31 | /**
32 | * Specifies the output audio format. Must be one of wav, mp3, flac, opus, or pcm16.
33 | */
34 | @JSONField(name = "format")
35 | @JsonProperty("format")
36 | private ChatAudioFormat format;
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/ChatAudioFormat.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat;
2 |
3 | import com.fasterxml.jackson.annotation.JsonValue;
4 |
5 | /**
6 | * @author Forest Wang
7 | * @package xyz.felh.openai.chat
8 | * @class ChatAudioFormat
9 | * @email forestwanglin@gmail.cn
10 | * @date 2024/10/24
11 | */
12 | public enum ChatAudioFormat {
13 |
14 | WAV("wav"),
15 | MP3("mp3"),
16 | FLAC("flac"),
17 | OPUS("opus"),
18 | PCM16("pcm16");
19 |
20 | private final String value;
21 |
22 | ChatAudioFormat(final String value) {
23 | this.value = value;
24 | }
25 |
26 | @JsonValue
27 | public String value() {
28 | return value;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/ChatAudioVoice.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat;
2 |
3 | import com.fasterxml.jackson.annotation.JsonValue;
4 |
5 | /**
6 | * @author Forest Wang
7 | * @package xyz.felh.openai.chat
8 | * @class ChatAudioVoice
9 | * @email forestwanglin@gmail.cn
10 | * @date 2024/10/24
11 | */
12 | public enum ChatAudioVoice {
13 |
14 | ALLOY("alloy"),
15 | ECHO("echo"),
16 | FABLE("fable"),
17 | ONYX("onyx"),
18 | NOVA("nova"),
19 | SHIMMER("shimmer");
20 |
21 | private final String value;
22 |
23 | ChatAudioVoice(final String value) {
24 | this.value = value;
25 | }
26 |
27 | @JsonValue
28 | public String value() {
29 | return value;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/ChatCompletionChoice.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.Data;
6 | import xyz.felh.openai.IOpenAiApiObject;
7 |
8 | @Data
9 | public class ChatCompletionChoice implements IOpenAiApiObject {
10 |
11 | /**
12 | * The reason the model stopped generating tokens. This will be stop if the model hit a natural stop point
13 | * or a provided stop sequence, length if the maximum number of tokens specified in the request was reached,
14 | * content_filter if content was omitted due to a flag from our content filters, tool_calls
15 | * if the model called a tool, or function_call (deprecated) if the model called a function.
16 | */
17 | @JSONField(name = "finish_reason")
18 | @JsonProperty("finish_reason")
19 | private String finishReason;
20 |
21 | /**
22 | * The index of the choice in the list of choices.
23 | */
24 | @JSONField(name = "index")
25 | @JsonProperty("index")
26 | private Integer index;
27 |
28 | /**
29 | * A chat completion message generated by the model.
30 | *
31 | * See {@link ChatMessage}
32 | */
33 | @JSONField(name = "message")
34 | @JsonProperty("message")
35 | private ChatMessage message;
36 |
37 | /**
38 | * A chat completion delta generated by streamed model responses.
39 | *
40 | * See {@link ChatMessage}
41 | */
42 | @JSONField(name = "delta")
43 | @JsonProperty("delta")
44 | private ChatMessage delta;
45 |
46 | /**
47 | * Log probability information for the choice.
48 | *
49 | * See {@link Logprobs}
50 | */
51 | @JSONField(name = "logprobs")
52 | @JsonProperty("logprobs")
53 | private Logprobs logprobs;
54 |
55 | }
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/ChatMessageRole.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat;
2 |
3 | import com.fasterxml.jackson.annotation.JsonValue;
4 |
5 | public enum ChatMessageRole {
6 |
7 | /**
8 | * content - Required The contents of the user message.
9 | * role - Required The role of the messages author, in this case user.
10 | */
11 | SYSTEM("system"),
12 | /**
13 | * content - Required The contents of the user message.
14 | * role - Required The role of the messages author, in this case user.
15 | */
16 | USER("user"),
17 | /**
18 | * content - Required The contents of the assistant message.
19 | * role - Required The role of the messages author, in this case assistant.
20 | * tool_calls - Optional The tool calls generated by the model, such as function calls.
21 | */
22 | ASSISTANT("assistant"),
23 | /**
24 | * role - Required The role of the messages author, in this case tool.
25 | * content - Required The contents of the tool message.
26 | * tool_call_id - Required Tool call that this message is responding to.
27 | */
28 | TOOL("tool");
29 |
30 | private final String value;
31 |
32 | ChatMessageRole(final String value) {
33 | this.value = value;
34 | }
35 |
36 | @JsonValue
37 | public String value() {
38 | return value;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/ChatModality.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat;
2 |
3 | import com.fasterxml.jackson.annotation.JsonValue;
4 |
5 | /**
6 | * @author Forest Wang
7 | * @package xyz.felh.openai.chat
8 | * @class ChatModality
9 | * @email forestwanglin@gmail.cn
10 | * @date 2024/10/24
11 | */
12 | public enum ChatModality {
13 |
14 | TEXT("text"),
15 |
16 | AUDIO("audio");
17 |
18 | private final String value;
19 |
20 | ChatModality(final String value) {
21 | this.value = value;
22 | }
23 |
24 | @JsonValue
25 | public String value() {
26 | return value;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/Logprobs.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat;
2 |
3 |
4 | import com.alibaba.fastjson2.annotation.JSONField;
5 | import com.fasterxml.jackson.annotation.JsonProperty;
6 | import lombok.AllArgsConstructor;
7 | import lombok.Builder;
8 | import lombok.Data;
9 | import lombok.NoArgsConstructor;
10 | import xyz.felh.openai.IOpenAiApiObject;
11 | import xyz.felh.openai.IOpenAiBean;
12 |
13 | import java.util.List;
14 |
15 | @Data
16 | public class Logprobs implements IOpenAiApiObject {
17 |
18 | /**
19 | * A list of message content tokens with log probability information.
20 | *
21 | * See {@link Content}
22 | */
23 | @JSONField(name = "content")
24 | @JsonProperty("content")
25 | private List
20 | * String, Optional
21 | *
22 | * Defaults to text, See {@link TypeValue}
23 | */
24 | @JSONField(name = "type")
25 | @JsonProperty("type")
26 | private TypeValue type;
27 |
28 | /**
29 | * It is required if type = json_schema
30 | */
31 | @JSONField(name = "json_schema")
32 | @JsonProperty("json_schema")
33 | private JsonSchema jsonSchema;
34 |
35 | public enum TypeValue {
36 | TEXT("text"),
37 | JSON_OBJECT("json_object"),
38 | JSON_SCHEMA("json_schema");
39 |
40 | private final String value;
41 |
42 | TypeValue(final String value) {
43 | this.value = value;
44 | }
45 |
46 | @JsonValue
47 | public String value() {
48 | return value;
49 | }
50 |
51 | public static TypeValue findByValue(String value) {
52 | return Arrays.stream(values()).filter(it ->
53 | it.value.equals(value)).findFirst().orElse(null);
54 | }
55 | }
56 |
57 | @Data
58 | @Builder
59 | @NoArgsConstructor
60 | @AllArgsConstructor
61 | public static class JsonSchema implements IOpenAiBean {
62 |
63 | /**
64 | * The name of the response format. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
65 | */
66 | @NonNull
67 | @JSONField(name = "name")
68 | @JsonProperty("name")
69 | private String name;
70 |
71 | /**
72 | * string
73 | *
74 | * Optional
75 | * A description of what the response format is for, used by the model to determine how to respond in the format.
76 | */
77 | @JSONField(name = "description")
78 | @JsonProperty("description")
79 | private String description;
80 |
81 | /**
82 | * The schema for the response format, described as a JSON Schema object.
83 | *
84 | * See Structured Outputs
85 | */
86 | @JSONField(name = "schema")
87 | @JsonProperty("schema")
88 | private Object schema;
89 |
90 | /**
91 | * boolean or null
92 | *
93 | * Optional
94 | * Defaults to false
95 | * Whether to enable strict schema adherence when generating the output. If set to true, the model will always follow the exact schema defined in the schema field. Only a subset of JSON Schema is supported when strict is true. To learn more, read the Structured Outputs guide.
96 | */
97 | @JSONField(name = "strict")
98 | @JsonProperty("strict")
99 | private Boolean strict;
100 |
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/tool/Function.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat.tool;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 |
7 | @Data
8 | @Builder
9 | @NoArgsConstructor
10 | @AllArgsConstructor
11 | public class Function {
12 |
13 | /**
14 | * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
15 | *
16 | * String
17 | */
18 | @NonNull
19 | @JSONField(name = "name")
20 | @JsonProperty("name")
21 | private String name;
22 |
23 | /**
24 | * A description of what the function does, used by the model to choose when and how to call the function.
25 | *
26 | * string, Optional
27 | */
28 | @JSONField(name = "description")
29 | @JsonProperty("description")
30 | private String description;
31 |
32 | /**
33 | * The parameters the functions accepts, described as a JSON Schema object. See the guide for examples, and the JSON Schema reference for documentation about the format.
34 | *
35 | * To describe a function that accepts no parameters, provide the value {"type": "object", "properties": {}}.
36 | */
37 | @JSONField(name = "parameters")
38 | @JsonProperty("parameters")
39 | private Object parameters;
40 |
41 | /**
42 | * boolean or null
43 | *
44 | * Optional
45 | * Defaults to false
46 | * Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the parameters field. Only a subset of JSON Schema is supported when strict is true. Learn more about Structured Outputs in the function calling guide.
47 | */
48 | @JSONField(name = "strict")
49 | @JsonProperty("strict")
50 | private Boolean strict;
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/tool/FunctionCall.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat.tool;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiBean;
7 |
8 | @Data
9 | @Builder
10 | @NoArgsConstructor
11 | @AllArgsConstructor
12 | public class FunctionCall implements IOpenAiBean {
13 |
14 | /**
15 | * The name of the function to call.
16 | */
17 | @NonNull
18 | @JSONField(name = "name")
19 | @JsonProperty("name")
20 | private String name;
21 |
22 | /**
23 | * The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
24 | */
25 | @NonNull
26 | @JSONField(name = "arguments")
27 | @JsonProperty("arguments")
28 | private String arguments;
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/tool/Tool.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat.tool;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiBean;
7 |
8 | @Data
9 | @Builder
10 | @NoArgsConstructor
11 | @AllArgsConstructor
12 | public class Tool implements IOpenAiBean {
13 |
14 | /**
15 | * The type of the tool. Currently, only function is supported.
16 | *
17 | * See {@link Type}
18 | */
19 | @NonNull
20 | @JSONField(name = "type")
21 | @JsonProperty("type")
22 | private Type type;
23 |
24 | /**
25 | * See {@link Function}
26 | */
27 | @NonNull
28 | @JSONField(name = "function")
29 | @JsonProperty("function")
30 | private Function function;
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/tool/ToolCall.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat.tool;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiBean;
7 |
8 | @Data
9 | @Builder
10 | @NoArgsConstructor
11 | @AllArgsConstructor
12 | public class ToolCall implements IOpenAiBean {
13 |
14 | /**
15 | * The ID of the tool call.
16 | */
17 | @NonNull
18 | @JSONField(name = "id")
19 | @JsonProperty("id")
20 | private String id;
21 |
22 | /**
23 | * The type of the tool. Currently, only function is supported.
24 | *
25 | * See {@link Type}
26 | */
27 | @NonNull
28 | @JSONField(name = "type")
29 | @JsonProperty("type")
30 | private Type type;
31 |
32 | /**
33 | * The function that the model called.
34 | *
35 | * See {@link FunctionCall}
36 | */
37 | @NonNull
38 | @JSONField(name = "function")
39 | @JsonProperty("function")
40 | private FunctionCall function;
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/tool/ToolChoice.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat.tool;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiBean;
7 |
8 | @Data
9 | @Builder
10 | @NoArgsConstructor
11 | @AllArgsConstructor
12 | public class ToolChoice implements IOpenAiBean {
13 |
14 | /**
15 | * The type of the tool. Currently, only function is supported.
16 | *
17 | * string, Optional
18 | *
19 | * See {@link Type}
20 | */
21 | @JSONField(name = "type")
22 | @JsonProperty("type")
23 | private Type type;
24 |
25 | /**
26 | * function object
27 | */
28 | @JSONField(name = "function")
29 | @JsonProperty("function")
30 | private Function function;
31 |
32 | @Data
33 | @Builder
34 | @NoArgsConstructor
35 | @AllArgsConstructor
36 | public static class Function implements IOpenAiBean {
37 |
38 | /**
39 | * The name of the function to call.
40 | *
41 | * string
42 | */
43 | @NonNull
44 | @JSONField(name = "name")
45 | @JsonProperty("name")
46 | private String name;
47 |
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/chat/tool/Type.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.chat.tool;
2 |
3 | import com.fasterxml.jackson.annotation.JsonValue;
4 | import lombok.Getter;
5 |
6 | import java.util.Arrays;
7 |
8 | @Getter
9 | public enum Type {
10 |
11 | FUNCTION("function"),
12 | ;
13 |
14 | private final String value;
15 |
16 | Type(final String value) {
17 | this.value = value;
18 | }
19 |
20 | @JsonValue
21 | public String value() {
22 | return value;
23 | }
24 |
25 | public static Type findByValue(String value) {
26 | return Arrays.stream(values()).filter(it ->
27 | it.value.equals(value)).findFirst().orElse(null);
28 | }
29 |
30 | }
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/embedding/CreateEmbeddingRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.embedding;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import com.fasterxml.jackson.annotation.JsonValue;
6 | import xyz.felh.openai.IOpenAiApiRequest;
7 | import lombok.*;
8 | import xyz.felh.openai.chat.ChatMessage;
9 |
10 | import java.util.Arrays;
11 |
12 | @Data
13 | @Builder
14 | @AllArgsConstructor
15 | @NoArgsConstructor(force = true)
16 | public class CreateEmbeddingRequest implements IOpenAiApiRequest {
17 |
18 | /**
19 | * Required
20 | * ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.
21 | */
22 | @NonNull
23 | @JSONField(name = "model")
24 | @JsonProperty("model")
25 | private String model;
26 |
27 | /**
28 | * string or array Required
29 | *
30 | * Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. The input must not exceed the max input tokens for the model (8192 tokens for text-embedding-ada-002), cannot be an empty string, and any array must be 2048 dimensions or less. Example Python code for counting tokens.
31 | *
32 | * string string - The string that will be turned into an embedding.
33 | *
34 | * array array - The array of strings that will be turned into an embedding. Each line's max is 8191
35 | *
36 | * array array - The array of integers that will be turned into an embedding.
37 | *
38 | * array array - The array of arrays containing integers that will be turned into an embedding.
39 | */
40 | @NonNull
41 | @JSONField(name = "input")
42 | @JsonProperty("input")
43 | private Object input;
44 |
45 | /**
46 | * string Optional Defaults to float
47 | *
48 | * The format to return the embeddings in. Can be either float or base64.
49 | * See {@link EncodingFormat}
50 | */
51 | @JSONField(name = "encoding_format")
52 | @JsonProperty("encoding_format")
53 | private EncodingFormat encodingFormat;
54 |
55 | /**
56 | * integer Optional
57 | *
58 | * The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
59 | */
60 | @JSONField(name = "dimensions")
61 | @JsonProperty("dimensions")
62 | private Integer dimensions;
63 |
64 | /**
65 | * Optional
66 | *
67 | * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
68 | */
69 | @JSONField(name = "user")
70 | @JsonProperty("user")
71 | private String user;
72 |
73 | @Getter
74 | public enum EncodingFormat {
75 |
76 | FLOAT("float"),
77 | BASE64("base64");
78 |
79 | private final String value;
80 |
81 | EncodingFormat(final String value) {
82 | this.value = value;
83 | }
84 |
85 | @JsonValue
86 | public String value() {
87 | return value;
88 | }
89 |
90 | public static EncodingFormat findByValue(String value) {
91 | return Arrays.stream(values()).filter(it ->
92 | it.value.equals(value)).findFirst().orElse(null);
93 | }
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/embedding/CreateEmbeddingResponse.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.embedding;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import xyz.felh.openai.IOpenAiApiResponse;
6 | import xyz.felh.openai.usage.Usage;
7 | import lombok.Data;
8 |
9 | import java.util.List;
10 |
11 | @Data
12 | public class CreateEmbeddingResponse implements IOpenAiApiResponse {
13 |
14 | @JSONField(name = "model")
15 | @JsonProperty("model")
16 | private String model;
17 |
18 | @JSONField(name = "object")
19 | @JsonProperty("object")
20 | private String object;
21 |
22 | /**
23 | * See {@link Embedding}
24 | */
25 | @JSONField(name = "data")
26 | @JsonProperty("data")
27 | private List
46 | * See {@link Purpose}
47 | *
48 | * assistants - Supported formats: ['c', 'cpp', 'csv', 'docx', 'html', 'java', 'json', 'md', 'pdf', 'php', 'pptx', 'py', 'rb', 'tex', 'txt', 'css', 'jpeg', 'jpg', 'js', 'gif', 'png', 'tar', 'ts', 'xlsx', 'xml', 'zip']
49 | */
50 | @JSONField(name = "purpose")
51 | @JsonProperty("purpose")
52 | private Purpose purpose;
53 |
54 |
55 | @Getter
56 | public enum Purpose {
57 |
58 | FINE_TUNE("fine-tune"),
59 | FINE_TUNE_RESULTS("fine-tune-results"),
60 | ASSISTANTS("assistants"),
61 | ASSISTANTS_OUTPUT("assistants_output"),
62 | VISION("vision"),
63 | BATCH("batch");
64 |
65 | private final String value;
66 |
67 | Purpose(final String value) {
68 | this.value = value;
69 | }
70 |
71 | @JsonValue
72 | public String value() {
73 | return value;
74 | }
75 |
76 | public static Purpose findByValue(String value) {
77 | return Arrays.stream(values()).filter(it ->
78 | it.value.equals(value)).findFirst().orElse(null);
79 | }
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/fineTuning/CreateFineTuningJobRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.fineTuning;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiApiRequest;
7 |
8 | @Data
9 | @Builder
10 | @AllArgsConstructor
11 | @NoArgsConstructor(force = true)
12 | public class CreateFineTuningJobRequest implements IOpenAiApiRequest {
13 |
14 | /**
15 | * The ID of an uploaded file that contains training data.
16 | * Your dataset must be formatted as a JSONL file. Additionally, you must upload your file with the purpose fine-tune.
17 | */
18 | @NonNull
19 | @JSONField(name = "training_file")
20 | @JsonProperty("training_file")
21 | private String trainingFile;
22 |
23 | /**
24 | * Optional
25 | *
26 | * The ID of an uploaded file that contains validation data.
27 | *
28 | * If you provide this file, the data is used to generate validation metrics periodically during fine-tuning.
29 | * These metrics can be viewed in the fine-tuning results file. The same data should not be present in both train and validation files.
30 | *
31 | * Your dataset must be formatted as a JSONL file. You must upload your file with the purpose fine-tune.
32 | */
33 | @JSONField(name = "validation_file")
34 | @JsonProperty("validation_file")
35 | private String validationFile;
36 |
37 | /**
38 | * The name of the model to fine-tune. You can select one of the supported models.
39 | */
40 | @NonNull
41 | @JSONField(name = "model")
42 | @JsonProperty("model")
43 | private String model;
44 |
45 | /**
46 | * Optional
47 | * The hyperparameters used for the fine-tuning job.
48 | */
49 | @JSONField(name = "hyperparameters")
50 | @JsonProperty("hyperparameters")
51 | private Hyperparameters hyperparameters;
52 |
53 | /**
54 | * A string of up to 18 characters that will be added to your fine-tuned model name.
55 | *
56 | * For example, a suffix of "custom-model-name" would produce a model name like ft:gpt-3.5-turbo:openai:custom-model-name:7p4lURel.
57 | */
58 | @JSONField(name = "suffix")
59 | @JsonProperty("suffix")
60 | private String suffix;
61 |
62 | }
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/fineTuning/FineTuningJobError.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.fineTuning;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.Data;
6 | import xyz.felh.openai.IOpenAiApiObject;
7 |
8 | @Data
9 | public class FineTuningJobError implements IOpenAiApiObject {
10 |
11 | /**
12 | * A machine-readable error code.
13 | */
14 | @JSONField(name = "code")
15 | @JsonProperty("code")
16 | private String code;
17 |
18 | /**
19 | * A human-readable error message.
20 | */
21 | @JSONField(name = "message")
22 | @JsonProperty("message")
23 | private String message;
24 |
25 | /**
26 | * The parameter that was invalid, usually training_file or validation_file. This field will be null if the failure was not parameter-specific.
27 | */
28 | @JSONField(name = "param")
29 | @JsonProperty("param")
30 | private String param;
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/fineTuning/FineTuningJobEvent.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.fineTuning;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.Data;
6 | import lombok.EqualsAndHashCode;
7 | import lombok.ToString;
8 | import xyz.felh.openai.OpenAiApiObjectWithId;
9 |
10 | @EqualsAndHashCode(callSuper = true)
11 | @Data
12 | @ToString(callSuper = true)
13 | public class FineTuningJobEvent extends OpenAiApiObjectWithId {
14 |
15 | public static String OBJECT = "fine_tuning.job.event";
16 |
17 | @JSONField(name = "created_at")
18 | @JsonProperty("created_at")
19 | private Long createdAt;
20 |
21 | @JSONField(name = "level")
22 | @JsonProperty("level")
23 | private String level;
24 |
25 | @JSONField(name = "message")
26 | @JsonProperty("message")
27 | private String message;
28 |
29 | @JSONField(name = "data")
30 | @JsonProperty("data")
31 | private Object data;
32 |
33 | @JSONField(name = "type")
34 | @JsonProperty("type")
35 | private String type;
36 |
37 | }
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/fineTuning/Hyperparameters.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.fineTuning;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.Data;
6 | import xyz.felh.openai.IOpenAiApiObject;
7 |
8 | @Data
9 | public class Hyperparameters implements IOpenAiApiObject {
10 |
11 | /**
12 | * string or integer
13 | *
14 | * The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset. "auto" decides the optimal number of epochs based on the size of the dataset. If setting the number manually, we support any number between 1 and 50 epochs.
15 | */
16 | @JSONField(name = "n_epochs")
17 | @JsonProperty("n_epochs")
18 | private Object nEpochs;
19 |
20 | /**
21 | * string or integer Optional Defaults to auto
22 | *
23 | * Number of examples in each batch. A larger batch size means that model parameters are updated less frequently, but with lower variance.
24 | */
25 | @JSONField(name = "batch_size")
26 | @JsonProperty("batch_size")
27 | private Object batchSize;
28 |
29 | /**
30 | * string or number Optional Defaults to auto
31 | *
32 | * Scaling factor for the learning rate. A smaller learning rate may be useful to avoid overfitting.
33 | */
34 | @JSONField(name = "learning_rate_multiplier")
35 | @JsonProperty("learning_rate_multiplier")
36 | private Object learningRateMultiplier;
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/image/BaseRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.image;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import xyz.felh.openai.IOpenAiApiRequest;
6 | import lombok.AllArgsConstructor;
7 | import lombok.Data;
8 | import lombok.NoArgsConstructor;
9 | import lombok.ToString;
10 | import lombok.experimental.SuperBuilder;
11 |
12 | @Data
13 | @ToString
14 | @AllArgsConstructor
15 | @NoArgsConstructor
16 | @SuperBuilder(toBuilder = true)
17 | public abstract class BaseRequest implements IOpenAiApiRequest {
18 |
19 | /**
20 | * The model to use for image generation.
21 | *
22 | * string, Optional
23 | * Defaults to dall-e-2
24 | *
25 | * When creating edit image
26 | *
27 | * The model to use for image generation. Only dall-e-2 is supported at this time.
28 | */
29 | @JSONField(name = "model")
30 | @JsonProperty("model")
31 | private String model;
32 |
33 | /**
34 | * The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 is supported.
35 | *
36 | * integer or null, Optional
37 | *
38 | * Defaults to 1
39 | */
40 | @JSONField(name = "n")
41 | @JsonProperty("n")
42 | private Integer n;
43 |
44 | /**
45 | * The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 for dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3
46 | *
47 | * string or null, Optional
48 | *
49 | * Defaults to 1024x1024
50 | *
51 | * See {@link ImageSize}
52 | */
53 | @JSONField(name = "size")
54 | @JsonProperty("size")
55 | private ImageSize size;
56 |
57 | /**
58 | * The format in which the generated images are returned. Must be one of url or b64_json.
59 | *
60 | * string or null, Optional
61 | *
62 | * Defaults to url
63 | *
64 | * See {@link ImageResponseFormat}
65 | */
66 | @JSONField(name = "response_format")
67 | @JsonProperty("response_format")
68 | private ImageResponseFormat responseFormat;
69 |
70 | /**
71 | * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
72 | *
73 | * Learn more.
74 | *
75 | * string, Optional
76 | */
77 | @JSONField(name = "user")
78 | @JsonProperty("user")
79 | private String user;
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/image/CreateImageRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.image;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import lombok.experimental.SuperBuilder;
7 |
8 | @EqualsAndHashCode(callSuper = true)
9 | @Data
10 | @AllArgsConstructor
11 | @NoArgsConstructor(force = true)
12 | @ToString(callSuper = true)
13 | @SuperBuilder(toBuilder = true)
14 | public class CreateImageRequest extends BaseRequest {
15 |
16 | /**
17 | * A text description of the desired image(s). The maximum length is 1000 characters for dall-e-2 and 4000 characters for dall-e-3.
18 | *
19 | * string, Required
20 | */
21 | @NonNull
22 | @JSONField(name = "prompt")
23 | @JsonProperty("prompt")
24 | private String prompt;
25 |
26 | /**
27 | * The quality of the image that will be generated. hd creates images with finer details and greater consistency across the image. This param is only supported for dall-e-3.
28 | *
29 | * string, Optional
30 | *
31 | * Default to standard
32 | *
33 | * See {@link ImageQuality}
34 | */
35 | @JSONField(name = "quality")
36 | @JsonProperty("quality")
37 | private ImageQuality quality;
38 |
39 | /**
40 | * The style of the generated images. Must be one of vivid or natural. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This param is only supported for dall-e-3.
41 | *
42 | * string or null, Optional
43 | *
44 | * Defaults to vivid
45 | *
46 | * See {@link ImageStyle}
47 | */
48 | @JSONField(name = "style")
49 | @JsonProperty("style")
50 | private ImageStyle style;
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/image/Image.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.image;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import xyz.felh.openai.IOpenAiApiObject;
6 | import lombok.Data;
7 |
8 | @Data
9 | public class Image implements IOpenAiApiObject {
10 |
11 | /**
12 | * The URL of the generated image, if response_format is url (default).
13 | */
14 | @JSONField(name = "url")
15 | @JsonProperty("url")
16 | private String url;
17 |
18 | /**
19 | * The base64-encoded JSON of the generated image, if response_format is b64_json.
20 | */
21 | @JSONField(name = "b64_json")
22 | @JsonProperty("b64_json")
23 | private String b64Json;
24 |
25 | /**
26 | * The prompt that was used to generate the image, if there was any revision to the prompt.
27 | */
28 | @JSONField(name = "revised_prompt")
29 | @JsonProperty("revised_prompt")
30 | private String revisedPrompt;
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/image/ImageModelType.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.image;
2 |
3 | import com.fasterxml.jackson.annotation.JsonValue;
4 | import lombok.Getter;
5 |
6 | import java.util.Arrays;
7 |
8 | @Getter
9 | public enum ImageModelType {
10 |
11 | DALL_E_2("dall-e-2"),
12 |
13 | DALL_E_3("dall-e-3");
14 |
15 | private final String value;
16 |
17 | ImageModelType(final String value) {
18 | this.value = value;
19 | }
20 |
21 | @JsonValue
22 | public String value() {
23 | return value;
24 | }
25 |
26 | public static ImageModelType findByValue(String value) {
27 | return Arrays.stream(values()).filter(it -> it.value().equals(value)).findFirst().orElse(null);
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/image/ImageQuality.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.image;
2 |
3 | import com.fasterxml.jackson.annotation.JsonValue;
4 |
5 | import java.util.Arrays;
6 |
7 | /**
8 | * This param is only supported for dall-e-3
9 | */
10 | public enum ImageQuality {
11 |
12 | STANDARD("standard"),
13 |
14 | HD("hd");
15 |
16 | private final String value;
17 |
18 | ImageQuality(final String value) {
19 | this.value = value;
20 | }
21 |
22 | @JsonValue
23 | public String value() {
24 | return value;
25 | }
26 |
27 | public static ImageQuality findByValue(String value) {
28 | return Arrays.stream(values()).filter(it -> it.value().equals(value)).findFirst().orElse(null);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/image/ImageResponse.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.image;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import xyz.felh.openai.IOpenAiApiObject;
6 | import xyz.felh.openai.IOpenAiApiResponse;
7 | import lombok.Data;
8 |
9 | import java.util.List;
10 |
11 | @Data
12 | public class ImageResponse implements IOpenAiApiResponse, IOpenAiApiObject {
13 |
14 | @JSONField(name = "created")
15 | @JsonProperty("created")
16 | private Long created;
17 |
18 | @JSONField(name = "data")
19 | @JsonProperty("data")
20 | private List
20 | * string
21 | */
22 | @NonNull
23 | @JSONField(name = "prompt")
24 | @JsonProperty("prompt")
25 | private String prompt;
26 |
27 | /**
28 | * The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
29 | *
30 | * string, Required
31 | */
32 | @JSONField(name = "image")
33 | @JsonProperty("image")
34 | private byte[] image;
35 | @JSONField(name = "image_path")
36 | @JsonProperty("image_path")
37 | private String imagePath;
38 |
39 | /**
40 | * An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
41 | *
42 | * string, Optional
43 | */
44 | @JSONField(name = "mask")
45 | @JsonProperty("mask")
46 | private byte[] mask;
47 | @JSONField(name = "mask_path")
48 | @JsonProperty("mask_path")
49 | private String maskPath;
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/image/variation/CreateVariationRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.image.variation;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import xyz.felh.openai.image.BaseRequest;
6 | import lombok.*;
7 | import lombok.experimental.SuperBuilder;
8 |
9 | @EqualsAndHashCode(callSuper = true)
10 | @Data
11 | @AllArgsConstructor
12 | @NoArgsConstructor(force = true)
13 | @ToString(callSuper = true)
14 | @SuperBuilder(toBuilder = true)
15 | public class CreateVariationRequest extends BaseRequest {
16 |
17 | /**
18 | * The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
19 | *
20 | * string, Required
21 | */
22 | @JSONField(name = "image")
23 | @JsonProperty("image")
24 | private byte[] image;
25 | @JSONField(name = "image_path")
26 | @JsonProperty("image_path")
27 | private String imagePath;
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/model/Model.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.model;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.Data;
6 | import lombok.EqualsAndHashCode;
7 | import xyz.felh.openai.OpenAiApiObjectWithId;
8 |
9 | /**
10 | * List and describe the various models available in the API.
11 | * You can refer to the Models documentation to understand what models are available and the differences between them.
12 | *
13 | * See document
14 | */
15 | @EqualsAndHashCode(callSuper = true)
16 | @Data
17 | public class Model extends OpenAiApiObjectWithId {
18 |
19 | public static String OBJECT = "model";
20 |
21 | /**
22 | * The Unix timestamp (in seconds) when the model was created.
23 | */
24 | @JSONField(name = "created")
25 | @JsonProperty("created")
26 | public Long created;
27 |
28 | /**
29 | * The organization that owns the model.
30 | */
31 | @JSONField(name = "owned_by")
32 | @JsonProperty("owned_by")
33 | private String ownedBy;
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/moderation/CreateModerationRequest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.moderation;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import lombok.*;
6 | import xyz.felh.openai.IOpenAiApiRequest;
7 |
8 | @Data
9 | @Builder
10 | @AllArgsConstructor
11 | @NoArgsConstructor(force = true)
12 | public class CreateModerationRequest implements IOpenAiApiRequest {
13 |
14 | /**
15 | * string or array Required
16 | *
17 | * The input text to classify
18 | */
19 | @NonNull
20 | @JSONField(name = "input")
21 | @JsonProperty("input")
22 | private Object input;
23 |
24 | /**
25 | * Optional Defaults to text-moderation-latest
26 | *
27 | * Two content moderations models are available: text-moderation-stable and text-moderation-latest.
28 | *
29 | * The default is text-moderation-latest which will be automatically upgraded over time. This ensures you are always
30 | * using our most accurate model. If you use text-moderation-stable, we will provide advanced notice before updating the model.
31 | * Accuracy of text-moderation-stable may be slightly lower than for text-moderation-latest.
32 | */
33 | @JSONField(name = "model")
34 | @JsonProperty("model")
35 | private String model;
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/core/src/main/java/xyz/felh/openai/moderation/CreateModerationResponse.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai.moderation;
2 |
3 | import com.alibaba.fastjson2.annotation.JSONField;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 | import xyz.felh.openai.IOpenAiApiResponse;
6 | import lombok.Data;
7 |
8 | import java.util.List;
9 |
10 | @Data
11 | public class CreateModerationResponse implements IOpenAiApiResponse {
12 |
13 | /**
14 | * The unique identifier for the moderation request.
15 | */
16 | @JSONField(name = "id")
17 | @JsonProperty("id")
18 | private String id;
19 |
20 | /**
21 | * The model used to generate the moderation results.
22 | */
23 | @JSONField(name = "model")
24 | @JsonProperty("model")
25 | private String model;
26 |
27 | /**
28 | * A list of moderation objects.
29 | *
30 | * See {@link Moderation}
31 | */
32 | @JSONField(name = "results")
33 | @JsonProperty("results")
34 | private List
34 | * Note that you can use this method to retrieve the correct encodings for snapshots of models, for
35 | * example "gpt-4-0314" or "gpt-3.5-turbo-0301".
36 | *
37 | * @param modelName the name of the model to get the encoding for
38 | * @return the encoding, if it exists
39 | */
40 | Optional
9 | * This library supports the encodings that are listed in {@link EncodingType} out of the box.
10 | * But if you want to use a custom encoding, you can use this class to pass the parameters to the library.
11 | * Use {@link EncodingRegistry#registerGptBytePairEncoding(GptBytePairEncodingParams)} to register your custom encoding
12 | * to the registry, so that you can easily use your encoding in conjunction with the predefined ones.
13 | *
14 | * The encoding parameters are:
15 | *
46 | * No further calls to this listener will be made.
47 | *
48 | * @param requestId request ID
49 | */
50 | public void onClosed(String requestId) {
51 | log.info("onClosed: {}", requestId);
52 | }
53 |
54 | /**
55 | * Invoked when an event source has been closed due to an error reading from or writing to the
56 | * network. Incoming events may have been lost. No further calls to this listener will be made.
57 | *
58 | * @param requestId request ID
59 | * @param t throwable
60 | * @param response response
61 | */
62 | public void onFailure(String requestId, Throwable t, Response response) {
63 | log.error("onFailure: {}", requestId, t);
64 | }
65 |
66 | /**
67 | * cancel eventSource
68 | */
69 | public void close() {
70 | if (eventSource != null) {
71 | eventSource.cancel();
72 | }
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/service/src/main/java/xyz/felh/baidu/BaiduAiApi.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.baidu;
2 |
3 | import io.reactivex.rxjava3.core.Single;
4 | import retrofit2.http.Body;
5 | import retrofit2.http.POST;
6 | import retrofit2.http.Path;
7 | import xyz.felh.baidu.chat.ChatCompletion;
8 | import xyz.felh.baidu.chat.CreateChatCompletionRequest;
9 | import xyz.felh.baidu.tokenizer.CreateTokenizerRequest;
10 | import xyz.felh.baidu.tokenizer.Tokenizer;
11 |
12 | /**
13 | * Retrofit2 API interface
14 | */
15 | public interface BaiduAiApi {
16 |
17 | @POST("/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/{model_path}")
18 | Single
19 | * See Options
20 | */
21 | public class SchemaUtils {
22 |
23 | public static JSONObject convert2Schema(Class> clazz) {
24 | SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12,
25 | OptionPreset.PLAIN_JSON)
26 | .without(Option.SCHEMA_VERSION_INDICATOR)
27 | .with(Option.FORBIDDEN_ADDITIONAL_PROPERTIES_BY_DEFAULT)
28 | .with(new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED));
29 | SchemaGeneratorConfig config = configBuilder.build();
30 | SchemaGenerator generator = new SchemaGenerator(config);
31 | JsonNode jsonSchema = generator.generateSchema(clazz);
32 | return JSONObject.parseObject(jsonSchema.toString());
33 | }
34 |
35 | public static void main(String[] args) {
36 | System.out.println(convert2Schema(GetWeatherParam.class));
37 | }
38 |
39 | @Data
40 | public static class GetWeatherParam {
41 | @JsonPropertyDescription("The city and state, e.g. San Francisco, CA")
42 | @JsonProperty(value = "location", required = true)
43 | private String location;
44 | @JsonProperty("unit")
45 | private Unit unit;
46 | @JsonProperty("age")
47 | private int age;
48 | }
49 |
50 | public enum Unit {
51 | celsius, fahrenheit
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/service/src/test/java/xyz/felh/openai/BaiduAiServiceTest.java:
--------------------------------------------------------------------------------
1 | package xyz.felh.openai;
2 |
3 | import com.alibaba.fastjson2.JSON;
4 | import com.alibaba.fastjson2.JSONObject;
5 | import com.fasterxml.jackson.databind.ObjectMapper;
6 | import lombok.extern.slf4j.Slf4j;
7 | import okhttp3.OkHttpClient;
8 | import org.junit.jupiter.api.Test;
9 | import retrofit2.Retrofit;
10 | import xyz.felh.StreamListener;
11 | import xyz.felh.baidu.BaiduAiApi;
12 | import xyz.felh.baidu.BaiduAiService;
13 | import xyz.felh.baidu.chat.*;
14 | import xyz.felh.baidu.interceptor.ExtractHeaderInterceptor;
15 | import xyz.felh.baidu.tokenizer.CreateTokenizerRequest;
16 |
17 | import java.time.Duration;
18 | import java.util.List;
19 | import java.util.concurrent.TimeUnit;
20 |
21 | import static xyz.felh.baidu.BaiduAiService.*;
22 |
23 | @Slf4j
24 | public class BaiduAiServiceTest {
25 |
26 |
27 | private BaiduAiService getBaiduAiService() {
28 | String ak = System.getenv("BAIDU_AK");
29 | String sk = System.getenv("BAIDU_SK");
30 | ObjectMapper mapper = defaultObjectMapper();
31 | OkHttpClient client = defaultClient(ak, sk, Duration.ofMillis(300000))
32 | .newBuilder()
33 | .addInterceptor(new ExtractHeaderInterceptor(responseHeaders -> log.debug("headers: {}", JSON.toJSONString(responseHeaders))))
34 | .build();
35 | Retrofit retrofit = defaultRetrofit(client, mapper);
36 | BaiduAiApi api = retrofit.create(BaiduAiApi.class);
37 | return new BaiduAiService(api, client);
38 | }
39 |
40 | @Test
41 | public void chat() throws InterruptedException {
42 | BaiduAiService service = getBaiduAiService();
43 |
44 | CreateChatCompletionRequest request = CreateChatCompletionRequest.builder()
45 | .messages(
46 | List.of(ChatMessage.builder()
47 | .role(ChatMessageRole.USER)
48 | .content("你好,请从1 数到20")
49 | .build())
50 | )
51 | .build();
52 | // log.info(JSON.toJSONString(service.chat(ChatCompletion.Model.ERNIE_SPEED_128K, request)));
53 |
54 | // request.setStream(true);
55 | // service.createStreamChat("asdf", ModelType.ERNIE_SPEED_128K, request, new StreamListener<>() {
56 | // @Override
57 | // public void onEvent(String requestId, ChatCompletion t) {
58 | // log.info(JSON.toJSONString(t));
59 | // }
60 | // });
61 | //
62 | // TimeUnit.MINUTES.sleep(5);
63 | log.info(JSONObject.toJSONString(service.tokenizer(CreateTokenizerRequest.builder()
64 | .prompt("asdfaksdlf哈哈哈哈")
65 | .model("ernie-4.0-8k")
66 | .build())));
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/web.felh.xyz.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forestwanglin/openai-java/15cb89d98cbf0a2683cb750eceb30cd1402380de/web.felh.xyz.png
--------------------------------------------------------------------------------
16 | *
21 | */
22 | public final class GptBytePairEncodingParams {
23 |
24 | private final String name;
25 | private final Pattern pattern;
26 | private final Map