ofermend commited on
Commit
624d26c
Β·
1 Parent(s): b0faf1a
Files changed (4) hide show
  1. Dockerfile +4 -1
  2. agent.py +29 -52
  3. requirements.txt +2 -2
  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
@@ -1,43 +1,13 @@
 
1
  from pydantic import Field, BaseModel
2
 
3
  from vectara_agentic.agent import Agent
4
  from vectara_agentic.tools import VectaraToolFactory
 
 
5
 
6
  initial_prompt = "How can I help you today?"
7
 
8
- prompt_old = """
9
- [
10
- {"role": "system", "content": "You are an AI assistant that forms a detailed and comprehensive answer to a user query based on search results that are provided to you." },
11
- {"role": "user", "content": "
12
- [INSTRUCTIONS]
13
- If the search results are irrelevant to the question respond with *** I do not have enough information to answer this question.***
14
- Search results may include tables in a markdown format.
15
- When answering a question using a table be careful about which rows and columns contain the answer and include all relevant information from the relevant rows and columns that the query is asking about.
16
- Do not base your response on information or knowledge that is not in the search results.
17
- Make sure your response is answering the query asked. If the query is related to an entity (such as a person or place), make sure you use search results related to that entity.
18
- Consider that each search result is a partial segment from a bigger text, and may be incomplete.
19
- Your output should always be in a single language - the $vectaraLangName language. Check spelling and grammar for the $vectaraLangName language.
20
- Search results for the query *** $vectaraQuery***, are listed below, some are text, some MAY be tables in markdown format.
21
- #foreach ($qResult in $vectaraQueryResultsDeduped)
22
- [$esc.java($foreach.index + 1)]
23
- #if($qResult.hasTable())
24
- Table Title: $qResult.getTable().title() || Table Description: $qResult.getTable().description() || Table Data:
25
- $qResult.getTable().markdown()
26
- #else
27
- $qResult.getText()
28
- #end
29
- #end
30
- Generate a comprehensive response to the query *** $vectaraQuery *** using information and facts in the search results provided.
31
- Give a slight preference to search results that appear earlier in the list.
32
- Include statistical and numerical evidence to support and contextualize your response.
33
- Your response should include all relevant information and values from the search results. Do not omit anything relevant.
34
- Prioritize a long, detailed, thorough and comprehensive response over a short one.
35
- Cite relevant search results in your answer following these specific instructions: $vectaraCitationInstructions
36
- Respond always in the $vectaraLangName language, and only in that language."}
37
- ]
38
- """
39
-
40
-
41
  prompt = """
42
  [
43
  {"role": "system", "content": "
@@ -120,16 +90,12 @@ prompt_new = """
120
  def create_assistant_tools(cfg):
121
 
122
  class QueryPublicationsArgs(BaseModel):
123
- query: str = Field(..., description="The user query, always in the form of a question?",
124
- examples=[
125
- "what are the risks reported?",
126
- "which drug was tested?",
127
- "what is the baseline population in the trial?"
128
- ]),
129
  name: str = Field(..., description="The name of the clinical trial")
130
 
131
- vec_factory = VectaraToolFactory(vectara_api_key=cfg.api_key,
132
- vectara_corpus_key=cfg.corpus_key)
 
 
133
  summarizer = 'vectara-summary-table-md-query-ext-jan-2025-gpt-4o'
134
  ask_publications = vec_factory.create_rag_tool(
135
  tool_name = "ask_publications",
@@ -138,9 +104,9 @@ def create_assistant_tools(cfg):
138
  """,
139
  tool_args_schema = QueryPublicationsArgs,
140
  reranker = "slingshot", rerank_k = 100, rerank_cutoff = 0.1,
141
- n_sentences_before = 1, n_sentences_after = 1, lambda_val = 0.1,
142
  summary_num_results = 15,
143
- max_tokens = 8192,
144
  vectara_summarizer = summarizer,
145
  include_citations = True,
146
  vectara_prompt_text = prompt,
@@ -148,27 +114,18 @@ def create_assistant_tools(cfg):
148
  verbose = False
149
  )
150
 
151
- class SearchPublicationsArgs(BaseModel):
152
- query: str = Field(..., description="The user query, always in the form of a question?",
153
- examples=[
154
- "what are the risks reported?",
155
- "which drug was tested?",
156
- "what is the baseline population in the trial?"
157
- ]),
158
  search_publications = vec_factory.create_search_tool(
159
  tool_name = "search_publications",
160
  tool_description = """
161
  Responds with a list of relevant publications that match the user query
162
  Use a high value for top_k (3 times what you think is needed) to make sure to get all relevant results.
163
  """,
164
- tool_args_schema = SearchPublicationsArgs,
165
  reranker = "mmr", rerank_k = 100, mmr_diversity_bias = 0.5,
166
  n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.3,
167
  save_history = True,
168
  verbose = False
169
  )
170
 
171
-
172
  return (
173
  [ask_publications, search_publications]
174
  )
@@ -200,11 +157,31 @@ def initialize_agent(_cfg, agent_progress_callback=None):
200
  7) Be consistent and comprehensive in your responses, ensuring that all relevant information is included.
201
  """
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  agent = Agent(
204
  tools=create_assistant_tools(_cfg),
205
  topic="Drug trials publications",
206
  custom_instructions=menarini_bot_instructions,
207
  agent_progress_callback=agent_progress_callback,
 
 
208
  )
209
  agent.report()
210
  return agent
 
1
+ import os
2
  from pydantic import Field, BaseModel
3
 
4
  from vectara_agentic.agent import Agent
5
  from vectara_agentic.tools import VectaraToolFactory
6
+ from vectara_agentic.types import ModelProvider, AgentType
7
+ from vectara_agentic.agent_config import AgentConfig
8
 
9
  initial_prompt = "How can I help you today?"
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  prompt = """
12
  [
13
  {"role": "system", "content": "
 
90
  def create_assistant_tools(cfg):
91
 
92
  class QueryPublicationsArgs(BaseModel):
 
 
 
 
 
 
93
  name: str = Field(..., description="The name of the clinical trial")
94
 
95
+ vec_factory = VectaraToolFactory(
96
+ vectara_api_key=cfg.api_key,
97
+ vectara_corpus_key=cfg.corpus_key
98
+ )
99
  summarizer = 'vectara-summary-table-md-query-ext-jan-2025-gpt-4o'
100
  ask_publications = vec_factory.create_rag_tool(
101
  tool_name = "ask_publications",
 
104
  """,
105
  tool_args_schema = QueryPublicationsArgs,
106
  reranker = "slingshot", rerank_k = 100, rerank_cutoff = 0.1,
107
+ n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.1,
108
  summary_num_results = 15,
109
+ max_tokens = 4096, max_response_chars = 8192,
110
  vectara_summarizer = summarizer,
111
  include_citations = True,
112
  vectara_prompt_text = prompt,
 
114
  verbose = False
115
  )
116
 
 
 
 
 
 
 
 
117
  search_publications = vec_factory.create_search_tool(
118
  tool_name = "search_publications",
119
  tool_description = """
120
  Responds with a list of relevant publications that match the user query
121
  Use a high value for top_k (3 times what you think is needed) to make sure to get all relevant results.
122
  """,
 
123
  reranker = "mmr", rerank_k = 100, mmr_diversity_bias = 0.5,
124
  n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.3,
125
  save_history = True,
126
  verbose = False
127
  )
128
 
 
129
  return (
130
  [ask_publications, search_publications]
131
  )
 
157
  7) Be consistent and comprehensive in your responses, ensuring that all relevant information is included.
158
  """
159
 
160
+ agent_config = AgentConfig(
161
+ agent_type = os.getenv("VECTARA_AGENTIC_AGENT_TYPE", AgentType.OPENAI.value),
162
+ main_llm_provider = os.getenv("VECTARA_AGENTIC_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value),
163
+ main_llm_model_name = os.getenv("VECTARA_AGENTIC_MAIN_MODEL_NAME", ""),
164
+ tool_llm_provider = os.getenv("VECTARA_AGENTIC_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value),
165
+ tool_llm_model_name = os.getenv("VECTARA_AGENTIC_TOOL_MODEL_NAME", ""),
166
+ observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER")
167
+ )
168
+ fallback_agent_config = AgentConfig(
169
+ agent_type = os.getenv("VECTARA_AGENTIC_FALLBACK_AGENT_TYPE", AgentType.OPENAI.value),
170
+ main_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value),
171
+ main_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_MODEL_NAME", ""),
172
+ tool_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value),
173
+ tool_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_MODEL_NAME", ""),
174
+ observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER")
175
+ )
176
+
177
+
178
  agent = Agent(
179
  tools=create_assistant_tools(_cfg),
180
  topic="Drug trials publications",
181
  custom_instructions=menarini_bot_instructions,
182
  agent_progress_callback=agent_progress_callback,
183
+ agent_config=agent_config,
184
+ fallback_agent_config=fallback_agent_config,
185
  )
186
  agent.report()
187
  return agent
requirements.txt CHANGED
@@ -1,9 +1,9 @@
1
  omegaconf==2.3.0
2
  python-dotenv==1.0.1
3
- streamlit==1.43.2
4
  streamlit_feedback==0.1.3
5
  uuid==1.30
6
  langdetect==1.0.9
7
  langcodes==3.4.0
8
- vectara-agentic==0.2.11
9
  torch==2.6.0
 
1
  omegaconf==2.3.0
2
  python-dotenv==1.0.1
3
+ streamlit==1.45.0
4
  streamlit_feedback==0.1.3
5
  uuid==1.30
6
  langdetect==1.0.9
7
  langcodes==3.4.0
8
+ vectara-agentic==0.2.15
9
  torch==2.6.0
st_app.py CHANGED
@@ -89,7 +89,7 @@ async def launch_bot():
89
  if st.session_state.prompt:
90
  with st.chat_message("assistant", avatar='πŸ€–'):
91
  st.session_state.status = st.status('Processing...', expanded=False)
92
- res = st.session_state.agent.chat(st.session_state.prompt)
93
  #res = escape_dollars_outside_latex(res)
94
  res = str(res)
95
  message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}
 
89
  if st.session_state.prompt:
90
  with st.chat_message("assistant", avatar='πŸ€–'):
91
  st.session_state.status = st.status('Processing...', expanded=False)
92
+ res = await st.session_state.agent.achat(st.session_state.prompt)
93
  #res = escape_dollars_outside_latex(res)
94
  res = str(res)
95
  message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}