qwen3 惊喜发布,用 ollama + solon ai (java) 尝个鲜
生活随笔
收集整理的這篇文章主要介紹了
qwen3 惊喜发布,用 ollama + solon ai (java) 尝个鲜
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
qwen3 驚喜發布了,帥!我們用 ollama 和 solon ai (java) 也來嘗個鮮。
1、先用 ollama 拉取模型
聽說,在個人電腦上用 4b 的參數,效果就很好了。
ollama run qwen3:4b
2、試試:Hello qwen3
用 solon-initializr ( https://solon.noear.org/start/ ),生成一個 solon-ai 模板項目。之后:
- 在應用屬性里添加配置(app.yml)
solon.ai.chat:
qwen3:
apiUrl: "http://127.0.0.1:11434/api/chat" # 使用完整地址(而不是 api_base)
provider: "ollama" # ollama 是有自己的專有接口格式,通過配置 provider 可識別方言
model: "qwen3:4b"
- 用配置器類構建通用聊天模型
@Configuration
public class DemoConfig {
@Bean
public ChatModel chatModel(@Inject("${solon.ai.chat.qwen3}") ChatConfig config) {
return ChatModel.of(config).build();
}
}
- 添加測試用的控制器
@Controller
public class DemoController {
@Inject
ChatModel chatModel;
@Mapping("hello")
public String hello(String message) throws IOException {
return chatModel.prompt(message).call().getMessage().getContent();
}
}
- 測試一下
啟動項目。打開瀏覽器地址:http://localhost:8080/hello?message=hello。效果良好:
3、嘗試把輸出改成 sse,方便打字效果的聊天窗口開發
@Controller
public class DemoController {
@Inject
ChatModel chatModel;
@Produces(MimeType.TEXT_EVENT_STREAM_UTF8_VALUE) //這個很重要,申明用 sse 格式渲染
@Mapping("hello")
public Flux<String> hello(String message) throws IOException {
return Flux.from(chatModel.prompt(message).stream())
.filter(resp -> resp.hasChoices())
.map(resp -> resp.getMessage().getContent());
}
}
- 測試一下
啟動項目。再次打開瀏覽器地址:http://localhost:8080/hello?message=hello。效果良好:
4、現在開始 RAG,以 “聯網搜索” 作為知識庫
這里把“聯網搜索”,做為一個知識庫使用(內部是動態搜索的)。用它作為 RAG 的外部檢索支持。
- 應用屬性加一塊配置
solon.ai.chat:
qwen3:
apiUrl: "http://127.0.0.1:11434/api/chat" # 使用完整地址(而不是 api_base)
provider: "ollama" # ollama 是有自己的專有接口格式,通過配置 provider 可識別方言
model: "qwen3:4b"
solon.ai.repo:
websearch:
apiUrl: "https://api.bochaai.com/v1/web-search" # 使用完整地址(而不是 api_base)
apiKey: "sk-demo..."
- 配置器類也調整下
@Configuration
public class DemoConfig {
@Bean
public ChatModel chatModel(@Inject("${solon.ai.chat.qwen3}") ChatConfig config) {
return ChatModel.of(config).build();
}
@Bean
public Repository repository(@Inject("${solon.ai.repo.websearch}") AiConfig config) {
return new WebSearchRepository(null, config);
}
}
- 再改改控制器(輸出重新寫回簡單的方式,不然不好截圖)
@Controller
public class DemoController {
@Inject
ChatModel chatModel;
@Inject
Repository repository;
@Mapping("hello")
public String hello(String message) throws IOException {
//檢索
List<Document> context = repository.search(new QueryCondition(message).limit(4));
//消息增強
ChatMessage chatMessage = UserMessage.augment(message, context);
//提交大模型并簡單返回(不然,截圖不好截)
return chatModel.prompt(chatMessage).call().getMessage().getContent();
}
}
- 測試一下
啟動項目。打開瀏覽器地址:http://localhost:8080/hello?message=solon%20%E6%98%AF%E8%B0%81%E5%BC%80%E5%8F%91%E7%9A%84%EF%BC%9F。效果良好:
5、再試個 Tool Call(即 Function Call)
修改下剛才的配置器,加個模型的默認工具。
@Configuration
public class DemoConfig {
@Bean
public ChatModel chatModel(@Inject("${solon.ai.chat.qwen3}") ChatConfig config) {
return ChatModel.of(config)
.defaultToolsAdd(new Tools())
.build();
}
public static class Tools {
@ToolMapping(description = "獲取指定城市的天氣情況")
public String get_weather(@ToolParam(description = "根據用戶提到的地點推測城市") String location) {
return "晴,24度";
}
}
}
- 測試一下
啟動項目。再次打開瀏覽器地址:http://localhost:8080/hello?message=杭州今天的天氣如何?。效果良好:
總結
以上是生活随笔為你收集整理的qwen3 惊喜发布,用 ollama + solon ai (java) 尝个鲜的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8.6K star!完全免费+本地运行+
- 下一篇: 21.7K star!全流程研发项目管理