Skip to content

Commit

Permalink
feat: Add example with Ollama and Hugging Face
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Oct 16, 2024
1 parent 17860c2 commit 0e35993
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
18 changes: 16 additions & 2 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 pull the _mistral_ large language model.
Then, use Ollama to pull the _llama3.2_ large language model.

```shell
ollama pull mistral
ollama pull llama3.2
```

Finally, run the Spring Boot application.
Expand Down Expand Up @@ -105,3 +105,17 @@ 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." -b
```

Ollama lets you run models directly from Hugging Face. Let's try that out.

First, pull the `hf.co/SanctumAI/Meta-Llama-3.1-8B-Instruct-GGUF` model from Hugging Face.

```shell
ollama pull hf.co/SanctumAI/Meta-Llama-3.1-8B-Instruct-GGUF
```

Then, send a request.

```shell
http :8080/chat/hugging-face question=="Why is a raven like a writing desk? Give a short answer." -b
```
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class ChatController {

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

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String question) {
return chatClient.prompt()
.user(question)
return chatClient
.prompt(question)
.options(ChatOptionsBuilder.builder()
.withTemperature(0.9)
.build())
Expand All @@ -41,19 +41,30 @@ String chatWithGenericOptions(@RequestParam(defaultValue = "What did Gandalf say

@GetMapping("/chat/provider-options")
String chatWithProviderOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String question) {
return chatClient.prompt()
.user(question)
return chatClient
.prompt(question)
.options(OllamaOptions.create()
.withModel("mistral")
.withModel("llama3.2")
.withRepeatPenalty(1.5))
.call()
.content();
}

@GetMapping("/chat/huggingface")
String chatWithHuggingFace(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String question) {
return chatClient
.prompt(question)
.options(ChatOptionsBuilder.builder()
.withModel("hf.co/SanctumAI/Meta-Llama-3.1-8B-Instruct-GGUF")
.build())
.call()
.content();
}

@GetMapping("/chat/stream")
Flux<String> chatStream(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String question) {
return chatClient.prompt()
.user(question)
return chatClient
.prompt(question)
.stream()
.content();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ String chatWithGenericOptions(@RequestParam(defaultValue = "What did Gandalf say
@GetMapping("/chat/provider-options")
String chatWithProviderOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String question) {
return chatModel.call(new Prompt(question, OllamaOptions.create()
.withModel("mistral")
.withModel("llama3.2")
.withRepeatPenalty(1.5)))
.getResult().getOutput().getContent();
}

@GetMapping("/chat/huggingface")
String chatWithHuggingFace(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String question) {
return chatModel.call(new Prompt(question, ChatOptionsBuilder.builder()
.withModel("hf.co/SanctumAI/Meta-Llama-3.1-8B-Instruct-GGUF")
.build()))
.getResult().getOutput().getContent();
}

@GetMapping("/chat/stream")
Flux<String> chatStream(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String question) {
return chatModel.stream(question);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ spring:
ollama:
chat:
options:
model: mistral
model: llama3.2
temperature: 0.7
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestcontainersConfiguration {
@RestartScope
@ServiceConnection
OllamaContainer ollama() {
return new OllamaContainer(DockerImageName.parse("ghcr.io/thomasvitale/ollama-mistral")
return new OllamaContainer(DockerImageName.parse("ghcr.io/thomasvitale/ollama-llama-3-2")
.asCompatibleSubstituteFor("ollama/ollama"));
}

Expand Down

0 comments on commit 0e35993

Please sign in to comment.