ofermend commited on
Commit
91ec79e
1 Parent(s): 72e1546

refactor streamlit app

Browse files
Files changed (1) hide show
  1. app.py +30 -30
app.py CHANGED
@@ -77,38 +77,41 @@ def create_tools(cfg):
77
  [query_financial_reports]
78
  )
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  def launch_bot(agent_type: AgentType):
81
  def reset():
82
  cfg = st.session_state.cfg
83
  st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "🦖"}]
84
  st.session_state.thinking_message = "Agent at work..."
 
85
 
86
- # Create the agent
87
- print("Creating agent...")
88
-
89
- def update_func(status_type: AgentStatusType, msg: str):
90
- output = f"{status_type.value} - {msg}"
91
- st.session_state.thinking_placeholder.text(output)
92
-
93
- financial_bot_instructions = """
94
- - You are a helpful financial assistant in conversation with a user.
95
- - Use your financial expertise when crafting a query to the tool, to ensure you get the most accurate responses.
96
- - A user may refer to a company's ticker instead of its full name - consider those the same when a user is asking about a company.
97
- - When using a query tool for a metric, make sure to provide the correct ticker and year and concise definition of the metric to avoid ambiguity.
98
- - Use tools when available instead of depending on your own knowledge, and consider the field descriptions carefully.
99
- - If you calculate a metric, make sure you have all the necessary information to complete the calculation. Don't guess.
100
- - Report financial data in a consistent manner. For example if you report revenue in thousands, always report revenue in thousands.
101
- - Be very careful not to report results you are not confident about.
102
- - Report results in the most relevant multiple. For example, reveuues in millions, not thousands.
103
- """
104
- st.session_state.agent = Agent(
105
- agent_type = agent_type,
106
- tools = create_tools(cfg),
107
- topic = "10-K annual financial reports",
108
- custom_instructions = financial_bot_instructions,
109
- update_func = update_func
110
- )
111
-
112
  if 'cfg' not in st.session_state:
113
  cfg = OmegaConf.create({
114
  'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
@@ -117,9 +120,7 @@ def launch_bot(agent_type: AgentType):
117
  })
118
  st.session_state.cfg = cfg
119
  reset()
120
-
121
  cfg = st.session_state.cfg
122
- st.set_page_config(page_title="Financial Assistant", layout="wide")
123
 
124
  # left side content
125
  with st.sidebar:
@@ -175,6 +176,5 @@ def launch_bot(agent_type: AgentType):
175
  sys.stdout.flush()
176
 
177
  if __name__ == "__main__":
178
- print("Starting up...")
179
- launch_bot(agent_type = AgentType.REACT)
180
 
 
77
  [query_financial_reports]
78
  )
79
 
80
+ @st.cache_resource
81
+ def initialize_agent(agent_type: AgentType, _cfg):
82
+ financial_bot_instructions = """
83
+ - You are a helpful financial assistant in conversation with a user.
84
+ - Use your financial expertise when crafting a query to the tool, to ensure you get the most accurate responses.
85
+ - A user may refer to a company's ticker instead of its full name - consider those the same when a user is asking about a company.
86
+ - When using a query tool for a metric, make sure to provide the correct ticker and year and concise definition of the metric to avoid ambiguity.
87
+ - Use tools when available instead of depending on your own knowledge, and consider the field descriptions carefully.
88
+ - If you calculate a metric, make sure you have all the necessary information to complete the calculation. Don't guess.
89
+ - Report financial data in a consistent manner. For example if you report revenue in thousands, always report revenue in thousands.
90
+ - Be very careful not to report results you are not confident about.
91
+ - Report results in the most relevant multiple. For example, revenues in millions, not thousands.
92
+ """
93
+
94
+ def update_func(status_type: AgentStatusType, msg: str):
95
+ output = f"{status_type.value} - {msg}"
96
+ st.session_state.thinking_placeholder.text(output)
97
+
98
+ agent = Agent(
99
+ agent_type=agent_type,
100
+ tools=create_tools(_cfg),
101
+ topic="10-K annual financial reports",
102
+ custom_instructions=financial_bot_instructions,
103
+ update_func=update_func
104
+ )
105
+ return agent
106
+
107
  def launch_bot(agent_type: AgentType):
108
  def reset():
109
  cfg = st.session_state.cfg
110
  st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "🦖"}]
111
  st.session_state.thinking_message = "Agent at work..."
112
+ st.session_state.agent = initialize_agent(agent_type, cfg)
113
 
114
+ st.set_page_config(page_title="Financial Assistant", layout="wide")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  if 'cfg' not in st.session_state:
116
  cfg = OmegaConf.create({
117
  'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
 
120
  })
121
  st.session_state.cfg = cfg
122
  reset()
 
123
  cfg = st.session_state.cfg
 
124
 
125
  # left side content
126
  with st.sidebar:
 
176
  sys.stdout.flush()
177
 
178
  if __name__ == "__main__":
179
+ launch_bot(agent_type=AgentType.OPENAI)
 
180