-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: return the sourceDocument when asking for documents info (#185)
* feat: return the sourceDocument when asking for documents info * feat: create summary when using documentChain
- Loading branch information
1 parent
22615b5
commit ce3c3c2
Showing
7 changed files
with
110 additions
and
132 deletions.
There are no files selected for viewing
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
70 changes: 0 additions & 70 deletions
70
packages/api/src/ai/services/agent-conversation.service.ts
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
packages/api/src/ai/services/document-conversation-chain.service.ts
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,72 @@ | ||
import { MemoryService } from '@/ai/services/memory.service'; | ||
import { SimpleConversationChainService } from '@/ai/services/simple-conversation-chain.service'; | ||
import { VectorDbService } from '@/ai/services/vector-db.service'; | ||
import { ChatDocument } from '@/common/types/chat'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { RetrievalQAChain, loadQAStuffChain } from 'langchain/chains'; | ||
import { BaseChatModel } from 'langchain/chat_models'; | ||
import { PromptTemplate } from 'langchain/prompts'; | ||
|
||
type DocumentConversationChainProps = { | ||
memoryService: MemoryService; | ||
vectorDbService: VectorDbService; | ||
simpleConversationChainService: SimpleConversationChainService; | ||
llmModel: BaseChatModel; | ||
documents: ChatDocument[]; | ||
roomId: string; | ||
summary?: string; | ||
}; | ||
@Injectable() | ||
export class DocumentConversationChain { | ||
defaultChatPrompt: string; | ||
|
||
constructor(private readonly args: DocumentConversationChainProps) { | ||
this.defaultChatPrompt = `Use the following context to answer the question at the end. | ||
{context} | ||
Question: {question} | ||
If you don't know the answer return only: Answer not found. | ||
But if you have an answer, provide the most detailed response you can.`; | ||
} | ||
|
||
async call({ input }: { input: string }) { | ||
const prompt = PromptTemplate.fromTemplate(this.defaultChatPrompt); | ||
|
||
for (const document of this.args.documents) { | ||
const vectorStore = | ||
await this.args.vectorDbService.getVectorDbClientForExistingCollection( | ||
this.args.roomId, | ||
document.meta.filename | ||
); | ||
|
||
const chain = new RetrievalQAChain({ | ||
combineDocumentsChain: loadQAStuffChain(this.args.llmModel, { | ||
prompt, | ||
}), | ||
retriever: vectorStore.asRetriever(), | ||
returnSourceDocuments: true, | ||
inputKey: 'input', | ||
}); | ||
|
||
const res = await chain.call({ | ||
input, | ||
}); | ||
|
||
if (!res.text.includes('Answer not found.')) { | ||
await this.args.memoryService.createMemoryWithDocumentInput( | ||
this.args.roomId, | ||
input, | ||
res.text, | ||
this.args.summary | ||
); | ||
return { output: res.text, source: res.sourceDocuments }; | ||
} | ||
} | ||
const simpleConversationChain = | ||
await this.args.simpleConversationChainService.getChain( | ||
this.args.roomId, | ||
this.args.llmModel, | ||
this.args.summary | ||
); | ||
return simpleConversationChain.call({ input }); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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