johnnygreco commited on
Commit
d16f4d9
Β·
1 Parent(s): 701ae0d

make work with langchain updates

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -1,13 +1,15 @@
1
  import os
2
- from pathlib import Path
3
 
4
  import gradio as gr
 
 
 
 
 
5
  import hpqa
6
- from langchain import VectorDBQA
7
- from langchain.llms import OpenAI
8
 
9
  os.environ["OPENAI_API_KEY"] = ""
10
- index_path = Path("data/hpqa_faiss_index_500")
11
 
12
 
13
  examples = [
@@ -20,19 +22,24 @@ examples = [
20
  ]
21
 
22
 
23
- def api(question, temperature, api_key=None):
24
  if api_key is None or len(api_key) == 0:
25
  return "You must provide an OpenAI API key to use this demo πŸ‘‡"
26
  if len(question) == 0:
27
  return ""
28
  document_store = hpqa.load_document_store(index_path, openai_api_key=api_key)
29
- chain = VectorDBQA.from_chain_type(
 
 
 
 
 
 
 
30
  llm=OpenAI(temperature=temperature, openai_api_key=api_key),
31
- chain_type="stuff",
32
- vectorstore=document_store,
33
- return_source_documents=True,
34
  )
35
- response = chain(question)
36
  return response["result"].strip()
37
 
38
 
@@ -49,8 +56,10 @@ with demo:
49
  btn = gr.Button("Submit", variant="primary")
50
  with gr.Column():
51
  answer = gr.Textbox(lines=4, label="Answer")
52
- openai_api_key = gr.Textbox(type="password", label="OpenAI API key")
53
- btn.click(api, [question, temperature, openai_api_key], answer)
 
 
54
  clear.click(lambda _: "", question, question)
55
  gr.Examples(examples, question)
56
  gr.Markdown("πŸ’» Checkout the `hpqa` source code on [GitHub](https://github.com/johnnygreco/hpqa).")
 
1
  import os
 
2
 
3
  import gradio as gr
4
+ from langchain.chains import RetrievalQA
5
+ from langchain_core.vectorstores import VectorStoreRetriever
6
+ from langchain_openai import OpenAI
7
+ from openai import OpenAI as OpenAIClient
8
+
9
  import hpqa
 
 
10
 
11
  os.environ["OPENAI_API_KEY"] = ""
12
+ index_path = hpqa.LOCAL_DATA_PATH / "hpqa_faiss_index_500"
13
 
14
 
15
  examples = [
 
22
  ]
23
 
24
 
25
+ def api(question, temperature, model, api_key=None):
26
  if api_key is None or len(api_key) == 0:
27
  return "You must provide an OpenAI API key to use this demo πŸ‘‡"
28
  if len(question) == 0:
29
  return ""
30
  document_store = hpqa.load_document_store(index_path, openai_api_key=api_key)
31
+
32
+ client = OpenAIClient(api_key=api_key)
33
+ models = [m.id for m in client.models.list().data if "gpt" in m.id if "vision" not in m.id]
34
+ if model not in models:
35
+ model_list = "\n".join(models)
36
+ return f"😬 {model} not a valid model name. Choose from:\n\n{model_list}"
37
+
38
+ chain = RetrievalQA.from_llm(
39
  llm=OpenAI(temperature=temperature, openai_api_key=api_key),
40
+ retriever=VectorStoreRetriever(vectorstore=document_store),
 
 
41
  )
42
+ response = chain.invoke(question)
43
  return response["result"].strip()
44
 
45
 
 
56
  btn = gr.Button("Submit", variant="primary")
57
  with gr.Column():
58
  answer = gr.Textbox(lines=4, label="Answer")
59
+ with gr.Row():
60
+ model = gr.Textbox(value="gpt-3.5-turbo-instruct", label="OpenAI Model")
61
+ openai_api_key = gr.Textbox(type="password", label="OpenAI API key")
62
+ btn.click(api, [question, temperature, model, openai_api_key], answer)
63
  clear.click(lambda _: "", question, question)
64
  gr.Examples(examples, question)
65
  gr.Markdown("πŸ’» Checkout the `hpqa` source code on [GitHub](https://github.com/johnnygreco/hpqa).")