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

Multiple llms Example #7

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ build/

### VS Code ###
.vscode/

### macOS ###
.DS_Store
2 changes: 2 additions & 0 deletions models/chat/multiple-llms/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/mvnw text eol=lf
*.cmd text eol=crlf
33 changes: 33 additions & 0 deletions models/chat/multiple-llms/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
19 changes: 19 additions & 0 deletions models/chat/multiple-llms/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
113 changes: 113 additions & 0 deletions models/chat/multiple-llms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Spring AI Multiple LLMs Demo

A Spring Boot application showcasing how to integrate and switch between multiple Large Language Models (LLMs) using Spring AI. This project demonstrates working with both OpenAI and Anthropic models in a command-line interface application.

## Overview

This application serves as a practical example of building a chat interface that can seamlessly switch between different AI providers. It leverages Spring AI's abstraction layer to provide a unified interface for interacting with various LLM providers while maintaining clean and maintainable code.

## Project Requirements

- Java 17 or later
- Spring Boot 3.2 or later
- Valid API keys for:
- OpenAI
- Anthropic

## Dependencies

The project uses the following key dependencies:

- Spring Boot
- Spring AI
- Spring AI OpenAI Support
- Spring AI Anthropic Support

## Configuration

The application requires configuration of API keys for both OpenAI and Anthropic. These are specified in `application.properties`:

```properties
spring.application.name=multiple-llms

# OpenAI configuration
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4

# Anthropic configuration
spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY}
spring.ai.anthropic.chat.options.model=claude-3-5-sonnet-20240620
```

Set your API keys as environment variables:

```bash
export OPENAI_API_KEY=your_openai_key_here
export ANTHROPIC_API_KEY=your_anthropic_key_here
```

## Getting Started

After setting up your environment variables, build the project using your preferred build tool. The application uses Spring Boot's standard build process.

## Running the Application

Execute the application using:

```bash
./mvnw spring-boot:run
```

Once running, you'll be presented with a model selection prompt:

```
Select your AI model:
1. OpenAI
2. Anthropic
Enter your choice (1 or 2):
```

After selecting a model, you can start chatting with the AI. Type 'exit' to quit the application.

## Code Examples

### Setting up Chat Clients

The application configures separate chat clients for each AI provider:

```java
@Bean
public ChatClient openAIChatClient(OpenAiChatModel chatModel) {
return ChatClient.create(chatModel);
}

@Bean
public ChatClient anthropicChatClient(AnthropicChatModel chatModel) {
return ChatClient.create(chatModel);
}
```

### Handling User Input

The application uses a simple command-line interface to interact with the selected model:

```java
while (true) {
System.out.print("\nUSER: ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("exit")) {
System.out.println("Goodbye!");
break;
}
try {
String response = chat.prompt(input).call().content();
System.out.println("ASSISTANT: " + response);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
System.out.println("Please try again.");
}
}
```


Built with ❤️ using Spring AI
Loading