-
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.
Improve ETL Pipeline and add initial Use Cases
Signed-off-by: Thomas Vitale <[email protected]>
- Loading branch information
1 parent
1e68816
commit 6375c3e
Showing
98 changed files
with
788 additions
and
669 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...rs/document-readers-text-ollama/README.md → ...estion-answering-with-documents/README.md
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
File renamed without changes.
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
48 changes: 48 additions & 0 deletions
48
...estion-answering-with-documents/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,48 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.ai.chat.ChatClient; | ||
import org.springframework.ai.chat.messages.UserMessage; | ||
import org.springframework.ai.chat.prompt.SystemPromptTemplate; | ||
import org.springframework.ai.document.Document; | ||
import org.springframework.ai.vectorstore.SearchRequest; | ||
import org.springframework.ai.vectorstore.VectorStore; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
class ChatService { | ||
|
||
private final ChatClient chatClient; | ||
private final VectorStore vectorStore; | ||
|
||
ChatService(ChatClient chatClient, VectorStore vectorStore) { | ||
this.chatClient = chatClient; | ||
this.vectorStore = vectorStore; | ||
} | ||
|
||
String chatWithDocument(String message) { | ||
var systemPromptTemplate = new SystemPromptTemplate(""" | ||
You are a helpful assistant, conversing with a user about the subjects contained in a set of documents. | ||
Use the information from the DOCUMENTS section to provide accurate answers. If unsure or if the answer | ||
isn't found in the DOCUMENTS section, simply state that you don't know the answer and do not mention | ||
the DOCUMENTS section. | ||
DOCUMENTS: | ||
{documents} | ||
"""); | ||
|
||
List<Document> similarDocuments = vectorStore.similaritySearch(SearchRequest.query(message).withTopK(5)); | ||
String content = similarDocuments.stream().map(Document::getContent).collect(Collectors.joining(System.lineSeparator())); | ||
|
||
Map<String,Object> model = Map.of("documents", content); | ||
var systemMessage = systemPromptTemplate.createMessage(model); | ||
|
||
var userMessage = new UserMessage(message); | ||
|
||
return chatClient.call(systemMessage, userMessage); | ||
} | ||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
...th-documents/src/main/java/com/thomasvitale/ai/spring/QuestionAnsweringWithDocuments.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,22 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.ai.embedding.EmbeddingClient; | ||
import org.springframework.ai.vectorstore.SimpleVectorStore; | ||
import org.springframework.ai.vectorstore.VectorStore; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@SpringBootApplication | ||
public class QuestionAnsweringWithDocuments { | ||
|
||
@Bean | ||
VectorStore vectorStore(EmbeddingClient embeddingClient) { | ||
return new SimpleVectorStore(embeddingClient); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(QuestionAnsweringWithDocuments.class, args); | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions
15
...cuments/src/test/java/com/thomasvitale/ai/spring/QuestionAnsweringWithDocumentsTests.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,15 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
@Disabled | ||
class QuestionAnsweringWithDocumentsTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...ocuments/src/test/java/com/thomasvitale/ai/spring/TestQuestionAnsweringWithDocuments.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,26 @@ | ||
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.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.context.annotation.Bean; | ||
import org.testcontainers.ollama.OllamaContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class TestQuestionAnsweringWithDocuments { | ||
|
||
@Bean | ||
@RestartScope | ||
@ServiceConnection | ||
OllamaContainer ollama() { | ||
return new OllamaContainer(DockerImageName.parse("ghcr.io/thomasvitale/ollama-llama3") | ||
.asCompatibleSubstituteFor("ollama/ollama")); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.from(QuestionAnsweringWithDocuments::main).with(TestQuestionAnsweringWithDocuments.class).run(args); | ||
} | ||
|
||
} |
52 changes: 0 additions & 52 deletions
52
...rs/document-readers-json-ollama/src/main/java/com/thomasvitale/ai/spring/ChatService.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.