Skip to content

Commit

Permalink
fix: removed run func wrapper, added real newlines to prompt template…
Browse files Browse the repository at this point in the history
… for streaming
  • Loading branch information
bracesproul committed Oct 12, 2023
1 parent e0abf02 commit aaf6163
Showing 1 changed file with 81 additions and 77 deletions.
158 changes: 81 additions & 77 deletions examples/src/chains/conversational_qa_streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,86 +8,90 @@ import { PromptTemplate } from "langchain/prompts";
import { StringOutputParser } from "langchain/schema/output_parser";
import { RunnableSequence } from "langchain/schema/runnable";

export const run = async () => {
/* Initialize the LLM & set streaming to true */
const model = new ChatOpenAI({
streaming: true,
});
/* 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());
const retriever = vectorStore.asRetriever();
/* Initialize the LLM & set streaming to true */
const model = new ChatOpenAI({
streaming: true,
});
/* 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());
const retriever = vectorStore.asRetriever();

const serializeDocs = (docs: Array<Document>) =>
docs.map((doc) => doc.pageContent).join("\n\n");
const serializeDocs = (docs: Array<Document>) =>
docs.map((doc) => doc.pageContent).join("\n\n");

/**
* Create a prompt template for generating an answer based on context and
* a question.
*
* Chat history will be an empty string if it's the first question.
*
* inputVariables: ["chatHistory", "context", "question"]
*/
const questionPrompt = PromptTemplate.fromTemplate(
`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.\n\nCONTEXT: {context}\n\nCHAT HISTORY: {chatHistory}\n\nQUESTION: {question}\n\nHelpful Answer:`
);
/**
* Create a prompt template for generating an answer based on context and
* a question.
*
* Chat history will be an empty string if it's the first question.
*
* inputVariables: ["chatHistory", "context", "question"]
*/
const questionPrompt = PromptTemplate.fromTemplate(
`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: {context}
----------
CHAT HISTORY: {chatHistory}
----------
QUESTION: {question}
----------
Helpful Answer:`
);

const chain = RunnableSequence.from([
{
question: (input: { question: string; chatHistory?: string }) =>
input.question,
chatHistory: (input: { question: string; chatHistory?: string }) =>
input.chatHistory ?? "",
context: async (input: { question: string; chatHistory?: string }) => {
const relevantDocs = await retriever.getRelevantDocuments(
input.question
);
const serialized = serializeDocs(relevantDocs);
return serialized;
},
const chain = RunnableSequence.from([
{
question: (input: { question: string; chatHistory?: string }) =>
input.question,
chatHistory: (input: { question: string; chatHistory?: string }) =>
input.chatHistory ?? "",
context: async (input: { question: string; chatHistory?: string }) => {
const relevantDocs = await retriever.getRelevantDocuments(input.question);
const serialized = serializeDocs(relevantDocs);
return serialized;
},
questionPrompt,
model,
new StringOutputParser(),
]);
},
questionPrompt,
model,
new StringOutputParser(),
]);

const stream = await chain.stream({
question: "What did the president say about Justice Breyer?",
});
const stream = await chain.stream({
question: "What did the president say about Justice Breyer?",
});

let streamedResult = "";
for await (const chunk of stream) {
streamedResult += chunk;
console.log(streamedResult);
}
/**
* The
* The president
* The president honored
* The president honored Justice
* The president honored Justice Stephen
* The president honored Justice Stephen B
* The president honored Justice Stephen Brey
* The president honored Justice Stephen Breyer
* The president honored Justice Stephen Breyer,
* The president honored Justice Stephen Breyer, a
* The president honored Justice Stephen Breyer, a retiring
* The president honored Justice Stephen Breyer, a retiring Justice
* The president honored Justice Stephen Breyer, a retiring Justice of
* The president honored Justice Stephen Breyer, a retiring Justice of the
* The president honored Justice Stephen Breyer, a retiring Justice of the United
* The president honored Justice Stephen Breyer, a retiring Justice of the United States
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court,
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court, for
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court, for his
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court, for his service
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court, for his service.
*/
};
let streamedResult = "";
for await (const chunk of stream) {
streamedResult += chunk;
console.log(streamedResult);
}
/**
* The
* The president
* The president honored
* The president honored Justice
* The president honored Justice Stephen
* The president honored Justice Stephen B
* The president honored Justice Stephen Brey
* The president honored Justice Stephen Breyer
* The president honored Justice Stephen Breyer,
* The president honored Justice Stephen Breyer, a
* The president honored Justice Stephen Breyer, a retiring
* The president honored Justice Stephen Breyer, a retiring Justice
* The president honored Justice Stephen Breyer, a retiring Justice of
* The president honored Justice Stephen Breyer, a retiring Justice of the
* The president honored Justice Stephen Breyer, a retiring Justice of the United
* The president honored Justice Stephen Breyer, a retiring Justice of the United States
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court,
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court, for
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court, for his
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court, for his service
* The president honored Justice Stephen Breyer, a retiring Justice of the United States Supreme Court, for his service.
*/

0 comments on commit aaf6163

Please sign in to comment.