Public
Edited
Aug 29, 2023
Insert cell
Insert cell
Insert cell
langchain = import("https://esm.sh/langchain/llms/openai");
Insert cell
OpenAI = langchain.OpenAI
Insert cell
viewof openaiApiKey = Inputs.text({label: "OpenAI API Key", placeholder: "Enter your OpenAI API Key"})
Insert cell
memory = import ("https://cdn.jsdelivr.net/npm/langchain/vectorstores/memory/+esm")
Insert cell
Memory = memory.MemoryVectorStore
Insert cell
openAIEmbeddings = import("https://esm.sh/langchain/embeddings/openai")
Insert cell
OpenAIEmbeddings = openAIEmbeddings.OpenAIEmbeddings
Insert cell
stateOfTheUnionSpeech = (await fetch("https://raw.githubusercontent.com/hwchase17/chroma-langchain/master/state_of_the_union.txt")).text()
Insert cell
Insert cell
embeddings = new OpenAIEmbeddings({openAIApiKey: openaiApiKey})
Insert cell
chains = import("https://esm.sh/langchain/chains")
Insert cell
RetrievalQAChain = chains.RetrievalQAChain
Insert cell
loadQAStuffChain = chains.loadQAStuffChain
Insert cell
splitter = import("https://esm.sh/langchain/text_splitter")
Insert cell
TokenTextSplitter = splitter.TokenTextSplitter
Insert cell
split = new TokenTextSplitter({
encodingName: "gpt2",
chunkSize: 200,
chunkOverlap: 0,
});
Insert cell
stateOfTheUnionSplit = await split.createDocuments(
[stateOfTheUnionSpeech],
{
chunkHeader: `DOCUMENT NAME: Joe Biden's State of the Union Speech\n\n---\n\n`,
appendChunkOverlapHeader: true,
}
);
Insert cell
vectorStore = new Memory(embeddings, {
collectionName: "testing"
});
Insert cell
ids = await vectorStore.addDocuments(stateOfTheUnionSplit);
Insert cell
resultOne = await vectorStore.similaritySearch("Ukrainian people", 3);
Insert cell
resultOne[0].pageContent
Insert cell
model = new OpenAI({ temperature: 0.7, openAIApiKey: openaiApiKey});
Insert cell
chain = new RetrievalQAChain({
combineDocumentsChain: loadQAStuffChain(model),
retriever: vectorStore.asRetriever(),
returnSourceDocuments: true,
});
Insert cell
res = await chain.call({
query: "Does Joe Biden like Putin?",
});
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more