File size: 1,590 Bytes
90aeb4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20645e
90aeb4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import NLTKTextSplitter
from langchain.vectorstores import Chroma
from langchain.docstore.document import Document
from langchain.prompts import PromptTemplate
from langchain.indexes.vectorstore import VectorstoreIndexCreator
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
import nltk
import os

nltk.download("punkt")

with open("guide1.txt") as f:
    hitchhikersguide = f.read()

text_splitter = NLTKTextSplitter()
texts = text_splitter.split_text(hitchhikersguide)


def make_inference(query, openai_key=""):
    os.environ["OPENAI_API_KEY"] = openai_key
    embeddings = OpenAIEmbeddings()
    docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
    chain = load_qa_chain(OpenAI(temperature=0), chain_type="refine")
    docs = docsearch.get_relevant_documents(query)
    return chain({"input_documents": docs, "question": query}, return_only_outputs=True)

if __name__ == "__main__":
    # make a gradio interface
    import gradio as gr

    gr.Interface(
        make_inference,
        [
            gr.inputs.Textbox(lines=2, label="Query"),
            gr.inputs.Textbox(lines=2, label="OpenAI Key"),
        ],
        gr.outputs.Textbox(label="Response"),
        title="🗣️TalkToMyDoc📄",
        description="🗣️TalkToMyDoc📄 is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.",
    ).launch()