Spring AI 整合 Ollama 工具支援

工程 | Christian Tzolov | 2024 年 7 月 26 日 | ...

本週早些時候,Ollama 推出了一個令人興奮的新功能:對大型語言模型(LLMs)的工具支援。

今天,我們激動地宣佈 Spring AI (1.0.0-SNAPSHOT) 已完全採納這一強大功能,將 Ollama 的函式呼叫能力帶入 Spring 生態系統。

Ollama 的工具支援允許模型決定何時呼叫外部函式以及如何使用返回的資料。這開啟了無限的可能性,從訪問即時資訊到執行復雜計算。Spring AI 採納了這一概念並將其與 Spring 生態系統無縫整合,使 Java 開發者能夠極其輕鬆地在其應用中利用此功能。Spring AI 的 Ollama 函式呼叫支援的關鍵特性包括

  • 輕鬆整合:將您的 Java 函式註冊為 Spring bean,並將其與 Ollama 模型一起使用。
  • 靈活配置:有多種方式註冊和配置函式。
  • 自動生成 JSON Schema:Spring AI 處理將您的 Java 方法轉換為 Ollama 可以理解的 JSON Schema 的過程。
  • 支援多個函式:在單個聊天會話中註冊和使用多個函式。
  • 執行時函式選擇:為每個 Prompt 動態選擇要啟用的函式。
  • 程式碼可移植性:在不同的 LLM 提供商(如 OpenAI、Mistral、VertexAI、Anthropic、Groq)之間重用您的應用程式程式碼,無需更改程式碼。

工作原理

  • 定義自定義 Java 函式並將其註冊到 Spring AI。

Restored Spring AI (1)

  • 傳送可能需要呼叫函式來完成答案的聊天請求。
  • 當 AI 模型確定需要呼叫函式時,它會生成一個包含函式名稱和引數的 JSON 物件。
  • Spring AI 攔截此請求,呼叫您的 Java 函式,並將結果返回給模型。
  • 模型將函式的輸出整合到其響應中。

入門

前提條件

您首先需要在本地機器上執行 Ollama (0.2.8+)。請參考 Ollama 專案官方 README 以開始在本地機器上執行模型。然後拉取一個支援工具的模型,例如 Llama 3.1MistralFirefunction v2Command-R +... 支援的模型列表可以在模型頁面的 Tools 分類下找到。

ollama run mistral

依賴

要開始使用 Spring AI 的 Ollama 函式呼叫功能,請將以下依賴項新增到您的專案中

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>

請參閱依賴管理部分,將 Spring AI BOM 新增到您的構建檔案中。

示例

以下是使用 Spring AI 和 Ollama 函式呼叫的簡單示例

@SpringBootApplication
public class OllamaApplication {

	public static void main(String[] args) {
		SpringApplication.run(OllamaApplication.class, args);
	}

	@Bean
	CommandLineRunner runner(ChatClient.Builder chatClientBuilder) {
		return args -> {
			var chatClient = chatClientBuilder.build();

			var response = chatClient.prompt()
				.user("What is the weather in Amsterdam and Paris?")
				.functions("weatherFunction") // reference by bean name.
				.call()
				.content();

			System.out.println(response);
		};
	}

	@Bean
	@Description("Get the weather in location")
	public Function<WeatherRequest, WeatherResponse> weatherFunction() {
		return new MockWeatherService();
	}

	public static class MockWeatherService implements Function<WeatherRequest, WeatherResponse> {

		public record WeatherRequest(String location, String unit) {}
		public record WeatherResponse(double temp, String unit) {}

		@Override
		public WeatherResponse apply(WeatherRequest request) {
			double temperature = request.location().contains("Amsterdam") ? 20 : 25;
			return new WeatherResponse(temperature, request.unit);
		}
	}
}

在這個示例中,當模型需要天氣資訊時,它將自動呼叫 weatherFunction bean,該 bean 可以獲取即時天氣資料。

預期響應如下所示:"阿姆斯特丹目前氣溫 20 攝氏度,巴黎目前氣溫 25 攝氏度。"

完整的示例程式碼可在以下地址找到:https://github.com/tzolov/ollama-tools

OpenAI 相容性

Ollama 與 OpenAI API 相容,您可以使用 Spring AI OpenAI 客戶端與 Ollama 進行通訊並使用工具。為此,您需要使用 OpenAI 客戶端,但要設定 base-url:spring.ai.openai.chat.base-url=https://:11434 並選擇提供的 Ollama Tools 模型之一:spring.ai.openai.chat.options.model=mistral

檢視 OllamaWithOpenAiChatModelIT.java 測試用例,瞭解如何透過 Spring AI OpenAI 使用 Ollama 的示例。

限制

正如 Ollama 部落格文章中所述,目前其 API 不支援 Streaming Tool CallsTool choice

一旦這些限制得到解決,Spring AI 也將準備好提供支援。

更多資訊

結論

透過基於 Ollama 創新的工具支援並將其整合到 Spring 生態系統中,Spring AI 為 Java 開發者建立 AI 增強型應用程式提供了一種強大的新方式。這一特性為建立更動態、更具響應性的 AI 驅動系統開啟了令人興奮的可能性,這些系統可以與真實世界的資料和服務互動。

使用 Spring AI 的 Ollama 函式呼叫的一些好處包括

  • 擴充套件 AI 能力:輕鬆地使用自定義功能和即時資料增強 AI 模型。
  • 無縫整合:在您的 AI 應用程式中利用現有的 Spring bean 和基礎設施。
  • 型別安全開發:使用強型別的 Java 函式,而不是處理原始 JSON。
  • 減少樣板程式碼:Spring AI 處理函式呼叫的複雜性,使您可以專注於業務邏輯。

我們鼓勵您試用這個新功能,並告訴我們您在專案中如何使用它。有關更詳細的資訊和高階用法,請查閱我們的官方文件。

使用 Spring AI 和 Ollama 愉快地編碼!

訂閱 Spring 新聞通訊

訂閱 Spring 新聞通訊,保持聯絡

訂閱

搶佔先機

VMware 提供培訓和認證,助力您的快速發展。

瞭解更多

獲取支援

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案,僅需一個簡單的訂閱。

瞭解更多

即將舉行的活動

檢視 Spring 社群所有即將舉行的活動。

檢視全部