From 7497debb376a73df4b1d30b504acf0c4abe680f5 Mon Sep 17 00:00:00 2001 From: lwb214 <12197587+lwb214@user.noreply.gitee.com> Date: Fri, 11 Oct 2024 22:49:11 +0800 Subject: [PATCH 1/4] =?UTF-8?q?spring-ai-alibaba=20=E4=BD=BF=E7=94=A8ollam?= =?UTF-8?q?a=E7=AD=89=E5=85=B6=E4=BB=96=E5=A4=A7=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ollama-example/pom.xml | 85 +++++++++++++++++++ .../ai/example/ollama/OllamaController.java | 24 ++++++ .../ollama/OllamaExampleApplication.java | 13 +++ .../src/main/resources/application.yml | 12 +++ .../ollama/OllamaExampleApplicationTests.java | 13 +++ 5 files changed, 147 insertions(+) create mode 100644 spring-ai-alibaba-examples/ollama-example/pom.xml create mode 100644 spring-ai-alibaba-examples/ollama-example/src/main/java/com/alibaba/cloud/ai/example/ollama/OllamaController.java create mode 100644 spring-ai-alibaba-examples/ollama-example/src/main/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplication.java create mode 100644 spring-ai-alibaba-examples/ollama-example/src/main/resources/application.yml create mode 100644 spring-ai-alibaba-examples/ollama-example/src/test/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplicationTests.java diff --git a/spring-ai-alibaba-examples/ollama-example/pom.xml b/spring-ai-alibaba-examples/ollama-example/pom.xml new file mode 100644 index 00000000..36cb51af --- /dev/null +++ b/spring-ai-alibaba-examples/ollama-example/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.3 + + + com.alibaba.cloud.ai + ollama-example + 0.0.1-SNAPSHOT + ollama-example + Demo project for Spring AI Alibaba + + + UTF-8 + UTF-8 + 17 + 17 + 3.1.1 + + + 1.0.0-M2 + + + + + + org.springframework.ai + spring-ai-bom + 1.0.0-M2 + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.ai + spring-ai-ollama-spring-boot-starter + ${spring-ai-alibaba.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + \ No newline at end of file diff --git a/spring-ai-alibaba-examples/ollama-example/src/main/java/com/alibaba/cloud/ai/example/ollama/OllamaController.java b/spring-ai-alibaba-examples/ollama-example/src/main/java/com/alibaba/cloud/ai/example/ollama/OllamaController.java new file mode 100644 index 00000000..5e8c4398 --- /dev/null +++ b/spring-ai-alibaba-examples/ollama-example/src/main/java/com/alibaba/cloud/ai/example/ollama/OllamaController.java @@ -0,0 +1,24 @@ +package com.alibaba.cloud.ai.example.ollama; + +import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +@RequestMapping("/ai") +public class OllamaController { + private final ChatModel chatModel; + public OllamaController(ChatModel chatModel) { + this.chatModel = chatModel; + } + @GetMapping("/chat") + public String chat(String input) { + ChatResponse response = chatModel.call(new Prompt(input)); + return response.getResult().getOutput().getContent(); + } + +} diff --git a/spring-ai-alibaba-examples/ollama-example/src/main/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplication.java b/spring-ai-alibaba-examples/ollama-example/src/main/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplication.java new file mode 100644 index 00000000..6067b0a9 --- /dev/null +++ b/spring-ai-alibaba-examples/ollama-example/src/main/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplication.java @@ -0,0 +1,13 @@ +package com.alibaba.cloud.ai.example.ollama; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class OllamaExampleApplication { + + public static void main(String[] args) { + SpringApplication.run(OllamaExampleApplication.class, args); + } + +} diff --git a/spring-ai-alibaba-examples/ollama-example/src/main/resources/application.yml b/spring-ai-alibaba-examples/ollama-example/src/main/resources/application.yml new file mode 100644 index 00000000..c93e4500 --- /dev/null +++ b/spring-ai-alibaba-examples/ollama-example/src/main/resources/application.yml @@ -0,0 +1,12 @@ +server: + port: 8090 +spring: + application: + name: ollama-example + ai: + ollama: + base-url: http://127.0.0.1:11434 + chat: + options: + model: llama2:latest + temperature: 0.4F \ No newline at end of file diff --git a/spring-ai-alibaba-examples/ollama-example/src/test/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplicationTests.java b/spring-ai-alibaba-examples/ollama-example/src/test/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplicationTests.java new file mode 100644 index 00000000..1a63e457 --- /dev/null +++ b/spring-ai-alibaba-examples/ollama-example/src/test/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplicationTests.java @@ -0,0 +1,13 @@ +package com.alibaba.cloud.ai.example.ollama; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class OllamaExampleApplicationTests { + + @Test + void contextLoads() { + } + +} From eed8a7ec242c16f09a43ec74ecca9b3dfb457dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=95=86=E5=8B=87=E5=A3=AB?= <125975774+lwb214@users.noreply.github.com> Date: Sat, 12 Oct 2024 10:06:08 +0800 Subject: [PATCH 2/4] Update pom.xml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在pom中加入模块 --- spring-ai-alibaba-examples/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-ai-alibaba-examples/pom.xml b/spring-ai-alibaba-examples/pom.xml index 2de1a38f..ad0df908 100644 --- a/spring-ai-alibaba-examples/pom.xml +++ b/spring-ai-alibaba-examples/pom.xml @@ -22,6 +22,7 @@ rag-example output-parser-example playground-flight-booking + ollama-example From ac458ea5ea895aee407193b9b98eb241ed03ab7a Mon Sep 17 00:00:00 2001 From: lwb214 <12197587+lwb214@user.noreply.gitee.com> Date: Sat, 12 Oct 2024 22:01:16 +0800 Subject: [PATCH 3/4] add README.md update pom.xml --- .../ollama-example/README.md | 42 +++++++++++++++++++ .../ollama-example/pom.xml | 9 +--- .../ollama/OllamaExampleApplicationTests.java | 13 ------ 3 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 spring-ai-alibaba-examples/ollama-example/README.md delete mode 100644 spring-ai-alibaba-examples/ollama-example/src/test/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplicationTests.java diff --git a/spring-ai-alibaba-examples/ollama-example/README.md b/spring-ai-alibaba-examples/ollama-example/README.md new file mode 100644 index 00000000..ec8d2ba7 --- /dev/null +++ b/spring-ai-alibaba-examples/ollama-example/README.md @@ -0,0 +1,42 @@ +# 如何本地部署大模型,这里用ollama来做示例 +## 1. 下载ollama +### 进入Ollama官网 https://ollama.com/ +下载对应自己的系统的ollama +## 2. 安装ollama +### 双击OllamaSetup.exe,进行安装 +>注意,在windows下安装时,是不允许选择安装位置的,默认是安装在系统盘的 +安装完毕后,打开终端进行验证,在终端中输入ollama,验证ollama是否安装成功 +> +> 如果ollama安装成功,会显示ollama的版本信息 +## 3. 设置模型存放目录 +### 3.1、在windows,ollama安装的模型,默认存放目录为C:/Users//.ollama/models +可以通过以下命令更改模型安装时的存放目录 +> 只设置当前用户(需要先创建D:\ollama_models目录) +setx OLLAMA_MODELS "D:\ollama_models" +### 3.2、重启终端 +>setx命令在windows中设置环境变量时,这个变量的更改只会在新打开的命令提示符窗口或终端会话中生效 +### 3.3、重启ollama服务 +> 在终端中输入ollama +## 4.查看Ollama支持的模型 +>https://ollama.com/library +> +>点击某个模型连接,比如llama2,可以看到模型详细的介绍 +## 5.模型安装 +>可以通过以下命令进行模型安装 +> +> ollama pull llama2 + +下载过程比较慢,耐心等待 +## 6.查看已安装的模型列表 +通过以下命令查看已安装的模型列表 +> ollama list +## 7.运行模型 +> ollama run llama2 + +出现send Message就说明部署好啦,可以使用模型对话了 +>退出模型命令 +> /bye +> +## 温馨提示 +本地部署的大模型默认端口为11434,访问地址为 http://127.0.0.1:11434 +#### 可以通过修改环境变量来允许外部访问 \ No newline at end of file diff --git a/spring-ai-alibaba-examples/ollama-example/pom.xml b/spring-ai-alibaba-examples/ollama-example/pom.xml index 36cb51af..62a77fab 100644 --- a/spring-ai-alibaba-examples/ollama-example/pom.xml +++ b/spring-ai-alibaba-examples/ollama-example/pom.xml @@ -21,8 +21,6 @@ 17 3.1.1 - - 1.0.0-M2 @@ -42,15 +40,10 @@ org.springframework.boot spring-boot-starter-web - - org.springframework.boot - spring-boot-starter-test - test - org.springframework.ai spring-ai-ollama-spring-boot-starter - ${spring-ai-alibaba.version} + 1.0.0-M2 diff --git a/spring-ai-alibaba-examples/ollama-example/src/test/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplicationTests.java b/spring-ai-alibaba-examples/ollama-example/src/test/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplicationTests.java deleted file mode 100644 index 1a63e457..00000000 --- a/spring-ai-alibaba-examples/ollama-example/src/test/java/com/alibaba/cloud/ai/example/ollama/OllamaExampleApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.alibaba.cloud.ai.example.ollama; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class OllamaExampleApplicationTests { - - @Test - void contextLoads() { - } - -} From 9050093a5fe0de70b47ae705431cbe9385d764e1 Mon Sep 17 00:00:00 2001 From: lwb214 <2897718178@qq.com> Date: Sat, 12 Oct 2024 22:27:42 +0800 Subject: [PATCH 4/4] add blank Signed-off-by: lwb214 <2897718178@qq.com> --- spring-ai-alibaba-examples/ollama-example/pom.xml | 2 +- .../ollama-example/src/main/resources/application.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-ai-alibaba-examples/ollama-example/pom.xml b/spring-ai-alibaba-examples/ollama-example/pom.xml index 62a77fab..b83de4d0 100644 --- a/spring-ai-alibaba-examples/ollama-example/pom.xml +++ b/spring-ai-alibaba-examples/ollama-example/pom.xml @@ -75,4 +75,4 @@ - \ No newline at end of file + diff --git a/spring-ai-alibaba-examples/ollama-example/src/main/resources/application.yml b/spring-ai-alibaba-examples/ollama-example/src/main/resources/application.yml index c93e4500..2a8c609e 100644 --- a/spring-ai-alibaba-examples/ollama-example/src/main/resources/application.yml +++ b/spring-ai-alibaba-examples/ollama-example/src/main/resources/application.yml @@ -9,4 +9,4 @@ spring: chat: options: model: llama2:latest - temperature: 0.4F \ No newline at end of file + temperature: 0.4F