Skip to content

Commit

Permalink
Add multimodality example with audio
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Nov 27, 2024
1 parent 445ec1a commit f47aa7d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions patterns/multimodality/multimodality-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Run the application.
> [!NOTE]
> These examples use the [httpie](https://httpie.io) CLI to send HTTP requests.
### Image

Call the application that will use a chat model to answer your question.

```shell
Expand All @@ -50,3 +52,11 @@ The image can also be fetched from a URL.
```shell
http :8080/chat/image/url question=="What's in the picture? Answer in one sentence" -b
```

### Audio

Call the application that will use a chat model to answer your question.

```shell
http :8080/chat/audio/file question=="What is this recording about? Give a short answer" -b
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.model.Media;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeTypeUtils;
Expand All @@ -19,6 +21,9 @@ class ChatController {

private final ChatClient chatClient;

@Value("classpath:speech.mp3")
private Resource audio;

@Value("classpath:tabby-cat.png")
private Resource image;

Expand Down Expand Up @@ -50,4 +55,18 @@ String chatImageUrl(String question) throws MalformedURLException {
.content();
}

@GetMapping("/chat/audio/file")
String chatAudioFile(String question) {
return chatClient.prompt()
.user(userSpec -> userSpec
.text(question)
.media(MimeTypeUtils.parseMimeType("audio/mp3"), audio)
)
.options(OpenAiChatOptions.builder()
.withModel(OpenAiApi.ChatModel.GPT_4_O_AUDIO_PREVIEW.getValue())
.build())
.call()
.content();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.Media;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeTypeUtils;
Expand All @@ -23,6 +25,9 @@ class ChatModelController {

private final ChatModel chatModel;

@Value("classpath:speech.mp3")
private Resource audio;

@Value("classpath:tabby-cat.png")
private Resource image;

Expand All @@ -49,4 +54,14 @@ String chatImageUrl(String question) throws MalformedURLException {
return chatResponse.getResult().getOutput().getContent();
}

@GetMapping("/chat/audio/file")
String chatAudioFile(String question) {
var userMessage = new UserMessage(question, new Media(MimeTypeUtils.parseMimeType("audio/mp3"), audio));
var prompt = new Prompt(userMessage, OpenAiChatOptions.builder()
.withModel(OpenAiApi.ChatModel.GPT_4_O_AUDIO_PREVIEW.getValue())
.build());
var chatResponse = chatModel.call(prompt);
return chatResponse.getResult().getOutput().getContent();
}

}
Binary file not shown.

0 comments on commit f47aa7d

Please sign in to comment.