-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
224 additions
and
60 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
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,49 @@ | ||
import { RetrievalQAChain } from 'langchain/chains' | ||
import { PromptTemplate } from 'langchain/prompts' | ||
import type { IVectorStorage } from '../storage' | ||
import type { PromptsLanguage, QARes } from '../types' | ||
import { ANSWER_IN_LANGUAGE } from '../const' | ||
import { getPromptsByTemplate } from '../utils' | ||
|
||
const prompt_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. | ||
{context} | ||
Question: {question} | ||
Answer in {language}:` | ||
|
||
export class QAChain { | ||
constructor(fields: { | ||
vectorStorage: IVectorStorage | ||
}) { | ||
this.vectorStorage = fields.vectorStorage | ||
} | ||
|
||
private vectorStorage | ||
|
||
async call(question: string, language: PromptsLanguage = 'zh-CN') { | ||
const retriever = await this.vectorStorage.getRetriever() | ||
const config = this.vectorStorage.getConfig() | ||
|
||
const kv = { | ||
language: ANSWER_IN_LANGUAGE[language], | ||
} | ||
const template = getPromptsByTemplate(prompt_template, kv) | ||
|
||
const chain = RetrievalQAChain.fromLLM( | ||
config.embeddingsInfo.llmModel, | ||
retriever, | ||
{ | ||
returnSourceDocuments: true, | ||
prompt: new PromptTemplate({ | ||
template, | ||
inputVariables: ['context', 'question'], | ||
}), | ||
}, | ||
) | ||
|
||
const res = await chain.call({ query: question }) | ||
|
||
return res as QARes | ||
} | ||
} |
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Oops, something went wrong.
78d0f6f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
star-nexus – ./
star-nexus.vercel.app
star-nexus-git-main-larchliu.vercel.app
star-nexus-larchliu.vercel.app