Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to configure a custom output parser #1126

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

anunnakian
Copy link
Contributor

Issue

#968

Change

Add the ability to configure a custom output parser, here is an example :

First of all, I define City class and my custom output parser by implementing OutputParser interface

@Data
@AllArgsConstructor
static class City {
    private String name;
}

static class CityOutputParser implements OutputParser<City> {

    @Override
    public City parse(String string) {
        return new City(string);
    }

    @Override
    public String formatInstructions() {
        return "A name of a city";
    }
}

Then, I use my custom output parser by adding an annotation on my method called @Output like this :

interface AiService {

        @Output(CityOutputParser.class)
        @UserMessage("What is the capital of Germany?")
        City chat();
}

Here is a test :

@Test
void test() {

    // given
    AiService aiService = AiServices.builder(AiService.class)
            .chatLanguageModel(chatLanguageModel)
            .build();

    // when-then
    assertThat(aiService.chat()).isEqualTo(new City("Berlin"));
    verify(chatLanguageModel).generate(singletonList(userMessage("What is the capital of Germany?A name of a city")));
}

General checklist

  • There are no breaking changes
  • I have added unit and integration tests for my change
  • I have manually run all the unit and integration tests in the module I have added/changed, and they are all green
  • I have manually run all the unit and integration tests in the core and main modules, and they are all green

Checklist for adding new model integration

  • I have added my new module in the BOM

Checklist for adding new embedding store integration

  • I have added a {NameOfIntegration}EmbeddingStoreIT that extends from either EmbeddingStoreIT or EmbeddingStoreWithFilteringIT
  • I have added my new module in the BOM

Checklist for changing existing embedding store integration

  • I have manually verified that the {NameOfIntegration}EmbeddingStore works correctly with the data persisted using the latest released version of LangChain4j

@langchain4j
Copy link
Owner

Related: #1176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 High priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants