Skip to content

Commit

Permalink
feat: make ollama work
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Dec 16, 2023
1 parent 394a0f5 commit 669079e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.ai.prompt.messages.Message;
import org.springframework.ai.prompt.messages.UserMessage;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
Expand All @@ -48,14 +49,24 @@ public class DocumentService {
private static final Logger LOGGER = LoggerFactory.getLogger(DocumentService.class);
private static final String ID = "id";
private static final String DISTANCE = "distance";
private static final Integer CHUNK_TOKEN_LIMIT = 5000;
private final DocumentRepository documentRepository;
private final DocumentVsRepository documentVsRepository;
private final AiClient aiClient;
private String systemPrompt = "You're assisting with questions about documents in a catalog.\n"
private final String systemPrompt = "You're assisting with questions about documents in a catalog.\n"
+ "Use the information from the DOCUMENTS section to provide accurate answers.\n"
+ "If unsure, simply state that you don't know.\n" + "\n" + "DOCUMENTS:\n" + "{documents}";

private final String ollamaPrompt = "You're assisting with questions about documents in a catalog.\n"
+ "Use the information from the DOCUMENTS section to provide accurate answers.\n"
+ "If unsure, simply state that you don't know.\n \n" + " {prompt} \n \n" + "DOCUMENTS:\n" + "{documents}";

@Value("${embedding-token-limit:1000}")
private Integer embeddingTokenLimit;
@Value("${document-token-limit:1000}")
private Integer documentTokenLimit;
@Value("${spring.profiles.active:}")
private String activeProfile;

public DocumentService(DocumentRepository documentRepository, DocumentVsRepository documentVsRepository,
AiClient aiClient) {
this.documentRepository = documentRepository;
Expand All @@ -70,7 +81,7 @@ public Long storeDocument(Document document) {
record TikaDocumentAndContent(org.springframework.ai.document.Document document, String content) {
}
var aiDocuments = tikaDocuments.stream()
.flatMap(myDocument1 -> this.splitStringToTokenLimit(myDocument1.getContent(), CHUNK_TOKEN_LIMIT)
.flatMap(myDocument1 -> this.splitStringToTokenLimit(myDocument1.getContent(), embeddingTokenLimit)
.stream().map(myStr -> new TikaDocumentAndContent(myDocument1, myStr)))
.map(myTikaRecord -> new org.springframework.ai.document.Document(myTikaRecord.content(),
myTikaRecord.document().getMetadata()))
Expand Down Expand Up @@ -99,8 +110,10 @@ public AiResult queryDocuments(SearchDto searchDto) {
.toList();
Message systemMessage = switch (searchDto.getSearchType()) {
case SearchDto.SearchType.DOCUMENT -> this.getSystemMessage(documentChunks,
(documentChunks.size() <= 0 ? 2000 : Math.floorDiv(2000, documentChunks.size())));
case SearchDto.SearchType.PARAGRAPH -> this.getSystemMessage(mostSimilar.stream().toList(), 2000);
(documentChunks.size() <= 0 ? this.documentTokenLimit : Math.floorDiv(this.documentTokenLimit, documentChunks.size())),
searchDto.getSearchString());
case SearchDto.SearchType.PARAGRAPH ->
this.getSystemMessage(mostSimilar.stream().toList(), this.documentTokenLimit, searchDto.getSearchString());
};
UserMessage userMessage = new UserMessage(searchDto.getSearchString());
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
Expand All @@ -123,14 +136,17 @@ public AiResult queryDocuments(SearchDto searchDto) {
return new AiResult(searchDto.getSearchString(), response.getGenerations(), documents);
}

private Message getSystemMessage(List<org.springframework.ai.document.Document> similarDocuments, int tokenLimit) {
private Message getSystemMessage(List<org.springframework.ai.document.Document> similarDocuments, int tokenLimit,
String prompt) {
String documents = similarDocuments.stream().map(entry -> entry.getContent())
.filter(myStr -> myStr != null && !myStr.isBlank())
.map(myStr -> this.cutStringToTokenLimit(myStr, tokenLimit)).collect(Collectors.joining("\n"));
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(this.systemPrompt);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("documents", documents));
SystemPromptTemplate systemPromptTemplate = this.activeProfile.contains("ollama")
? new SystemPromptTemplate(this.ollamaPrompt)
: new SystemPromptTemplate(this.systemPrompt);
systemPromptTemplate = new SystemPromptTemplate("{prompt} \n");
Message systemMessage = systemPromptTemplate.createMessage(Map.of("documents", documents, "prompt", prompt));
return systemMessage;

}

private List<String> splitStringToTokenLimit(String documentStr, int tokenLimit) {
Expand Down
4 changes: 3 additions & 1 deletion backend/src/main/resources/application-ollama.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
spring.ai.ollama.base-url=${OLLAMA-BASE-URL:http://localhost:11434}
spring.liquibase.change-log=classpath:/dbchangelog/db.changelog-master-ollama.xml
spring.ai.ollama.model=stable-beluga:13b
spring.liquibase.change-log=classpath:/dbchangelog/db.changelog-master-ollama.xml
document-token-limit=2500
3 changes: 3 additions & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ server.servlet.session.timeout=30s

spring.jackson.parser.allow-unquoted-control-chars=true
spring.ai.openai.api-key=${OPENAI-API-KEY:23418pajkfdsadlöa}

embedding-token-limit=500
document-token-limit=2000

0 comments on commit 669079e

Please sign in to comment.