-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: function calling example (#213) * release: 0.22.1 --------- Co-authored-by: Tomer Aberbach <[email protected]> Co-authored-by: stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com>
- Loading branch information
1 parent
6390dfd
commit a7ff995
Showing
6 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
".": "0.22.0" | ||
".": "0.22.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
openai-java-example/src/main/java/com/openai/example/FunctionCallingAsyncExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.openai.example; | ||
|
||
import static com.openai.core.ObjectMappers.jsonMapper; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.openai.client.OpenAIClientAsync; | ||
import com.openai.client.okhttp.OpenAIOkHttpClientAsync; | ||
import com.openai.core.JsonObject; | ||
import com.openai.core.JsonValue; | ||
import com.openai.models.*; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class FunctionCallingAsyncExample { | ||
private FunctionCallingAsyncExample() {} | ||
|
||
public static void main(String[] args) { | ||
// Configures using one of: | ||
// - The `OPENAI_API_KEY` environment variable | ||
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables | ||
OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv(); | ||
|
||
ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder() | ||
.model(ChatModel.GPT_3_5_TURBO) | ||
.maxCompletionTokens(2048) | ||
.addTool(ChatCompletionTool.builder() | ||
.function(FunctionDefinition.builder() | ||
.name("get-sdk-quality") | ||
.description("Gets the quality of the given SDK.") | ||
.parameters(FunctionParameters.builder() | ||
.putAdditionalProperty("type", JsonValue.from("object")) | ||
.putAdditionalProperty( | ||
"properties", JsonValue.from(Map.of("name", Map.of("type", "string")))) | ||
.putAdditionalProperty("required", JsonValue.from(List.of("name"))) | ||
.putAdditionalProperty("additionalProperties", JsonValue.from(false)) | ||
.build()) | ||
.build()) | ||
.build()) | ||
.addUserMessage("How good are the following SDKs: OpenAI Java SDK, Unknown Company SDK") | ||
.build(); | ||
|
||
client.chat() | ||
.completions() | ||
.create(createParams) | ||
.thenAccept(completion -> completion.choices().stream() | ||
.map(ChatCompletion.Choice::message) | ||
.flatMap(message -> { | ||
message.content().ifPresent(System.out::println); | ||
return message.toolCalls().stream().flatMap(Collection::stream); | ||
}) | ||
.forEach(toolCall -> System.out.println(callFunction(toolCall.function())))) | ||
.join(); | ||
} | ||
|
||
private static String callFunction(ChatCompletionMessageToolCall.Function function) { | ||
if (!function.name().equals("get-sdk-quality")) { | ||
throw new IllegalArgumentException("Unknown function: " + function.name()); | ||
} | ||
|
||
JsonValue arguments; | ||
try { | ||
arguments = JsonValue.from(jsonMapper().readTree(function.arguments())); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalArgumentException("Bad function arguments", e); | ||
} | ||
|
||
String sdkName = ((JsonObject) arguments).values().get("name").asStringOrThrow(); | ||
if (sdkName.contains("OpenAI")) { | ||
return sdkName + ": It's robust and polished!"; | ||
} | ||
|
||
return sdkName + ": *shrug*"; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
openai-java-example/src/main/java/com/openai/example/FunctionCallingExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.openai.example; | ||
|
||
import static com.openai.core.ObjectMappers.jsonMapper; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.openai.client.OpenAIClient; | ||
import com.openai.client.okhttp.OpenAIOkHttpClient; | ||
import com.openai.core.JsonObject; | ||
import com.openai.core.JsonValue; | ||
import com.openai.models.*; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class FunctionCallingExample { | ||
private FunctionCallingExample() {} | ||
|
||
public static void main(String[] args) { | ||
// Configures using one of: | ||
// - The `OPENAI_API_KEY` environment variable | ||
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables | ||
OpenAIClient client = OpenAIOkHttpClient.fromEnv(); | ||
|
||
ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder() | ||
.model(ChatModel.GPT_3_5_TURBO) | ||
.maxCompletionTokens(2048) | ||
.addTool(ChatCompletionTool.builder() | ||
.function(FunctionDefinition.builder() | ||
.name("get-sdk-quality") | ||
.description("Gets the quality of the given SDK.") | ||
.parameters(FunctionParameters.builder() | ||
.putAdditionalProperty("type", JsonValue.from("object")) | ||
.putAdditionalProperty( | ||
"properties", JsonValue.from(Map.of("name", Map.of("type", "string")))) | ||
.putAdditionalProperty("required", JsonValue.from(List.of("name"))) | ||
.putAdditionalProperty("additionalProperties", JsonValue.from(false)) | ||
.build()) | ||
.build()) | ||
.build()) | ||
.addUserMessage("How good are the following SDKs: OpenAI Java SDK, Unknown Company SDK") | ||
.build(); | ||
|
||
client.chat().completions().create(createParams).choices().stream() | ||
.map(ChatCompletion.Choice::message) | ||
.flatMap(message -> { | ||
message.content().ifPresent(System.out::println); | ||
return message.toolCalls().stream().flatMap(Collection::stream); | ||
}) | ||
.forEach(toolCall -> System.out.println(callFunction(toolCall.function()))); | ||
} | ||
|
||
private static String callFunction(ChatCompletionMessageToolCall.Function function) { | ||
if (!function.name().equals("get-sdk-quality")) { | ||
throw new IllegalArgumentException("Unknown function: " + function.name()); | ||
} | ||
|
||
JsonValue arguments; | ||
try { | ||
arguments = JsonValue.from(jsonMapper().readTree(function.arguments())); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalArgumentException("Bad function arguments", e); | ||
} | ||
|
||
String sdkName = ((JsonObject) arguments).values().get("name").asStringOrThrow(); | ||
if (sdkName.contains("OpenAI")) { | ||
return sdkName + ": It's robust and polished!"; | ||
} | ||
|
||
return sdkName + ": *shrug*"; | ||
} | ||
} |