File size: 1,667 Bytes
531df03
 
 
 
 
 
 
4b5c989
 
531df03
 
 
 
 
 
4b5c989
374915b
4b5c989
374915b
4b5c989
 
531df03
4b5c989
b2a9886
4b5c989
 
 
b2a9886
531df03
 
4b5c989
 
531df03
 
 
 
 
 
 
 
 
 
 
 
 
2cf4056
d171bdb
4b5c989
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
43
44
45
46
47
48
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
import os

setup_complete = False

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

text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
texts = text_splitter.split_text(hitchhikersguide)

def get_api_key(input_1, input_2):
    if len(input_1) >= len(input_2):
        os.environ['OPENAI_API_KEY'] = input_1
    else:
        os.environ['OPENAI_API_KEY'] = input_2
    return True

def setup_chain():
    global embeddings, docsearch, chain, setup_complete
    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="stuff")
    setup_complete = True

def make_inference(query):
    if not setup_complete:
        setup_chain()
    docs = docsearch.get_relevant_documents(query)
    return(chain.run(input_documents=docs, question=query))

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

    gr.Interface(
        make_inference,
        [
            gr.inputs.Textbox(lines=2, label="Query"),
        ],
        gr.outputs.Textbox(label="Response"),
        title="Query Hitchhiker's Guide",
        description="What would Douglas Adams say if he saw you query The Hitchhiker's Guide to the Galaxy with AI? Try it for yourself...",
    ).launch(auth=get_api_key)