|
| 1 | +package io.github.alexcheng1982.springai.dashscope; |
| 2 | + |
| 3 | +import com.alibaba.dashscope.tools.FunctionDefinition; |
| 4 | +import com.alibaba.dashscope.tools.ToolFunction; |
| 5 | +import com.alibaba.dashscope.utils.JsonUtils; |
| 6 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 7 | +import com.github.victools.jsonschema.generator.Option; |
| 8 | +import com.github.victools.jsonschema.generator.OptionPreset; |
| 9 | +import com.github.victools.jsonschema.generator.SchemaGenerator; |
| 10 | +import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; |
| 11 | +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; |
| 12 | +import com.github.victools.jsonschema.generator.SchemaVersion; |
| 13 | +import io.github.alexcheng1982.springai.dashscope.api.DashscopeApi; |
| 14 | +import io.github.alexcheng1982.springai.dashscope.api.DashscopeModelName; |
| 15 | +import java.util.List; |
| 16 | +import java.util.function.Function; |
| 17 | +import org.springframework.ai.chat.prompt.Prompt; |
| 18 | +import org.springframework.ai.model.function.FunctionCallback; |
| 19 | +import org.springframework.ai.model.function.FunctionCallbackContext; |
| 20 | +import org.springframework.ai.model.function.FunctionCallbackWrapper; |
| 21 | + |
| 22 | +public class FunctionExampleMain { |
| 23 | + |
| 24 | + record AddRequest(int left, int right) { |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + record AddResponse(int result) { |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + static class AddFunctionTool implements Function<AddRequest, AddResponse> { |
| 33 | + |
| 34 | + @Override |
| 35 | + public AddResponse apply(AddRequest addRequest) { |
| 36 | + return new AddResponse(addRequest.left + addRequest.right); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + static class SimpleFunctionCallbackContext extends FunctionCallbackContext { |
| 41 | + |
| 42 | + private final FunctionCallback functionCallback; |
| 43 | + |
| 44 | + SimpleFunctionCallbackContext(FunctionCallback functionCallback) { |
| 45 | + this.functionCallback = functionCallback; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public FunctionCallback getFunctionCallback(String beanName, |
| 50 | + String defaultDescription) { |
| 51 | + return functionCallback; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + void runFunction() { |
| 56 | + SchemaGeneratorConfigBuilder configBuilder = |
| 57 | + new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, |
| 58 | + OptionPreset.PLAIN_JSON); |
| 59 | + SchemaGeneratorConfig config = configBuilder.with( |
| 60 | + Option.EXTRA_OPEN_API_FORMAT_VALUES) |
| 61 | + .without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build(); |
| 62 | + SchemaGenerator generator = new SchemaGenerator(config); |
| 63 | + ObjectNode jsonSchema = generator.generateSchema(AddFunctionTool.class); |
| 64 | + |
| 65 | + FunctionDefinition fd = FunctionDefinition.builder().name("add") |
| 66 | + .description("add two number") |
| 67 | + .parameters( |
| 68 | + JsonUtils.parseString(jsonSchema.toString()).getAsJsonObject()) |
| 69 | + .build(); |
| 70 | + var options = DashscopeChatOptions.builder() |
| 71 | + .withModel(DashscopeModelName.QWEN_MAX) |
| 72 | + .withTemperature(0.2f) |
| 73 | + .withTools(List.of( |
| 74 | + ToolFunction.builder() |
| 75 | + .type("function") |
| 76 | + .function(fd) |
| 77 | + .build() |
| 78 | + )) |
| 79 | + .withFunction("add") |
| 80 | + .build(); |
| 81 | + var tool = new AddFunctionTool(); |
| 82 | + var context = new SimpleFunctionCallbackContext( |
| 83 | + FunctionCallbackWrapper.builder(tool) |
| 84 | + .withName("add") |
| 85 | + .withDescription("add two numbers") |
| 86 | + .build()); |
| 87 | + var client = new DashscopeChatClient(new DashscopeApi(), context); |
| 88 | + var response = client.call(new Prompt("add 100 to 200", options)); |
| 89 | + System.out.println(response.getResult().getOutput().getContent()); |
| 90 | + } |
| 91 | + |
| 92 | + public static void main(String[] args) { |
| 93 | + new FunctionExampleMain().runFunction(); |
| 94 | + } |
| 95 | +} |
0 commit comments