Spaces:
Sleeping
Sleeping
updates
Browse files
app.py
CHANGED
@@ -1,117 +1,15 @@
|
|
1 |
-
|
2 |
-
import os
|
3 |
from PIL import Image
|
4 |
import sys
|
5 |
|
6 |
-
from omegaconf import OmegaConf
|
7 |
import streamlit as st
|
8 |
from streamlit_pills import pills
|
9 |
|
10 |
-
from
|
11 |
-
load_dotenv(override=True)
|
12 |
|
13 |
-
from
|
14 |
-
from vectara_agent.agent import Agent, AgentStatusType
|
15 |
-
from vectara_agent.tools import ToolsFactory, VectaraToolFactory
|
16 |
-
from vectara_agent.tools_catalog import rephrase_text
|
17 |
|
18 |
-
teaching_styles = ['Inquiry-based', 'Socratic', 'traditional']
|
19 |
-
languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn',
|
20 |
-
'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'}
|
21 |
initial_prompt = "How can I help you today?"
|
22 |
|
23 |
-
def create_assistant_tools(cfg):
|
24 |
-
|
25 |
-
def adjust_response_to_student(
|
26 |
-
text: str = Field(description='the text to adjust. may include citations in markdown format.'),
|
27 |
-
age: int = Field(description='the age of the student. An integer'),
|
28 |
-
style: str = Field(description='teaching style'),
|
29 |
-
language: str = Field(description='the language')
|
30 |
-
|
31 |
-
) -> str:
|
32 |
-
"""
|
33 |
-
Rephrase the provided text to match the student's age, desired teaching style and language.
|
34 |
-
"""
|
35 |
-
instructions = f'''
|
36 |
-
The following is some text, which may include citations in markdown format.
|
37 |
-
Adjust the response to match the student's age of {age}, the {style} teaching style.
|
38 |
-
Where possible, maintain the citations in context of the response (instead of listing them at the end).
|
39 |
-
When showing citations, use markdown in the following format: [[N](URL)], where N is the citation sequential number.
|
40 |
-
You can renumber citations if needed.
|
41 |
-
For example, in the inquiry-based teaching style, choose to ask questions that encourage the student to think critically instead of repsonding directly with the answer.
|
42 |
-
Or in the socratic teaching style, choose to ask questions that lead the student to the answer.
|
43 |
-
Always respond in the {language} language.''' \
|
44 |
-
.replace("{style}", cfg.style) \
|
45 |
-
.replace("{language}", cfg.language) \
|
46 |
-
.replace("{student_age}", str(cfg.student_age))
|
47 |
-
|
48 |
-
return rephrase_text(text, instructions)
|
49 |
-
|
50 |
-
|
51 |
-
class JusticeHarvardArgs(BaseModel):
|
52 |
-
query: str = Field(..., description="The user query.")
|
53 |
-
|
54 |
-
vec_factory = VectaraToolFactory(vectara_api_key=cfg.api_key,
|
55 |
-
vectara_customer_id=cfg.customer_id,
|
56 |
-
vectara_corpus_id=cfg.corpus_id)
|
57 |
-
tools_factory = ToolsFactory()
|
58 |
-
query_tool = vec_factory.create_rag_tool(
|
59 |
-
tool_name = "justice_harvard_query",
|
60 |
-
tool_description = """
|
61 |
-
Answer questions about the justice, morality, politics and related topics,
|
62 |
-
based on transcripts of recordings from the Justice Harvard class that includes a lot of content on these topics.
|
63 |
-
When using the tool it's best to ask simple short questions. You can break complex questions into sub-queries.
|
64 |
-
""",
|
65 |
-
tool_args_schema = JusticeHarvardArgs,
|
66 |
-
reranker = "multilingual_reranker_v1", rerank_k = 100,
|
67 |
-
n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
|
68 |
-
summary_num_results = 10,
|
69 |
-
vectara_summarizer = 'vectara-summary-ext-24-05-med-omni',
|
70 |
-
include_citations = True,
|
71 |
-
)
|
72 |
-
|
73 |
-
return (
|
74 |
-
[tools_factory.create_tool(tool) for tool in
|
75 |
-
[
|
76 |
-
adjust_response_to_student,
|
77 |
-
]
|
78 |
-
] +
|
79 |
-
tools_factory.standard_tools() +
|
80 |
-
tools_factory.guardrail_tools() +
|
81 |
-
[query_tool]
|
82 |
-
)
|
83 |
-
|
84 |
-
def initialize_agent(_cfg):
|
85 |
-
if 'agent' in st.session_state:
|
86 |
-
return st.session_state.agent
|
87 |
-
|
88 |
-
bot_instructions = f"""
|
89 |
-
- You are a helpful teacher assistant, with expertise in education in various teaching styles.
|
90 |
-
- Obtain information using tools to answer the user's query.
|
91 |
-
- If the tool cannot provide information relevant to the user's query, tell the user that you are unable to provide an answer.
|
92 |
-
- If the tool can provide relevant information, use the adjust_response_to_student tool
|
93 |
-
to rephrase the text (including citations if any) to ensure it fits the student's age of {_cfg.student_age},
|
94 |
-
the {_cfg.style} teaching style and the {_cfg.language} language.
|
95 |
-
- When including links to the Justice Harvard videos, the url should include the start time (from the metadata).
|
96 |
-
For example: https://youtube.com/watch?v=0O2Rq4HJBxw&t=1234 represents a link to a youtube video starting at 1234 seconds.
|
97 |
-
- When showing citations, renumber them if needed and use markdown in the following format: [[N](URL)], where N is the citation sequential number.
|
98 |
-
- Response in a concise and clear manner, and provide the most relevant information to the student.
|
99 |
-
- Never discuss politics, and always respond politely.
|
100 |
-
"""
|
101 |
-
|
102 |
-
def update_func(status_type: AgentStatusType, msg: str):
|
103 |
-
if status_type != AgentStatusType.AGENT_UPDATE:
|
104 |
-
output = f"{status_type.value} - {msg}"
|
105 |
-
st.session_state.log_messages.append(output)
|
106 |
-
|
107 |
-
agent = Agent(
|
108 |
-
tools=create_assistant_tools(_cfg),
|
109 |
-
topic="justice, morality, politics, and philosophy",
|
110 |
-
custom_instructions=bot_instructions,
|
111 |
-
update_func=update_func
|
112 |
-
)
|
113 |
-
return agent
|
114 |
-
|
115 |
def toggle_logs():
|
116 |
st.session_state.show_logs = not st.session_state.show_logs
|
117 |
|
@@ -124,58 +22,56 @@ def show_example_questions():
|
|
124 |
return True
|
125 |
return False
|
126 |
|
|
|
|
|
|
|
|
|
127 |
|
128 |
def launch_bot():
|
129 |
def reset():
|
130 |
-
cfg = st.session_state.cfg
|
131 |
st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "🦖"}]
|
132 |
st.session_state.thinking_message = "Agent at work..."
|
133 |
-
st.session_state.agent = initialize_agent(cfg)
|
134 |
st.session_state.log_messages = []
|
135 |
st.session_state.prompt = None
|
136 |
st.session_state.ex_prompt = None
|
137 |
st.session_state.first_turn = True
|
138 |
st.session_state.show_logs = False
|
|
|
139 |
|
140 |
-
st.set_page_config(page_title="Justice Harvard Teaching Assistant", layout="wide")
|
141 |
if 'cfg' not in st.session_state:
|
142 |
-
cfg =
|
143 |
-
'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
|
144 |
-
'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
|
145 |
-
'api_key': str(os.environ['VECTARA_API_KEY']),
|
146 |
-
'style': teaching_styles[0],
|
147 |
-
'language': 'English',
|
148 |
-
'student_age': 18,
|
149 |
-
'examples': os.environ.get('QUERY_EXAMPLES', None)
|
150 |
-
})
|
151 |
-
st.session_state.cfg = cfg
|
152 |
st.session_state.style = cfg.style
|
153 |
st.session_state.language = cfg.language
|
154 |
st.session_state.student_age = cfg.student_age
|
|
|
155 |
st.session_state.ex_prompt = None
|
156 |
example_messages = [example.strip() for example in cfg.examples.split(",")] if cfg.examples else []
|
157 |
st.session_state.example_messages = [em for em in example_messages if len(em)>0]
|
158 |
reset()
|
159 |
|
160 |
cfg = st.session_state.cfg
|
|
|
161 |
|
162 |
# left side content
|
163 |
with st.sidebar:
|
164 |
image = Image.open('Vectara-logo.png')
|
165 |
st.image(image, width=175)
|
166 |
-
st.markdown("##
|
|
|
167 |
|
168 |
st.markdown("\n")
|
169 |
cfg.style = st.selectbox('Teacher Style:', teaching_styles)
|
170 |
if st.session_state.style != cfg.style:
|
171 |
st.session_state.style = cfg.style
|
172 |
reset()
|
|
|
173 |
|
174 |
st.markdown("\n")
|
175 |
cfg.language = st.selectbox('Language:', languages.keys())
|
176 |
if st.session_state.language != cfg.language:
|
177 |
-
st.session_state.
|
178 |
reset()
|
|
|
179 |
|
180 |
st.markdown("\n")
|
181 |
cfg.student_age = st.number_input(
|
@@ -185,6 +81,7 @@ def launch_bot():
|
|
185 |
if st.session_state.student_age != cfg.student_age:
|
186 |
st.session_state.student_age = cfg.student_age
|
187 |
reset()
|
|
|
188 |
|
189 |
st.markdown("\n\n")
|
190 |
bc1, _ = st.columns([1, 1])
|
@@ -197,10 +94,8 @@ def launch_bot():
|
|
197 |
st.markdown(
|
198 |
"## How this works?\n"
|
199 |
"This app was built with [Vectara](https://vectara.com).\n\n"
|
200 |
-
"It demonstrates the use of Agentic
|
201 |
)
|
202 |
-
st.markdown("---")
|
203 |
-
|
204 |
|
205 |
if "messages" not in st.session_state.keys():
|
206 |
reset()
|
@@ -243,6 +138,7 @@ def launch_bot():
|
|
243 |
st.markdown(res)
|
244 |
st.session_state.ex_prompt = None
|
245 |
st.session_state.prompt = None
|
|
|
246 |
st.rerun()
|
247 |
|
248 |
log_placeholder = st.empty()
|
@@ -256,8 +152,6 @@ def launch_bot():
|
|
256 |
st.button("Show Logs", on_click=toggle_logs)
|
257 |
|
258 |
sys.stdout.flush()
|
259 |
-
|
260 |
-
|
261 |
if __name__ == "__main__":
|
262 |
-
launch_bot()
|
263 |
-
|
|
|
|
|
|
|
1 |
from PIL import Image
|
2 |
import sys
|
3 |
|
|
|
4 |
import streamlit as st
|
5 |
from streamlit_pills import pills
|
6 |
|
7 |
+
from vectara_agent.agent import AgentStatusType
|
|
|
8 |
|
9 |
+
from agent import initialize_agent, get_agent_config, teaching_styles, languages
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
11 |
initial_prompt = "How can I help you today?"
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def toggle_logs():
|
14 |
st.session_state.show_logs = not st.session_state.show_logs
|
15 |
|
|
|
22 |
return True
|
23 |
return False
|
24 |
|
25 |
+
def update_func(status_type: AgentStatusType, msg: str):
|
26 |
+
if status_type != AgentStatusType.AGENT_UPDATE:
|
27 |
+
output = f"{status_type.value} - {msg}"
|
28 |
+
st.session_state.log_messages.append(output)
|
29 |
|
30 |
def launch_bot():
|
31 |
def reset():
|
|
|
32 |
st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "🦖"}]
|
33 |
st.session_state.thinking_message = "Agent at work..."
|
|
|
34 |
st.session_state.log_messages = []
|
35 |
st.session_state.prompt = None
|
36 |
st.session_state.ex_prompt = None
|
37 |
st.session_state.first_turn = True
|
38 |
st.session_state.show_logs = False
|
39 |
+
st.session_state.agent = initialize_agent(cfg, update_func=update_func) # must reset agent to apply new settings
|
40 |
|
|
|
41 |
if 'cfg' not in st.session_state:
|
42 |
+
cfg = get_agent_config()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
st.session_state.style = cfg.style
|
44 |
st.session_state.language = cfg.language
|
45 |
st.session_state.student_age = cfg.student_age
|
46 |
+
st.session_state.cfg = cfg
|
47 |
st.session_state.ex_prompt = None
|
48 |
example_messages = [example.strip() for example in cfg.examples.split(",")] if cfg.examples else []
|
49 |
st.session_state.example_messages = [em for em in example_messages if len(em)>0]
|
50 |
reset()
|
51 |
|
52 |
cfg = st.session_state.cfg
|
53 |
+
st.set_page_config(page_title=cfg['title'], layout="wide")
|
54 |
|
55 |
# left side content
|
56 |
with st.sidebar:
|
57 |
image = Image.open('Vectara-logo.png')
|
58 |
st.image(image, width=175)
|
59 |
+
st.markdown(f"## {cfg['demo_welcome']}")
|
60 |
+
st.markdown(f"{cfg['demo_description']}")
|
61 |
|
62 |
st.markdown("\n")
|
63 |
cfg.style = st.selectbox('Teacher Style:', teaching_styles)
|
64 |
if st.session_state.style != cfg.style:
|
65 |
st.session_state.style = cfg.style
|
66 |
reset()
|
67 |
+
st.rerun()
|
68 |
|
69 |
st.markdown("\n")
|
70 |
cfg.language = st.selectbox('Language:', languages.keys())
|
71 |
if st.session_state.language != cfg.language:
|
72 |
+
st.session_state.language = cfg.language
|
73 |
reset()
|
74 |
+
st.rerun()
|
75 |
|
76 |
st.markdown("\n")
|
77 |
cfg.student_age = st.number_input(
|
|
|
81 |
if st.session_state.student_age != cfg.student_age:
|
82 |
st.session_state.student_age = cfg.student_age
|
83 |
reset()
|
84 |
+
st.rerun()
|
85 |
|
86 |
st.markdown("\n\n")
|
87 |
bc1, _ = st.columns([1, 1])
|
|
|
94 |
st.markdown(
|
95 |
"## How this works?\n"
|
96 |
"This app was built with [Vectara](https://vectara.com).\n\n"
|
97 |
+
"It demonstrates the use of Agentic RAG functionality with Vectara"
|
98 |
)
|
|
|
|
|
99 |
|
100 |
if "messages" not in st.session_state.keys():
|
101 |
reset()
|
|
|
138 |
st.markdown(res)
|
139 |
st.session_state.ex_prompt = None
|
140 |
st.session_state.prompt = None
|
141 |
+
st.session_state.first_turn = False
|
142 |
st.rerun()
|
143 |
|
144 |
log_placeholder = st.empty()
|
|
|
152 |
st.button("Show Logs", on_click=toggle_logs)
|
153 |
|
154 |
sys.stdout.flush()
|
155 |
+
|
|
|
156 |
if __name__ == "__main__":
|
157 |
+
launch_bot()
|
|