JLW commited on
Commit
9fb96db
·
1 Parent(s): f4e1e49

Change to conversational agent

Browse files
Files changed (2) hide show
  1. app.py +32 -13
  2. videos/tempfile.mp4 +2 -2
app.py CHANGED
@@ -1,39 +1,58 @@
1
  import io
2
  import os
 
3
  import datetime
4
- import openai
5
  import gradio as gr
6
  import requests
 
7
 
8
- from langchain.agents import load_tools, initialize_agent, get_all_tool_names
 
9
  from langchain.llms import OpenAI
10
 
11
  news_api_key = os.environ["NEWS_API_KEY"]
12
  tmdb_bearer_token = os.environ["TMDB_BEARER_TOKEN"]
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def set_openai_api_key(api_key, agent):
16
- if api_key:
17
- tool_names = get_all_tool_names()
18
 
 
 
 
19
  os.environ["OPENAI_API_KEY"] = api_key
20
- llm = OpenAI(model_name="text-davinci-003", temperature=0)
21
  os.environ["OPENAI_API_KEY"] = ""
22
-
23
- tools = load_tools(tool_names, llm=llm, news_api_key=news_api_key, tmdb_bearer_token=tmdb_bearer_token)
24
- agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
25
- return agent
26
 
27
 
28
- def chat(inp, history, agent):
 
 
 
29
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
30
  print("inp: " + inp)
31
  history = history or []
32
- output = agent.run(inp)
 
 
 
 
 
33
  history.append((inp, output))
34
-
35
  html_video, temp_file = do_html_video_speak(output)
36
-
37
  return history, history, html_video, temp_file
38
 
39
 
 
1
  import io
2
  import os
3
+ from typing import Optional, Tuple
4
  import datetime
 
5
  import gradio as gr
6
  import requests
7
+ from langchain import ConversationChain
8
 
9
+ from langchain.agents import load_tools, initialize_agent
10
+ from langchain.chains.conversation.memory import ConversationBufferMemory
11
  from langchain.llms import OpenAI
12
 
13
  news_api_key = os.environ["NEWS_API_KEY"]
14
  tmdb_bearer_token = os.environ["TMDB_BEARER_TOKEN"]
15
 
16
 
17
+ def load_chain():
18
+ """Logic for loading the chain you want to use should go here."""
19
+ llm = OpenAI(temperature=0)
20
+ tool_names = ['serpapi', 'pal-math', 'pal-colored-objects', 'news-api', 'tmdb-api', 'open-meteo-api']
21
+
22
+ memory = ConversationBufferMemory(memory_key="chat_history")
23
+
24
+ tools = load_tools(tool_names, llm=llm, news_api_key=news_api_key, tmdb_bearer_token=tmdb_bearer_token)
25
+ chain = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True, memory=memory)
26
+ return chain
27
+
28
+
29
  def set_openai_api_key(api_key, agent):
30
+ """Set the api key and return chain.
 
31
 
32
+ If no api_key, then None is returned.
33
+ """
34
+ if api_key:
35
  os.environ["OPENAI_API_KEY"] = api_key
36
+ chain = load_chain()
37
  os.environ["OPENAI_API_KEY"] = ""
38
+ return chain
 
 
 
39
 
40
 
41
+ def chat(
42
+ inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
43
+ ):
44
+ """Execute the chat functionality."""
45
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
46
  print("inp: " + inp)
47
  history = history or []
48
+ # If chain is None, that is because no API key was provided.
49
+ if chain is None:
50
+ history.append((inp, "Please paste your OpenAI key to use"))
51
+ return history, history, None, None
52
+ # Run chain and append input.
53
+ output = chain.run(input=inp)
54
  history.append((inp, output))
 
55
  html_video, temp_file = do_html_video_speak(output)
 
56
  return history, history, html_video, temp_file
57
 
58
 
videos/tempfile.mp4 CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:a7092e00a5aecba25726afcc0f2cd1fcc10d02803debc2e38e5166ffdf8a306b
3
- size 175544
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:00c48e6bd31e9fac3d2ffc83a9cdc322a45d9b1d37b5beba8bca79b401f48e4d
3
+ size 154382