学历教育网站建设asp网站下载
学历教育网站建设,asp网站下载,vs2008怎么做网站,建设银行网站银行登录文章目录SpringAI学习笔记一、SpringAI入门案例#xff08;调用本地ollama部署的大模型#xff09;1.1 环境准备1.2 代码案例SpringAI学习笔记
一、SpringAI入门案例#xff08;调用本地ollama部署的大模型#xff09;
1.1 环境准备
需要下载ollama#xff0c;并且拉取…文章目录SpringAI学习笔记一、SpringAI入门案例调用本地ollama部署的大模型1.1 环境准备1.2 代码案例SpringAI学习笔记一、SpringAI入门案例调用本地ollama部署的大模型1.1 环境准备需要下载ollama并且拉取一个大模型到本地详见教程本地部署大模型使用ollama框架版本Spring AI1.0.0Spring Boot3.4.5JDK17大模型调用三件套获取Api-key本地简单搭建的大模型不需要这个获取模型名获取base Url开发地址1.2 代码案例步骤一、新建Module如下步骤二、配置依赖?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.4.5/versionrelativePath/!-- lookup parent from repository --/parentgroupIdcom.example/groupIdartifactIddemo1/artifactIdversion0.0.1-SNAPSHOT/versionnamedemo1/namedescriptiondemo1/descriptionurl/propertiesjava.version17/java.versionspring-ai.version1.0.0/spring-ai.version/propertiesdependencies!--Java Web依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--Ollama依赖--dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-model-ollama/artifactId/dependency!--MySQL数据库依赖--dependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.42/version/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion${spring-ai.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project步骤三、配置配置文件application.yaml文件配置如下spring:application:name:demo-aiai:ollama:#ollama没有apiKey如果链接的是阿里云百炼平台那些是需要配置apiKey的base-url:http://localhost:11434#本地ollama的端口号默认是11434chat:options:temperature:0.7model:qwen3.5:0.8b#模型名称#大模型对话中文乱码使用UTF-8编码处理server:servlet:encoding:enabled:trueforce:truecharset:UTF-8配置ChatClient给Spring管理我们再写个配置文件CommonConfiguration.java如下packagecom.example.demo1.config;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.ollama.OllamaChatModel;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;ConfigurationpublicclassCommonConfiguration{//将我们本地模型通过配置直接交给Spring容器做管理方便使用BeanpublicChatClientchatClient(OllamaChatModelchatModel){returnChatClient.builder(chatModel).defaultSystem(你是一个傻白甜属性的AI助手你的名字叫小七请以小七的口吻来回答用户的问题)//系统设定.build();}}步骤四、开放接口给用户使用packagecom.example.demo1.controller;importjakarta.annotation.Resource;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importreactor.core.publisher.Flux;RestControllerRequestMapping(/ai)publicclassChatController{ResourceprivateChatClientchatClient;//阻塞式响应RequestMapping(/callChat)publicStringcallChat(Stringrequest){returnchatClient.prompt().user(request).call().content();}//流式响应RequestMapping(/streamChat)publicFluxStringstreamChat(Stringrequest){returnchatClient.prompt().user(request).stream().content();}}启动项目游戏开始了想要查看流式的输出形式那么访问网址http://localhost:8080/ai/streamChat?request输入你的问题想要查看阻塞式的输出形式那么访问网址http://localhost:8080/ai/callChat?request输入你的问题