之前我们通过阿里提供的cloud ai对接了通义千问。cloud ai对接通义千问
那么接下来我们尝试一些别的模型看一下,其实这个文章主要是表达一种对接方式,其他的都大同小异。都可以依此方法进行处理。
一、明确模型参数
本次我们对接的理论支持来自于阿里云提供的文档。阿里云大3-6b模型文档
我们看到他其实支持多种调用方式,包括sdk和http,我本人是不喜欢sdk的,因为会有冲突或者版本之类的问题,不如直接调用三方,把问题都扔到三方侧。所以我们这里来展示一下使用http的调用方式。
而且大模型的chat一般都是流式的,非流式的没啥技术含量而且效果很low。所以我们直接参考这部分内容即可,

我们看到他们的服务端其实是支持SSE的推流方式的,具体SSE是啥可以自行百度。
而流式和非流式的区别就在于请求参数的设置。如果你配置了,那大模型端就会给你按照流式响应。

在有了以上理论支持之后,我们就来测试一下。
二、代码接入
我们看到他的示例请求参数为:
curl --location 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \ # 这里写你的appkey
--header 'Content-Type: application/json' \
--header 'X-DashScope-SSE: enable' \ # 开启流式
--data '{
"model": "chatglm3-6b", # 模型名字
"input":{
"messages":[
{
"role": "user",
"content": "你好,请介绍一下故宫"
}
]
},
"parameters": {
"result_format": "message"
}
}'
所以我们可以找到关键点就在以上三处,至于如何申请appkey,可以参考官方。
那么我们接下来就使用okhttp这种支持事件响应的来对接流式的输出。
1、编写返回内容反序列化类
首先我们先来处理返回格式,我决定用一个java类来接受,具体你觉得不灵活可以直接用Json,怎么弄都行。
我们来看一下官网的响应示例格式。
{
"output":{
"choices":[{
"message":{
"content":"\n 故宫是中国北京市中心的一座明清两代的皇宫,现已成为博物馆。故宫是中国最具代表性的古建筑之一,也是世界文化遗产之一,以其丰富的文化遗产和精美的建筑艺术而闻名于世界。故宫占地面积达72万平方米,拥有9000多间房屋和70多座建筑,由大小湖泊、宫殿、花园和殿堂组成,是中国古代宫殿建筑之精华。","role":"assistant"},"finish_reason":"stop"}]},"usage":{
"total_tokens":105,"input_tokens":24,"output_tokens":81},"request_id":"9d970376-4ba3-98b8-8387-f95702280341"}
我们看到他是个字符串,然后在流式的最后一句他的finish_reason的值是stop,这时候我们就可以结束推流。
OK,我们就来接收一下。
import lombok.Data;
@Data
public class Chatglm36bResponse {
private Output output;
private Usage usage;
private String requestId;
@Data
public static class Output {
private Choice[] choices;
@Data
public static class Choice {
private Message message;
private String finishReason;
@Data
public static class Message {
private String content;
private String role;
}
}
}
@Data
public static class Usage {
private int totalTokens;
private int inputTokens;
private int outputTokens;
}
}
2、编写event事件监听器
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import java.io.IOException;
@EqualsAndHashCode(callSuper = true)
@Data
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class ChatEventSourceListener extends EventSourceListener {
private String clientId;
@Override
public void onOpen(EventSource eventSource, Response response) {
log.info("ChatEventSourceListener onOpen invoke");
super.onOpen(eventSource, response);
}
@Override
public void onEvent(EventSource eventSource, String id, String type, String data) {
log.info("ChatEventSourceListener onEvent invoke");
Chatglm36bResponse chatglm36bResponse = JSONObject.parseObject(data, Chatglm36bResponse.class);
Chatglm36bResponse.Output output = chatglm36bResponse.getOutput();
Chatglm36bResponse.Output.Choice[] choices = output.getChoices();
for (Chatglm36bResponse.Output.Choice choice : choices) {
String finishReason = choice.getFinishReason();
String content = choice.getMessage().getContent();
log.</

最低0.47元/天 解锁文章
3538

被折叠的 条评论
为什么被折叠?



