bstraehle commited on
Commit
380dcc1
·
1 Parent(s): dfa45b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -11
app.py CHANGED
@@ -1,6 +1,13 @@
1
  import gradio as gr
2
 
3
- from openai import OpenAI
 
 
 
 
 
 
 
4
 
5
  config = {
6
  "max_tokens": 1000,
@@ -8,30 +15,45 @@ config = {
8
  "temperature": 0,
9
  }
10
 
 
 
 
 
 
 
 
 
 
 
11
  def invoke(openai_api_key, prompt):
12
  if (openai_api_key == ""):
13
  raise gr.Error("OpenAI API Key is required.")
14
  if (prompt == ""):
15
  raise gr.Error("Prompt is required.")
16
 
17
- content = ""
18
 
19
  try:
20
- client = OpenAI(api_key = openai_api_key)
21
-
22
- completion = client.chat.completions.create(
23
- max_tokens = config["max_tokens"],
24
- messages = [{"role": "user", "content": prompt}],
25
- model = config["model"],
26
- temperature = config["temperature"],)
 
 
 
 
 
27
 
28
- content = completion.choices[0].message.content
29
  except Exception as e:
30
  err_msg = e
31
 
32
  raise gr.Error(e)
33
 
34
- return content
35
 
36
  description = """<a href='https://www.gradio.app/'>Gradio</a> UI using the <a href='https://openai.com/'>OpenAI</a> API
37
  with <a href='https://openai.com/research/gpt-4'>gpt-4</a> model."""
 
1
  import gradio as gr
2
 
3
+ from datetime import date
4
+ from langchain.agents import tool
5
+ from langchain.agents.agent_toolkits import create_python_agent
6
+ from langchain.agents import load_tools, initialize_agent
7
+ from langchain.agents import AgentType
8
+ from langchain.tools.python.tool import PythonREPLTool
9
+ from langchain.python import PythonREPL
10
+ from langchain.chat_models import ChatOpenAI
11
 
12
  config = {
13
  "max_tokens": 1000,
 
15
  "temperature": 0,
16
  }
17
 
18
+ @tool
19
+ def time(text: str) -> str:
20
+ """Returns todays date, use this for any \
21
+ questions related to knowing todays date. \
22
+ The input should always be an empty string, \
23
+ and this function will always return todays \
24
+ date - any date mathmatics should occur \
25
+ outside this function."""
26
+ return str(date.today())
27
+
28
  def invoke(openai_api_key, prompt):
29
  if (openai_api_key == ""):
30
  raise gr.Error("OpenAI API Key is required.")
31
  if (prompt == ""):
32
  raise gr.Error("Prompt is required.")
33
 
34
+ result = ""
35
 
36
  try:
37
+ llm = ChatOpenAI(temperature=0, model="gpt-4")
38
+
39
+ tools = load_tools(["llm-math","wikipedia"], llm=llm)
40
+
41
+ agent= initialize_agent(
42
+ tools + [time],
43
+ llm,
44
+ agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
45
+ handle_parsing_errors=True,
46
+ verbose = True)
47
+
48
+ result = agent(prompt)
49
 
50
+ #content = completion.choices[0].message.content
51
  except Exception as e:
52
  err_msg = e
53
 
54
  raise gr.Error(e)
55
 
56
+ return result
57
 
58
  description = """<a href='https://www.gradio.app/'>Gradio</a> UI using the <a href='https://openai.com/'>OpenAI</a> API
59
  with <a href='https://openai.com/research/gpt-4'>gpt-4</a> model."""