|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | + |
| 4 | + |
| 5 | +# Question Answering Pipeline with LangChain and Epsilla |
| 6 | +# Step1. Install the required packages |
| 7 | +""" |
| 8 | +pip install langchain |
| 9 | +pip install openai |
| 10 | +pip install tiktoken |
| 11 | +pip install pyepsilla |
| 12 | +pip install -U langchain-community |
| 13 | +pip install -U langchain-openai |
| 14 | +""" |
| 15 | + |
| 16 | + |
| 17 | +# Step2. Configure the OpenAI API Key |
| 18 | +import os |
| 19 | + |
| 20 | +os.environ["OPENAI_API_KEY"] = "*****" |
| 21 | + |
| 22 | + |
| 23 | +# Step3. Load the documents |
| 24 | +from langchain.document_loaders import WebBaseLoader |
| 25 | +from langchain.text_splitter import CharacterTextSplitter |
| 26 | +from langchain_openai import OpenAIEmbeddings |
| 27 | + |
| 28 | +loader = WebBaseLoader( |
| 29 | + "https://raw.githubusercontent.com/hwchase17/chat-your-data/master/state_of_the_union.txt" |
| 30 | +) |
| 31 | +documents = loader.load() |
| 32 | +documents = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0).split_documents( |
| 33 | + documents |
| 34 | +) |
| 35 | +embeddings = OpenAIEmbeddings() |
| 36 | + |
| 37 | + |
| 38 | +# Step4. Load the vector store |
| 39 | +from langchain.vectorstores import Epsilla |
| 40 | +from pyepsilla import vectordb |
| 41 | + |
| 42 | +client = vectordb.Client(protocol="https", host="demo.epsilla.com", port="443") |
| 43 | + |
| 44 | +status_code, response = client.load_db("MyDB", "/data/MyDB") |
| 45 | +print(status_code, response) |
| 46 | + |
| 47 | +vector_store = Epsilla.from_documents( |
| 48 | + documents, |
| 49 | + embeddings, |
| 50 | + client, |
| 51 | + db_path="/data/MyDB", |
| 52 | + db_name="MyDB", |
| 53 | + collection_name="MyCollection", |
| 54 | +) |
| 55 | + |
| 56 | +# Step4. Create the QA for Retrieval |
| 57 | +from langchain.chains import RetrievalQA |
| 58 | +from langchain_openai import OpenAI |
| 59 | + |
| 60 | +qa = RetrievalQA.from_chain_type( |
| 61 | + llm=OpenAI(), chain_type="stuff", retriever=vector_store.as_retriever() |
| 62 | +) |
| 63 | +query = "What did the president say about Ketanji Brown Jackson" |
| 64 | +resp = qa.invoke(query) |
| 65 | +print("resp:", resp) |
0 commit comments