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

spring-ai-alibaba 接入Tushare查询股票行情

最近spring-ai-alibaba主干分支新增了对Tushare的支持,一起来看看如何使用

简单样例

老样子,分三步进行:

第一步:添加依赖

		<dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter-tool-calling-tushare</artifactId></dependency>

第二步:添加配置

spring.ai.alibaba.toolcalling.tushare.token=your token

第三步:代码调用

    @GetMapping("/tool")public String tool(String input) {return chatClient.prompt().toolNames("tushareGetStockQuotes").user(input).call().content();}

扩展知识

接口固定只能查询日行情,不够用想要调用其他接口怎么办?

在引入前面jar包和添加配置后,自定义一个bean来搞定

public class TushareCustomServiceimplements Function<TushareCustomService.Request, TushareCustomService.Response> {private final WebClientTool webClientTool;private final TushareProperties tushareProperties;public TushareCustomService(JsonParseTool jsonParseTool, TushareProperties tushareProperties) {this.webClientTool = WebClientTool.builder(jsonParseTool, tushareProperties).build();this.tushareProperties = tushareProperties;}/*** 获取股票日行情* @param tsCode 股票代码,例如000001.SZ* @param startDate 开始日期,格式yyyyMMdd* @param endDate 结束日期,格式yyyyMMdd* @return https://tushare.pro/document/2?doc_id=27*/private String getStockQuotes(String tsCode, String startDate, String endDate) {try {//第二步:准备请求参数Map<String, String> params = new HashMap<>(3);params.put("ts_code", tsCode);params.put("start_date", startDate);params.put("end_date", endDate);//第三步,修改api_name和fields//api_name=daily,daily即tushare接口名称//fields为想要接收的字段Map<String, Object> valueMap = Map.of("api_name", "daily", "token", tushareProperties.getToken(), "params",params, "fields", "ts_code,trade_date,open,high,low,close,pre_close,change,pct_chg,vol,amount");return webClientTool.post("", valueMap).block();}catch (Exception e) {throw new RuntimeException("Failed to get stock quotes", e);}}@Overridepublic Response apply(Request request) {try {return new Response(this.getStockQuotes(request.tsCode, request.startDate, request.endDate));}catch (Exception e) {return new Response("Error occurred while processing the request.");}}//第一步:修改Request请求字段@JsonClassDescription("根据股票代码或日期获取股票日行情")public record Request(@JsonProperty(value = "ts_code") @JsonPropertyDescription("股票代码,例如000001.SZ") String tsCode,@JsonProperty(value = "start_date") @JsonPropertyDescription("开始日期,格式yyyyMMdd") String startDate,@JsonProperty(value = "end_date") @JsonPropertyDescription("结束日期,格式yyyyMMdd") String endDate) {}public record Response(String message) {}}

三步套用上面的代码:

第一步:修改Request字段,修改为希望大模型从对话中提取的字段,也即调用tushare接口的参数

第二步:修改请求参数,将调用接口时传递的params修改为接口所需入参

第三步:修改接口名称api_name和返回字段列表fields

然后将该类传递给ChatClient即可

ps:该包目前还在主干分支中,使用需自行打包或等下一个版本(>1.0.0.2)

http://www.lryc.cn/news/587792.html

相关文章:

  • 【C++进阶】---- 多态
  • SpringBoot3整合“Spring Security+JWT”快速实现demo示例与Apifox测试
  • 鸿蒙开发NDK之---- 如何将ArkTs的类型转化成C++对应的类型(基础类型,包含部分代码解释)
  • 系统化构建产品开发体系
  • androidstudio 高低版本兼容
  • 机构参与度及其Python数据获取示例
  • 迁移学习:知识复用的智能迁移引擎 | 从理论到实践的跨域赋能范式
  • 【Canvas与五星】六种五星画法
  • MIPI DSI (一) MIPI DSI 联盟概述
  • 【leetcode】231. 2的幂
  • ASP.NET Core中数据绑定原理实现详解
  • Android模块化架构:基于依赖注入和服务定位器的解耦方案
  • iOS如何查看电池容量?理解系统限制与开发者级能耗调试方法
  • H.264编解码(NAL)
  • 前端docx库实现将html页面导出word
  • 蜻蜓I即时通讯水银版系统直播功能模块二次开发文档-详细的直播功能模块文档范例-卓伊凡|麻子
  • 文档处理控件Aspose.Words教程:从 C# 中的 Word 文档中提取页面
  • 【飞牛云fnOS】告别数据孤岛:飞牛云fnOS私人资料管家
  • Python爬虫实战:研究PyMongo库相关技术
  • crawl4ai--bitcointalk爬虫实战项目
  • 嵌入式硬件篇---ne555定时器
  • 嵌入式硬件篇---晶体管的分类
  • Android 中 实现格式化字符串
  • Selenium动态网页爬虫编写与解释
  • 【Linux】Jenkins Lts 配置构建 Maven 项目
  • C++之哈希表的基本介绍以及其自我实现(开放定址法版本)
  • Maven 依赖原则和依赖冲突
  • 【Spring AI Alibaba】接入大模型
  • openGL学习(EBO)
  • Spring 学习笔记