shaolang commited on
Commit
c29132e
·
1 Parent(s): 506c72c

Add prompt for own openai api key

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -11,13 +11,19 @@ with open("guide1.txt") as f:
11
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
12
  texts = text_splitter.split_text(hitchhikersguide)
13
 
14
- embeddings = OpenAIEmbeddings()
 
 
15
 
16
- docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
17
 
18
- chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
 
 
 
 
 
 
19
 
20
- def make_inference(query):
21
  docs = docsearch.get_relevant_documents(query)
22
  return(chain.run(input_documents=docs, question=query))
23
 
@@ -28,9 +34,10 @@ if __name__ == "__main__":
28
  gr.Interface(
29
  make_inference,
30
  [
 
31
  gr.inputs.Textbox(lines=2, label="Query"),
32
  ],
33
  gr.outputs.Textbox(label="Response"),
34
  title="🗣️TalkToMyDoc📄",
35
  description="🗣️TalkToMyDoc📄 is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.",
36
- ).launch()
 
11
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
12
  texts = text_splitter.split_text(hitchhikersguide)
13
 
14
+ chain = None
15
+ embeddings = None
16
+ docsearch = None
17
 
 
18
 
19
+ def make_inference(openai_api_key, query):
20
+ global chain, embeddings, docsearch
21
+
22
+ if chain is None:
23
+ chain = load_qa_chain(OpenAI(temperature=0, openai_api_key=openai_api_key.strip()), chain_type="stuff")
24
+ embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
25
+ docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
26
 
 
27
  docs = docsearch.get_relevant_documents(query)
28
  return(chain.run(input_documents=docs, question=query))
29
 
 
34
  gr.Interface(
35
  make_inference,
36
  [
37
+ gr.inputs.Textbox(lines=1, label="OpenAI API Key"),
38
  gr.inputs.Textbox(lines=2, label="Query"),
39
  ],
40
  gr.outputs.Textbox(label="Response"),
41
  title="🗣️TalkToMyDoc📄",
42
  description="🗣️TalkToMyDoc📄 is a tool that allows you to ask questions about a document. In this case - Hitch Hitchhiker's Guide to the Galaxy.",
43
+ ).launch()