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

springboot 对接 ollama

spring ai 对接 ollama

引入依赖

<dependency><groupId>io.springboot.ai</groupId><artifactId>spring-ai-ollama-spring-boot-starter</artifactId><version>1.0.0</version>
</dependency>

这里因为使用的是快照版本所以需要配置 spring repository 的快照的仓库

<repositories><repository><name>spring-milestones</name><id>spring-milestones</id><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository>
</repositories>

配置文件

spring:application:name: study-spring-aiai:ollama:# 这里填写 ollama 对应的地址信息就行base-url: http://192.168.4.11:11434chat:options:model: qwen2:0.5b

编码实现

编码是相对比较简单的,我们这里提供一个实例

package org.study.ai.controller;import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @author xl-9527* @since 2024/10/25**/
@RestController
@RequestMapping("ai/chat-models")
public class AiRequestController {private final OllamaChatClient ollamaChatClient;public AiRequestController(final OllamaChatClient ollamaChatClient) {this.ollamaChatClient = ollamaChatClient;}@GetMapping("ollama/dialogue")public Object dialogue(@RequestParam(name = "msg") String msg) {UserMessage userMessage = new UserMessage(msg);ChatResponse callResponse = ollamaChatClient.call(new Prompt(userMessage,OllamaOptions.create().withModel("qwen2:0.5b")));Generation result = callResponse.getResult();return result.getOutput().getContent();}
}
http://www.lryc.cn/news/503568.html

相关文章:

  • 【数据库】选择题+填空+简答
  • 从0开始写android 之xwindow
  • The Past, Present and Future of Apache Flink
  • 多模块应用、发布使用第三方库(持续更新中)
  • An error happened while trying to locate the file on the Hub and we cannot f
  • UE5安装Fab插件
  • Linux C语言操作sqlite3数据库
  • 【人工智能】因果推断与数据分析:用Python探索数据间的因果关系
  • freeswitch(30秒自动挂断)
  • 大模型呼入机器人有哪些功能特点?(转)
  • 网络工程师常用软件之配置对比软件
  • Linux之远程登录
  • #渗透测试#漏洞挖掘#红蓝攻防#js分析(上)
  • 数智读书笔记系列006 协同进化:人类与机器融合的未来
  • 操作系统(7)处理机调度
  • folly之侵入式链表
  • GPT-5 训练遇阻:预期目标难达成与交付延期的深度剖析
  • C缺陷与陷阱 — 3 深入理解表达式
  • Linux常用指令-----中
  • k8s 部署方式kustomization和helm的区别
  • Alogrithm:骑士走棋盘
  • Oracle 与 达梦 数据库 对比
  • [COLM 2024] V-STaR: Training Verifiers for Self-Taught Reasoners
  • 【Python】使用Selenium的find_element模块获取网页上的大段文字和表格的方法(建议收藏!)
  • 蓝桥杯刷题——day4
  • 内网是如何访问到互联网(H3C源NAT)
  • 源码分析之Openlayers中的Zoom缩放控件
  • k8s的ConfigMap是什么, 为什么设计ConfigMap, 如何使用ConfigMap
  • fiddler设置抓取https,还抓取不到https如何解决?
  • Python高性能web框架-FastApi教程:(1)创建一个简单的FastApi