ofermend commited on
Commit
57077ba
β€’
1 Parent(s): 983d6cf

version bump

Browse files
Files changed (4) hide show
  1. agent.py +3 -3
  2. app.py +3 -151
  3. requirements.txt +2 -2
  4. st_app.py +164 -0
agent.py CHANGED
@@ -32,8 +32,7 @@ def create_assistant_tools(cfg):
32
  vec_factory = VectaraToolFactory(vectara_api_key=cfg.api_key,
33
  vectara_customer_id=cfg.customer_id,
34
  vectara_corpus_id=cfg.corpus_id)
35
- tools_factory = ToolsFactory()
36
-
37
  ask_hackernews = vec_factory.create_rag_tool(
38
  tool_name = "ask_hackernews",
39
  tool_description = """
@@ -43,7 +42,7 @@ def create_assistant_tools(cfg):
43
  reranker = "multilingual_reranker_v1", rerank_k = 100,
44
  n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
45
  summary_num_results = 10,
46
- vectara_summarizer = 'vectara-experimental-summary-ext-2023-12-11-med-omni',
47
  include_citations = True,
48
  )
49
 
@@ -125,6 +124,7 @@ def create_assistant_tools(cfg):
125
  all_stories = '---------\n\n'.join(texts)
126
  return summarize_text(all_stories)
127
 
 
128
  return (
129
  [ask_hackernews] +
130
  [tools_factory.create_tool(tool) for tool in
 
32
  vec_factory = VectaraToolFactory(vectara_api_key=cfg.api_key,
33
  vectara_customer_id=cfg.customer_id,
34
  vectara_corpus_id=cfg.corpus_id)
35
+ summarizer = 'vectara-experimental-summary-ext-2023-12-11-med-omni'
 
36
  ask_hackernews = vec_factory.create_rag_tool(
37
  tool_name = "ask_hackernews",
38
  tool_description = """
 
42
  reranker = "multilingual_reranker_v1", rerank_k = 100,
43
  n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
44
  summary_num_results = 10,
45
+ vectara_summarizer = summarizer,
46
  include_citations = True,
47
  )
48
 
 
124
  all_stories = '---------\n\n'.join(texts)
125
  return summarize_text(all_stories)
126
 
127
+ tools_factory = ToolsFactory()
128
  return (
129
  [ask_hackernews] +
130
  [tools_factory.create_tool(tool) for tool in
app.py CHANGED
@@ -1,165 +1,17 @@
1
- from PIL import Image
2
- import sys
3
  import uuid
4
 
5
  import nest_asyncio
6
  import asyncio
7
 
8
- import streamlit as st
9
- from streamlit_pills import pills
10
- from streamlit_feedback import streamlit_feedback
11
-
12
- from vectara_agentic.agent import AgentStatusType
13
-
14
- from utils import thumbs_feedback, escape_dollars_outside_latex, send_amplitude_data
15
-
16
- from agent import initialize_agent, get_agent_config
17
-
18
- initial_prompt = "How can I help you today?"
19
-
20
  # Setup for HTTP API Calls to Amplitude Analytics
21
  if 'device_id' not in st.session_state:
22
  st.session_state.device_id = str(uuid.uuid4())
23
 
24
- headers = {
25
- 'Content-Type': 'application/json',
26
- 'Accept': '*/*'
27
- }
28
-
29
  if "feedback_key" not in st.session_state:
30
- st.session_state.feedback_key = 0
31
-
32
- def toggle_logs():
33
- st.session_state.show_logs = not st.session_state.show_logs
34
-
35
- def show_example_questions():
36
- if len(st.session_state.example_messages) > 0 and st.session_state.first_turn:
37
- selected_example = pills("Queries to Try:", st.session_state.example_messages, index=None)
38
- if selected_example:
39
- st.session_state.ex_prompt = selected_example
40
- st.session_state.first_turn = False
41
- return True
42
- return False
43
-
44
- def update_func(status_type: AgentStatusType, msg: str):
45
- if status_type != AgentStatusType.AGENT_UPDATE:
46
- output = f"{status_type.value} - {msg}"
47
- st.session_state.log_messages.append(output)
48
-
49
- async def launch_bot():
50
- def reset():
51
- st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "πŸ¦–"}]
52
- st.session_state.thinking_message = "Agent at work..."
53
- st.session_state.log_messages = []
54
- st.session_state.prompt = None
55
- st.session_state.ex_prompt = None
56
- st.session_state.first_turn = True
57
- st.session_state.show_logs = False
58
- if 'agent' not in st.session_state:
59
- st.session_state.agent = initialize_agent(cfg, update_func=update_func)
60
-
61
- if 'cfg' not in st.session_state:
62
- cfg = get_agent_config()
63
- st.session_state.cfg = cfg
64
- st.session_state.ex_prompt = None
65
- example_messages = [example.strip() for example in cfg.examples.split(";")] if cfg.examples else []
66
- st.session_state.example_messages = [em for em in example_messages if len(em)>0]
67
- reset()
68
- cfg = st.session_state.cfg
69
-
70
- # left side content
71
- with st.sidebar:
72
- image = Image.open('Vectara-logo.png')
73
- st.image(image, width=175)
74
- st.markdown(f"## {cfg['demo_welcome']}")
75
- st.markdown(f"{cfg['demo_description']}")
76
-
77
- st.markdown("\n\n")
78
- bc1, _ = st.columns([1, 1])
79
- with bc1:
80
- if st.button('Start Over'):
81
- reset()
82
- st.rerun()
83
-
84
- st.divider()
85
- st.markdown(
86
- "## How this works?\n"
87
- "This app was built with [Vectara](https://vectara.com).\n\n"
88
- "It demonstrates the use of Agentic RAG functionality with Vectara"
89
- )
90
-
91
- if "messages" not in st.session_state.keys():
92
- reset()
93
-
94
- # Display chat messages
95
- for message in st.session_state.messages:
96
- with st.chat_message(message["role"], avatar=message["avatar"]):
97
- st.write(message["content"])
98
-
99
- example_container = st.empty()
100
- with example_container:
101
- if show_example_questions():
102
- example_container.empty()
103
- st.session_state.first_turn = False
104
- st.rerun()
105
-
106
- # User-provided prompt
107
- if st.session_state.ex_prompt:
108
- prompt = st.session_state.ex_prompt
109
- else:
110
- prompt = st.chat_input()
111
- if prompt:
112
- st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
113
- st.session_state.prompt = prompt # Save the prompt in session state
114
- st.session_state.log_messages = []
115
- st.session_state.show_logs = False
116
- with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
117
- print(f"Starting new question: {prompt}\n")
118
- st.write(prompt)
119
- st.session_state.ex_prompt = None
120
-
121
- # Generate a new response if last message is not from assistant
122
- if st.session_state.prompt:
123
- with st.chat_message("assistant", avatar='πŸ€–'):
124
- with st.spinner(st.session_state.thinking_message):
125
- res = st.session_state.agent.chat(st.session_state.prompt)
126
- res = escape_dollars_outside_latex(res)
127
- message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}
128
- st.session_state.messages.append(message)
129
- st.markdown(res)
130
-
131
- send_amplitude_data(
132
- user_query=st.session_state.messages[-2]["content"],
133
- bot_response=st.session_state.messages[-1]["content"],
134
- demo_name=cfg['demo_name']
135
- )
136
-
137
- st.session_state.ex_prompt = None
138
- st.session_state.prompt = None
139
- st.session_state.first_turn = False
140
- st.rerun()
141
-
142
- # Record user feedback
143
- if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != initial_prompt):
144
- streamlit_feedback(
145
- feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
146
- kwargs = {"user_query": st.session_state.messages[-2]["content"],
147
- "bot_response": st.session_state.messages[-1]["content"],
148
- "demo_name": cfg["demo_name"]}
149
- )
150
-
151
- log_placeholder = st.empty()
152
- with log_placeholder.container():
153
- if st.session_state.show_logs:
154
- st.button("Hide Logs", on_click=toggle_logs)
155
- for msg in st.session_state.log_messages:
156
- st.text(msg)
157
- else:
158
- if len(st.session_state.log_messages) > 0:
159
- st.button("Show Logs", on_click=toggle_logs)
160
-
161
 
162
- sys.stdout.flush()
163
 
164
  if __name__ == "__main__":
165
  st.set_page_config(page_title="Hacker News Assistant", layout="wide")
 
1
+ import streamlit as st
2
+ from st_app import launch_bot
3
  import uuid
4
 
5
  import nest_asyncio
6
  import asyncio
7
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Setup for HTTP API Calls to Amplitude Analytics
9
  if 'device_id' not in st.session_state:
10
  st.session_state.device_id = str(uuid.uuid4())
11
 
 
 
 
 
 
12
  if "feedback_key" not in st.session_state:
13
+ st.session_state.feedback_key = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
 
15
 
16
  if __name__ == "__main__":
17
  st.set_page_config(page_title="Hacker News Assistant", layout="wide")
requirements.txt CHANGED
@@ -3,7 +3,7 @@ python-dotenv==1.0.1
3
  streamlit==1.32.2
4
  streamlit_pills==0.3.0
5
  streamlit_feedback==0.1.3
 
6
  langdetect==1.0.9
7
  langcodes==3.4.0
8
- uuid==1.30
9
- vectara-agentic==0.1.15
 
3
  streamlit==1.32.2
4
  streamlit_pills==0.3.0
5
  streamlit_feedback==0.1.3
6
+ uuid==1.30
7
  langdetect==1.0.9
8
  langcodes==3.4.0
9
+ vectara-agentic==0.1.16
 
st_app.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import sys
3
+ import uuid
4
+
5
+ import streamlit as st
6
+ from streamlit_pills import pills
7
+ from streamlit_feedback import streamlit_feedback
8
+
9
+ from utils import thumbs_feedback, escape_dollars_outside_latex, send_amplitude_data
10
+
11
+ from vectara_agentic.agent import AgentStatusType
12
+ from agent import initialize_agent, get_agent_config
13
+
14
+ initial_prompt = "How can I help you today?"
15
+
16
+ def toggle_logs():
17
+ st.session_state.show_logs = not st.session_state.show_logs
18
+
19
+ def show_example_questions():
20
+ if len(st.session_state.example_messages) > 0 and st.session_state.first_turn:
21
+ selected_example = pills("Queries to Try:", st.session_state.example_messages, index=None)
22
+ if selected_example:
23
+ st.session_state.ex_prompt = selected_example
24
+ st.session_state.first_turn = False
25
+ return True
26
+ return False
27
+
28
+ def update_func(status_type: AgentStatusType, msg: str):
29
+ if status_type != AgentStatusType.AGENT_UPDATE:
30
+ output = f"{status_type.value} - {msg}"
31
+ st.session_state.log_messages.append(output)
32
+
33
+ async def launch_bot():
34
+ def reset():
35
+ st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "πŸ¦–"}]
36
+ st.session_state.thinking_message = "Agent at work..."
37
+ st.session_state.log_messages = []
38
+ st.session_state.prompt = None
39
+ st.session_state.ex_prompt = None
40
+ st.session_state.first_turn = True
41
+ st.session_state.logs_enabled = False
42
+ st.session_state.show_logs = False
43
+ if 'agent' not in st.session_state:
44
+ st.session_state.agent = initialize_agent(cfg, update_func=update_func)
45
+ else:
46
+ st.session_state.agent.clear_memory()
47
+
48
+ if 'cfg' not in st.session_state:
49
+ cfg = get_agent_config()
50
+ st.session_state.cfg = cfg
51
+ st.session_state.ex_prompt = None
52
+ example_messages = [example.strip() for example in cfg.examples.split(";")] if cfg.examples else []
53
+ st.session_state.example_messages = [em for em in example_messages if len(em)>0]
54
+ reset()
55
+
56
+ cfg = st.session_state.cfg
57
+
58
+ # left side content
59
+ with st.sidebar:
60
+ image = Image.open('Vectara-logo.png')
61
+ st.image(image, width=175)
62
+ st.markdown(f"## {cfg['demo_welcome']}")
63
+ st.markdown(f"{cfg['demo_description']}")
64
+
65
+ st.markdown("\n\n")
66
+ bc1, bc2 = st.columns([1, 1])
67
+ with bc1:
68
+ if st.button('Start Over'):
69
+ reset()
70
+ st.rerun()
71
+ with bc2: # Updated button for enabling/disabling logs
72
+ if st.session_state.logs_enabled:
73
+ if st.button('Disable Logs', key='disable_logs'):
74
+ st.session_state.logs_enabled = False
75
+ st.rerun()
76
+ else:
77
+ if st.button('Enable Logs', key='enable_logs'):
78
+ st.session_state.logs_enabled = True
79
+ st.rerun()
80
+
81
+ st.divider()
82
+ st.markdown(
83
+ "## How this works?\n"
84
+ "This app was built with [Vectara](https://vectara.com).\n\n"
85
+ "It demonstrates the use of Agentic RAG functionality with Vectara"
86
+ )
87
+
88
+ if "messages" not in st.session_state.keys():
89
+ reset()
90
+
91
+ # Display chat messages
92
+ for message in st.session_state.messages:
93
+ with st.chat_message(message["role"], avatar=message["avatar"]):
94
+ st.write(message["content"])
95
+
96
+ example_container = st.empty()
97
+ with example_container:
98
+ if show_example_questions():
99
+ example_container.empty()
100
+ st.session_state.first_turn = False
101
+ st.rerun()
102
+
103
+ # User-provided prompt
104
+ if st.session_state.ex_prompt:
105
+ prompt = st.session_state.ex_prompt
106
+ else:
107
+ prompt = st.chat_input()
108
+ if prompt:
109
+ st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
110
+ st.session_state.prompt = prompt # Save the prompt in session state
111
+ st.session_state.log_messages = []
112
+ st.session_state.show_logs = False
113
+ with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
114
+ print(f"Starting new question: {prompt}\n")
115
+ st.write(prompt)
116
+ st.session_state.ex_prompt = None
117
+
118
+ # Generate a new response if last message is not from assistant
119
+ if st.session_state.prompt:
120
+ with st.chat_message("assistant", avatar='πŸ€–'):
121
+ with st.spinner(st.session_state.thinking_message):
122
+ res = st.session_state.agent.chat(st.session_state.prompt)
123
+ res = escape_dollars_outside_latex(res)
124
+ message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}
125
+ st.session_state.messages.append(message)
126
+ st.markdown(res)
127
+
128
+ send_amplitude_data(
129
+ user_query=st.session_state.messages[-2]["content"],
130
+ bot_response=st.session_state.messages[-1]["content"],
131
+ demo_name=cfg['demo_name']
132
+ )
133
+
134
+ st.session_state.ex_prompt = None
135
+ st.session_state.prompt = None
136
+ st.session_state.first_turn = False
137
+ st.rerun()
138
+
139
+ # Record user feedback
140
+ if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != initial_prompt):
141
+ if st.session_state.show_logs and st.session_state.logs_enabled: # Only show logs if enabled
142
+ streamlit_feedback(
143
+ feedback_type="thumbs", on_submit=thumbs_feedback, key=st.session_state.feedback_key,
144
+ kwargs={"user_query": st.session_state.messages[-2]["content"],
145
+ "bot_response": st.session_state.messages[-1]["content"],
146
+ "demo_name": cfg["demo_name"]}
147
+ )
148
+
149
+ log_placeholder = st.empty()
150
+ with log_placeholder.container():
151
+ if st.session_state.logs_enabled: # Show logs button only if log toggle is enabled
152
+ if st.session_state.show_logs:
153
+ st.button("Hide Logs", on_click=toggle_logs)
154
+ for msg in st.session_state.log_messages:
155
+ if len(msg) > 100: # Use text_area for longer messages
156
+ st.text_area(label="Log", value=msg, height=100, disabled=True)
157
+ else:
158
+ st.text(msg)
159
+ else:
160
+ if len(st.session_state.log_messages) > 0:
161
+ st.button("Show Logs", on_click=toggle_logs)
162
+
163
+
164
+ sys.stdout.flush()