| 34 | import com.alibaba.dashscope.tools.ToolCallFunction; |
| 35 | |
| 36 | public class AssistantFunctionCall { |
| 37 | public class AddFunctionTool { |
| 38 | private int left; |
| 39 | private int right; |
| 40 | |
| 41 | public AddFunctionTool(int left, int right) { |
| 42 | this.left = left; |
| 43 | this.right = right; |
| 44 | } |
| 45 | |
| 46 | public int call() { |
| 47 | return left + right; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static ToolFunction buildFunction() { |
| 52 | SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, |
| 53 | OptionPreset.PLAIN_JSON); |
| 54 | SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES) |
| 55 | .without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build(); |
| 56 | SchemaGenerator generator = new SchemaGenerator(config); |
| 57 | |
| 58 | // generate jsonSchema of function. |
| 59 | ObjectNode jsonSchema = generator.generateSchema(AddFunctionTool.class); |
| 60 | |
| 61 | // call with tools of function call, jsonSchema.toString() is jsonschema String. |
| 62 | FunctionDefinition fd = FunctionDefinition.builder().name("add").description("add two number") |
| 63 | .parameters(JsonUtils.parseString(jsonSchema.toString()).getAsJsonObject()).build(); |
| 64 | return ToolFunction.builder().function(fd).build(); |
| 65 | } |
| 66 | static public Assistant createAssistant() throws ApiException, NoApiKeyException{ |
| 67 | AssistantParam assistantParam = AssistantParam.builder() |
| 68 | .model("qwen-max") // model must be set. |
| 69 | .description("a helper assistant") |
| 70 | .name("system") // name必须填写 |
| 71 | .instructions("You are a helpful assistant. When asked a question, use tools wherever possible.") |
| 72 | .tool(buildFunction()) |
| 73 | .build(); |
| 74 | Assistants assistants = new Assistants(); |
| 75 | return assistants.create(assistantParam); |
| 76 | } |
| 77 | |
| 78 | static public void run(String assistantId) throws ApiException, NoApiKeyException, InvalidateParameter, InputRequiredException, InterruptedException{ |
| 79 | // create a thread |
| 80 | Threads threads = new Threads(); |
| 81 | AssistantThread assistantThread = threads.create(ThreadParam.builder().build()); |
| 82 | |
| 83 | Runs runs = new Runs(); |
| 84 | // create a new message |
| 85 | TextMessageParam textMessageParam = TextMessageParam.builder().role("user").content("Add 87787 to 788988737.").build(); |
| 86 | Messages messages = new Messages(); |
| 87 | ThreadMessage threadMessage = messages.create(assistantThread.getId(), textMessageParam); |
| 88 | System.out.println(threadMessage); |
| 89 | RunParam runParam = RunParam.builder().assistantId(assistantId).build(); |
| 90 | Run run = runs.create(assistantThread.getId(), runParam); |
| 91 | while(true){ |
| 92 | if(run.getStatus().equals(Run.Status.CANCELLED) || |
| 93 | run.getStatus().equals(Run.Status.COMPLETED) || |
nothing calls this directly
no outgoing calls
no test coverage detected