You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> ⚠️OpenAI has deprecated all Engine-based APIs. See [Deprecated Endpoints](https://github.com/TheoKanning/openai-java#deprecated-endpoints) below for more info.
3
+
> ⚠️OpenAI has deprecated all Engine-based APIs.
4
+
> See [Deprecated Endpoints](https://github.com/TheoKanning/openai-java#deprecated-endpoints) below
5
+
> for more info.
4
6
5
7
# OpenAI-Java
8
+
6
9
Java libraries for using OpenAI's GPT apis. Supports GPT-3, ChatGPT, and GPT-4.
7
10
8
11
Includes the following artifacts:
12
+
9
13
-`api` : request/response POJOs for the GPT APIs.
10
14
-`client` : a basic retrofit client for the GPT endpoints, includes the `api` module
11
-
-`service` : A basic service class that creates and calls the client. This is the easiest way to get started.
15
+
-`service` : A basic service class that creates and calls the client. This is the easiest way to
If you want to make your own client, just import the POJOs from the `api` module.
49
61
Your client will need to use snake case to work with the OpenAI API.
50
62
51
63
### Retrofit client
52
-
If you're using retrofit, you can import the `client` module and use the [OpenAiApi](client/src/main/java/com/theokanning/openai/OpenAiApi.java).
53
-
You'll have to add your auth token as a header (see [AuthenticationInterceptor](client/src/main/java/com/theokanning/openai/AuthenticationInterceptor.java))
64
+
65
+
If you're using retrofit, you can import the `client` module and use
66
+
the [OpenAiApi](client/src/main/java/com/theokanning/openai/OpenAiApi.java).
67
+
You'll have to add your auth token as a header (
68
+
see [AuthenticationInterceptor](client/src/main/java/com/theokanning/openai/AuthenticationInterceptor.java))
54
69
and set your converter factory to use snake case and only include non-null fields.
55
70
56
71
### OpenAiService
57
-
If you're looking for the fastest solution, import the `service` module and use [OpenAiService](service/src/main/java/com/theokanning/openai/service/OpenAiService.java).
58
72
59
-
> ⚠️The OpenAiService in the client module is deprecated, please switch to the new version in the service module.
73
+
If you're looking for the fastest solution, import the `service` module and
74
+
use [OpenAiService](service/src/main/java/com/theokanning/openai/service/OpenAiService.java).
75
+
76
+
> ⚠️The OpenAiService in the client module is deprecated, please switch to the new version in the
77
+
> service module.
78
+
60
79
```java
61
80
OpenAiService service =newOpenAiService("your_token");
You can create your functions and define their executors easily using the ChatFunction class, along with any of your custom classes that will serve to define their available parameters. You can also process the functions with ease, with the help of an executor called FunctionExecutor.
130
+
131
+
You can create your functions and define their executors easily using the ChatFunction class, along
132
+
with any of your custom classes that will serve to define their available parameters. You can also
133
+
process the functions with ease, with the help of an executor called FunctionExecutor.
102
134
103
135
First we declare our function parameters:
136
+
104
137
```java
105
138
public class Weather {
106
139
@JsonPropertyDescription("City and state, for example:León, Guanajuato")
@@ -122,7 +155,9 @@ public static class WeatherResponse {
122
155
}
123
156
```
124
157
125
-
Next, we declare the function itself and associate it with an executor, in this example we will fake a response from some API:
158
+
Next, we declare the function itself and associate it with an executor, in this example we will fake
159
+
a response from some API:
160
+
126
161
```java
127
162
ChatFunction.builder()
128
163
.name("get_weather")
@@ -131,82 +166,124 @@ ChatFunction.builder()
131
166
.build()
132
167
```
133
168
134
-
Then, we employ the FunctionExecutor object from the 'service' module to assist with execution and transformation into an object that is ready for the conversation:
169
+
Then, we employ the FunctionExecutor object from the 'service' module to assist with execution and
170
+
transformation into an object that is ready for the conversation:
171
+
135
172
```java
136
173
List<ChatFunction> functionList = // list with functions
137
-
FunctionExecutor functionExecutor = new FunctionExecutor(functionList);
174
+
FunctionExecutor
175
+
functionExecutor =new
176
+
177
+
FunctionExecutor(functionList);
138
178
139
179
List<ChatMessage> messages = new ArrayList<>();
140
-
ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), "Tell me the weather in Barcelona.");
141
-
messages.add(userMessage);
180
+
ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(),
ChatFunctionCall functionCall = responseMessage.getFunctionCall(); // might be null, but in this case it is certainly a call to our 'get_weather' function.
> **Note:** The `FunctionExecutor` class is part of the 'service' module.
158
207
159
-
You can also create your own function executor. The return object of `ChatFunctionCall.getArguments()` is a JsonNode for simplicity and should be able to help you with that.
208
+
You can also create your own function executor. The return object
209
+
of `ChatFunctionCall.getArguments()` is a JsonNode for simplicity and should be able to help you
210
+
with that.
160
211
161
-
For a more in-depth look, refer to a conversational example that employs functions in: [OpenAiApiFunctionsExample.java](example/src/main/java/example/OpenAiApiFunctionsExample.java).
162
-
Or for an example using functions and stream: [OpenAiApiFunctionsWithStreamExample.java](example/src/main/java/example/OpenAiApiFunctionsWithStreamExample.java)
212
+
For a more in-depth look, refer to a conversational example that employs functions
Make sure that OpenAI is available in your country.
198
270
199
271
### Why doesn't OpenAiService support x configuration option?
272
+
200
273
Many projects use OpenAiService, and in order to support them best I've kept it extremely simple.
201
274
You can create your own OpenAiApi instance to customize headers, timeouts, base urls etc.
202
-
If you want features like retry logic and async calls, you'll have to make an `OpenAiApi` instance and call it directly instead of using `OpenAiService`
275
+
If you want features like retry logic and async calls, you'll have to make an `OpenAiApi` instance
276
+
and call it directly instead of using `OpenAiService`
203
277
204
278
## Deprecated Endpoints
205
-
OpenAI has deprecated engine-based endpoints in favor of model-based endpoints.
206
-
For example, instead of using `v1/engines/{engine_id}/completions`, switch to `v1/completions` and specify the model in the `CompletionRequest`.
279
+
280
+
OpenAI has deprecated engine-based endpoints in favor of model-based endpoints.
281
+
For example, instead of using `v1/engines/{engine_id}/completions`, switch to `v1/completions` and
282
+
specify the model in the `CompletionRequest`.
207
283
The code includes upgrade instructions for all deprecated endpoints.
208
284
209
285
I won't remove the old endpoints from this library until OpenAI shuts them down.
0 commit comments