1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| private HunyuanClient client;
@Before public void init() { String SecretId = "你的ID"; String SecretKey = "你的key"; Credential cred = new Credential(SecretId, SecretKey); ClientProfile clientProfile = new ClientProfile(); client = new HunyuanClient(cred, "ap-guangzhou", clientProfile); }
@Test public void test_completions() {
try { ChatCompletionsRequest request = new ChatCompletionsRequest(); request.setModel("hunyuan-lite"); request.setStream(true); Message[] messages = new Message[1]; Message message = new Message();
message.setRole("user"); message.setContent("写一个python冒泡排序"); messages[0] = message; request.setMessages(messages);
ChatCompletionsResponse response = client.ChatCompletions(request);
if (response.isStream()) { Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); for (SSEResponseModel.SSE e : response) { ChatCompletionsResponse eventModel = gson.fromJson(e.Data, ChatCompletionsResponse.class); Choice[] choices = eventModel.getChoices(); if (choices.length > 0) { System.out.print(choices[0].getDelta().getContent()); } if ("stop".equals(choices[0].getFinishReason())) { response.close(); break; } } }
} catch (TencentCloudSDKException e) { e.printStackTrace(); } }
|