watsonx.ai Chat
透過 watsonx.ai,您可以於本地端執行各種大型語言模型 (LLM) 並從中產生文字。Spring AI 透過 WatsonxAiChatModel
支援 watsonx.ai 文字產生功能。
自動配置
Spring AI 為 watsonx.ai Chat Client 提供 Spring Boot 自動配置。若要啟用它,請將以下相依性新增至您專案的 Maven pom.xml
檔案
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-watsonx-ai-spring-boot-starter</artifactId>
</dependency>
或 Gradle build.gradle
建置檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-watsonx-ai-spring-boot-starter'
}
聊天屬性
連線屬性
前綴 spring.ai.watsonx.ai
用作屬性前綴,讓您可以連線至 watsonx.ai。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.watsonx.ai.base-url |
要連線的 URL |
|
spring.ai.watsonx.ai.stream-endpoint |
串流端點 |
ml/v1/text/generation_stream?version=2023-05-29 |
spring.ai.watsonx.ai.text-endpoint |
文字端點 |
ml/v1/text/generation?version=2023-05-29 |
spring.ai.watsonx.ai.project-id |
專案 ID |
- |
spring.ai.watsonx.ai.iam-token |
IBM Cloud 帳戶 IAM 權杖 |
- |
配置屬性
前綴 spring.ai.watsonx.ai.chat
是屬性前綴,可讓您為 Watsonx.AI 配置聊天模型實作。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.watsonx.ai.chat.enabled |
啟用 Watsonx.AI 聊天模型。 |
true |
spring.ai.watsonx.ai.chat.options.temperature |
模型的溫度。提高溫度會使模型回答更具創造力。 |
0.7 |
spring.ai.watsonx.ai.chat.options.top-p |
與 top-k 協同運作。較高的值(例如 0.95)會產生更多樣化的文字,而較低的值(例如 0.2)會產生更集中且保守的文字。 |
1.0 |
spring.ai.watsonx.ai.chat.options.top-k |
降低產生無意義內容的可能性。較高的值(例如 100)會提供更多樣化的答案,而較低的值(例如 10)會更保守。 |
50 |
spring.ai.watsonx.ai.chat.options.decoding-method |
解碼是模型用於選擇產生輸出中符記的過程。 |
greedy |
spring.ai.watsonx.ai.chat.options.max-new-tokens |
設定 LLM 跟隨的符記限制。 |
20 |
spring.ai.watsonx.ai.chat.options.min-new-tokens |
設定 LLM 必須產生的符記數量。 |
0 |
spring.ai.watsonx.ai.chat.options.stop-sequences |
設定 LLM 應何時停止。(例如,["\n\n\n"])然後當 LLM 產生三個連續換行符時,它將終止。在產生「最小符記」參數中指定的符記數量之後,才會忽略停止序列。 |
- |
spring.ai.watsonx.ai.chat.options.repetition-penalty |
設定懲罰重複的強度。較高的值(例如 1.8)將更強烈地懲罰重複,而較低的值(例如 1.1)將更寬鬆。 |
1.0 |
spring.ai.watsonx.ai.chat.options.random-seed |
產生可重複的結果,每次都設定相同的隨機種子值。 |
隨機產生 |
spring.ai.watsonx.ai.chat.options.model |
模型是要使用的 LLM 模型的識別碼。 |
google/flan-ul2 |
執行階段選項
WatsonxAiChatOptions.java 提供模型配置,例如要使用的模型、溫度、頻率懲罰等。
在啟動時,可以使用 WatsonxAiChatModel(api, options)
建構函式或 spring.ai.watsonxai.chat.options.*
屬性配置預設選項。
在執行階段,您可以透過將新的、特定於請求的選項新增至 Prompt
呼叫來覆寫預設選項。例如,覆寫特定請求的預設模型和溫度
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
WatsonxAiChatOptions.builder()
.withTemperature(0.4)
.build()
));
除了模型特定的 WatsonxAiChatOptions.java 之外,您還可以使用可攜式 ChatOptions 實例,使用 ChatOptionsBuilder#builder() 建立。 |
如需更多資訊,請前往 watsonx-parameters-info |
使用範例
public class MyClass {
private final static String MODEL = "google/flan-ul2";
private final WatsonxAiChatModel chatModel;
@Autowired
MyClass(WatsonxAiChatModel chatModel) {
this.chatModel = chatModel;
}
public String generate(String userInput) {
WatsonxAiOptions options = WatsonxAiOptions.create()
.withModel(MODEL)
.withDecodingMethod("sample")
.withRandomSeed(1);
Prompt prompt = new Prompt(new SystemMessage(userInput), options);
var results = this.chatModel.call(prompt);
var generatedText = results.getResult().getOutput().getContent();
return generatedText;
}
public String generateStream(String userInput) {
WatsonxAiOptions options = WatsonxAiOptions.create()
.withModel(MODEL)
.withDecodingMethod("greedy")
.withRandomSeed(2);
Prompt prompt = new Prompt(new SystemMessage(userInput), options);
var results = this.chatModel.stream(prompt).collectList().block(); // wait till the stream is resolved (completed)
var generatedText = results.stream()
.map(generation -> generation.getResult().getOutput().getContent())
.collect(Collectors.joining());
return generatedText;
}
}