package com.yisoo.wenxin.demo;
import cn.hutool.json.JSONUtil;
import com.yisoo.wenxin.WenXinClient;
import com.yisoo.wenxin.constant.ModelE;
import com.yisoo.wenxin.entity.ChatResponse;
import com.yisoo.wenxin.entity.MessageItem;
import com.yisoo.wenxin.sse.ConsoleExampleEventSourceListener;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class WenxinSpringbootDemoApplicationTests {
@Autowired
WenXinClient wenXinClient;
/**
* (1)messages成员不能为空,1个成员表示单轮对话,多个成员表示多轮对话
* (2)最后一个message为当前请求的信息,前面的message为历史对话信息
* (3)必须为奇数个成员,成员中message的role必须依次为user、assistant
* (4)最后一个message的content长度(即此轮对话的问题)不能超过2000个字符;
* 如果messages中content总长度大于2000字符,系统会依次遗忘最早的历史会话,直到content的总长度不超过2000个字符
*/
@Test
void contextLoads1() {
// 问答
List<MessageItem> messages = new ArrayList<>();
messages.add(MessageItem.builder().role(MessageItem.Role.USER).content("帮我用Python实现计算斐波那契数列").build());
ChatResponse chat = wenXinClient.chat(messages, ModelE.ERNIE_Bot);
System.out.println(JSONUtil.toJsonStr(chat));
/*
{"id":"as-hfgfbitirj","object":"chat.completion","created":1691127828,"isTruncated":false,"result":"当然可以!以下是一个简单的Python函数,用于计算斐波那契数列的前n项:\n\n\n```python\ndef fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib\n```\n这个函数接受一个正整数n作为输入,并返回一个包含前n个斐波那契数的列表。如果n为0或1,函数将返回相应的特殊情况。否则,它将通过迭代计算并返回前n个斐波那契数。\n\n你可以通过调用这个函数并传入你想要计算的斐波那契数列的项数来计算斐波那契数列。例如,要计算前10个斐波那契数,可以这样调用函数:\n\n\n```python\nfibonacci(10)\n```\n这将返回一个包含前10个斐波那契数的列表,如下所示:\n\n\n```python\n[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n```\n希望这可以帮助你实现斐波那契数列的计算!","needClearHistory":false,"usage":{"promptTokens":14,"completionTokens":277,"totalTokens":291}}
*/
}
@Test
void contextLoads2() throws InterruptedException {
// 流式问答
List<MessageItem> messages = new ArrayList<>();
messages.add(MessageItem.builder().role(MessageItem.Role.USER).content("帮我用Python实现计算斐波那契数列").build());
wenXinClient.streamChat(messages, new ConsoleExampleEventSourceListener(), ModelE.ERNIE_Bot);
/*
2023-08-04 13:46:08.063 INFO 9832 --- ...: 建立sse连接...
2023-08-04 13:46:08.078 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127967,"sentence_id":0,"is_end":false,"is_truncated":false,"result":"当然可以!","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":4,"total_tokens":18}}
2023-08-04 13:46:09.257 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127968,"sentence_id":1,"is_end":false,"is_truncated":false,"result":"以下是一个简单的Python函数,用于计算斐波那契数列的前n项:\n\n\n```python\ndef fibonacci(n):\n if n \u003c= 0:\n return []\n elif n == 1:\n return [0]\n ","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":45,"total_tokens":63}}
2023-08-04 13:46:10.453 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127969,"sentence_id":2,"is_end":false,"is_truncated":false,"result":"else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib\n```\n这个函数接受一个正整数n","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":42,"total_tokens":105}}
2023-08-04 13:46:10.859 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127970,"sentence_id":3,"is_end":false,"is_truncated":false,"result":"作为输入,并返回一个包含前n个斐波那契数列的列表。","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":23,"total_tokens":128}}
2023-08-04 13:46:11.255 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127970,"sentence_id":4,"is_end":false,"is_truncated":false,"result":"如果n小于或等于0,则返回一个空列表。","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":17,"total_tokens":145}}
2023-08-04 13:46:11.450 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127970,"sentence_id":5,"is_end":false,"is_truncated":false,"result":"如果n等于1,则返回一个只包含0的列表。","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":18,"total_tokens":163}}
2023-08-04 13:46:12.255 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127971,"sentence_id":6,"is_end":false,"is_truncated":false,"result":"\n\n否则,我们初始化一个包含前两个斐波那契数的列表[0, 1],然后使用一个循环来计算剩余的数字。","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":40,"total_tokens":203}}
2023-08-04 13:46:12.853 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127972,"sentence_id":7,"is_end":false,"is_truncated":false,"result":"在每次迭代中,我们将前两个数字相加,并将结果添加到列表的末尾。","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":28,"total_tokens":231}}
2023-08-04 13:46:13.652 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127972,"sentence_id":8,"is_end":false,"is_truncated":false,"result":"这个过程一直持续到我们计算出前n个数字。","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":19,"total_tokens":250}}
2023-08-04 13:46:14.452 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127973,"sentence_id":9,"is_end":false,"is_truncated":false,"result":"\n\n要使用这个函数,只需要调用它并传入你想要的斐波那契数列的项数,例如:\n\n\n```python\nprint(fibonacci(10)) # 将输出 [0, 1, 1, 2","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":44,"total_tokens":294}}
2023-08-04 13:46:15.256 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127974,"sentence_id":10,"is_end":false,"is_truncated":false,"result":", 3, 5, 8, 13, 21, 34]\n```\n这将输出前10个斐波那契数列的数字。","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":25,"total_tokens":319}}
2023-08-04 13:46:15.451 INFO 9832 --- ...: 返回数据:{"id":"as-hu9m9cwazm","object":"chat.completion","created":1691127974,"sentence_id":11,"is_end":true,"is_truncated":false,"result":"","need_clear_history":false,"usage":{"prompt_tokens":14,"completion_tokens":0,"total_tokens":319}}
2023-08-04 13:46:15.451 INFO 9832 --- ...: 返回数据结束了
2023-08-04 13:46:15.451 INFO 9832 --- ...: 关闭sse连接...
*/
}
@Test
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 百度文心一言Java库(支持问答和对话、流式输出、同步输出、SpringBoot调用样例)+使用说明.zip 特性 - 支持流式输出 - 支持同步输出 - 支持对话式,支持问答式,接口统一。 - 已支持ERNIE-Bot和ERNIE-Bot-turbo 【备注】 1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载体验! 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈!
资源推荐
资源详情
资源评论
收起资源包目录
百度文心一言Java库(支持问答和对话、流式输出、同步输出、SpringBoot调用样例)+使用说明.zip (19个子文件)
pom.xml 787B
wenxin-springboot-demo
pom.xml 2KB
src
test
java
com
yisoo
wenxin
demo
WenxinSpringbootDemoApplicationTests.java 13KB
main
resources
application.yml 210B
java
com
yisoo
wenxin
demo
WenxinSpringbootDemoApplication.java 342B
config
BaiduConfig.java 1003B
使用说明.md 7KB
wenxin-core
pom.xml 2KB
src
main
java
com
yisoo
wenxin
constant
ModelE.java 647B
WenXinClient.java 6KB
sse
ConsoleExampleEventSourceListener.java 2KB
entity
ChatUsage.java 347B
ChatResponse.java 762B
ChatBody.java 887B
MessageItem.java 1KB
exception
WenXinException.java 426B
config
WenXinConfig.java 2KB
interceptor
WenXinLogger.java 418B
WenXinInterceptor.java 808B
共 19 条
- 1
资源评论
- 2301_763135182024-03-26资源内容总结的很到位,内容详实,很受用,学到了~
.whl
- 粉丝: 3803
- 资源: 4619
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- js基础但是这个烂怂东西要求标题不能少于10个字才能上传然后我其实还没有写完之后再修订吧.md
- electron-tabs-master
- Unity3D 布朗运动算法插件 Brownian Motion
- 鼎微R16中控升级包R16-4.5.10-20170221及强制升级方法
- 鼎微R16中控升级包公版UI 2015及强制升级方法,救砖包
- 基于CSS与JavaScript的积分系统设计源码
- 生物化学作业_1_生物化学作业资料.pdf
- 基于libgdx引擎的Java开发连连看游戏设计源码
- 基于MobileNetV3的SSD目标检测算法PyTorch实现设计源码
- 基于Java JDK的全面框架设计源码学习项目
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功