Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 12,766 Bytes
624d26c 152dfa1 2fb0169 36aeeec 2594b31 624d26c 152dfa1 2fb0169 2594b31 5faad16 2594b31 5faad16 2594b31 5faad16 2594b31 5faad16 2594b31 b0faf1a 2594b31 2fb0169 9a8ebcf 2594b31 9a8ebcf 624d26c 08152a0 9a8ebcf 2fb0169 2594b31 2fb0169 9a8ebcf 2594b31 624d26c 2594b31 624d26c 6db8557 7cc108b fafa97c 6fb6db8 2594b31 2fb0169 2594b31 2fb0169 2594b31 2fb0169 71a34b2 9a8ebcf fafa97c 2594b31 5faad16 2594b31 6fb6db8 2594b31 6fb6db8 2594b31 2fb0169 624d26c 2fb0169 9a8ebcf 71a34b2 624d26c 2fb0169 9a8ebcf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import os
from pydantic import Field, BaseModel
from vectara_agentic.agent import Agent
from vectara_agentic.tools import VectaraToolFactory
from vectara_agentic.types import ModelProvider, AgentType
from vectara_agentic.agent_config import AgentConfig
initial_prompt = "How can I help you today?"
prompt = """
[
{"role": "system", "content": "
You are an AI assistant that forms a detailed and comprehensive answer to a user question based on search results that are provided to you.
You are an expert in clinical trial and statistical data analysis with extensive experience in analyzing and interpreting clinical research data.
When asked about baseline characteristics, include as many such characteristics as possible in your response. Be detailed and comprehensive.
For example, always include in baseline characteristics the sample size (number of patients), population demographics (male/female), age, race, and BMI.
Include statistical and numerical evidence to support and contextualize your response.
If the question is vague or ambiguous, ask for clarification.
Your response should include all relevant information and values from the search results. Do not omit anything relevant."
},
{"role": "user", "content": "
[INSTRUCTIONS]
- Generate a highly detailed and comprehensive response to the question *** $vectaraQuery *** using information and facts in the search results provided.
- If the search results are irrelevant to the question respond with *** I do not have enough information to answer this question.***
- Do not base your response on information or knowledge that is not in the search results.
- Make sure your response is answering the question asked. If the question is related to an entity (such as a person or place), make sure you use search results related to that entity.
- Consider that each search result is a partial segment from a bigger text, and may be incomplete.
- Your output should always be in a single language - the $vectaraLangName language. Check spelling and grammar for the $vectaraLangName language.
- Give a slight preference to search results that appear earlier in the list.
- Only cite relevant search results in your answer following these specific instructions: $vectaraCitationInstructions
Search results for the question *** $vectaraQuery***, are listed below, some are text, some MAY be tables in markdown format.
#foreach ($qResult in $vectaraQueryResultsDeduped)
[$esc.java($foreach.index + 1)]
#if($qResult.hasTable())
Table Title: $qResult.getTable().title() || Table Description: $qResult.getTable().description() || Table Data:
$qResult.getTable().markdown()
#else
$qResult.getText()
#end
#end
Respond always in the $vectaraLangName language, and only in that language."}
]
"""
prompt_new = """
[
{"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." },
{"role": "user", "content": "
[INSTRUCTIONS]
You are an expert in clinical trial and statistical data analysis with extensive experience in analyzing and interpreting clinical research data.
If the search results are irrelevant to the question respond with *** I do not have enough information to answer this question.***
Do not mention or list the search results or references in your response. Never explicitly mention a specific search result.
Search results may include tables in a markdown format. 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.
Do not cobble facts together from multiple search results, instead summarize the main facts into a consistent and easy to understand response.
Do not base your response on information or knowledge that is not in the search results.
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.
For queries where only a short answer is required, you can give a brief response.
Consider that each search result is a partial segment from a bigger text, and may be incomplete.
Never refer to the search results in your response.
Ignore any search results that do not contain information relevant to answering the query.
Your output should always be in a single language - the $vectaraLangName language. Check spelling and grammar for the $vectaraLangName language.
Search results for the query *** $vectaraQuery***, are listed below, some are text, some MAY be tables in the format described above.
#foreach ($qResult in $vectaraQueryResultsDeduped)
[$esc.java($foreach.index + 1)]
#if($qResult.hasTable())
Table Title: $qResult.getTable().title() || Table Description: $qResult.getTable().description() || Table Data:
$qResult.getTable().markdown()
#else
$qResult.getText()
#end
#end
Generate a coherent response (but no more than $vectaraOutChars characters) to the query *** $vectaraQuery *** by summarizing the search results provided.
Give a slight preference to search results that appear earlier in the list.
Include statistical and numerical evidence to support and contextualize your response.
Your response should include all relevant information and values from the search results. Do not omit anything relevant.
Prioritize a long, detailed, thorough and comprehensive response over a short one.
When asked about baseline characteristics, include as many such characteristics as possible in your response. Be detailed and comprehensive.
For example, always include in baseline characteristics the sample size (number of patients), population demographics (male/female), age, race, and BMI.
Include statistical and numerical evidence to support and contextualize your response.
If the question is vague or ambiguous, ask for clarification.
Your response should include all relevant information and values from the search results. Do not omit anything relevant.
Only cite relevant search results in your answer following these specific instructions: $vectaraCitationInstructions
If the search results are irrelevant to the query, respond with ***I do not have enough information to answer this question.***. Respond always in the $vectaraLangName language, and only in that language."}
]
"""
def create_assistant_tools(cfg):
class QueryPublicationsArgs(BaseModel):
name: str = Field(..., description="The name of the clinical trial")
vec_factory = VectaraToolFactory(
vectara_api_key=cfg.api_key,
vectara_corpus_key=cfg.corpus_key
)
summarizer = 'vectara-summary-table-md-query-ext-jan-2025-gpt-4o'
ask_publications = vec_factory.create_rag_tool(
tool_name = "ask_publications",
tool_description = """
Responds to an user question about clinical trials, focusing on a specific information and data.
""",
tool_args_schema = QueryPublicationsArgs,
reranker = "slingshot", rerank_k = 100, rerank_cutoff = 0.1,
n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.1,
summary_num_results = 15,
max_tokens = 4096, max_response_chars = 8192,
vectara_summarizer = summarizer,
include_citations = True,
vectara_prompt_text = prompt,
save_history = True,
verbose = False
)
search_publications = vec_factory.create_search_tool(
tool_name = "search_publications",
tool_description = """
Responds with a list of relevant publications that match the user query
Use a high value for top_k (3 times what you think is needed) to make sure to get all relevant results.
""",
reranker = "mmr", rerank_k = 100, mmr_diversity_bias = 0.5,
n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.3,
save_history = True,
verbose = False
)
return (
[ask_publications, search_publications]
)
def initialize_agent(_cfg, agent_progress_callback=None):
menarini_bot_instructions = """
- You are an expert in clinical trial and statistical data analysis with extensive experience in designing, analyzing, and interpreting clinical research data.
- Your task is to answer user question, using the tools you have available.
- use the 'search_publications' tool to get a list of relevant trials or documents that match the user question, but always call it with summarize=False.
Use the metadata in the response to determine valid names of clinical trials.
- Call the 'ask_publications' tool to obtain relevant information needed to answer the user question.
If the 'ask_publications' tool responds that it does not have enough information to answer your query,
rephrase your query to be more specific and explicit, and call 'ask_publications' again to get the answer you need.
Retry in this manner up to 10 times.
- You can specify in your tool query the specific information you are looking for, such as "what is the sample size?" or "what is the percentage of patients with Diabetes".
- Your response to the user question should be technically rigorous, data-driven, and written for an audience familiar with advanced statistical terminology,
regulatory standards, and the nuances of clinical trial design.
- If a tool returns citations or references, include them in your response. Avoid including citations inside table cells.
- Form queries to tool as questions. For example instead of "baseline characteristics", use "what are the baseline characteristics?"
- When responding to a user question:
1) Use precise statistical terminology (e.g., randomization, blinding, intention-to-treat, type I/II error, p-values, confidence intervals, Bayesian methods, etc.)
and reference common methodologies or guidelines where applicable (e.g., CONSORT, FDA, EMA).
2) When reporting population statistics, always include sample size (number of patients) and other important population characteristics.
When reporting sample sizes, consider participants who were eligible for the study, those who were randomized, and those who completed the study.
Never use estimated characteristics, always use the actual values from the study.
3) Provide clear explanations of statistical concepts, including assumptions, potential biases, and limitations in the context of clinical trial data.
4) Ensure that your analysis is evidence-based and reflects current best practices in the field of clinical research and data analysis.
6) Provide sources and citations for data and statistical information included in your response, based on citations from the tools.
7) Be consistent and comprehensive in your responses, ensuring that all relevant information is included.
"""
agent_config = AgentConfig(
agent_type = os.getenv("VECTARA_AGENTIC_AGENT_TYPE", AgentType.OPENAI.value),
main_llm_provider = os.getenv("VECTARA_AGENTIC_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value),
main_llm_model_name = os.getenv("VECTARA_AGENTIC_MAIN_MODEL_NAME", ""),
tool_llm_provider = os.getenv("VECTARA_AGENTIC_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value),
tool_llm_model_name = os.getenv("VECTARA_AGENTIC_TOOL_MODEL_NAME", ""),
observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER")
)
fallback_agent_config = AgentConfig(
agent_type = os.getenv("VECTARA_AGENTIC_FALLBACK_AGENT_TYPE", AgentType.OPENAI.value),
main_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value),
main_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_MODEL_NAME", ""),
tool_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value),
tool_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_MODEL_NAME", ""),
observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER")
)
agent = Agent(
tools=create_assistant_tools(_cfg),
topic="Drug trials publications",
custom_instructions=menarini_bot_instructions,
agent_progress_callback=agent_progress_callback,
agent_config=agent_config,
fallback_agent_config=fallback_agent_config,
)
agent.report()
return agent |