Spring AI 對 Ollama 工具的支援

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

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

今天,我們很高興地宣佈,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 動態選擇要啟用的函式。
  • 程式碼可移植性:無需更改程式碼,即可將應用程式程式碼與 OpenAI、Mistral、VertexAI、Anthropic、Groq 等不同的 LLM 提供商重複使用。

工作原理

  • 定義自定義 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

依賴項

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

<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 社群所有即將舉行的活動。

檢視所有