-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update output parsers and embedding samples
- Loading branch information
1 parent
f76ad94
commit cc65400
Showing
54 changed files
with
953 additions
and
8 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
...dels-ollama/src/test/java/com/thomasvitale/ai/spring/TestChatModelsOllamaApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Chat Models: OpenAI | ||
|
||
```shell | ||
./gradlew bootRun | ||
./gradlew bootTestRun | ||
``` | ||
|
||
```shell | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...dels-openai/src/test/java/com/thomasvitale/ai/spring/TestChatModelsOpenaiApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class TestChatModelsOpenaiApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.from(ChatModelsOpenaiApplication::main).with(TestChatModelsOpenaiApplication.class).run(args); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Prompts Basic: Ollama | ||
|
||
```shell | ||
./gradlew bootTestRun | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/simple | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/prompt | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/full | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ spring: | |
ai: | ||
ollama: | ||
chat: | ||
model: mistral | ||
model: llama2 | ||
options: | ||
temperature: 0.7 | ||
threads: | ||
|
16 changes: 16 additions & 0 deletions
16
...cs-ollama/src/test/java/com/thomasvitale/ai/spring/TestPromptBasicsOllamaApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Prompts Basic: OpenAI | ||
|
||
```shell | ||
./gradlew bootTestRun | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/simple | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/prompt | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/full | ||
``` |
13 changes: 13 additions & 0 deletions
13
...s-openai/src/test/java/com/thomasvitale/ai/spring/TestPromptsBasicsOpenAiApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class TestPromptsBasicsOpenAiApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.from(PromptsBasicsOpenAiApplication::main).with(TestPromptsBasicsOpenAiApplication.class).run(args); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
HELP.md | ||
.gradle | ||
build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
bin/ | ||
!**/src/main/**/bin/ | ||
!**/src/test/**/bin/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
out/ | ||
!**/src/main/**/out/ | ||
!**/src/test/**/out/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Prompts Messages: Ollama | ||
|
||
```shell | ||
./gradlew bootTestRun | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/single | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/multiple | ||
``` | ||
|
||
```shell | ||
http --raw "What is the capital of Italy?" :8080/ai/chat/external | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
plugins { | ||
id 'java' | ||
id 'org.springframework.boot' | ||
id 'io.spring.dependency-management' | ||
} | ||
|
||
group = 'com.thomasvitale' | ||
version = '0.0.1-SNAPSHOT' | ||
|
||
java { | ||
sourceCompatibility = '21' | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven { url 'https://repo.spring.io/milestone' } | ||
maven { url 'https://repo.spring.io/snapshot' } | ||
} | ||
|
||
dependencies { | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
|
||
implementation "org.springframework.ai:spring-ai-ollama-spring-boot-starter:${springAiVersion}" | ||
|
||
testAndDevelopmentOnly 'org.springframework.boot:spring-boot-devtools' | ||
|
||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
testImplementation 'org.springframework.boot:spring-boot-testcontainers' | ||
testImplementation 'org.testcontainers:junit-jupiter' | ||
} | ||
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} |
31 changes: 31 additions & 0 deletions
31
...mpts/prompts-messages-ollama/src/main/java/com/thomasvitale/ai/spring/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
class ChatController { | ||
|
||
private final ChatService chatService; | ||
|
||
ChatController(ChatService chatService) { | ||
this.chatService = chatService; | ||
} | ||
|
||
@PostMapping("/ai/chat/single") | ||
String chatWithSingleMessage(@RequestBody String input) { | ||
return chatService.chatWithSingleMessage(input).getContent(); | ||
} | ||
|
||
@PostMapping("/ai/chat/multiple") | ||
String chatWithMultipleMessages(@RequestBody String input) { | ||
return chatService.chatWithMultipleMessages(input).getContent(); | ||
} | ||
|
||
@PostMapping("/ai/chat/external") | ||
String chatWithExternalMessage(@RequestBody String input) { | ||
return chatService.chatWithExternalMessage(input).getContent(); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
02-prompts/prompts-messages-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.ai.chat.ChatClient; | ||
import org.springframework.ai.prompt.Prompt; | ||
import org.springframework.ai.prompt.messages.AssistantMessage; | ||
import org.springframework.ai.prompt.messages.SystemMessage; | ||
import org.springframework.ai.prompt.messages.UserMessage; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
class ChatService { | ||
|
||
private final ChatClient chatClient; | ||
|
||
private final Resource systemMessageResource; | ||
|
||
ChatService(ChatClient chatClient, @Value("classpath:/prompts/system-message.st") Resource systemMessageResource) { | ||
this.chatClient = chatClient; | ||
this.systemMessageResource = systemMessageResource; | ||
} | ||
|
||
AssistantMessage chatWithSingleMessage(String message) { | ||
var userMessage = new UserMessage(message); | ||
var prompt = new Prompt(userMessage); | ||
var chatResponse = chatClient.generate(prompt); | ||
return new AssistantMessage(chatResponse.getGeneration().getContent(), chatResponse.getGeneration().getProperties()); | ||
} | ||
|
||
AssistantMessage chatWithMultipleMessages(String message) { | ||
var systemMessage = new SystemMessage(""" | ||
You are a helpful and polite assistant. | ||
Answer in one sentence using a very formal language | ||
and starting the answer with a formal greeting. | ||
"""); | ||
var userMessage = new UserMessage(message); | ||
var prompt = new Prompt(List.of(systemMessage, userMessage)); | ||
var chatResponse = chatClient.generate(prompt); | ||
return new AssistantMessage(chatResponse.getGeneration().getContent(), chatResponse.getGeneration().getProperties()); | ||
} | ||
|
||
AssistantMessage chatWithExternalMessage(String message) { | ||
var systemMessage = new SystemMessage(systemMessageResource); | ||
var userMessage = new UserMessage(message); | ||
var prompt = new Prompt(List.of(systemMessage, userMessage)); | ||
var chatResponse = chatClient.generate(prompt); | ||
return new AssistantMessage(chatResponse.getGeneration().getContent(), chatResponse.getGeneration().getProperties()); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...ages-ollama/src/main/java/com/thomasvitale/ai/spring/PromptMessagesOllamaApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class PromptMessagesOllamaApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(PromptMessagesOllamaApplication.class, args); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
02-prompts/prompts-messages-ollama/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
spring: | ||
ai: | ||
ollama: | ||
chat: | ||
model: llama2 | ||
options: | ||
temperature: 0.7 | ||
threads: | ||
virtual: | ||
enabled: true |
3 changes: 3 additions & 0 deletions
3
02-prompts/prompts-messages-ollama/src/main/resources/prompts/system-message.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
You are a funny and hilarious assistant. | ||
Answer in one sentence using a very informal language | ||
and starting the answer with a knock knowck joke. |
13 changes: 13 additions & 0 deletions
13
...ollama/src/test/java/com/thomasvitale/ai/spring/PromptMessagesOllamaApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class PromptMessagesOllamaApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...-ollama/src/test/java/com/thomasvitale/ai/spring/TestPromptMessagesOllamaApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.devtools.restart.RestartScope; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class TestPromptMessagesOllamaApplication { | ||
|
||
@Bean | ||
@RestartScope | ||
@Scope("singleton") // needed because of https://github.com/spring-projects/spring-boot/issues/35786 | ||
GenericContainer<?> ollama(DynamicPropertyRegistry properties) { | ||
var ollama = new GenericContainer<>("ghcr.io/thomasvitale/ollama-llama2") | ||
.withExposedPorts(11434); | ||
properties.add("spring.ai.ollama.base-url", | ||
() -> "http://%s:%s".formatted(ollama.getHost(), ollama.getMappedPort(11434))); | ||
return ollama; | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.from(PromptMessagesOllamaApplication::main).with(TestPromptMessagesOllamaApplication.class).run(args); | ||
} | ||
|
||
} |
Oops, something went wrong.