-
Notifications
You must be signed in to change notification settings - Fork 128
/
webquery.py
36 lines (32 loc) · 1.54 KB
/
webquery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os, trafilatura
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
from langchain.docstore.document import Document
class WebQuery:
def __init__(self, openai_api_key = None) -> None:
self.embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
os.environ["OPENAI_API_KEY"] = openai_api_key
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
self.llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
self.chain = None
self.db = None
def ask(self, question: str) -> str:
if self.chain is None:
response = "Please, add a document."
else:
docs = self.db.get_relevant_documents(question)
response = self.chain.run(input_documents=docs, question=question)
return response
def ingest(self, url: str) -> str:
result = trafilatura.extract(trafilatura.fetch_url(url))
documents = [Document(page_content=result, metadata={"source": url})]
splitted_documents = self.text_splitter.split_documents(documents)
self.db = Chroma.from_documents(splitted_documents, self.embeddings).as_retriever()
self.chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
return "Success"
def forget(self) -> None:
self.db = None
self.chain = None