Skip to content

Commit

Permalink
Use new Output Conversion API
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Vitale <[email protected]>
  • Loading branch information
ThomasVitale committed May 9, 2024
1 parent 3d3ada6 commit c51473f
Show file tree
Hide file tree
Showing 23 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Output Parsers: Ollama
# Output Converters: Ollama

Parsing the LLM output as structured objects (Beans, Map, List) via Ollama.
Converting the LLM output to structured objects (Beans, Map, List) via Ollama.

## Running the application

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.ai.converter.MapOutputConverter;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.ai.parser.BeanOutputParser;
import org.springframework.ai.parser.ListOutputParser;
import org.springframework.ai.parser.MapOutputParser;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.stereotype.Service;

Expand All @@ -23,50 +23,50 @@ class ChatService {
}

ArtistInfo chatWithBeanOutput(MusicQuestion question) {
var outputParser = new BeanOutputParser<>(ArtistInfo.class);
var outputConverter = new BeanOutputConverter<>(ArtistInfo.class);

var userPromptTemplate = new PromptTemplate("""
Tell me name and band of one musician famous for playing in a {genre} band.
Consider only the musicians that play the {instrument} in that band.
{format}
""");
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputParser.getFormat());
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputConverter.getFormat());
var userMessage = userPromptTemplate.createMessage(model);

var chatResponse = chatClient.call(new Prompt(userMessage, OllamaOptions.create()
.withFormat("json")));
return outputParser.parse(chatResponse.getResult().getOutput().getContent());
return outputConverter.convert(chatResponse.getResult().getOutput().getContent());
}

Map<String,Object> chatWithMapOutput(MusicQuestion question) {
var outputParser = new MapOutputParser();
var outputConverter = new MapOutputConverter();

var userPromptTemplate = new PromptTemplate("""
Tell me name and band of one musician famous for playing in a {genre} band.
Consider only the musicians that play the {instrument} in that band.
{format}
""");
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputParser.getFormat());
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputConverter.getFormat());
var userMessage = userPromptTemplate.createMessage(model);

var chatResponse = chatClient.call(new Prompt(userMessage, OllamaOptions.create()
.withFormat("json")));
return outputParser.parse(chatResponse.getResult().getOutput().getContent());
return outputConverter.convert(chatResponse.getResult().getOutput().getContent());
}

List<String> chatWithListOutput(MusicQuestion question) {
var outputParser = new ListOutputParser(new DefaultConversionService());
var outputConverter = new ListOutputConverter(new DefaultConversionService());

var userPromptTemplate = new PromptTemplate("""
Tell me the names of three musicians famous for playing in a {genre} band.
Consider only the musicians that play the {instrument} in that band.
{format}
""");
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputParser.getFormat());
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputConverter.getFormat());
var userMessage = userPromptTemplate.createMessage(model);

var result = chatClient.call(userMessage);
return outputParser.parse(result);
return outputConverter.convert(result);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Output Parsers: OpenAI
# Output Converters: OpenAI

Parsing the LLM output as structured objects (Beans, Map, List) via OpenAI.
Converting the LLM output to structured objects (Beans, Map, List) via OpenAI.

## Running the application

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.ai.converter.MapOutputConverter;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.parser.BeanOutputParser;
import org.springframework.ai.parser.ListOutputParser;
import org.springframework.ai.parser.MapOutputParser;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.stereotype.Service;

Expand All @@ -24,52 +24,52 @@ class ChatService {
}

ArtistInfo chatWithBeanOutput(MusicQuestion question) {
var outputParser = new BeanOutputParser<>(ArtistInfo.class);
var outputConverter = new BeanOutputConverter<>(ArtistInfo.class);

var userPromptTemplate = new PromptTemplate("""
Tell me name and band of one musician famous for playing in a {genre} band.
Consider only the musicians that play the {instrument} in that band.
{format}
""");
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputParser.getFormat());
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputConverter.getFormat());
var userMessage = userPromptTemplate.createMessage(model);

var chatResponse = chatClient.call(new Prompt(userMessage, OpenAiChatOptions.builder()
.withResponseFormat(new OpenAiApi.ChatCompletionRequest.ResponseFormat("json_object"))
.build()));
return outputParser.parse(chatResponse.getResult().getOutput().getContent());
return outputConverter.convert(chatResponse.getResult().getOutput().getContent());
}

Map<String,Object> chatWithMapOutput(MusicQuestion question) {
var outputParser = new MapOutputParser();
var outputConverter = new MapOutputConverter();

var userPromptTemplate = new PromptTemplate("""
Tell me name and band of one musician famous for playing in a {genre} band.
Consider only the musicians that play the {instrument} in that band.
{format}
""");
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputParser.getFormat());
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputConverter.getFormat());
var userMessage = userPromptTemplate.createMessage(model);

var chatResponse = chatClient.call(new Prompt(userMessage, OpenAiChatOptions.builder()
.withResponseFormat(new OpenAiApi.ChatCompletionRequest.ResponseFormat("json_object"))
.build()));
return outputParser.parse(chatResponse.getResult().getOutput().getContent());
return outputConverter.convert(chatResponse.getResult().getOutput().getContent());
}

List<String> chatWithListOutput(MusicQuestion question) {
var outputParser = new ListOutputParser(new DefaultConversionService());
var outputConverter = new ListOutputConverter(new DefaultConversionService());

var userPromptTemplate = new PromptTemplate("""
Tell me the names of three musicians famous for playing in a {genre} band.
Consider only the musicians that play the {instrument} in that band.
{format}
""");
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputParser.getFormat());
Map<String,Object> model = Map.of("instrument", question.instrument(), "genre", question.genre(), "format", outputConverter.getFormat());
var userMessage = userPromptTemplate.createMessage(model);

var result = chatClient.call(userMessage);
return outputParser.parse(result);
return outputConverter.convert(result);
}

}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ Samples showing how to build Java applications powered by Generative AI and LLMs
| [prompts-templates-ollama](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/02-prompts/prompts-templates-ollama) | Prompting using templates with LLMs via Ollama. |
| [prompts-templates-openai](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/02-prompts/prompts-templates-openai) | Prompting using templates with LLMs via OpenAI. |

### 3. Output Parsers
### 3. Output Converters

| Project | Description |
|------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|
| [output-parsers-ollama](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/03-output-parsers/output-parsers-ollama) | Parsing the LLM output as structured objects (Beans, Map, List) via Ollama. |
| [output-parsers-openai](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/03-output-parsers/output-parsers-openai) | Parsing the LLM output as structured objects (Beans, Map, List) via Open AI. |
| Project | Description |
|---------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------|
| [output-converters-ollama](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/03-output-converters/output-converters-ollama) | Converting the LLM output to structured objects (Beans, Map, List) via Ollama. |
| [output-converters-openai](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/03-output-converters/output-converters-openai) | Converting the LLM output to structured objects (Beans, Map, List) via Open AI. |

### 4. Embedding Models

Expand Down
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ include '02-prompts:prompts-templates-openai'
include '02-prompts:prompts-multimodality-ollama'
include '02-prompts:prompts-multimodality-openai'

include '03-output-parsers:output-parsers-ollama'
include '03-output-parsers:output-parsers-openai'
include '03-output-converters:output-converters-ollama'
include '03-output-converters:output-converters-openai'

include '04-embedding-models:embedding-models-mistral-ai'
include '04-embedding-models:embedding-models-ollama'
Expand Down

0 comments on commit c51473f

Please sign in to comment.