Skip to content

Commit

Permalink
Extend chat-models and prompts samples
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Aug 25, 2024
1 parent 375f172 commit c8d030c
Show file tree
Hide file tree
Showing 87 changed files with 1,108 additions and 287 deletions.
10 changes: 5 additions & 5 deletions 01-chat-models/chat-models-mistral-ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,29 @@ You can now call the application that will use Mistral AI to generate text based
This example uses [httpie](https://httpie.io) to send HTTP requests.

```shell
http :8080/chat
http :8080/chat -b
```

Try passing your custom prompt and check the result.

```shell
http :8080/chat question=="What is the capital of Italy?"
http :8080/chat question=="What is the capital of Italy?" -b
```

The next request is configured with a custom temperature value to obtain a more creative, yet less precise answer.

```shell
http :8080/chat/generic-options question=="Why is a raven like a writing desk? Give a short answer."
http :8080/chat/generic-options question=="Why is a raven like a writing desk? Give a short answer." -b
```

The next request is configured with Mistral AI-specific customizations.

```shell
http :8080/chat/provider-options question=="What can you see beyond what you can see? Give a short answer."
http :8080/chat/provider-options question=="What can you see beyond what you can see? Give a short answer." -b
```

The final request returns the model's answer as a stream.

```shell
http --stream :8080/chat/stream question=="Why is a raven like a writing desk? Answer in 3 paragraphs."
http --stream :8080/chat/stream question=="Why is a raven like a writing desk? Answer in 3 paragraphs." -b
```
1 change: 1 addition & 0 deletions 01-chat-models/chat-models-mistral-ai/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot'
id 'io.spring.dependency-management'
id 'org.graalvm.buildtools.native'
}

group = 'com.thomasvitale'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* Chat examples using the high-level ChatClient API.
*/
@RestController
class ChatClientController {
class ChatController {

private final ChatClient chatClient;

ChatClientController(ChatClient.Builder chatClientBuilder) {
ChatController(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thomasvitale.ai.spring;
package com.thomasvitale.ai.spring.model;

import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
Expand Down
8 changes: 4 additions & 4 deletions 01-chat-models/chat-models-multiple-providers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ This example uses [httpie](https://httpie.io) to send HTTP requests.
Using OpenAI:

```shell
http :8080/chat/openai question=="What is the capital of Italy?"
http :8080/chat/openai question=="What is the capital of Italy?" -b
```

Using Mistral AI:

```shell
http :8080/chat/mistral-ai question=="What is the capital of Italy?"
http :8080/chat/mistral-ai question=="What is the capital of Italy?" -b
```

The next request is configured with OpenAI-specific customizations.

```shell
http :8080/chat/openai-options question=="Why is a raven like a writing desk? Give a short answer."
http :8080/chat/openai-options question=="Why is a raven like a writing desk? Give a short answer." -b
```

The next request is configured with Mistral AI-specific customizations.

```shell
http :8080/chat/mistral-ai-options question=="Why is a raven like a writing desk? Give a short answer."
http :8080/chat/mistral-ai-options question=="Why is a raven like a writing desk? Give a short answer." -b
```
1 change: 1 addition & 0 deletions 01-chat-models/chat-models-multiple-providers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot'
id 'io.spring.dependency-management'
id 'org.graalvm.buildtools.native'
}

group = 'com.thomasvitale'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
* Chat examples using the high-level ChatClient API.
*/
@RestController
class ChatClientController {
class ChatController {

private final ChatClient mistralAichatClient;
private final ChatClient openAichatClient;

ChatClientController(MistralAiChatModel mistralAiChatModel, OpenAiChatModel openAiChatModel) {
ChatController(MistralAiChatModel mistralAiChatModel, OpenAiChatModel openAiChatModel) {
this.mistralAichatClient = ChatClient.builder(mistralAiChatModel).build();
this.openAichatClient = ChatClient.builder(openAiChatModel).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thomasvitale.ai.spring;
package com.thomasvitale.ai.spring.model;

import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.mistralai.MistralAiChatModel;
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 @@ -53,10 +53,10 @@ The application relies on Ollama for providing LLMs. You can either run Ollama l
### Ollama as a native application

First, make sure you have [Ollama](https://ollama.ai) installed on your laptop.
Then, use Ollama to run the _mistral_ large language model. That's what we'll use in this example.
Then, use Ollama to pull the _mistral_ large language model.

```shell
ollama run mistral
ollama pull mistral
```

Finally, run the Spring Boot application.
Expand All @@ -79,29 +79,29 @@ You can now call the application that will use Ollama to generate text based on
This example uses [httpie](https://httpie.io) to send HTTP requests.

```shell
http :8080/chat
http :8080/chat -b
```

Try passing your custom prompt and check the result.

```shell
http :8080/chat question=="What is the capital of Italy?"
http :8080/chat question=="What is the capital of Italy?" -b
```

The next request is configured with a custom temperature value to obtain a more creative, yet less precise answer.

```shell
http :8080/chat/generic-options question=="Why is a raven like a writing desk? Give a short answer."
http :8080/chat/generic-options question=="Why is a raven like a writing desk? Give a short answer." -b
```

The next request is configured with Ollama-specific customizations.

```shell
http :8080/chat/provider-options question=="What can you see beyond what you can see? Give a short answer."
http :8080/chat/provider-options question=="What can you see beyond what you can see? Give a short answer." -b
```

The final request returns the model's answer as a stream.

```shell
http --stream :8080/chat/stream question=="Why is a raven like a writing desk? Answer in 3 paragraphs."
http --stream :8080/chat/stream question=="Why is a raven like a writing desk? Answer in 3 paragraphs." -b
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* Chat examples using the high-level ChatClient API.
*/
@RestController
class ChatClientController {
class ChatController {

private final ChatClient chatClient;

ChatClientController(ChatClient.Builder chatClientBuilder) {
ChatController(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thomasvitale.ai.spring;
package com.thomasvitale.ai.spring.model;

import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
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 @@ -71,29 +71,29 @@ You can now call the application that will use OpenAI to generate text based on
This example uses [httpie](https://httpie.io) to send HTTP requests.

```shell
http :8080/chat
http :8080/chat -b
```

Try passing your custom prompt and check the result.

```shell
http :8080/chat question=="What is the capital of Italy?"
http :8080/chat question=="What is the capital of Italy?" -b
```

The next request is configured with a custom temperature value to obtain a more creative, yet less precise answer.

```shell
http :8080/chat/generic-options question=="Why is a raven like a writing desk? Give a short answer."
http :8080/chat/generic-options question=="Why is a raven like a writing desk? Give a short answer." -b
```

The next request is configured with Open AI-specific customizations.

```shell
http :8080/chat/provider-options question=="What can you see beyond what you can see? Give a short answer."
http :8080/chat/provider-options question=="What can you see beyond what you can see? Give a short answer." -b
```

The final request returns the model's answer as a stream.

```shell
http --stream :8080/chat/stream question=="Why is a raven like a writing desk? Answer in 3 paragraphs."
http --stream :8080/chat/stream question=="Why is a raven like a writing desk? Answer in 3 paragraphs." -b
```
1 change: 1 addition & 0 deletions 01-chat-models/chat-models-openai/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot'
id 'io.spring.dependency-management'
id 'org.graalvm.buildtools.native'
}

group = 'com.thomasvitale'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* Chat examples using the high-level ChatClient API.
*/
@RestController
class ChatClientController {
class ChatController {

private final ChatClient chatClient;

ChatClientController(ChatClient.Builder chatClientBuilder) {
ChatController(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.thomasvitale.ai.spring;
package com.thomasvitale.ai.spring.model;

import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
Expand Down
14 changes: 7 additions & 7 deletions 02-prompts/prompts-basics-ollama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ The application relies on Ollama for providing LLMs. You can either run Ollama l
### Ollama as a native application

First, make sure you have [Ollama](https://ollama.ai) installed on your laptop.
Then, use Ollama to run the _mistral_ large language model.
Then, use Ollama to pull the _mistral_ large language model.

```shell
ollama run mistral
ollama pull mistral
```

Finally, run the Spring Boot application.
Expand All @@ -23,25 +23,25 @@ Finally, run the Spring Boot application.

### Ollama as a dev service with Testcontainers

The application relies on the native Testcontainers support in Spring Boot to spin up an Ollama service with a _mistral_ model at startup time.
The application relies on the native Testcontainers support in Spring Boot to spin up an Ollama service at startup time.

```shell
./gradlew bootTestRun
```

## Calling the application

You can now call the application that will use Ollama and _mistral_ to generate an answer to your questions.
You can now call the application that will use Ollama to generate an answer to your questions.
This example uses [httpie](https://httpie.io) to send HTTP requests.

```shell
http --raw "What is the capital of Italy?" :8080/chat/simple
http --raw "What is the capital of Italy?" :8080/chat/simple -b --pretty none
```

```shell
http --raw "What is the capital of Italy?" :8080/chat/prompt
http --raw "What is the capital of Italy?" :8080/chat/prompt -b --pretty none
```

```shell
http --raw "What is the capital of Italy?" :8080/chat/full
http --raw "What is the capital of Italy?" :8080/chat/full -b
```
3 changes: 0 additions & 3 deletions 02-prompts/prompts-basics-ollama/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ dependencies {
testAndDevelopmentOnly 'org.springframework.boot:spring-boot-devtools'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.springframework.ai:spring-ai-spring-boot-testcontainers'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.testcontainers:ollama'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
* Chat examples using the high-level ChatClient API.
*/
@RestController
class ChatController {

Expand All @@ -15,18 +18,18 @@ class ChatController {
}

@PostMapping("/chat/simple")
String chatWithText(@RequestBody String input) {
return chatService.chatWithText(input);
String chatWithText(@RequestBody String question) {
return chatService.chatWithText(question);
}

@PostMapping("/chat/prompt")
String chatWithPrompt(@RequestBody String input) {
return chatService.chatWithPrompt(input).getResult().getOutput().getContent();
String chatWithPrompt(@RequestBody String question) {
return chatService.chatWithPrompt(question).getResult().getOutput().getContent();
}

@PostMapping("/chat/full")
ChatResponse chatWithPromptAndFullResponse(@RequestBody String message) {
return chatService.chatWithPrompt(message);
ChatResponse chatWithPromptAndFullResponse(@RequestBody String question) {
return chatService.chatWithPrompt(question);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.stereotype.Service;

/**
* Chat examples using the high-level ChatClient API.
*/
@Service
class ChatService {

Expand All @@ -14,12 +17,12 @@ class ChatService {
this.chatClient = chatClientBuilder.build();
}

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

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

}
Loading

0 comments on commit c8d030c

Please sign in to comment.