Modify the sample code

- Modify how OpenAiChatOption is used
- Modify how OllamaOptions is used

Signed-off-by: birariro <vps32@naver.com>
This commit is contained in:
birariro
2025-02-25 16:42:36 +09:00
committed by Ilayaperumal Gopinathan
parent 2c7283483e
commit 1599c2e500
5 changed files with 27 additions and 23 deletions

View File

@@ -145,13 +145,13 @@ ChatResponse response = chatModel.call(
new Prompt( new Prompt(
"Generate the names of 5 famous pirates.", "Generate the names of 5 famous pirates.",
OpenAiChatOptions.builder() OpenAiChatOptions.builder()
.withModel("deepseek-chat") .model("deepseek-chat")
.withTemperature(0.4) .temperature(0.4)
.build() .build()
)); ));
---- ----
TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptionsBuilder.java[ChatOptionsBuilder#builder()]. TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions#builder()].
== Function Calling == Function Calling

View File

@@ -150,13 +150,13 @@ ChatResponse response = chatModel.call(
new Prompt( new Prompt(
"Generate the names of 5 famous pirates.", "Generate the names of 5 famous pirates.",
OpenAiChatOptions.builder() OpenAiChatOptions.builder()
.withModel("mixtral-8x7b-32768") .model("mixtral-8x7b-32768")
.withTemperature(0.4) .temperature(0.4)
.build() .build()
)); ));
---- ----
TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptionsBuilder.java[ChatOptionsBuilder#builder()]. TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions#builder()].
== Function Calling == Function Calling
@@ -303,9 +303,9 @@ Next, create a `OpenAiChatModel` and use it for text generations:
---- ----
var openAiApi = new OpenAiApi("https://api.groq.com/openai", System.getenv("GROQ_API_KEY")); var openAiApi = new OpenAiApi("https://api.groq.com/openai", System.getenv("GROQ_API_KEY"));
var openAiChatOptions = OpenAiChatOptions.builder() var openAiChatOptions = OpenAiChatOptions.builder()
.withModel("llama3-70b-8192") .model("llama3-70b-8192")
.withTemperature(0.4) .temperature(0.4)
.withMaxTokens(200) .maxTokens(200)
.build(); .build();
var chatModel = new OpenAiChatModel(this.openAiApi, this.openAiChatOptions); var chatModel = new OpenAiChatModel(this.openAiApi, this.openAiChatOptions);

View File

@@ -121,13 +121,13 @@ ChatResponse response = chatModel.call(
new Prompt( new Prompt(
"Generate the names of 5 famous pirates.", "Generate the names of 5 famous pirates.",
OpenAiChatOptions.builder() OpenAiChatOptions.builder()
.withModel("mixtral-8x7b-32768") .model("mixtral-8x7b-32768")
.withTemperature(0.4) .temperature(0.4)
.build() .build()
)); ));
---- ----
TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptionsBuilder.java[ChatOptionsBuilder#builder()]. TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions#builder()].
== Function Calling == Function Calling

View File

@@ -309,8 +309,8 @@ String jsonSchema = """
Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
OllamaOptions.builder() OllamaOptions.builder()
.withModel(OllamaModel.LLAMA3_2.getName()) .model(OllamaModel.LLAMA3_2.getName())
.withFormat(new ObjectMapper().readValue(jsonSchema, Map.class)) .format(new ObjectMapper().readValue(jsonSchema, Map.class))
.build()); .build());
ChatResponse response = this.ollamaChatModel.call(this.prompt); ChatResponse response = this.ollamaChatModel.call(this.prompt);
@@ -340,8 +340,8 @@ var outputConverter = new BeanOutputConverter<>(MathReasoning.class);
Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
OllamaOptions.builder() OllamaOptions.builder()
.withModel(OllamaModel.LLAMA3_2.getName()) .model(OllamaModel.LLAMA3_2.getName())
.withFormat(outputConverter.getJsonSchemaMap()) .format(outputConverter.getJsonSchemaMap())
.build()); .build());
ChatResponse response = this.ollamaChatModel.call(this.prompt); ChatResponse response = this.ollamaChatModel.call(this.prompt);
@@ -466,10 +466,14 @@ Next, create an `OllamaChatModel` instance and use it to send requests for text
---- ----
var ollamaApi = new OllamaApi(); var ollamaApi = new OllamaApi();
var chatModel = new OllamaChatModel(this.ollamaApi, var chatModel = OllamaChatModel.builder()
OllamaOptions.create() .ollamaApi(ollamaApi)
.model(OllamaOptions.DEFAULT_MODEL) .defaultOptions(
.temperature(0.9)); OllamaOptions.builder()
.model(OllamaModel.MISTRAL)
.temperature(0.9)
.build())
.build();
ChatResponse response = this.chatModel.call( ChatResponse response = this.chatModel.call(
new Prompt("Generate the names of 5 famous pirates.")); new Prompt("Generate the names of 5 famous pirates."));
@@ -508,7 +512,7 @@ var request = ChatRequest.builder("orca-mini")
.content("What is the capital of Bulgaria and what is the size? " .content("What is the capital of Bulgaria and what is the size? "
+ "What is the national anthem?") + "What is the national anthem?")
.build())) .build()))
.options(OllamaOptions.create().temperature(0.9)) .options(OllamaOptions.builder().temperature(0.9).build())
.build(); .build();
ChatResponse response = this.ollamaApi.chat(this.request); ChatResponse response = this.ollamaApi.chat(this.request);
@@ -519,7 +523,7 @@ var request2 = ChatRequest.builder("orca-mini")
.messages(List.of(Message.builder(Role.USER) .messages(List.of(Message.builder(Role.USER)
.content("What is the capital of Bulgaria and what is the size? " + "What is the national anthem?") .content("What is the capital of Bulgaria and what is the size? " + "What is the national anthem?")
.build())) .build()))
.options(OllamaOptions.create().temperature(0.9).toMap()) .options(OllamaOptions.builder().temperature(0.9).build().toMap())
.build(); .build();
Flux<ChatResponse> streamingResponse = this.ollamaApi.streamingChat(this.request2); Flux<ChatResponse> streamingResponse = this.ollamaApi.streamingChat(this.request2);

View File

@@ -147,7 +147,7 @@ ChatResponse response = chatModel.call(
)); ));
---- ----
TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java#L97[ChatOptions#builder()]. TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions#builder()].
== Function Calling == Function Calling