-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for built-in memory to ConversationalQARetrievalChain (#…
…1463) * Adds support for built-in memory to ConversationalQARetrievalChain * Fix typo in docs * More docs updates * Fix typo in docs
- Loading branch information
1 parent
8e7fcc8
commit b780ab4
Showing
6 changed files
with
279 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
import { ConversationalRetrievalQAChain } from "langchain/chains"; | ||
import { HNSWLib } from "langchain/vectorstores/hnswlib"; | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; | ||
import { BufferMemory } from "langchain/memory"; | ||
import * as fs from "fs"; | ||
|
||
export const run = async () => { | ||
const text = fs.readFileSync("state_of_the_union.txt", "utf8"); | ||
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 }); | ||
const docs = await textSplitter.createDocuments([text]); | ||
const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings()); | ||
const fasterModel = new ChatOpenAI({ | ||
modelName: "gpt-3.5-turbo", | ||
}); | ||
const slowerModel = new ChatOpenAI({ | ||
modelName: "gpt-4", | ||
}); | ||
const chain = ConversationalRetrievalQAChain.fromLLM( | ||
slowerModel, | ||
vectorStore.asRetriever(), | ||
{ | ||
memory: new BufferMemory({ | ||
memoryKey: "chat_history", | ||
returnMessages: true, | ||
}), | ||
questionGeneratorChainOptions: { | ||
llm: fasterModel, | ||
}, | ||
} | ||
); | ||
/* Ask it a question */ | ||
const question = "What did the president say about Justice Breyer?"; | ||
const res = await chain.call({ question }); | ||
console.log(res); | ||
|
||
const followUpRes = await chain.call({ question: "Was that nice?" }); | ||
console.log(followUpRes); | ||
}; |
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 @@ | ||
import { OpenAI } from "langchain/llms/openai"; | ||
import { ConversationalRetrievalQAChain } from "langchain/chains"; | ||
import { HNSWLib } from "langchain/vectorstores/hnswlib"; | ||
import { OpenAIEmbeddings } from "langchain/embeddings/openai"; | ||
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; | ||
import * as fs from "fs"; | ||
|
||
export const run = async () => { | ||
/* Initialize the LLM to use to answer the question */ | ||
const model = new OpenAI({}); | ||
/* Load in the file we want to do question answering over */ | ||
const text = fs.readFileSync("state_of_the_union.txt", "utf8"); | ||
/* Split the text into chunks */ | ||
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 }); | ||
const docs = await textSplitter.createDocuments([text]); | ||
/* Create the vectorstore */ | ||
const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings()); | ||
/* Create the chain */ | ||
const chain = ConversationalRetrievalQAChain.fromLLM( | ||
model, | ||
vectorStore.asRetriever() | ||
); | ||
/* Ask it a question */ | ||
const question = "What did the president say about Justice Breyer?"; | ||
const res = await chain.call({ question, chat_history: [] }); | ||
console.log(res); | ||
/* Ask it a follow up question */ | ||
const chatHistory = question + res.text; | ||
const followUpRes = await chain.call({ | ||
question: "Was that nice?", | ||
chat_history: chatHistory, | ||
}); | ||
console.log(followUpRes); | ||
}; |
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
b780ab4
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:
langchainjs-docs – ./
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-langchain.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
js.langchain.com