当前位置: 首页 > article >正文

星火大模型接入及文本生成HTTP流式、非流式接口(JAVA)

文章目录

  • 一、接入星火大模型
  • 二、基于JAVA实现HTTP非流式接口
    • 1.配置
    • 2.接口实现
      • (1)分析接口请求
      • (2)代码实现
    • 3.功能测试
      • (1)测试对话功能
      • (2)测试记住上下文功能
  • 三、基于JAVA实现HTTP流式接口
    • 1.接口实现
      • (1)分析接口请求
      • (2)代码实现
    • 2.功能测试
      • (1)测试对话功能
      • (2)测试记住上下文功能

一、接入星火大模型

首先,需要在讯飞开放平台(https://passport.xfyun.cn/login)进行登录:

在这里插入图片描述

  • 点击这个+创建应用:
    在这里插入图片描述

  • 创建应用:
    在这里插入图片描述

  • 查看创建好的应用:

在这里插入图片描述

点击创建的应用可以查看各种鉴权信息,如调用http接口需要的APIPassword。

二、基于JAVA实现HTTP非流式接口

非流式,即一次性返回生成的内容,返回为一个接口;流式,即一边生成一边返回,会返回多个接口,所有接口的文本内容拼接起来构成完整的回答。

本文以模型Spark Lite为例,重要信息(APIPassword、接口地址)在这个页面查看:https://console.xfyun.cn/services/cbm
接口文档在这个地址查看:https://www.xfyun.cn/doc/spark/HTTP%E8%B0%83%E7%94%A8%E6%96%87%E6%A1%A3.html#_1-%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E

1.配置

  • 需要先创建好Spring Boot项目,配置applicaiton.yml如下:
port: 9000

# 星火大模型相关配置
spark:
  # http鉴权相关配置
  http:
    httpApiPassword: ""
  # 模型相关配置,参考https://console.xfyun.cn/services/cbm
  model:
    temperature: 0.5 #核采样阈值,用于决定结果随机性,取值越高随机性越强,即相同的问题得到的不同答案的可能性越高。取值范围 (0,1],默认为0.5
    topK: 4 # 平衡生成文本的质量和多样性,较小的 k 值会减少随机性,使得输出更加稳定;而较大的 k 值会增加随机性,产生更多新颖的输出。取值范围[1, 6],默认为4
    max_tokens: 1024 # 允许回复的最大tokens数量
    modelName: "lite"
    apiHttpUrl: "https://spark-api-open.xf-yun.com/v1/chat/completions"
    historyRememberCount: 100 # 记录的上下文条数,为0则表示单轮问答不记住上下文
  • pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spark</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.6</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.43</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.7.18</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <version>2.7.18</version>
        </dependency>
    </dependencies>

</project>

2.接口实现

(1)分析接口请求

  • 请求头:
Content-Type: application/json
Authorization: Bearer 123456
  • 请求体:
{
    "model": "generalv3.5",
    "user": "用户唯一id",
    "messages": [
        {
            "role": "system",
            "content": "你是知识渊博的助理"
        },
        {
            "role": "user",
            "content": "你好,讯飞星火"
        }
    ],
    // 下面是可选参数
    "temperature": 0.5,
    "top_k": 4,
    "stream": false,
    "max_tokens": 1024
}
  • 正常响应:
{
    "code": 0,
    "message": "Success",
    "sid": "cha000b0003@dx1905cd86d6bb86d552",
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "你好,我是由科大讯飞构建的星火认知智能模型。\n如果你有任何问题或者需要帮助的地方,请随时告诉我!我会尽力为你提供解答和支持。请问有什么可以帮到你的吗?"
            },
            "index": 0
        }
    ],
    "usage": {
        "prompt_tokens": 6,
        "completion_tokens": 42,
        "total_tokens": 48
    }
}
  • 错误响应:
{
    "error": {
        "message": "invalid user",
        "type": "api_error",
        "param": null,
        "code": null
    }
}

(2)代码实现

  • 主方法:
	//从application.yml中引入一些必要的配置
	@Value("${spark.http.httpApiPassword}")
    private String httpApiPassword;

    @Value("${spark.model.apiHttpUrl}")
    private String apiUrl;

    @Value("${spark.model.temperature}")
    private float temperature;

    @Value("${spark.model.topK}")
    private int topK;

    @Value("${spark.model.modelName}")
    private String model;

    @Value("${spark.model.max_tokens}")
    private int maxTokens;

    @Value("${spark.model.historyRememberCount}")
    private int historyRememberCount;

    private final ObjectMapper objectMapper = new ObjectMapper();
    
    public SparkResponse completions(String systemContent, String userContent, String userId) throws IOException {
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        // 设置请求头
        connection.setRequestProperty("Authorization", "Bearer " + httpApiPassword);
        connection.setRequestProperty("Content-Type", "application/json");
        // 设置请求体,发起请求
        connection.setDoOutput(true);
        connection.getOutputStream().write(buildRequestBody(systemContent, userContent, userId,false).toJSONString().getBytes());
        // 获取响应
        SparkResponse res = parseResponse(connection);
        // 将响应加到上下文中
        if(historyRememberCount>0){
            rememberCompletionsAnswer(userId,res);
        }
        return res;
    }
  • 构建请求体:
private JSONObject buildRequestBody(String systemContent, String userContent, String userId, boolean isStreaming) {
        // 加入一些大模型参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("model", model);
        requestBody.put("temperature", temperature);
        requestBody.put("top_k", topK);
        requestBody.put("max_tokens", maxTokens);
        requestBody.put("stream", isStreaming);
        JSONArray messages = new JSONArray();
        // 将历史上下文加入请求中
        if (historyRememberCount > 0&&!historyDialoguesMap.isEmpty()&&historyDialoguesMap.containsKey(userId)) {
            messages.addAll(historyDialoguesMap.get(userId));
        }
        // 构建此轮dialogue并加入到上下文中
        Dialogue messageSystem = null;
        if (StringUtils.isNotEmpty(systemContent)) {
            messageSystem = new Dialogue("system", systemContent);
            messages.add(messageSystem);
        }
        Dialogue messageUser = new Dialogue("user", userContent);
        messages.add(messageUser);
        if(historyRememberCount > 0){
            historyInsert(userId,messageSystem);
            historyInsert(userId,messageUser);
        }
        requestBody.put("messages", messages);
        return requestBody;
    }

	/**
     * 上下文添加方法
     */
    public void historyInsert(String userId, Dialogue dialogue) {
        if(dialogue == null){
            return;
        }
        if (historyDialoguesMap.containsKey(userId)) {
            List<Dialogue> historyDialogues = historyDialoguesMap.get(userId);
            historyDialogues.add(dialogue);
            if (historyDialogues.size() > historyRememberCount) {
                historyDialogues.remove(0);
            }
        }else{
            List<Dialogue> historyDialogues = new ArrayList<>();
            historyDialogues.add(dialogue);
            historyDialoguesMap.put(userId,historyDialogues);
            if (historyDialogues.size() > historyRememberCount) {
                historyDialogues.remove(0);
            }
        }
    }
  • 定义响应体类:
/**
 * 星火大模型返回类型封装
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class SparkResponse implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer code;
    private String message;
    private String sid;
    private String id;
    private Long created;
    private List<Choices> choices;
    private JSONObject usage;
    private JSONObject error;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Choices implements Serializable {

    private static final long serialVersionUID = 1L;

    private Dialogue message;
    private Dialogue delta;
    private int index;
}
/**
 * 与大模型的会话
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dialogue {
    /**
     * 角色:system  user  assistant
     */
    private String role;
    private String content;
    private int index;

    public Dialogue(String role, String content) {
        this.role=role;
        this.content=content;
    }
}
  • 获取响应:
    private SparkResponse parseResponse(HttpURLConnection connection) throws IOException {
        int responseCode = connection.getResponseCode();
        log.info("SparkHttpAPI completions responseCode:{}", responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) {
            try (InputStream inputStream = connection.getInputStream()) {
                return objectMapper.readValue(inputStream, SparkResponse.class);
            }
        } else {
            throw new SparkResponseParseException("NonStreaming Completions Result Parse Error");
        }
    }
  • 将响应加到上下文中:
 public void rememberCompletionsAnswer(String userId, SparkResponse res){
        historyInsert(userId, res.getChoices().get(0).getMessage());
    }

3.功能测试

(1)测试对话功能

 @Autowired
    private SparkHttpAPI sparkHttpAPITest;

    @Test
    void completions() {
        String systemContent = "你是知识渊博的助理";
        String userContent = "告诉我讯飞星火认知大模型的应用场景有哪些?";
        try{
            SparkResponse ans= sparkHttpAPITest.completions(systemContent,userContent,"111");
            System.out.println(ans);
        }catch (Exception e){
            log.error("测试不通过",e);
        }
    }

在这里插入图片描述

(2)测试记住上下文功能

 @Test
    void historyTest() {
        String systemContent = "你是知识渊博的律师";
        String userContent = "小王偷了小李门口的鞋子,小王犯法吗?";
        try {
            SparkResponse ans = sparkHttpAPITest.completions(systemContent, userContent, "111");
            System.out.println(ans);
            userContent = "小王犯了什么罪";
            ans = sparkHttpAPITest.completions(null, userContent, "111");
            System.out.println(ans);
        } catch (Exception e) {
            log.error("测试不通过", e);
        }
    }

在这里插入图片描述

三、基于JAVA实现HTTP流式接口

接口文档也在这个地址查看:https://www.xfyun.cn/doc/spark/HTTP%E8%B0%83%E7%94%A8%E6%96%87%E6%A1%A3.html#_1-%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E

1.接口实现

(1)分析接口请求

  • 响应格式:

相比非流式略微有一些不同,响应分成了多次,最后一个data:[DONE]为结束标志。

data:{"code":0,"message":"Success","sid":"cha000b000c@dx1905cf38fc8b86d552","id":"cha000b000c@dx1905cf38fc8b86d552","created":1719546385,"choices":[{"delta":{"role":"assistant","content":"你好"},"index":0}]}

data:{"code":0,"message":"Success","sid":"cha000b000c@dx1905cf38fc8b86d552","id":"cha000b000c@dx1905cf38fc8b86d552","created":1719546385,"choices":[{"delta":{"role":"assistant","content":",很高兴"},"index":0}]}

data:{"code":0,"message":"Success","sid":"cha000b000c@dx1905cf38fc8b86d552","id":"cha000b000c@dx1905cf38fc8b86d552","created":1719546385,"choices":[{"delta":{"role":"assistant","content":"为你解答问题"},"index":0}]}

data:{"code":0,"message":"Success","sid":"cha000b000c@dx1905cf38fc8b86d552","id":"cha000b000c@dx1905cf38fc8b86d552","created":1719546385,"choices":[{"delta":{"role":"assistant","content":"。\n"},"index":0}]}

data:{"code":0,"message":"Success","sid":"cha000b000c@dx1905cf38fc8b86d552","id":"cha000b000c@dx1905cf38fc8b86d552","created":1719546387,"choices":[{"delta":{"role":"assistant","content":"我是讯飞星火认知大模型,由科大讯飞构建的认知智能系统。"},"index":0}]}

data:{"code":0,"message":"Success","sid":"cha000b000c@dx1905cf38fc8b86d552","id":"cha000b000c@dx1905cf38fc8b86d552","created":1719546388,"choices":[{"delta":{"role":"assistant","content":"我具备与人类进行自然交流的能力,可以高效地满足各领域的认知智能需求。"},"index":0}]}

data:{"code":0,"message":"Success","sid":"cha000b000c@dx1905cf38fc8b86d552","id":"cha000b000c@dx1905cf38fc8b86d552","created":1719546389,"choices":[{"delta":{"role":"assistant","content":"无论你有什么问题或者需要帮助的地方,我都将尽我所能提供支持和解决方案。请随时告诉我你的需求!"},"index":0}]}

data:{"code":0,"message":"Success","sid":"cha000b000c@dx1905cf38fc8b86d552","id":"cha000b000c@dx1905cf38fc8b86d552","created":1719546389,"choices":[{"delta":{"role":"assistant","content":""},"index":0}],"usage":{"prompt_tokens":6,"completion_tokens":68,"total_tokens":74}}

data:[DONE]

(2)代码实现

  • 主方法:
    public void streamingCompletions(String systemContent, String userContent, Consumer<SparkResponse> responseConsumer, String userId) throws IOException {
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        // 设置请求头
        connection.setRequestProperty("Authorization", "Bearer " + httpApiPassword);
        connection.setRequestProperty("Content-Type", "application/json");
        // 设置请求体
        connection.setDoOutput(true);
        connection.getOutputStream().write(buildRequestBody(systemContent, userContent, userId,true).toJSONString().getBytes());
        // 获取响应
        parseResponseStreaming(connection, responseConsumer, userId);
    }
  • 解析响应:
private void parseResponseStreaming(HttpURLConnection connection, Consumer<SparkResponse> responseConsumer, String userId) throws IOException {
        int responseCode = connection.getResponseCode();
        log.info("SparkHttpAPI completions responseCode:{}", responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) {
            try (InputStream inputStream = connection.getInputStream();
                 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    if (!line.trim().isEmpty()) {
                        line = line.replaceFirst("data: ", "");
                        if (line.equals("[DONE]")) {
                            return;
                        }
                        try {
                            SparkResponse response = objectMapper.readValue(line, SparkResponse.class);
                            if(historyRememberCount>0){
                                rememberStreamingAnswer(userId,response);
                            }
                            responseConsumer.accept(response); // 将解析的响应传递给调用方
                        } catch (Exception e) {
                            log.error("Failed to parse response line: {}", line, e);
                        }
                    }
                }
            }
        } else {
            throw new SparkResponseParseException("Streaming Completions Result Parse Error");
        }
    }
  • 记住响应到上下文中:
public void rememberStreamingAnswer(String userId, SparkResponse res){
        historyInsert(userId, res.getChoices().get(0).getDelta());
    }

2.功能测试

(1)测试对话功能

    @Test
    void streamingCompletions() {
        String systemContent = "你是知识渊博的助理";
        String userContent = "告诉我讯飞星火认知大模型的应用场景有哪些?";
        try {
            sparkHttpAPITest.streamingCompletions(systemContent, userContent, SparkHttpResponseHandler::handleResponse, "111");
        } catch (Exception e) {
            log.error("测试不通过", e);
        }
    }
  • 其中的SparkHttpResponseHandler用于处理流式响应
/**
 * 处理流式接口响应
 */
public class SparkHttpResponseHandler {
    public static void handleResponse(SparkResponse response) {
        System.out.println("Received response: " + response);
    }
}
  • 执行结果:
2025-01-24 10:21:29.901  INFO 25120 --- [           main] org.example.llm.SparkHttpAPI             : SparkHttpAPI completions responseCode:200
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685289, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=我是科大讯飞研发, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685289, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=的以中文为核心的, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=新一代认知智能大, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=模型,我能够在与, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=人自然的对话互动, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=的过程中,同时提, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=供以下多种能力:, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content= 1. 内容生成, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=能力:我可以生成, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=多种类型多种风格, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=的内容,例如邮件, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685290, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=、文案、公文、作, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=文、对话等; 2, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=. 自然语言理解, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=能力:我可以理解, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=对话和篇章,并可, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=以实现文本摘要、, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=信息抽取、多语言, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=翻译、阅读理解、, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=情感分析等能力;, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685291, choices=[Choices(message=null, delta=Dialogue(role=assistant, content= 3. 知识问答, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=能力:我可以回答, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=各种各样的问题,, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=包括医学知识、百, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=科知识、天文地理, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=等; 4. 推理, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=能力:我拥有基于, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=思维链的推理能力, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=,能够进行科学推, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=理、常识推理等;, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685292, choices=[Choices(message=null, delta=Dialogue(role=assistant, content= 5. 数学能力, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=:我具备数学思维, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=,能理解数学问题, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=,进行计算、推理, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=、证明,并能给出, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=解题步骤。 6., index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content= 代码理解与生成, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=能力:我可以进行, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=代码理解、代码修, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=改以及代码生成等, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685293, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=工作; 此外,我, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=还具备对话游戏、, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=角色扮演等特色能, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=力,等待你的探索, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=。 我可以对多元, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=能力实现融合统一, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=,对真实场景下的, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=需求,我具备提出, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=问题、规划问题、, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=解决问题的闭环能, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685294, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=力。进一步地,我, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=可以持续从海量数, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=据和大规模知识中, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=不断学习进化,这, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=些能力使得我能够, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=在多个行业和领域, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=发挥越来越重要的, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=作用。 同时基于, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=我的能力,我将结, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=合科大讯飞以及行, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685295, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=业生态伙伴的相关, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685296, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=产品,完成多模态, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685296, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=理解和生成等相关, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685296, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=工作。, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000beda5@dx194961d49a99a4b532, id=cha000beda5@dx194961d49a99a4b532, created=1737685296, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=null, index=0), index=0)], usage={"prompt_tokens":12,"completion_tokens":304,"total_tokens":316}, error=null)

(2)测试记住上下文功能

    @Test
    void historyTest() {
        String systemContent = "你是知识渊博的律师";
        String userContent = "小王偷了小李门口的鞋子,小王犯法吗?";
        try {
            sparkHttpAPITest.streamingCompletions(systemContent, userContent, SparkHttpResponseHandler::handleResponse,"111");
            userContent = "小王犯了什么罪";
            sparkHttpAPITest.streamingCompletions(null, userContent, SparkHttpResponseHandler::handleResponse,"111");
        } catch (Exception e) {
            log.error("测试不通过", e);
        }
    }
  • 执行结果:
2025-01-24 10:24:38.667  INFO 33000 --- [           main] org.example.llm.SparkHttpAPI             : SparkHttpAPI completions responseCode:200
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685478, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685478, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=王的行为, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685478, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=是否构成犯罪, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685478, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=,取决于具体的法律, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685478, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=条文和司法解释, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685479, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685479, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=在大多数情况下,偷窃是一种违法行为,因为它侵犯了他人的财产权。然而,如果小王只是出于好奇或者误认为鞋子属于他而拿走,那么这种行为可能不会被视为犯罪。

, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685480, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=此外,如果小王的行为是出于善意,例如他误以为鞋子是自己的,那么这种行为可能不会被认定为犯罪。但是,如果他的行为导致了小李的财产损失,那么他可能需要承担相应的赔偿责任。

, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf839@dx19496202bdeb8f2532, id=cha000bf839@dx19496202bdeb8f2532, created=1737685480, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=总之,小王是否犯法需要根据具体情况来判断。如果您有关于这个问题的疑问,建议您咨询专业的律师以获取准确的法律建议。, index=0), index=0)], usage={"prompt_tokens":35,"completion_tokens":129,"total_tokens":164}, error=null)
2025-01-24 10:24:41.009  INFO 33000 --- [           main] org.example.llm.SparkHttpAPI             : SparkHttpAPI completions responseCode:200
Received response: SparkResponse(code=0, message=Success, sid=cha000bf847@dx194962034c8b8f2532, id=cha000bf847@dx194962034c8b8f2532, created=1737685481, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf847@dx194962034c8b8f2532, id=cha000bf847@dx194962034c8b8f2532, created=1737685481, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=王的行为, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf847@dx194962034c8b8f2532, id=cha000bf847@dx194962034c8b8f2532, created=1737685481, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=可能构成盗窃, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf847@dx194962034c8b8f2532, id=cha000bf847@dx194962034c8b8f2532, created=1737685481, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=罪。, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf847@dx194962034c8b8f2532, id=cha000bf847@dx194962034c8b8f2532, created=1737685482, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=根据《中华人民共和国刑法》第二百六十四条,盗窃公私财物,数额较大或者多次盗窃的,处三年以下有期徒刑、拘役或者管制,并处或者单处罚金;数额巨大或者有其他严重情节的,处三年以上十年以下有期徒刑,并处罚金;, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf847@dx194962034c8b8f2532, id=cha000bf847@dx194962034c8b8f2532, created=1737685482, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=数额特别巨大或者有其他特别严重情节的,处十年以上有期徒刑或者无期徒刑,并处罚金或者没收财产。

然而,如果小王是出于好奇或者误认为鞋子属于他而拿走,那么这种行为可能不会被视为犯罪。, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf847@dx194962034c8b8f2532, id=cha000bf847@dx194962034c8b8f2532, created=1737685483, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=此外,如果小王的行为导致了小李的财产损失,那么他可能需要承担相应的赔偿责任。

请注意,这只是一般情况下的法律解释,具体情况需要根据案件的具体事实和证据来判断。, index=0), index=0)], usage=null, error=null)
Received response: SparkResponse(code=0, message=Success, sid=cha000bf847@dx194962034c8b8f2532, id=cha000bf847@dx194962034c8b8f2532, created=1737685483, choices=[Choices(message=null, delta=Dialogue(role=assistant, content=如果您有关于这个问题的疑问,建议您咨询专业的律师以获取准确的法律建议。, index=0), index=0)], usage={"prompt_tokens":218,"completion_tokens":175,"total_tokens":393}, error=null)


http://www.kler.cn/a/517531.html

相关文章:

  • C#,入门教程(05)——Visual Studio 2022源程序(源代码)自动排版的功能动画图示
  • kafka-保姆级配置说明(consumer)
  • Unity|小游戏复刻|见缝插针1(C#)
  • ray.rllib-入门实践-11: 自定义模型/网络
  • DELL EDI:需求分析及注意事项
  • 理解 Rust 的所有权:内存管理的独特之道
  • 【深度学习基础】多层感知机 | 数值稳定性和模型初始化
  • 软件开发学习路线——roadmap
  • java.io包中StringWriter类和PrintWriter类作用
  • 上海亚商投顾:沪指冲高回落 大金融板块全天强势 上海亚商投
  • rust学习-rust中的保留字
  • python编写Socket程序
  • vue3表格数据分2个表格序号连续展示
  • 基于亿坊PHP框架构建物联网解决方案的优势分析!
  • 【QT】- QThread类介绍和线程的创建
  • 「 机器人 」扑翼飞行器的数据驱动建模核心方法
  • Django基础之ORM
  • arm-linux平台、rk3288 SDL移植
  • (算法竞赛)使用广度优先搜索(BFS)解决迷宫最短路径问题
  • 14.杂谈:领域知识库与知识图谱:概念、关系与重要性
  • 虚拟头节点和双指针解决链表问题(合并,与分解操作,力扣题目为例)
  • 微信小程序date picker的一些说明
  • C++实现设计模式---职责链模式 (Chain of Responsibility)
  • Python结构
  • CMake函数参数
  • 前端【8】HTML+CSS+javascript实战项目----实现一个简单的待办事项列表 (To-Do List)