createChatCompletion(CreateChatCompletionRequest request) {
17 | String createChatCompletionUrl = "/chat/completions";
18 | return client.post()
19 | .uri(createChatCompletionUrl)
20 | .bodyValue(request)
21 | .retrieve()
22 | .bodyToMono(CreateChatCompletionResponse.class);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/io/github/reactiveclown/openaiwebfluxclient/client/chat/ChoiceData.java:
--------------------------------------------------------------------------------
1 | package io.github.reactiveclown.openaiwebfluxclient.client.chat;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 |
5 | public record ChoiceData(@JsonProperty("index") Integer index,
6 | @JsonProperty("message") MessageData message,
7 | @JsonProperty("finish_reason") String finishReason) {
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/io/github/reactiveclown/openaiwebfluxclient/client/chat/CreateChatCompletionRequest.java:
--------------------------------------------------------------------------------
1 | package io.github.reactiveclown.openaiwebfluxclient.client.chat;
2 |
3 | import com.fasterxml.jackson.annotation.JsonInclude;
4 | import com.fasterxml.jackson.annotation.JsonInclude.Include;
5 | import com.fasterxml.jackson.annotation.JsonProperty;
6 |
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | /**
11 | * CreateChatCompletionRequest.
12 | *
13 | * @param model - ID of the model to use.
14 | * See the model endpoint compatibility table for details on which models work with the Chat API.
15 | * @param messages - The messages to generate chat completions for, in the chat format.
16 | * @param temperature - What sampling temperature to use, between 0 and 2.
17 | * Higher values like 0.8 will make the output more random,
18 | * while lower values like 0.2 will make it more focused and deterministic.
19 | *
20 | * We generally recommend altering this or top_p but not both.
21 | * @param topP - An alternative to sampling with temperature,
22 | * called nucleus sampling,
23 | * where the model considers the results of the tokens with top_p probability mass.
24 | * So 0.1 means only the tokens comprising the top 10% probability mass are considered.
25 | *
26 | * We generally recommend altering this or temperature but not both.
27 | * @param n - How many chat completion choices to generate for each input message.
28 | * @param stop - Up to 4 sequences where the API will stop generating further tokens.
29 | * @param maxTokens - The maximum number of tokens to generate in the chat completion.
30 | *
31 | * The total length of input tokens and generated tokens
32 | * is limited by the model's context length.
33 | * @param presencePenalty - Number between -2.0 and 2.0.
34 | * Positive values penalize new tokens based on whether they appear in the text so far,
35 | * increasing the model's likelihood to talk about new topics.
36 | * @param frequencyPenalty - Number between -2.0 and 2.0.
37 | * Positive values penalize new tokens based on their existing frequency in the text so far,
38 | * decreasing the model's likelihood to repeat the same line verbatim.
39 | * @param logitBias - Modify the likelihood of specified tokens appearing in the completion.
40 | *
41 | * Accepts a json object that maps tokens (specified by their token ID in the tokenizer)
42 | * to an associated bias value from -100 to 100. Mathematically,
43 | * the bias is added to the logits generated by the model prior to sampling.
44 | * The exact effect will vary per model,
45 | * but values between -1 and 1 should decrease or increase likelihood of selection;
46 | * values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
47 | * @param user - A unique identifier representing your end-user,
48 | * which can help OpenAI to monitor and detect abuse
49 | */
50 | @JsonInclude(Include.NON_NULL)
51 | public record CreateChatCompletionRequest(@JsonProperty("model") String model,
52 | @JsonProperty("messages") List messages,
53 | @JsonProperty("temperature") Double temperature,
54 | @JsonProperty("top_p") Double topP,
55 | @JsonProperty("n") Integer n,
56 | @JsonProperty("stop") List stop,
57 | @JsonProperty("max_tokens") Integer maxTokens,
58 | @JsonProperty("presence_penalty") Double presencePenalty,
59 | @JsonProperty("frequency_penalty") Double frequencyPenalty,
60 | @JsonProperty("logit_bias") Map logitBias,
61 | @JsonProperty("user") String user) {
62 | public CreateChatCompletionRequest {
63 | if (model == null || model.isBlank())
64 | throw new IllegalArgumentException("model value can't be null or blank");
65 |
66 | if (messages == null || messages.isEmpty())
67 | throw new IllegalArgumentException("messages can't be null or empty");
68 | }
69 |
70 | public static Builder builder(String model, List messages) {
71 | return new Builder(model, messages);
72 | }
73 |
74 | public static final class Builder {
75 | private final String model;
76 | private final List messages;
77 | private Double temperature;
78 | private Double topP;
79 | private Integer n;
80 | private List stop;
81 | private Integer maxTokens;
82 | private Double presencePenalty;
83 | private Double frequencyPenalty;
84 | private Map logitBias;
85 | private String user;
86 |
87 | public CreateChatCompletionRequest build() {
88 | return new CreateChatCompletionRequest(
89 | model, messages, temperature,
90 | topP, n, stop, maxTokens,
91 | presencePenalty, frequencyPenalty, logitBias,
92 | user);
93 | }
94 |
95 | public Builder(String model, List messages) {
96 | this.model = model;
97 | this.messages = messages;
98 | }
99 |
100 | public Builder temperature(Double temperature) {
101 | this.temperature = temperature;
102 | return this;
103 | }
104 |
105 | public Builder topP(Double topP) {
106 | this.topP = topP;
107 | return this;
108 | }
109 |
110 | public Builder n(Integer n) {
111 | this.n = n;
112 | return this;
113 | }
114 |
115 | public Builder stop(String stop) {
116 | this.stop = List.of(stop);
117 | return this;
118 | }
119 |
120 | public Builder stop(List stop) {
121 | this.stop = stop;
122 | return this;
123 | }
124 |
125 | public Builder maxTokens(Integer maxTokens) {
126 | this.maxTokens = maxTokens;
127 | return this;
128 | }
129 |
130 | public Builder presencePenalty(Double presencePenalty) {
131 | this.presencePenalty = presencePenalty;
132 | return this;
133 | }
134 |
135 | public Builder frequencyPenalty(Double frequencyPenalty) {
136 | this.frequencyPenalty = frequencyPenalty;
137 | return this;
138 | }
139 |
140 | public Builder logitBias(Map logitBias) {
141 | this.logitBias = logitBias;
142 | return this;
143 | }
144 |
145 | public Builder user(String user) {
146 | this.user = user;
147 | return this;
148 | }
149 |
150 | }
151 | }
--------------------------------------------------------------------------------
/src/main/java/io/github/reactiveclown/openaiwebfluxclient/client/chat/CreateChatCompletionResponse.java:
--------------------------------------------------------------------------------
1 | package io.github.reactiveclown.openaiwebfluxclient.client.chat;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import io.github.reactiveclown.openaiwebfluxclient.client.UsageData;
5 |
6 | import java.util.List;
7 |
8 | public record CreateChatCompletionResponse(@JsonProperty("id") String id,
9 | @JsonProperty("object") String object,
10 | @JsonProperty("created") Long created,
11 | @JsonProperty("model") String model,
12 | @JsonProperty("choices") List choices,
13 | @JsonProperty("usage") UsageData usage) {
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/src/main/java/io/github/reactiveclown/openaiwebfluxclient/client/chat/MessageData.java:
--------------------------------------------------------------------------------
1 | package io.github.reactiveclown.openaiwebfluxclient.client.chat;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 |
5 | public record MessageData(@JsonProperty("role") String role,
6 | @JsonProperty("content") String content){}
7 |
--------------------------------------------------------------------------------
/src/main/java/io/github/reactiveclown/openaiwebfluxclient/client/completions/CompletionsService.java:
--------------------------------------------------------------------------------
1 | package io.github.reactiveclown.openaiwebfluxclient.client.completions;
2 |
3 | import reactor.core.publisher.Mono;
4 |
5 | public interface CompletionsService {
6 |
7 | /**
8 | * Creates a completion for the provided prompt and parameters.
9 | *
10 | * @param request {@link CreateCompletionRequest}
11 | * @return A Mono of {@link CreateCompletionResponse}
12 | */
13 | Mono createCompletion(CreateCompletionRequest request);
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/io/github/reactiveclown/openaiwebfluxclient/client/completions/CompletionsServiceImpl.java:
--------------------------------------------------------------------------------
1 | package io.github.reactiveclown.openaiwebfluxclient.client.completions;
2 |
3 | import org.springframework.stereotype.Service;
4 | import org.springframework.web.reactive.function.client.WebClient;
5 | import reactor.core.publisher.Mono;
6 |
7 | @Service
8 | public class CompletionsServiceImpl implements CompletionsService{
9 |
10 | private final WebClient client;
11 |
12 | public CompletionsServiceImpl(WebClient client){
13 | this.client = client;
14 | }
15 |
16 | @Override
17 | public Mono createCompletion(CreateCompletionRequest request) {
18 | String createCompletionUri = "/completions";
19 | return client.post()
20 | .uri(createCompletionUri)
21 | .bodyValue(request)
22 | .retrieve()
23 | .bodyToMono(CreateCompletionResponse.class);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/io/github/reactiveclown/openaiwebfluxclient/client/completions/CreateCompletionRequest.java:
--------------------------------------------------------------------------------
1 | package io.github.reactiveclown.openaiwebfluxclient.client.completions;
2 |
3 | import com.fasterxml.jackson.annotation.JsonInclude;
4 | import com.fasterxml.jackson.annotation.JsonInclude.Include;
5 | import com.fasterxml.jackson.annotation.JsonProperty;
6 |
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | /**
11 | * CreateCompletionRequest.
12 | *
13 | * @param model - ID of the model to use.
14 | * You can use the List models API to see all of your available models,
15 | * or see our Model overview for descriptions of them.
16 | * @param prompt - The prompt(s) to generate completions for,
17 | * encoded as a string, array of strings, array of tokens, or array of token arrays.
18 | *
19 | * Note that {@literal <}|endoftext|{@literal >} is the document separator that the model sees during training,
20 | * so if a prompt is not specified the model will generate as if
21 | * from the beginning of a new document.
22 | * @param suffix - The suffix that comes after a completion of inserted text.
23 | * @param maxTokens - The maximum number of tokens to generate in the completion.
24 | *
25 | * The token count of your prompt plus max_tokens cannot exceed the model's context length.
26 | * Most models have a context length of 2048 tokens
27 | * (except for the newest models, which support 4096).
28 | * @param temperature - What sampling temperature to use, between 0 and 2.
29 | * Higher values like 0.8 will make the output more random,
30 | * while lower values like 0.2 will make it more focused and deterministic.
31 | *
32 | * We generally recommend altering this or top_p but not both.
33 | * @param topP - An alternative to sampling with temperature, called nucleus sampling,
34 | * where the model considers the results of the tokens with top_p probability mass.
35 | * So 0.1 means only the tokens comprising the top 10% probability mass are considered.
36 | *
37 | * We generally recommend altering this or temperature but not both.
38 | * @param n - How many completions to generate for each prompt.
39 | *
40 | * Note: Because this parameter generates many completions,
41 | * it can quickly consume your token quota.
42 | * Use carefully and ensure that you have reasonable settings for max_tokens and stop.
43 | * @param logprobs - Include the log probabilities on the logprobs most likely tokens,
44 | * as well the chosen tokens. For example, if logprobs is 5,
45 | * the API will return a list of the 5 most likely tokens.
46 | * The API will always return the logprob of the sampled token,
47 | * so there may be up to logprobs+1 elements in the response.
48 | *
49 | * The maximum value for logprobs is 5. If you need more than this,
50 | * please contact us through our Help center and describe your use case.
51 | * @param echo - Echo back the prompt in addition to the completion
52 | * @param stop - Up to 4 sequences where the API will stop generating further tokens.
53 | * The returned text will not contain the stop sequence
54 | * @param presencePenalty - Number between -2.0 and 2.0.
55 | * Positive values penalize new tokens based on whether they appear in the text so far,
56 | * increasing the model's likelihood to talk about new topics.
57 | * @param frequencyPenalty - Number between -2.0 and 2.0.
58 | * Positive values penalize new tokens based on their existing frequency in the text so far,
59 | * decreasing the model's likelihood to repeat the same line verbatim.
60 | * @param bestOf - Generates best_of completions server-side and returns the "best"
61 | * (the one with the highest log probability per token). Results cannot be streamed.
62 | *
63 | * When used with n, best_of controls
64 | * the number of candidate completions
65 | * and n specifies how many to return – best_of must be greater than n.
66 | *
67 | * Note: Because this parameter generates many completions,
68 | * it can quickly consume your token quota. Use carefully and ensure
69 | * that you have reasonable settings for max_tokens and stop.
70 | * @param logitBias - Modify the likelihood of specified tokens appearing in the completion.
71 | *
72 | * Accepts a json object that maps tokens
73 | * (specified by their token ID in the GPT tokenizer)
74 | * to an associated bias value from -100 to 100.
75 | * Mathematically, the bias is added to the logits generated by the model prior to sampling.
76 | * The exact effect will vary per model,
77 | * but values between -1 and 1 should decrease or increase likelihood of selection;
78 | * values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
79 | *
80 | * As an example,
81 | * you can pass {"50256": -100} to prevent the {@literal <}|endoftext|{@literal >} token from being generated.
82 | * @param user - A unique identifier representing your end-user,
83 | * which can help OpenAI to monitor and detect abuse.
84 | */
85 | @JsonInclude(Include.NON_NULL)
86 | public record CreateCompletionRequest(@JsonProperty("model") String model,
87 | @JsonProperty("prompt") List> prompt,
88 | @JsonProperty("suffix") String suffix,
89 | @JsonProperty("max_tokens") Integer maxTokens,
90 | @JsonProperty("temperature") Double temperature,
91 | @JsonProperty("top_p") Double topP,
92 | @JsonProperty("n") Integer n,
93 | @JsonProperty("logprobs") Integer logprobs,
94 | @JsonProperty("echo") Boolean echo,
95 | @JsonProperty("stop") List stop,
96 | @JsonProperty("presence_penalty") Double presencePenalty,
97 | @JsonProperty("frequency_penalty") Double frequencyPenalty,
98 | @JsonProperty("best_of") Integer bestOf,
99 | @JsonProperty("logit_bias") Map logitBias,
100 | @JsonProperty("user") String user) {
101 | public CreateCompletionRequest {
102 | if (model == null || model.isBlank())
103 | throw new IllegalArgumentException("model value can't be null or blank");
104 | }
105 |
106 | public static Builder builder(String model){
107 | return new Builder(model);
108 | }
109 |
110 | public static final class Builder {
111 | private final String model;
112 | private List> prompt;
113 | private String suffix;
114 | private Integer maxTokens;
115 | private Double temperature;
116 | private Double topP;
117 | private Integer n;
118 | private Integer logprobs;
119 | private Boolean echo;
120 | private List stop;
121 | private Double presencePenalty;
122 | private Double frequencyPenalty;
123 | private Integer bestOf;
124 | private Map logitBias;
125 | private String user;
126 |
127 | public CreateCompletionRequest build() {
128 | return new CreateCompletionRequest(
129 | model, prompt, suffix,
130 | maxTokens, temperature, topP,
131 | n, logprobs,
132 | echo, stop, presencePenalty,
133 | frequencyPenalty, bestOf, logitBias,
134 | user);
135 | }
136 |
137 | public Builder(String model) {
138 | this.model = model;
139 | }
140 |
141 | public Builder prompt(String prompt) {
142 | this.prompt = List.of(List.of(prompt));
143 | return this;
144 | }
145 |
146 | public Builder promptList(List prompt) {
147 | this.prompt = List.of(prompt);
148 | return this;
149 | }
150 |
151 | public Builder promptListOfLists(List> prompt) {
152 | this.prompt = prompt;
153 | return this;
154 | }
155 |
156 | public Builder suffix(String suffix) {
157 | this.suffix = suffix;
158 | return this;
159 | }
160 |
161 | public Builder maxTokens(Integer maxTokens) {
162 | this.maxTokens = maxTokens;
163 | return this;
164 | }
165 |
166 | public Builder temperature(Double temperature) {
167 | this.temperature = temperature;
168 | return this;
169 | }
170 |
171 | public Builder topP(Double topP) {
172 | this.topP = topP;
173 | return this;
174 | }
175 |
176 | public Builder n(Integer n) {
177 | this.n = n;
178 | return this;
179 | }
180 |
181 | public Builder logprobs(Integer logprobs) {
182 | this.logprobs = logprobs;
183 | return this;
184 | }
185 |
186 | public Builder echo(Boolean echo) {
187 | this.echo = echo;
188 | return this;
189 | }
190 |
191 | public Builder stop(String stop) {
192 | this.stop = List.of(stop);
193 | return this;
194 | }
195 |
196 | public Builder stop(List stop) {
197 | this.stop = stop;
198 | return this;
199 | }
200 |
201 | public Builder presencePenalty(Double presencePenalty) {
202 | this.presencePenalty = presencePenalty;
203 | return this;
204 | }
205 |
206 | public Builder frequencyPenalty(Double frequencyPenalty) {
207 | this.frequencyPenalty = frequencyPenalty;
208 | return this;
209 | }
210 |
211 | public Builder bestOf(Integer bestOf) {
212 | this.bestOf = bestOf;
213 | return this;
214 | }
215 |
216 | public Builder logitBias(Map logitBias) {
217 | this.logitBias = logitBias;
218 | return this;
219 | }
220 |
221 | public Builder user(String user) {
222 | this.user = user;
223 | return this;
224 | }
225 |
226 | }
227 | }
228 |
229 |
--------------------------------------------------------------------------------
/src/main/java/io/github/reactiveclown/openaiwebfluxclient/client/completions/CreateCompletionResponse.java:
--------------------------------------------------------------------------------
1 | package io.github.reactiveclown.openaiwebfluxclient.client.completions;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import io.github.reactiveclown.openaiwebfluxclient.client.UsageData;
5 |
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | public record CreateCompletionResponse(@JsonProperty("id") String id,
10 | @JsonProperty("object") String object,
11 | @JsonProperty("created") Long created,
12 | @JsonProperty("model") String model,
13 | @JsonProperty("choices") List choices,
14 | @JsonProperty("usage") UsageData usage) {
15 | }
16 |
17 | record Choice(@JsonProperty("text") String text,
18 | @JsonProperty("index") Integer index,
19 | @JsonProperty("logprobs") Logprobs logprobs,
20 | @JsonProperty("finish_reason") String finishReason) {
21 | }
22 |
23 | record Logprobs(@JsonProperty("tokens") List tokens,
24 | @JsonProperty("token_logprobs") List tokenLogprobs,
25 | @JsonProperty("top_logprobs") List