Skip to content

Commit

Permalink
feat: Use new Model and Client APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Vitale <[email protected]>
  • Loading branch information
ThomasVitale committed May 23, 2024
1 parent 5f05d43 commit 775652a
Show file tree
Hide file tree
Showing 62 changed files with 572 additions and 394 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
Expand All @@ -24,25 +21,27 @@ class ChatService {
}

String chatWithDocument(String message) {
var systemPromptTemplate = new SystemPromptTemplate("""
var systemPromptTemplate = """
You are a helpful assistant, conversing with a user about the subjects contained in a set of documents.
Use the information from the DOCUMENTS section to provide accurate answers. If unsure or if the answer
isn't found in the DOCUMENTS section, simply state that you don't know the answer and do not mention
the DOCUMENTS section.
DOCUMENTS:
{documents}
""");
""";

List<Document> similarDocuments = vectorStore.similaritySearch(SearchRequest.query(message).withTopK(5));
String content = similarDocuments.stream().map(Document::getContent).collect(Collectors.joining(System.lineSeparator()));

Map<String,Object> model = Map.of("documents", content);
var systemMessage = systemPromptTemplate.createMessage(model);

var userMessage = new UserMessage(message);

return chatClient.call(systemMessage, userMessage);
return chatClient.prompt()
.system(systemSpec -> systemSpec
.text(systemPromptTemplate)
.param("documents", content)
)
.user(message)
.call()
.content();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.embedding.EmbeddingClient;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatModel;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.boot.SpringApplication;
Expand All @@ -10,13 +12,18 @@
@SpringBootApplication
public class QuestionAnsweringWithDocuments {

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

@Bean
VectorStore vectorStore(EmbeddingClient embeddingClient) {
return new SimpleVectorStore(embeddingClient);
ChatClient chatClient(ChatModel chatModel) {
return ChatClient.builder(chatModel).build();
}

public static void main(String[] args) {
SpringApplication.run(QuestionAnsweringWithDocuments.class, args);
@Bean
VectorStore vectorStore(EmbeddingModel embeddingModel) {
return new SimpleVectorStore(embeddingModel);
}

}
12 changes: 6 additions & 6 deletions 01-chat-models/chat-models-mistral-ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ Text generation with LLMs via Mistral AI.

## Description

Spring AI provides a `ChatClient` abstraction for integrating with LLMs via several providers, including Mistral AI.
Spring AI provides a `ChatModel` abstraction for integrating with LLMs via several providers, including Mistral AI.

When using the _Spring AI Mistral AI Spring Boot Starter_, a `ChatClient` object is autoconfigured for you to use Mistral AI.
When using the _Spring AI Mistral AI Spring Boot Starter_, a `ChatModel` object is autoconfigured for you to use Mistral AI.

```java
@RestController
class ChatController {
private final ChatClient chatClient;
private final ChatModel chatModel;

ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
ChatController(ChatModel chatModel) {
this.chatModel = chatModel;
}

@GetMapping("/chat")
String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(message);
return chatModel.call(message);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.mistralai.MistralAiChatOptions;
Expand All @@ -11,28 +11,28 @@
@RestController
class ChatController {

private final ChatClient chatClient;
private final ChatModel chatModel;

ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
ChatController(ChatModel chatModel) {
this.chatModel = chatModel;
}

@GetMapping("/chat")
String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(message);
return chatModel.call(message);
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(new Prompt(message, ChatOptionsBuilder.builder()
return chatModel.call(new Prompt(message, ChatOptionsBuilder.builder()
.withTemperature(0.9f)
.build()))
.getResult().getOutput().getContent();
}

@GetMapping("/chat/mistral-ai-options")
String chatWithMistralAiOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(new Prompt(message, MistralAiChatOptions.builder()
return chatModel.call(new Prompt(message, MistralAiChatOptions.builder()
.withModel("open-mixtral-8x7b")
.withSafePrompt(true)
.build()))
Expand Down
14 changes: 7 additions & 7 deletions 01-chat-models/chat-models-ollama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ Text generation with LLMs via Ollama.

## Description

Spring AI provides a `ChatClient` abstraction for integrating with LLMs via several providers, including Ollama.
Spring AI provides a `ChatModel` abstraction for integrating with LLMs via several providers, including Ollama.

When using the _Spring AI Ollama Spring Boot Starter_, a `ChatClient` object is autoconfigured for you to use Ollama.
When using the _Spring AI Ollama Spring Boot Starter_, a `ChatModel` object is autoconfigured for you to use Ollama.

```java
@RestController
class ChatController {
private final ChatClient chatClient;
private final ChatModel chatModel;

ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
ChatController(ChatModel chatModel) {
this.chatModel = chatModel;
}

@GetMapping("/chat")
String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(message);
return chatModel.call(message);
}
}
```
Expand Down Expand Up @@ -63,7 +63,7 @@ http :8080/chat
Try passing your custom prompt and check the result.

```shell
http :8080/ai/chat message=="What is the capital of Italy?"
http :8080/chat message=="What is the capital of Italy?"
```

The next request is configured with a custom temperature value to obtain a more creative, yet less precise answer.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.api.OllamaOptions;
Expand All @@ -11,28 +11,28 @@
@RestController
class ChatController {

private final ChatClient chatClient;
private final ChatModel chatModel;

ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
ChatController(ChatModel chatModel) {
this.chatModel = chatModel;
}

@GetMapping("/chat")
String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(message);
return chatModel.call(message);
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(new Prompt(message, ChatOptionsBuilder.builder()
return chatModel.call(new Prompt(message, ChatOptionsBuilder.builder()
.withTemperature(1.3f)
.build()))
.getResult().getOutput().getContent();
}

@GetMapping("/chat/ollama-options")
String chatWithOllamaOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(new Prompt(message, OllamaOptions.create()
return chatModel.call(new Prompt(message, OllamaOptions.create()
.withModel("llama3")
.withRepeatPenalty(1.5f)))
.getResult().getOutput().getContent();
Expand Down
10 changes: 5 additions & 5 deletions 01-chat-models/chat-models-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ Text generation with LLMs via OpenAI.

## Description

Spring AI provides a `ChatClient` abstraction for integrating with LLMs via several providers, including OpenAI.
Spring AI provides a `ChatModel` abstraction for integrating with LLMs via several providers, including OpenAI.

When using the _Spring AI OpenAI Spring Boot Starter_, a `ChatClient` object is autoconfigured for you to use OpenAI.
When using the _Spring AI OpenAI Spring Boot Starter_, a `ChatModel` object is autoconfigured for you to use OpenAI.

```java
@RestController
class ChatController {
private final ChatClient chatClient;
private final ChatModel chatModel;

ChatController(ChatClient chatClient) {
ChatController(ChatModel chatModel) {
this.chatClient = chatClient;
}

@GetMapping("/chat")
String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(message);
return chatModel.call(message);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatOptions;
Expand All @@ -11,28 +11,28 @@
@RestController
class ChatController {

private final ChatClient chatClient;
private final ChatModel chatModel;

ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
ChatController(ChatModel chatModel) {
this.chatModel = chatModel;
}

@GetMapping("/chat")
String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(message);
return chatModel.call(message);
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(new Prompt(message, ChatOptionsBuilder.builder()
return chatModel.call(new Prompt(message, ChatOptionsBuilder.builder()
.withTemperature(1.3f)
.build()))
.getResult().getOutput().getContent();
}

@GetMapping("/chat/openai-options")
String chatWithOpenAiOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(new Prompt(message, OpenAiChatOptions.builder()
return chatModel.call(new Prompt(message, OpenAiChatOptions.builder()
.withModel("gpt-4-turbo")
.withUser("jon.snow")
.withFrequencyPenalty(1.3f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4o
model: gpt-3.5-turbo
temperature: 0.7
threads:
virtual:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class ChatService {
}

String chatWithText(String message) {
return chatClient.call(message);
return chatClient.prompt().user(message).call().content();
}

ChatResponse chatWithPrompt(String message) {
return chatClient.call(new Prompt(message));
return chatClient.prompt(new Prompt(message)).call().chatResponse();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatModel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class PromptsBasicsOllamaApplication {
Expand All @@ -10,4 +13,9 @@ public static void main(String[] args) {
SpringApplication.run(PromptsBasicsOllamaApplication.class, args);
}

@Bean
ChatClient chatClient(ChatModel chatModel) {
return ChatClient.builder(chatModel).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class ChatService {
}

String chatWithText(String message) {
return chatClient.call(message);
return chatClient.prompt().user(message).call().content();
}

ChatResponse chatWithPrompt(String message) {
return chatClient.call(new Prompt(message));
return chatClient.prompt(new Prompt(message)).call().chatResponse();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatModel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class PromptsBasicsOpenAiApplication {
Expand All @@ -10,4 +13,9 @@ public static void main(String[] args) {
SpringApplication.run(PromptsBasicsOpenAiApplication.class, args);
}

@Bean
ChatClient chatClient(ChatModel chatModel) {
return ChatClient.builder(chatModel).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4o
model: gpt-3.5-turbo
temperature: 0.7
threads:
virtual:
Expand Down
Loading

0 comments on commit 775652a

Please sign in to comment.