Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Jan 25, 2024
1 parent 80fecb2 commit c894b34
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
42 changes: 42 additions & 0 deletions 01-chat-models/chat-models-ollama/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,67 @@
# Chat Models: Ollama

Text generation with LLMs via Ollama.

## Description

Spring AI provides a `ChatClient` abstraction for integrating with LLMs via several providers, including Ollama.

When using the _Spring AI Ollama Spring Boot Starter_, a `ChatClient` object is autoconfigured for you to use Ollama.
By default, the _llama2_ model is used.

```java
@RestController
class ChatController {
private final ChatClient chatClient;

ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
}

@GetMapping("/ai/chat")
String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(message);
}
}
```

## Running the application

The application relies on Ollama for providing LLMs. You can either run Ollama locally on your laptop (macOS or Linux), or rely on the Testcontainers support in Spring Boot to spin up an Ollama service automatically.

### When using Ollama

First, make sure you have [Ollama](https://ollama.ai) installed on your laptop (macOS or Linux).
Then, use Ollama to run the _llama2_ large language model.

```shell
ollama run llama2
```

Finally, run the Spring Boot application.

```shell
./gradlew bootRun
```

### When using Docker/Podman

The application relies on the native Testcontainers support in Spring Boot to spin up an Ollama service with a _llama2_ model at startup time.

```shell
./gradlew bootTestRun
```

## Calling the application

You can now call the application that will use Ollama and llama2 to generate text based on a default prompt.

```shell
http :8080/ai/chat
```

Try passing your custom prompt and check the result.

```shell
http :8080/ai/chat message=="What is the capital of Italy?"
```
38 changes: 37 additions & 1 deletion 01-chat-models/chat-models-openai/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,59 @@
# Chat Models: OpenAI

Text generation with LLMs via OpenAI.

## Description

Spring AI provides a `ChatClient` abstraction for integrating with LLMs via several providers, including OpenAI.

When using the _Spring AI OpenAI Spring Boot Starter_, a `ChatClient` object is autoconfigured for you to use OpenAI.
By default, the _gpt-3.5-turbo_ model is used.

```java
@RestController
class ChatController {
private final ChatClient chatClient;

ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
}

@GetMapping("/ai/chat")
String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
return chatClient.call(message);
}
}
```

## Running the application

The application relies on an OpenAI API for providing LLMs.

### When using OpenAI

First, make sure you have an OpenAI account.
Then, define an environment variable with the OpenAI API Key associated to your OpenAI account as the value.

```shell
export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>
```

Finally, run the Spring Boot application.

```shell
./gradlew bootRun
```

## Calling the application

You can now call the application that will use Ollama and llama2 to generate text based on a default prompt.

```shell
http :8080/ai/chat
```

Try passing your custom prompt and check the result.

```shell
http :8080/ai/chat message=="What's the capital of Italy?"
http :8080/ai/chat message=="What is the capital of Italy?"
```
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Samples showing how to build Java applications powered by Generative AI and LLMs

### 1. Chat Models

| Project | Description |
|---------------------------------------------------------------------------------------------------------------------------|---------------|
| [chat-models-ollama](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/01-chat-models/chat-models-ollama) | _Coming soon_ |
| [chat-models-openai](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/01-chat-models/chat-models-openai) | _Coming soon_ |
| Project | Description |
|---------------------------------------------------------------------------------------------------------------------------|---------------------------------------|
| [chat-models-ollama](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/01-chat-models/chat-models-ollama) | Text generation with LLMs via Ollama. |
| [chat-models-openai](https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/01-chat-models/chat-models-openai) | Text generation with LLMs via OpenAI. |

### 2. Prompts

Expand Down

0 comments on commit c894b34

Please sign in to comment.