lekkalar commited on
Commit
9434df8
1 Parent(s): e6d13b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -1,37 +1,46 @@
1
  import gradio as gr
2
  import os
3
- import openai
4
- import json
5
- import re
6
 
7
- import gradio as gr
8
- import langchain
9
- from langchain import OpenAI,PromptTemplate,LLMChain
10
-
11
- #need to plugin the part to prompt the user to input the API Key
12
- import os
13
- #os.environ["OPENAI_API_KEY"] = "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"
14
-
15
- llm = OpenAI(model_name="text-davinci-003",openai_api_key=openai_api_key)
16
-
17
- template = 'question:{question}'
18
- prompt = PromptTemplate(template=template, input_variables=["question"])
19
-
20
- chain = LLMChain(llm=llm,prompt=prompt)
21
-
22
-
23
- def answer(question,openai_api_key):
24
- os.environ["OPENAI_API_KEY"] = openai_api_key
25
- out=chain.run(question)
26
- return out
27
-
28
- demo = gr.Interface(fn=answer,
29
- inputs=[
30
- gr.Textbox(label="Question:"),
31
  gr.Textbox(label="OpenAI API KEY:", placeholder="sk-...", type="password")
32
  ],
33
- outputs='text',
34
- title="LangChain Demo",
35
- examples=[['What is the greatest threat to the birds of paradise?']])
36
- demo.launch()
 
 
 
 
 
 
 
37
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
 
 
 
3
 
4
+ from langchain.agents import load_tools
5
+ from langchain.agents import initialize_agent
6
+ from langchain.agents import AgentType
7
+ from langchain.llms import OpenAI
8
+
9
+ def get_response(input, google_cse_id, google_api_key, openai_api_key):
10
+ os.environ["GOOGLE_CSE_ID"] = google_cse_id
11
+ os.environ["GOOGLE_API_KEY"] = google_api_key
12
+ os.environ["OPENAI_API_KEY"] = openai_api_key
13
+
14
+ llm = OpenAI(temperature=0)
15
+ tools = load_tools(["google-search", "llm-math"], llm=llm)
16
+ agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)
17
+
18
+ response = agent({"input": input})
19
+ return response["output"], response["intermediate_steps"]
20
+
21
+ iface = gr.Interface(
22
+ fn=get_response,
23
+ inputs=[
24
+ gr.Textbox(label="Goal definition:"),
25
+ gr.Textbox(label="Google CSE - ID:", type="text"),
26
+ gr.Textbox(label="Google API KEY:", type="password"),
 
27
  gr.Textbox(label="OpenAI API KEY:", placeholder="sk-...", type="password")
28
  ],
29
+ outputs=[
30
+ gr.Textbox(label="Goal output:"),
31
+ gr.Json(label="Intermediate Steps")
32
+ ],
33
+ title="GPT Agents Demo",
34
+ description="Demo application of gpt-based agents including two tools (google-search and llm-math). The result and intermediate steps are included."
35
+ )
36
+
37
+ # error capturing in integration as a component
38
+
39
+ error_message = ""
40
 
41
+ try:
42
+ iface.queue(concurrency_count=20)
43
+ iface.launch()
44
+ except Exception as e:
45
+ error_message = "An error occurred: " + str(e)
46
+ iface.outputs[1].value = error_message