diff --git a/samples/src/main/ai.z.openapi.samples/FunctionCallingExample.java b/samples/src/main/ai.z.openapi.samples/FunctionCallingExample.java new file mode 100644 index 0000000..8c8cdf6 --- /dev/null +++ b/samples/src/main/ai.z.openapi.samples/FunctionCallingExample.java @@ -0,0 +1,89 @@ +package ai.z.openapi.samples; + +import ai.z.openapi.ZaiClient; +import ai.z.openapi.service.model.*; +import ai.z.openapi.core.Constants; +import java.util.*; + +public class FunctionCallingExample { + + // Simulate weather API + public static Map getWeather(String location, String date) { + Map weather = new HashMap<>(); + weather.put("location", location); + weather.put("date", date != null ? date : "today"); + weather.put("weather", "sunny"); + weather.put("temperature", "25°C"); + weather.put("humidity", "60%"); + return weather; + } + + // Simulate stock API + public static Map getStockPrice(String symbol) { + Map stock = new HashMap<>(); + stock.put("symbol", symbol); + stock.put("price", 150.25); + stock.put("change", "+2.5%"); + return stock; + } + + public static void main(String[] args) { + ZaiClient client = ZaiClient.builder().apiKey("your_api_key").build(); + + // Define function tools + Map properties = new HashMap<>(); + ChatFunctionParameterProperty locationProperty = ChatFunctionParameterProperty + .builder().type("string").description("City name, for example: Beijing").build(); + properties.put("location", locationProperty); + ChatFunctionParameterProperty unitProperty = ChatFunctionParameterProperty + .builder().type("string").enums(Arrays.asList("celsius", "fahrenheit")).build(); + properties.put("unit", unitProperty); + ChatTool weatherTool = ChatTool.builder() + .type(ChatToolType.FUNCTION.value()) + .function(ChatFunction.builder() + .name("get_weather") + .description("Get weather information for a specified location") + .parameters(ChatFunctionParameters.builder() + .type("object") + .properties(properties) + .required(Collections.singletonList("location")) + .build()) + .build()) + .build(); + + // Create request + ChatCompletionCreateParams request = ChatCompletionCreateParams.builder() + .model(Constants.ModelChatGLM4_5) + .messages(Collections.singletonList( + ChatMessage.builder() + .role(ChatMessageRole.USER.value()) + .content("How's the weather in Beijing today?") + .build() + )) + .tools(Collections.singletonList(weatherTool)) + .toolChoice("auto") + .build(); + + // Send request + ChatCompletionResponse response = client.chat().createChatCompletion(request); + + if (response.isSuccess()) { + // Handle function calling + ChatMessage assistantMessage = response.getData().getChoices().get(0).getMessage(); + if (assistantMessage.getToolCalls() != null && !assistantMessage.getToolCalls().isEmpty()) { + for (ToolCalls toolCall : assistantMessage.getToolCalls()) { + String functionName = toolCall.getFunction().getName(); + + if ("get_weather".equals(functionName)) { + Map result = getWeather("Beijing", null); + System.out.println("Weather info: " + result); + } + } + } else { + System.out.println(assistantMessage.getContent()); + } + } else { + System.err.println("Error: " + response.getMsg()); + } + } +} \ No newline at end of file