|
| 1 | +import { AzureCosmosDBNoSQLVectorStore } from "@langchain/azure-cosmosdb"; |
| 2 | +import { ChatPromptTemplate } from "@langchain/core/prompts"; |
| 3 | +import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai"; |
| 4 | +import { createStuffDocumentsChain } from "langchain/chains/combine_documents"; |
| 5 | +import { createRetrievalChain } from "langchain/chains/retrieval"; |
| 6 | +import { TextLoader } from "langchain/document_loaders/fs/text"; |
| 7 | +import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters"; |
| 8 | + |
| 9 | +// Load documents from file |
| 10 | +const loader = new TextLoader("./state_of_the_union.txt"); |
| 11 | +const rawDocuments = await loader.load(); |
| 12 | +const splitter = new RecursiveCharacterTextSplitter({ |
| 13 | + chunkSize: 1000, |
| 14 | + chunkOverlap: 0, |
| 15 | +}); |
| 16 | +const documents = await splitter.splitDocuments(rawDocuments); |
| 17 | + |
| 18 | +// Create Azure Cosmos DB vector store |
| 19 | +const store = await AzureCosmosDBNoSQLVectorStore.fromDocuments( |
| 20 | + documents, |
| 21 | + new OpenAIEmbeddings(), |
| 22 | + { |
| 23 | + databaseName: "langchain", |
| 24 | + containerName: "documents", |
| 25 | + } |
| 26 | +); |
| 27 | + |
| 28 | +// Performs a similarity search |
| 29 | +const resultDocuments = await store.similaritySearch( |
| 30 | + "What did the president say about Ketanji Brown Jackson?" |
| 31 | +); |
| 32 | + |
| 33 | +console.log("Similarity search results:"); |
| 34 | +console.log(resultDocuments[0].pageContent); |
| 35 | +/* |
| 36 | + Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. |
| 37 | +
|
| 38 | + Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. |
| 39 | +
|
| 40 | + One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. |
| 41 | +
|
| 42 | + And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence. |
| 43 | +*/ |
| 44 | + |
| 45 | +// Use the store as part of a chain |
| 46 | +const model = new ChatOpenAI({ model: "gpt-3.5-turbo-1106" }); |
| 47 | +const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([ |
| 48 | + [ |
| 49 | + "system", |
| 50 | + "Answer the user's questions based on the below context:\n\n{context}", |
| 51 | + ], |
| 52 | + ["human", "{input}"], |
| 53 | +]); |
| 54 | + |
| 55 | +const combineDocsChain = await createStuffDocumentsChain({ |
| 56 | + llm: model, |
| 57 | + prompt: questionAnsweringPrompt, |
| 58 | +}); |
| 59 | + |
| 60 | +const chain = await createRetrievalChain({ |
| 61 | + retriever: store.asRetriever(), |
| 62 | + combineDocsChain, |
| 63 | +}); |
| 64 | + |
| 65 | +const res = await chain.invoke({ |
| 66 | + input: "What is the president's top priority regarding prices?", |
| 67 | +}); |
| 68 | + |
| 69 | +console.log("Chain response:"); |
| 70 | +console.log(res.answer); |
| 71 | +/* |
| 72 | + The president's top priority is getting prices under control. |
| 73 | +*/ |
| 74 | + |
| 75 | +// Clean up |
| 76 | +await store.delete(); |
0 commit comments