ofermend commited on
Commit
2458bb6
1 Parent(s): 534b221

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +23 -11
  2. prompts.py +1 -0
  3. requirements.txt +1 -0
app.py CHANGED
@@ -14,17 +14,21 @@ from llama_index.llms.openai import OpenAI
14
  from llama_index.core.tools import QueryEngineTool, ToolMetadata
15
  from llama_index.core.utils import print_text
16
  from llama_index.core.agent.react.formatter import ReActChatFormatter
 
 
 
17
  from llama_index.core.tools import FunctionTool
18
 
 
19
  from prompts import prompt_template
20
 
21
- learning_styles = ['traditional', 'Inquiry-based', 'Socratic']
22
  languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn',
23
  'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'}
24
  initial_prompt = "How can I help you today?"
25
 
26
 
27
- def launch_bot():
28
  def reset():
29
  cfg = st.session_state.cfg
30
  llm = OpenAI(model="gpt-4o", temperature=0)
@@ -79,12 +83,19 @@ def launch_bot():
79
  prompt = prompt_template.replace("{style}", cfg.style) \
80
  .replace("{language}", cfg.language) \
81
  .replace("{student_age}", str(cfg.student_age))
82
-
83
- st.session_state.agent = ReActAgent.from_tools(
84
- tools=[vectara_tool, rephrase_tool], llm=llm,
85
- verbose=True,
86
- react_chat_formatter = ReActChatFormatter(system_header=prompt)
87
- )
 
 
 
 
 
 
 
88
 
89
 
90
  if 'cfg' not in st.session_state:
@@ -92,7 +103,7 @@ def launch_bot():
92
  'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
93
  'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
94
  'api_key': str(os.environ['VECTARA_API_KEY']),
95
- 'style': learning_styles[0],
96
  'language': 'English',
97
  'student_age': 18
98
  })
@@ -112,7 +123,7 @@ def launch_bot():
112
  st.markdown("## Welcome to the Justice Harvard e-learning assistant demo.\n\n\n")
113
 
114
  st.markdown("\n")
115
- cfg.style = st.selectbox('Learning Style:', learning_styles)
116
  if st.session_state.style != cfg.style:
117
  st.session_state.style = cfg.style
118
  reset()
@@ -173,5 +184,6 @@ def launch_bot():
173
  sys.stdout.flush()
174
 
175
  if __name__ == "__main__":
176
- launch_bot()
 
177
 
 
14
  from llama_index.core.tools import QueryEngineTool, ToolMetadata
15
  from llama_index.core.utils import print_text
16
  from llama_index.core.agent.react.formatter import ReActChatFormatter
17
+
18
+ from llama_index.core.agent import ReActAgent
19
+ from llama_index.agent.openai import OpenAIAgent
20
  from llama_index.core.tools import FunctionTool
21
 
22
+
23
  from prompts import prompt_template
24
 
25
+ teaching_styles = ['traditional', 'Inquiry-based', 'Socratic']
26
  languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn',
27
  'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'}
28
  initial_prompt = "How can I help you today?"
29
 
30
 
31
+ def launch_bot(agent_type: str):
32
  def reset():
33
  cfg = st.session_state.cfg
34
  llm = OpenAI(model="gpt-4o", temperature=0)
 
83
  prompt = prompt_template.replace("{style}", cfg.style) \
84
  .replace("{language}", cfg.language) \
85
  .replace("{student_age}", str(cfg.student_age))
86
+ tools = [vectara_tool, rephrase_tool]
87
+ if agent_type == 'react':
88
+ st.session_state.agent = ReActAgent.from_tools(
89
+ tools=tools, llm=llm, verbose=True,
90
+ react_chat_formatter = ReActChatFormatter(system_header=prompt),
91
+ max_iterations = 20,
92
+ )
93
+ elif agent_type == 'openai':
94
+ st.session_state.agent = OpenAIAgent.from_tools(
95
+ tools=tools, llm=llm, verbose=True,
96
+ system_prompt=prompt)
97
+ else:
98
+ raise ValueError(f"Unknown agent type: {agent_type}")
99
 
100
 
101
  if 'cfg' not in st.session_state:
 
103
  'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
104
  'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
105
  'api_key': str(os.environ['VECTARA_API_KEY']),
106
+ 'style': teaching_styles[0],
107
  'language': 'English',
108
  'student_age': 18
109
  })
 
123
  st.markdown("## Welcome to the Justice Harvard e-learning assistant demo.\n\n\n")
124
 
125
  st.markdown("\n")
126
+ cfg.style = st.selectbox('Teacher Style:', teaching_styles)
127
  if st.session_state.style != cfg.style:
128
  st.session_state.style = cfg.style
129
  reset()
 
184
  sys.stdout.flush()
185
 
186
  if __name__ == "__main__":
187
+ print("Starting up...")
188
+ launch_bot(agent_type = 'openai')
189
 
prompts.py CHANGED
@@ -63,6 +63,7 @@ Answer: Sorry, I cannot answer your question.
63
 
64
  ADDITIONAL INSTRUCTIONS:
65
  - When using a tool, break down complex questions into a set of shorter questions, and ask the tool about each of them.
 
66
  - You must make at least one use of a tool for each question before responding.
67
  - Make sure your response relies on the tools you have used and the information from those tools.
68
  - Do not base your response on information that was not provided by the tools.
 
63
 
64
  ADDITIONAL INSTRUCTIONS:
65
  - When using a tool, break down complex questions into a set of shorter questions, and ask the tool about each of them.
66
+ - If a tool does not respond with a clear answer or cannot answer the query properly, try to rephrase your query or break it down into sub-queries to help it respond properly.
67
  - You must make at least one use of a tool for each question before responding.
68
  - Make sure your response relies on the tools you have used and the information from those tools.
69
  - Do not base your response on information that was not provided by the tools.
requirements.txt CHANGED
@@ -6,4 +6,5 @@ streamlit==1.32.2
6
  translate==3.6.1
7
  llama-index==0.10.42
8
  llama-index-indices-managed-vectara==0.1.4
 
9
  pydantic==1.10.15
 
6
  translate==3.6.1
7
  llama-index==0.10.42
8
  llama-index-indices-managed-vectara==0.1.4
9
+ llama-index-agent-openai==0.1.5
10
  pydantic==1.10.15