| 19 | import com.alibaba.dashscope.threads.runs.Runs; |
| 20 | |
| 21 | public class AssistantCallSearch { |
| 22 | static public Assistant createAssistant() throws ApiException, NoApiKeyException{ |
| 23 | AssistantParam assistantParam = AssistantParam.builder() |
| 24 | .model("qwen-max") // model must be set. |
| 25 | .description("a helper assistant") |
| 26 | .name("system") // name必须填写 |
| 27 | .instructions("You are a helpful assistant. When asked a question, use tools wherever possible.") |
| 28 | .tool(ToolQuarkSearch.builder().build()) |
| 29 | .build(); |
| 30 | Assistants assistants = new Assistants(); |
| 31 | return assistants.create(assistantParam); |
| 32 | } |
| 33 | |
| 34 | static public void run(String assistantId) throws ApiException, NoApiKeyException, InvalidateParameter, InputRequiredException, InterruptedException{ |
| 35 | // create a thread |
| 36 | Threads threads = new Threads(); |
| 37 | AssistantThread assistantThread = threads.create(ThreadParam.builder().build()); |
| 38 | |
| 39 | Runs runs = new Runs(); |
| 40 | // create a new message |
| 41 | TextMessageParam textMessageParam = TextMessageParam.builder().role("user").content("请帮忙查询今日北京天气?").build(); |
| 42 | Messages messages = new Messages(); |
| 43 | ThreadMessage threadMessage = messages.create(assistantThread.getId(), textMessageParam); |
| 44 | System.out.println(threadMessage); |
| 45 | RunParam runParam = RunParam.builder().assistantId(assistantId).build(); |
| 46 | Run run = runs.create(assistantThread.getId(), runParam); |
| 47 | while(true){ |
| 48 | if(run.getStatus().equals(Run.Status.CANCELLED) || |
| 49 | run.getStatus().equals(Run.Status.COMPLETED) || |
| 50 | run.getStatus().equals(Run.Status.FAILED) || |
| 51 | run.getStatus().equals(Run.Status.REQUIRES_ACTION)|| |
| 52 | run.getStatus().equals(Run.Status.EXPIRED)){ |
| 53 | break; |
| 54 | }else{ |
| 55 | Thread.sleep(1000); |
| 56 | } |
| 57 | run = runs.retrieve(assistantThread.getId(), run.getId()); |
| 58 | System.out.println(run); |
| 59 | } |
| 60 | |
| 61 | GeneralListParam listParam = GeneralListParam.builder().limit(100l).build(); |
| 62 | ListResult<ThreadMessage> threadMessages = messages.list(assistantThread.getId(), listParam); |
| 63 | for(ThreadMessage threadMessage2: threadMessages.getData()){ |
| 64 | System.out.println(threadMessage2); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException { |
| 70 | Assistant assistant = createAssistant(); |
| 71 | run(assistant.getId()); |
| 72 | } |
| 73 | } |
nothing calls this directly
no outgoing calls
no test coverage detected