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
{{ message }}
This repository was archived by the owner on Jun 6, 2024. It is now read-only.
* Initial commit
* Updated Readme
* Added the ability to publish to the local repository.
* Stylized the README a bit more.
* Fixed typo
* Fixed visibility of variables in example
* Improved the usage of functions with an executor.
* Reverted changes.
* Added information about functions to the README.md
* Change reversed.
* Added some helper method and cleaned the example a little bit.
* 🐛 custom serialization of function_call parameter should be an object or a string
* Fixed example and added static instantiation method
* Added null check
* Set project version to SNAPSHOT and fixed vulnerable dependency
* Allowing objectmapper to be overriden in order to supply a custom object mapper which is required when using the library in Kotlin
* Updating the constructor to use the setter to be consistant
* Added usage of streams with functions
* Removed .DS_Store files and updated .gitignore
* Small fixes and improvements and added tests
* Simplified example a little and updated README
---------
Co-authored-by: Lorenzo Caenazzo <[email protected]>
Co-authored-by: David Billings <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+81-1
Original file line number
Diff line number
Diff line change
@@ -95,6 +95,70 @@ OpenAiApi api = retrofit.create(OpenAiApi.class);
95
95
OpenAiService service =newOpenAiService(api);
96
96
```
97
97
98
+
### Functions
99
+
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.
100
+
101
+
First we declare our function parameters:
102
+
```java
103
+
publicclassWeather {
104
+
@JsonPropertyDescription("City and state, for example: León, Guanajuato")
105
+
publicString location;
106
+
@JsonPropertyDescription("The temperature unit, can be 'celsius' or 'fahrenheit'")
107
+
@JsonProperty(required=true)
108
+
publicWeatherUnit unit;
109
+
}
110
+
publicenumWeatherUnit {
111
+
CELSIUS, FAHRENHEIT;
112
+
}
113
+
publicstaticclassWeatherResponse {
114
+
publicString location;
115
+
publicWeatherUnit unit;
116
+
publicint temperature;
117
+
publicString description;
118
+
119
+
// constructor
120
+
}
121
+
```
122
+
123
+
Next, we declare the function itself and associate it with an executor, in this example we will fake a response from some API:
124
+
```java
125
+
ChatFunction.builder()
126
+
.name("get_weather")
127
+
.description("Get the current weather of a location")
128
+
.executor(Weather.class, w ->newWeatherResponse(w.location, w.unit, newRandom().nextInt(50), "sunny"))
129
+
.build()
130
+
```
131
+
132
+
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:
133
+
```java
134
+
List<ChatFunction> functionList =// list with functions
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.
156
+
157
+
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.
158
+
159
+
For a more in-depth look, refer to a conversational example that employs functions in: [OpenAiApiFunctionsExample.java](example/src/main/java/example/OpenAiApiFunctionsExample.java).
160
+
Or for an example using functions and stream: [OpenAiApiFunctionsWithStreamExample.java](example/src/main/java/example/OpenAiApiFunctionsWithStreamExample.java)
161
+
98
162
### Streaming thread shutdown
99
163
If you want to shut down your process immediately after streaming responses, call `OpenAiService.shutdownExecutor()`.
100
164
This is not necessary for non-streaming calls.
@@ -103,14 +167,30 @@ This is not necessary for non-streaming calls.
103
167
All the [example](example/src/main/java/example/OpenAiApiExample.java) project requires is your OpenAI api token
Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionRequest.java
+24
Original file line number
Diff line number
Diff line change
@@ -94,4 +94,28 @@ public class ChatCompletionRequest {
94
94
* A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
95
95
*/
96
96
Stringuser;
97
+
98
+
/**
99
+
* A list of the available functions.
100
+
*/
101
+
List<ChatFunction> functions;
102
+
103
+
/**
104
+
* Controls how the model responds to function calls, as specified in the <a href="https://platform.openai.com/docs/api-reference/chat/create#chat/create-function_call">OpenAI documentation</a>.
0 commit comments