ofermend commited on
Commit
96f9748
·
1 Parent(s): 13ff026
Files changed (4) hide show
  1. Dockerfile +4 -1
  2. agent.py +32 -12
  3. requirements.txt +3 -3
  4. st_app.py +1 -1
Dockerfile CHANGED
@@ -7,12 +7,15 @@ COPY ./requirements.txt /app/requirements.txt
7
  RUN pip3 install --no-cache-dir --upgrade pip
8
  RUN pip3 install --no-cache-dir wheel setuptools build
9
  RUN pip3 install --no-cache-dir --use-pep517 -r /app/requirements.txt
10
-
11
  # User
12
  RUN useradd -m -u 1000 user
13
  USER user
14
  ENV HOME /home/user
15
  ENV PATH $HOME/.local/bin:$PATH
 
 
 
16
 
17
  WORKDIR $HOME
18
  RUN mkdir app
 
7
  RUN pip3 install --no-cache-dir --upgrade pip
8
  RUN pip3 install --no-cache-dir wheel setuptools build
9
  RUN pip3 install --no-cache-dir --use-pep517 -r /app/requirements.txt
10
+
11
  # User
12
  RUN useradd -m -u 1000 user
13
  USER user
14
  ENV HOME /home/user
15
  ENV PATH $HOME/.local/bin:$PATH
16
+ ENV TIKTOKEN_CACHE_DIR $HOME/.cache/tiktoken
17
+
18
+ RUN mkdir -p $HOME/.cache/tiktoken
19
 
20
  WORKDIR $HOME
21
  RUN mkdir app
agent.py CHANGED
@@ -11,6 +11,7 @@ from vectara_agentic.agent import Agent
11
  from vectara_agentic.agent_config import AgentConfig
12
  from vectara_agentic.tools import ToolsFactory, VectaraToolFactory
13
  from vectara_agentic.tools_catalog import ToolsCatalog
 
14
 
15
  teaching_styles = ['Inquiry-based', 'Socratic', 'traditional']
16
  languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn',
@@ -22,8 +23,10 @@ class AgentTools:
22
  self.tools_factory = ToolsFactory()
23
  self.agent_config = agent_config
24
  self.cfg = _cfg
25
- self.vec_factory = VectaraToolFactory(vectara_api_key=_cfg.api_key,
26
- vectara_corpus_key=_cfg.corpus_key)
 
 
27
 
28
  def adjust_response_to_student(
29
  self,
@@ -54,23 +57,22 @@ class AgentTools:
54
 
55
 
56
  def get_tools(self):
57
- class JusticeHarvardArgs(BaseModel):
58
- query: str = Field(..., description="The user query.")
59
-
60
  vec_factory = VectaraToolFactory(vectara_api_key=self.cfg.api_key,vectara_corpus_key=self.cfg.corpus_key)
61
- summarizer = 'vectara-summary-ext-24-05-med-omni'
62
  query_tool = vec_factory.create_rag_tool(
63
  tool_name = "ask_about_justice_harvard",
64
  tool_description = """
65
  Answer questions about the justice, morality, politics and related topics,
66
  based on transcripts of recordings from the Justice Harvard class that includes a lot of content on these topics.
67
  """,
68
- tool_args_schema = JusticeHarvardArgs,
69
- reranker = "multilingual_reranker_v1", rerank_k = 100,
70
  n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
71
- summary_num_results = 10,
 
72
  vectara_summarizer = summarizer,
73
  include_citations = True,
 
 
74
  )
75
 
76
  tools_factory = ToolsFactory()
@@ -80,7 +82,7 @@ class AgentTools:
80
  self.adjust_response_to_student,
81
  ]
82
  ] +
83
- tools_factory.standard_tools() +
84
  tools_factory.guardrail_tools() +
85
  [query_tool]
86
  )
@@ -89,7 +91,8 @@ def initialize_agent(_cfg, agent_progress_callback=None):
89
  bot_instructions = f"""
90
  - You are a helpful teacher assistant, with expertise in education in various teaching styles.
91
  - Obtain information using tools to answer the user's query.
92
- - If the tool cannot provide information relevant to the user's query, tell the user that you are unable to provide an answer.
 
93
  - If the tool can provide relevant information, use the adjust_response_to_student tool
94
  to rephrase the text (including citations if any) to ensure it fits the student's age of {_cfg.student_age},
95
  the {_cfg.style} teaching style and the {_cfg.language} language.
@@ -99,9 +102,26 @@ def initialize_agent(_cfg, agent_progress_callback=None):
99
  - Response in a concise and clear manner, and provide the most relevant information to the student.
100
  - Never discuss politics, and always respond politely.
101
  """
102
- agent_config = AgentConfig()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  agent = Agent(
104
  agent_config=agent_config,
 
105
  tools=AgentTools(_cfg, agent_config).get_tools(),
106
  topic="justice, morality, politics, and philosophy",
107
  custom_instructions=bot_instructions,
 
11
  from vectara_agentic.agent_config import AgentConfig
12
  from vectara_agentic.tools import ToolsFactory, VectaraToolFactory
13
  from vectara_agentic.tools_catalog import ToolsCatalog
14
+ from vectara_agentic.types import ModelProvider, AgentType
15
 
16
  teaching_styles = ['Inquiry-based', 'Socratic', 'traditional']
17
  languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn',
 
23
  self.tools_factory = ToolsFactory()
24
  self.agent_config = agent_config
25
  self.cfg = _cfg
26
+ self.vec_factory = VectaraToolFactory(
27
+ vectara_api_key=_cfg.api_key,
28
+ vectara_corpus_key=_cfg.corpus_key
29
+ )
30
 
31
  def adjust_response_to_student(
32
  self,
 
57
 
58
 
59
  def get_tools(self):
 
 
 
60
  vec_factory = VectaraToolFactory(vectara_api_key=self.cfg.api_key,vectara_corpus_key=self.cfg.corpus_key)
61
+ summarizer = 'vectara-summary-table-md-query-ext-jan-2025-gpt-4o'
62
  query_tool = vec_factory.create_rag_tool(
63
  tool_name = "ask_about_justice_harvard",
64
  tool_description = """
65
  Answer questions about the justice, morality, politics and related topics,
66
  based on transcripts of recordings from the Justice Harvard class that includes a lot of content on these topics.
67
  """,
68
+ reranker = "multilingual_reranker_v1", rerank_k = 100, rerank_cutoff = 0.3,
 
69
  n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
70
+ summary_num_results = 15,
71
+ max_tokens = 4096, max_response_chars = 8192,
72
  vectara_summarizer = summarizer,
73
  include_citations = True,
74
+ verbose = False,
75
+ save_history = True,
76
  )
77
 
78
  tools_factory = ToolsFactory()
 
82
  self.adjust_response_to_student,
83
  ]
84
  ] +
85
+ tools_factory.standard_tools() +
86
  tools_factory.guardrail_tools() +
87
  [query_tool]
88
  )
 
91
  bot_instructions = f"""
92
  - You are a helpful teacher assistant, with expertise in education in various teaching styles.
93
  - Obtain information using tools to answer the user's query.
94
+ - If the tool cannot provide information relevant to the user's query, try calling the tool again with a rephrased query.
95
+ If it fails 3 times, then tell the user that you are unable to provide an answer.
96
  - If the tool can provide relevant information, use the adjust_response_to_student tool
97
  to rephrase the text (including citations if any) to ensure it fits the student's age of {_cfg.student_age},
98
  the {_cfg.style} teaching style and the {_cfg.language} language.
 
102
  - Response in a concise and clear manner, and provide the most relevant information to the student.
103
  - Never discuss politics, and always respond politely.
104
  """
105
+ agent_config = AgentConfig(
106
+ agent_type = os.getenv("VECTARA_AGENTIC_AGENT_TYPE", AgentType.OPENAI.value),
107
+ main_llm_provider = os.getenv("VECTARA_AGENTIC_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value),
108
+ main_llm_model_name = os.getenv("VECTARA_AGENTIC_MAIN_MODEL_NAME", ""),
109
+ tool_llm_provider = os.getenv("VECTARA_AGENTIC_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value),
110
+ tool_llm_model_name = os.getenv("VECTARA_AGENTIC_TOOL_MODEL_NAME", ""),
111
+ observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER")
112
+ )
113
+ fallback_agent_config = AgentConfig(
114
+ agent_type = os.getenv("VECTARA_AGENTIC_FALLBACK_AGENT_TYPE", AgentType.OPENAI.value),
115
+ main_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value),
116
+ main_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_MODEL_NAME", ""),
117
+ tool_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value),
118
+ tool_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_MODEL_NAME", ""),
119
+ observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER")
120
+ )
121
+
122
  agent = Agent(
123
  agent_config=agent_config,
124
+ fallback_agent_config=fallback_agent_config,
125
  tools=AgentTools(_cfg, agent_config).get_tools(),
126
  topic="justice, morality, politics, and philosophy",
127
  custom_instructions=bot_instructions,
requirements.txt CHANGED
@@ -1,10 +1,10 @@
1
  omegaconf==2.3.0
2
  python-dotenv==1.0.1
3
- streamlit==1.41.1
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.2.9
10
- torch==2.6.0
 
1
  omegaconf==2.3.0
2
  python-dotenv==1.0.1
3
+ streamlit==1.45.0
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.2.15
10
+ torch==2.6.0
st_app.py CHANGED
@@ -153,7 +153,7 @@ async def launch_bot():
153
  if st.session_state.prompt:
154
  with st.chat_message("assistant", avatar='🤖'):
155
  st.session_state.status = st.status('Processing...', expanded=False)
156
- response = st.session_state.agent.chat(st.session_state.prompt)
157
  res = escape_dollars_outside_latex(response.response)
158
  message = {"role": "assistant", "content": res, "avatar": '🤖'}
159
  st.session_state.messages.append(message)
 
153
  if st.session_state.prompt:
154
  with st.chat_message("assistant", avatar='🤖'):
155
  st.session_state.status = st.status('Processing...', expanded=False)
156
+ response = await st.session_state.agent.achat(st.session_state.prompt)
157
  res = escape_dollars_outside_latex(response.response)
158
  message = {"role": "assistant", "content": res, "avatar": '🤖'}
159
  st.session_state.messages.append(message)