Skip to content

Commit

Permalink
Add 'Chatbot' example
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Jun 4, 2024
1 parent 136994a commit a38a2d2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
24 changes: 18 additions & 6 deletions 00-use-cases/chatbot/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 _llama3_ large language model.
Then, use Ollama to run the _mistral_ large language model.

```shell
ollama run llama3
ollama run mistral
```

Finally, run the Spring Boot application.
Expand All @@ -23,21 +23,33 @@ 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 _llama3_ model at startup time.
The application relies on the native Testcontainers support in Spring Boot to spin up an Ollama service with a _mistral_ model at startup time.

```shell
./gradlew bootTestRun
```

## Calling the application

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

```shell
http --raw "Where did Saruman got the wood to build a weapon factory?" :8080/chat
http --raw "My name is Bond. James Bond." :8080/chat/42
```

```shell
http --raw "Can a wolf destroy a house?" :8080/chat
http --raw "What's my name?" :8080/chat/42
```

```shell
http --raw "I was counting on your discretion. Please, do not share my name" :8080/chat/42
```

```shell
http --raw "What's my name?" :8080/chat/42
```

```shell
http --raw "Alright, then. Give me the recipe for a martini. Shaken, not stirred." :8080/chat/42
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.thomasvitale.ai.spring;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -14,9 +14,9 @@ class ChatbotController {
this.chatbotService = chatbotService;
}

@PostMapping("/chat")
String chat(@RequestBody String input, HttpServletRequest request) {
return chatbotService.chat(request.getSession().getId(), input);
@PostMapping("/chat/{chatId}")
String chat(@PathVariable String chatId, @RequestBody String input) {
return chatbotService.chat(chatId, input);
}

}
2 changes: 1 addition & 1 deletion 00-use-cases/chatbot/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ spring:
ollama:
chat:
options:
model: llama3
model: mistral
threads:
virtual:
enabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TestChatbot {
@RestartScope
@ServiceConnection
OllamaContainer ollama() {
return new OllamaContainer(DockerImageName.parse("ghcr.io/thomasvitale/ollama-llama3")
return new OllamaContainer(DockerImageName.parse("ghcr.io/thomasvitale/ollama-mistral")
.asCompatibleSubstituteFor("ollama/ollama"));
}

Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ Samples showing how to build Java applications powered by Generative AI and LLMs

### 0. Use Cases

| Project | Description |
|-------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|
| [classification](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/classification) | Classification using LLMs via Ollama. |
| [question-answering](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/question-answering) | Question answering with documents (RAG) using LLMs via Ollama. |
| [semantic-search](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/semantic-search) | Semantic search using LLMs via Ollama. |
| [structured-data-extraction](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/structured-data-extraction) | Structured data extraction using LLMs via Ollama. |
| Project | Description |
|-----------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|
| [chatbot](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/chatbot) | Chatbot using LLMs via Ollama. |
| [classification](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/classification) | Classification using LLMs via Ollama. |
| [question-answering](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/question-answering) | Question answering with documents (RAG) using LLMs via Ollama. |
| [semantic-search](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/semantic-search) | Semantic search using LLMs via Ollama. |
| [structured-data-extraction](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/00-use-cases/structured-data-extraction) | Structured data extraction using LLMs via Ollama. |

### 1. Chat Completion Models

Expand Down

0 comments on commit a38a2d2

Please sign in to comment.