Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
8 |
-
import
|
9 |
-
from langchain import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
gr.Textbox(label="Question:"),
|
31 |
gr.Textbox(label="OpenAI API KEY:", placeholder="sk-...", type="password")
|
32 |
],
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|