import { OpenAI } from "langchain/llms/openai"; import { ConversationalRetrievalQAChain…
- User
import { OpenAI } from "langchain/llms/openai"; import { ConversationalRetrievalQAChain } from "langchain/chains"; import { Chroma } from "langchain/vectorstores/chroma"; import { OpenAIEmbeddings } from "langchain/embeddings/openai"; import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; import * as fs from "fs";
to run this first run a chroma server with
chroma run --path /path/to/dataexport 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("python_notes", "utf8"); / Split the text into chunks / const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 }); const docs = await textSplitter.createDocuments([text]); / Create the vectorstore / const vectorStore = await Chroma.fromDocuments(docs, new OpenAIEmbeddings(), { collectionName: "python_notes", }); / Create the chain / const chain = ConversationalRetrievalQAChain.fromLLM( model, vectorStore.asRetriever() ); / Ask it a question */ const question = "Differences between scripting language and programming language?"; const res = await chain.call({ question, chat_history: [] }); console.log(res); };
- Assistant
run();
- User
Convert this code into python
- Assistant
I have converted the code into Python for you:
pythonfrom langchain.llms.openai import OpenAI from langchain.chains import ConversationalRetrievalQAChain from langchain.vectorstores.chroma import Chroma from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter import os def run(): # Initialize the LLM to use to answer the question model = OpenAI() # Load in the file we want to do question answering over with open("python_notes", "r") as file: text = file.read() # Split the text into chunks textSplitter = RecursiveCharacterTextSplitter(chunkSize=1000) docs = textSplitter.createDocuments([text]) # Create the vectorstore vectorStore = Chroma.fromDocuments(docs, OpenAIEmbeddings(), collectionName="python_notes") # Create the chain chain = ConversationalRetrievalQAChain.fromLLM(model, vectorStore.asRetriever()) # Ask it a question question = "Differences between scripting language and programming language?" res = chain.call({"question": question, "chat_history": []}) print(res) if __name__ == "__main__": run() - User
- Assistant
- User
NameError Traceback (most recent call last) Cell In[3], line 4 2 import chromadb 3 from chromadb.config import Settings ----> 4 DIR = os.path.dirname(os.path.abspath(file)) 5 DB_PATH = os.path.join(DIR, 'data') 6 chroma_client = chromadb.PersistentClient(path=DB_PATH, settings=Settings(allow_reset=True, anonymized_telemetry=False))
NameError: name 'file' is not defined
- Assistant
- User
- Assistant