Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Oct 31, 2024
1 parent a245ab0 commit 9d08bef
Show file tree
Hide file tree
Showing 48 changed files with 476 additions and 705 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.ai.mistralai.MistralAiChatOptions;
import org.springframework.ai.mistralai.api.MistralAiApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

Expand All @@ -22,15 +21,15 @@ class ChatController {
}

@GetMapping("/chat")
String chat(@RequestParam String question) {
String chat(String question) {
return chatClient
.prompt(question)
.call()
.content();
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam String question) {
String chatWithGenericOptions(String question) {
return chatClient
.prompt(question)
.options(ChatOptionsBuilder.builder()
Expand All @@ -42,7 +41,7 @@ String chatWithGenericOptions(@RequestParam String question) {
}

@GetMapping("/chat/provider-options")
String chatWithProviderOptions(@RequestParam String question) {
String chatWithProviderOptions(String question) {
return chatClient
.prompt(question)
.options(MistralAiChatOptions.builder()
Expand All @@ -53,7 +52,7 @@ String chatWithProviderOptions(@RequestParam String question) {
}

@GetMapping("/chat/stream")
Flux<String> chatStream(@RequestParam String question) {
Flux<String> chatStream(String question) {
return chatClient
.prompt(question)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.ai.mistralai.api.MistralAiApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

Expand All @@ -25,12 +24,12 @@ class ChatModelController {
}

@GetMapping("/chat")
String chat(@RequestParam String question) {
String chat(String question) {
return chatModel.call(question);
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam String question) {
String chatWithGenericOptions(String question) {
return chatModel.call(new Prompt(question, ChatOptionsBuilder.builder()
.withModel(MistralAiApi.ChatModel.OPEN_MIXTRAL_7B.getName())
.withTemperature(0.9)
Expand All @@ -39,7 +38,7 @@ String chatWithGenericOptions(@RequestParam String question) {
}

@GetMapping("/chat/provider-options")
String chatWithProviderOptions(@RequestParam String question) {
String chatWithProviderOptions(String question) {
return chatModel.call(new Prompt(question, MistralAiChatOptions.builder()
.withSafePrompt(true)
.build()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -26,23 +25,23 @@ class ChatController {
}

@GetMapping("/chat/mistral-ai")
String chatMistralAi(@RequestParam String question) {
String chatMistralAi(String question) {
return mistralAichatClient
.prompt(question)
.call()
.content();
}

@GetMapping("/chat/openai")
String chatOpenAi(@RequestParam String question) {
String chatOpenAi(String question) {
return openAichatClient
.prompt(question)
.call()
.content();
}

@GetMapping("/chat/mistral-ai-options")
String chatWithMistralAiOptions(@RequestParam String question) {
String chatWithMistralAiOptions(String question) {
return mistralAichatClient
.prompt(question)
.options(MistralAiChatOptions.builder()
Expand All @@ -54,7 +53,7 @@ String chatWithMistralAiOptions(@RequestParam String question) {
}

@GetMapping("/chat/openai-options")
String chatWithOpenAiOptions(@RequestParam String question) {
String chatWithOpenAiOptions(String question) {
return openAichatClient
.prompt(question)
.options(OpenAiChatOptions.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -28,17 +27,17 @@ class ChatModelController {
}

@GetMapping("/chat/mistral-ai")
String chatMistralAi(@RequestParam String question) {
String chatMistralAi(String question) {
return mistralAiChatModel.call(question);
}

@GetMapping("/chat/openai")
String chatOpenAi(@RequestParam String question) {
String chatOpenAi(String question) {
return openAiChatModel.call(question);
}

@GetMapping("/chat/mistral-ai-options")
String chatWithMistralAiOptions(@RequestParam String question) {
String chatWithMistralAiOptions(String question) {
return mistralAiChatModel.call(new Prompt(question, MistralAiChatOptions.builder()
.withModel(MistralAiApi.ChatModel.OPEN_MIXTRAL_7B.getValue())
.withTemperature(1.0)
Expand All @@ -47,7 +46,7 @@ String chatWithMistralAiOptions(@RequestParam String question) {
}

@GetMapping("/chat/openai-options")
String chatWithOpenAiOptions(@RequestParam String question) {
String chatWithOpenAiOptions(String question) {
return openAiChatModel.call(new Prompt(question, OpenAiChatOptions.builder()
.withModel(OpenAiApi.ChatModel.GPT_4_O_MINI.getValue())
.withTemperature(1.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

Expand All @@ -21,15 +20,15 @@ class ChatController {
}

@GetMapping("/chat")
String chat(@RequestParam String question) {
String chat(String question) {
return chatClient
.prompt(question)
.call()
.content();
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam String question) {
String chatWithGenericOptions(String question) {
return chatClient
.prompt(question)
.options(ChatOptionsBuilder.builder()
Expand All @@ -41,7 +40,7 @@ String chatWithGenericOptions(@RequestParam String question) {
}

@GetMapping("/chat/provider-options")
String chatWithProviderOptions(@RequestParam String question) {
String chatWithProviderOptions(String question) {
return chatClient
.prompt(question)
.options(OllamaOptions.builder()
Expand All @@ -52,7 +51,7 @@ String chatWithProviderOptions(@RequestParam String question) {
}

@GetMapping("/chat/huggingface")
String chatWithHuggingFace(@RequestParam String question) {
String chatWithHuggingFace(String question) {
return chatClient
.prompt(question)
.options(ChatOptionsBuilder.builder()
Expand All @@ -63,7 +62,7 @@ String chatWithHuggingFace(@RequestParam String question) {
}

@GetMapping("/chat/stream")
Flux<String> chatStream(@RequestParam String question) {
Flux<String> chatStream(String question) {
return chatClient
.prompt(question)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

Expand All @@ -24,12 +23,12 @@ class ChatModelController {
}

@GetMapping("/chat")
String chat(@RequestParam String question) {
String chat(String question) {
return chatModel.call(question);
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam String question) {
String chatWithGenericOptions(String question) {
return chatModel.call(new Prompt(question, ChatOptionsBuilder.builder()
.withModel("llama3.2:1b")
.withTemperature(0.9)
Expand All @@ -38,23 +37,23 @@ String chatWithGenericOptions(@RequestParam String question) {
}

@GetMapping("/chat/provider-options")
String chatWithProviderOptions(@RequestParam String question) {
String chatWithProviderOptions(String question) {
return chatModel.call(new Prompt(question, OllamaOptions.builder()
.withRepeatPenalty(1.5)
.build()))
.getResult().getOutput().getContent();
}

@GetMapping("/chat/huggingface")
String chatWithHuggingFace(@RequestParam String question) {
String chatWithHuggingFace(String question) {
return chatModel.call(new Prompt(question, ChatOptionsBuilder.builder()
.withModel("hf.co/SanctumAI/Llama-3.2-1B-Instruct-GGUF")
.build()))
.getResult().getOutput().getContent();
}

@GetMapping("/chat/stream")
Flux<String> chatStream(@RequestParam String question) {
Flux<String> chatStream(String question) {
return chatModel.stream(question);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

Expand All @@ -22,15 +21,15 @@ class ChatController {
}

@GetMapping("/chat")
String chat(@RequestParam String question) {
String chat(String question) {
return chatClient
.prompt(question)
.call()
.content();
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam String question) {
String chatWithGenericOptions(String question) {
return chatClient
.prompt(question)
.options(ChatOptionsBuilder.builder()
Expand All @@ -42,7 +41,7 @@ String chatWithGenericOptions(@RequestParam String question) {
}

@GetMapping("/chat/provider-options")
String chatWithProviderOptions(@RequestParam String question) {
String chatWithProviderOptions(String question) {
return chatClient
.prompt(question)
.options(OpenAiChatOptions.builder()
Expand All @@ -53,7 +52,7 @@ String chatWithProviderOptions(@RequestParam String question) {
}

@GetMapping("/chat/stream")
Flux<String> chatStream(@RequestParam String question) {
Flux<String> chatStream(String question) {
return chatClient
.prompt(question)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

Expand All @@ -25,12 +24,12 @@ class ChatModelController {
}

@GetMapping("/chat")
String chat(@RequestParam String question) {
String chat(String question) {
return chatModel.call(question);
}

@GetMapping("/chat/generic-options")
String chatWithGenericOptions(@RequestParam String question) {
String chatWithGenericOptions(String question) {
return chatModel.call(new Prompt(question, ChatOptionsBuilder.builder()
.withModel(OpenAiApi.ChatModel.GPT_4_O_MINI.getValue())
.withTemperature(0.9)
Expand All @@ -39,15 +38,15 @@ String chatWithGenericOptions(@RequestParam String question) {
}

@GetMapping("/chat/provider-options")
String chatWithProviderOptions(@RequestParam String question) {
String chatWithProviderOptions(String question) {
return chatModel.call(new Prompt(question, OpenAiChatOptions.builder()
.withLogprobs(true)
.build()))
.getResult().getOutput().getContent();
}

@GetMapping("/chat/stream")
Flux<String> chatStream(@RequestParam String question) {
Flux<String> chatStream(String question) {
return chatModel.stream(question);
}

Expand Down
28 changes: 15 additions & 13 deletions 04-multimodality/multimodality-ollama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@

Multimodality with LLMs via Ollama.

## Running the application

The application relies on Ollama for providing LLMs. You can either run Ollama locally on your laptop, or rely on the Testcontainers support in Spring Boot to spin up an Ollama service automatically.
Either way, Spring AI will take care of pulling the needed Ollama models if not already available in your instance.
## Ollama

### Ollama as a native application
The application relies on Ollama for providing LLMs. You can either run Ollama locally on your laptop,
or rely on the Testcontainers support in Spring Boot to spin up an Ollama service automatically.
If you choose the first option, make sure you have [Ollama](https://ollama.ai) installed and running on your laptop.
Either way, Spring AI will take care of pulling the needed Ollama models when the application starts,
if they are not available yet on your machine.

First, make sure you have [Ollama](https://ollama.ai) installed on your laptop.
## Running the application

Then, run the Spring Boot application.
If you're using the native Ollama application, run the application as follows.

```shell
./gradlew bootRun
```

### Ollama as a dev service with Testcontainers

The application relies on the native Testcontainers support in Spring Boot to spin up an Ollama service at startup time.
If you want to rely on the native Testcontainers support in Spring Boot to spin up an Ollama service at startup time,
run the application as follows.

```shell
./gradlew bootTestRun
```

## Calling the application

You can now call the application that will use Ollama to generate text based on a default image.
This example uses [httpie](https://httpie.io) to send HTTP requests.
> [!NOTE]
> These examples use the [httpie](https://httpie.io) CLI to send HTTP requests.
Call the application that will use a chat model to answer your question.

```shell
http :8080/chat/image/file -b
http :8080/chat/image/file question=="What do you see in this picture? Give a short answer" -b
```

Try passing your custom prompt and check the result.
Expand Down
Loading

0 comments on commit 9d08bef

Please sign in to comment.