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

Declarative AI Services #12

Merged
merged 5 commits into from Mar 25, 2024
Merged

Conversation

langchain4j
Copy link
Owner

@langchain4j langchain4j commented Mar 23, 2024

Currently the only way to create an instance of an AI Service is to manually use the builder:

Assistant assistant = AiServices.builder(Assistant.class)
    .chatLanguageModel(model)
    .tools(tool)
    .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
    ...
    .build();

While this is pretty simple and flexible, this can be made even more simple and natural for the Spring Boot users.

With this change, any interface annotated with @AiService will be automatically registered as a bean and configured to use all the LangChain4j components (beans) available in the context (e.g. chat model, chat memory, tools, etc.).

Here is an example of the simplest declarative AI Service:

@AiService
interface Assistant {

    String chat(String userMessage);
}
langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}

By configuring this property, an instance of the OpenAiChatModel will be autoconfigured as a bean. (This is not new, it was already possible with langchain4j-open-ai-spring-boot-starter.)

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-spring-boot-starter</artifactId>
    <version>0.29.0</version>
</dependency>

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
    <version>0.29.0</version>
</dependency>

When the application starts, autoconfiguration in langchain4j-spring-boot-starter will scan the classes of the project (starting from the package where @SpringBootApplication is located) and will find all interfaces annotated with @AiService.
It will use all available components (such as OpenAiChatModel in this case) when creating an instance of an AI Service.

It is also possible to customize which components should be used for the particular AI Service:

@Bean
ChatLanguageModel myCustomChatModel() {
    return OpenAiChatModel.withApiKey(...);
}

@AiService(wiringMode = EXPLICIT, chatModel = "myCustomChatModel")
interface Assistant {

    String chat(String userMessage);
}

@langchain4j langchain4j marked this pull request as ready for review March 25, 2024 17:09
@langchain4j langchain4j merged commit a017bcc into main Mar 25, 2024
1 of 5 checks passed
@langchain4j langchain4j changed the title WIP: declarative AI services and EasyRAG Declarative AI Services Mar 26, 2024
@langchain4j langchain4j deleted the declarative_ai_services_and_easy_rag branch April 8, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant