lukestanley commited on
Commit
626b1eb
1 Parent(s): 96045c8

State fix and linting

Browse files
Files changed (1) hide show
  1. app.py +10 -51
app.py CHANGED
@@ -2,17 +2,14 @@ from typing import List, Tuple, Dict, Generator
2
  from langchain.llms import OpenAI
3
  import gradio as gr
4
 
5
-
6
  model_name = "gpt-3.5-turbo"
7
- LLM = OpenAI(model_name=model_name, temperature=0.9)
8
-
9
 
10
  def create_history_messages(history: List[Tuple[str, str]]) -> List[dict]:
11
  history_messages = [{"role": "user", "content": m[0]} for m in history]
12
  history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
13
  return history_messages
14
 
15
-
16
  def create_formatted_history(history_messages: List[dict]) -> List[Tuple[str, str]]:
17
  formatted_history = []
18
  user_messages = []
@@ -39,16 +36,15 @@ def create_formatted_history(history_messages: List[dict]) -> List[Tuple[str, st
39
 
40
  return formatted_history
41
 
42
-
43
- history_messages = [{"role": "system", "content": "A helpful assistant."}]
44
-
45
-
46
  def chat(
47
- message: str, history=[], client=LLM.client
48
  ) -> Generator[Tuple[List[Tuple[str, str]], List[Tuple[str, str]]], None, None]:
49
- global history_messages
50
- history_messages.append({"role": "user", "content": message})
 
 
51
 
 
52
  # We have no content for the assistant's response yet but we will update this:
53
  history_messages.append({"role": "assistant", "content": ""})
54
 
@@ -71,13 +67,9 @@ def chat(
71
  if "finish_reason" in choice and choice["finish_reason"] == "stop":
72
  break
73
  formatted_history = create_formatted_history(history_messages)
74
- yield formatted_history, formatted_history
75
-
76
 
77
- # Define the settings for the Gradio ChatBot interface
78
  chatbot = gr.Chatbot(label="Chat").style(color_map=("yellow", "purple"))
79
-
80
- # Define the main interface with input components for the API key, message, and state
81
  iface = gr.Interface(
82
  fn=chat,
83
  inputs=[
@@ -85,40 +77,7 @@ iface = gr.Interface(
85
  "state",
86
  ],
87
  outputs=[chatbot, "state"],
88
- allow_flagging="never", # Hide the "flag" button
89
  )
90
 
91
- # Launch the Gradio interface
92
- iface.queue().launch()
93
-
94
-
95
- # TODO: figure out how to chain with the Chat API. Or just wait for others to do it.,, The API is only a few days old, afterall.
96
- history_messages = [
97
- {
98
- "role": "system",
99
- "content": "A helpful assistant that performs web searches.\nA helpful assistant that performs web searches.\nAssistant can say these verbs:\n\n- `Thought: I need to search for 'search term'`\n\n- `Thought: I need to read the URL: 'URL'`\n\n- `Thought: I have the information I need.`\n\n- `Say: 'message'`\nThe date is 3rd March 2023.",
100
- },
101
- {"role": "user", "content": "Who is the President of the USA?"},
102
- {
103
- "role": "assistant",
104
- "content": "Thought: I need to search for the latest infomation.",
105
- },
106
- {"role": "assistant", "content": "search('USA president Wikipedia')"},
107
- {
108
- "role": "system",
109
- "content": "Search results for 'USA president Wikipedia'.\n\nhttps://en.wikipedia.org/wiki/President_of_the_United_States\nPresident of the United States - Wikipedia\nThe president of the United States ( POTUS) [A] is the head of state and head of government of the United States of America. The president directs the executive branch of the federal government and is the commander-in-chief of the United States Armed Forces . The power of the presidency has grown substantially [11] since the first president ...",
110
- },
111
- {
112
- "role": "assistant",
113
- "content": "load_url_text('https://en.wikipedia.org/wiki/President_of_the_United_States')",
114
- },
115
- {
116
- "role": "system",
117
- "content": "President of the United States\nPresident of the\nUnited States of America\nSeal of the President of the United States.svg\n\nPresidential seal\nFlag of the President of the United States.svg\n\nPresidential flag\n\nJoe Biden presidential portrait (cropped).jpg\n\nIncumbent\nJoe Biden\nsince January 20, 2021\n\n Executive branch of the U.S. government\n Executive Office of the President\n\nStyle\t\n\n",
118
- },
119
- {"role": "assistant", "content": "Thought: I have the information I need."},
120
- {
121
- "role": "assistant",
122
- "content": "Say: The current President of the USA is Joe Biden.",
123
- },
124
- ]
 
2
  from langchain.llms import OpenAI
3
  import gradio as gr
4
 
 
5
  model_name = "gpt-3.5-turbo"
6
+ LLM = OpenAI(model_name=model_name, temperature=0.1)
 
7
 
8
  def create_history_messages(history: List[Tuple[str, str]]) -> List[dict]:
9
  history_messages = [{"role": "user", "content": m[0]} for m in history]
10
  history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
11
  return history_messages
12
 
 
13
  def create_formatted_history(history_messages: List[dict]) -> List[Tuple[str, str]]:
14
  formatted_history = []
15
  user_messages = []
 
36
 
37
  return formatted_history
38
 
 
 
 
 
39
  def chat(
40
+ message: str, state, client=LLM.client
41
  ) -> Generator[Tuple[List[Tuple[str, str]], List[Tuple[str, str]]], None, None]:
42
+ history_messages = state
43
+ if history_messages == None:
44
+ history_messages = []
45
+ history_messages.append({"role": "system", "content": "A helpful assistant."})
46
 
47
+ history_messages.append({"role": "user", "content": message})
48
  # We have no content for the assistant's response yet but we will update this:
49
  history_messages.append({"role": "assistant", "content": ""})
50
 
 
67
  if "finish_reason" in choice and choice["finish_reason"] == "stop":
68
  break
69
  formatted_history = create_formatted_history(history_messages)
70
+ yield formatted_history, history_messages
 
71
 
 
72
  chatbot = gr.Chatbot(label="Chat").style(color_map=("yellow", "purple"))
 
 
73
  iface = gr.Interface(
74
  fn=chat,
75
  inputs=[
 
77
  "state",
78
  ],
79
  outputs=[chatbot, "state"],
80
+ allow_flagging="never",
81
  )
82
 
83
+ iface.queue().launch()