MCPcopy Create free account
hub / github.com/dashscope/dashscope-sdk-java / run

Method run

samples/AssistantFunctionCall.java:78–147  ·  view source on GitHub ↗
(String assistantId)

Source from the content-addressed store, hash-verified

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) ||
94 run.getStatus().equals(Run.Status.FAILED) ||
95 run.getStatus().equals(Run.Status.REQUIRES_ACTION)||
96 run.getStatus().equals(Run.Status.EXPIRED)){
97 break;
98 }else{
99 Thread.sleep(1000);
100 }
101 run = runs.retrieve(assistantThread.getId(), run.getId());
102 }
103 if(run.getStatus().equals(Run.Status.REQUIRES_ACTION)){
104 // submit action output.
105 RequiredAction requiredAction = run.getRequiredAction();
106 if(requiredAction.getType().equals("submit_tool_outputs")){
107 ToolCallBase toolCall = requiredAction.getSubmitToolOutputs().getToolCalls().get(0);
108 if (toolCall.getType().equals("function")) {
109 // get function call name and argument, both String.
110 String functionName = ((ToolCallFunction) toolCall).getFunction().getName();
111 String functionId = ((ToolCallFunction)toolCall).getId();
112 String functionArgument = ((ToolCallFunction) toolCall).getFunction().getArguments();
113 if (functionName.equals("add")) {
114 // Create the function object.
115 AddFunctionTool addFunction =
116 JsonUtils.fromJson(functionArgument, AddFunctionTool.class);
117 // call function.
118 int sum = addFunction.call();
119 System.out.println(sum);
120 SubmitToolOutputsParam submitToolOutputsParam = SubmitToolOutputsParam.builder()
121 .toolOutput(ToolOutput.builder().toolCallId(functionId).output(String.valueOf(sum)).build())
122 .build();
123 run = runs.submitToolOutputs(assistantThread.getId(), run.getId(), submitToolOutputsParam);
124 }
125 }
126 }
127 }
128 while(true){
129 if(run.getStatus().equals(Run.Status.CANCELLED) ||
130 run.getStatus().equals(Run.Status.COMPLETED) ||
131 run.getStatus().equals(Run.Status.FAILED) ||
132 run.getStatus().equals(Run.Status.REQUIRES_ACTION)||
133 run.getStatus().equals(Run.Status.EXPIRED)){
134 break;
135 }else{

Callers 1

mainMethod · 0.95

Calls 15

createMethod · 0.95
createMethod · 0.95
createMethod · 0.95
retrieveMethod · 0.95
getTypeMethod · 0.95
fromJsonMethod · 0.95
callMethod · 0.95
submitToolOutputsMethod · 0.95
listMethod · 0.95
builderMethod · 0.80
getIdMethod · 0.80
getToolCallsMethod · 0.80

Tested by

no test coverage detected