腾讯混元AI模型使用

文档示例地址:https://github.com/TencentCloud/tencentcloud-sdk-java/blob/master/pom.xmlicon-default.png?t=N7T8https://github.com/TencentCloud/tencentcloud-sdk-java/blob/master/pom.xml

依赖坐标(腾讯云所有产品sdk下载到本地)

<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>3.1.1014</version>
</dependency>

单独引入

<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java-hunyuan</artifactId>
    <version>3.1.1010</version>
</dependency>
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.SSEResponseModel;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.hunyuan.v20230901.HunyuanClient;
import com.tencentcloudapi.hunyuan.v20230901.models.ChatStdRequest;
import com.tencentcloudapi.hunyuan.v20230901.models.ChatStdResponse;
import com.tencentcloudapi.hunyuan.v20230901.models.Choice;
import com.tencentcloudapi.hunyuan.v20230901.models.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping
public class HunYuanAi {
    @GetMapping("/ai")
    public String streamRes(String wd, HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        String SecretKey = "SecretKey";
        String SecretId = "SecretId";
        try {
            //核心设置数据流格式响应头
            response.setContentType("text/event-stream");
            response.setCharacterEncoding("UTF-8");
            Credential cred = new Credential(SecretId, SecretKey);
            ClientProfile clientProfile = new ClientProfile();
            HunyuanClient client = new HunyuanClient(cred, "ap-guangzhou", clientProfile);
            ChatStdRequest req = new ChatStdRequest();
            Message msg = new Message();
            msg.setRole("user");
            msg.setContent(wd);
            req.setMessages(new Message[]{msg});
            req.setStream(true);
            ChatStdResponse resp = client.ChatStd(req);
            if (req.getStream()) {
                Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
                for (SSEResponseModel.SSE e : resp) {
                    ChatStdResponse eventModel = gson.fromJson(e.Data, ChatStdResponse.class);
                    Choice[] choices = eventModel.getChoices();
                    if (choices.length > 0) {
                        String res = choices[0].getDelta().getContent();
                        outputStream.write(res.getBytes());
                        outputStream.flush();
                    }
                    // 如果希望在任意时刻中止事件流, 使用 resp.close() + break
                    boolean iWantToCancelNow = false;
                    if (iWantToCancelNow) {
                        outputStream.close();
                        resp.close();
                        break;
                    }
                }
            }
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return "";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值