|
| 1 | +package com.openai.example; |
| 2 | + |
| 3 | +import static com.openai.core.ObjectMappers.jsonMapper; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 6 | +import com.openai.client.OpenAIClientAsync; |
| 7 | +import com.openai.client.okhttp.OpenAIOkHttpClientAsync; |
| 8 | +import com.openai.core.JsonObject; |
| 9 | +import com.openai.core.JsonValue; |
| 10 | +import com.openai.models.*; |
| 11 | +import java.util.Collection; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public final class FunctionCallingAsyncExample { |
| 16 | + private FunctionCallingAsyncExample() {} |
| 17 | + |
| 18 | + public static void main(String[] args) { |
| 19 | + // Configures using one of: |
| 20 | + // - The `OPENAI_API_KEY` environment variable |
| 21 | + // - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables |
| 22 | + OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv(); |
| 23 | + |
| 24 | + ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder() |
| 25 | + .model(ChatModel.GPT_3_5_TURBO) |
| 26 | + .maxCompletionTokens(2048) |
| 27 | + .addTool(ChatCompletionTool.builder() |
| 28 | + .function(FunctionDefinition.builder() |
| 29 | + .name("get-sdk-quality") |
| 30 | + .description("Gets the quality of the given SDK.") |
| 31 | + .parameters(FunctionParameters.builder() |
| 32 | + .putAdditionalProperty("type", JsonValue.from("object")) |
| 33 | + .putAdditionalProperty( |
| 34 | + "properties", JsonValue.from(Map.of("name", Map.of("type", "string")))) |
| 35 | + .putAdditionalProperty("required", JsonValue.from(List.of("name"))) |
| 36 | + .putAdditionalProperty("additionalProperties", JsonValue.from(false)) |
| 37 | + .build()) |
| 38 | + .build()) |
| 39 | + .build()) |
| 40 | + .addUserMessage("How good are the following SDKs: OpenAI Java SDK, Unknown Company SDK") |
| 41 | + .build(); |
| 42 | + |
| 43 | + client.chat() |
| 44 | + .completions() |
| 45 | + .create(createParams) |
| 46 | + .thenAccept(completion -> completion.choices().stream() |
| 47 | + .map(ChatCompletion.Choice::message) |
| 48 | + .flatMap(message -> { |
| 49 | + message.content().ifPresent(System.out::println); |
| 50 | + return message.toolCalls().stream().flatMap(Collection::stream); |
| 51 | + }) |
| 52 | + .forEach(toolCall -> System.out.println(callFunction(toolCall.function())))) |
| 53 | + .join(); |
| 54 | + } |
| 55 | + |
| 56 | + private static String callFunction(ChatCompletionMessageToolCall.Function function) { |
| 57 | + if (!function.name().equals("get-sdk-quality")) { |
| 58 | + throw new IllegalArgumentException("Unknown function: " + function.name()); |
| 59 | + } |
| 60 | + |
| 61 | + JsonValue arguments; |
| 62 | + try { |
| 63 | + arguments = JsonValue.from(jsonMapper().readTree(function.arguments())); |
| 64 | + } catch (JsonProcessingException e) { |
| 65 | + throw new IllegalArgumentException("Bad function arguments", e); |
| 66 | + } |
| 67 | + |
| 68 | + String sdkName = ((JsonObject) arguments).values().get("name").asStringOrThrow(); |
| 69 | + if (sdkName.contains("OpenAI")) { |
| 70 | + return sdkName + ": It's robust and polished!"; |
| 71 | + } |
| 72 | + |
| 73 | + return sdkName + ": *shrug*"; |
| 74 | + } |
| 75 | +} |
0 commit comments