Spaces:
Build error
Build error
final push
Browse files- README.md +93 -2
- agent.py +13 -11
- app.py +22 -28
- test_app.ipynb +0 -0
- test_nebius_mode.ipynb +105 -0
- test_query_FAISS.ipynb +0 -0
- tool_create_FAISS_vector.py +19 -4
- tool_query_FAISS_vector.py +11 -8
README.md
CHANGED
|
@@ -13,9 +13,100 @@ tags:
|
|
| 13 |
- building-mcp-track-enterprise
|
| 14 |
---
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
|
| 19 |
link to social media : https://www.linkedin.com/feed/update/urn:li:activity:7400904066005966848/
|
| 20 |
|
| 21 |
-
link to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
- building-mcp-track-enterprise
|
| 14 |
---
|
| 15 |
|
| 16 |
+
|
| 17 |
|
| 18 |
|
| 19 |
link to social media : https://www.linkedin.com/feed/update/urn:li:activity:7400904066005966848/
|
| 20 |
|
| 21 |
+
link to vidéo :
|
| 22 |
+
|
| 23 |
+
# Project description
|
| 24 |
+
|
| 25 |
+
This MCP server provides a multi-agent, tool-augmented reasoning system designed for clinical research, biomedical analysis, and scientific information retrieval. It integrates structured clinical trial querying, PubMed literature search, PDF parsing, RAG-based document retrieval, and web research into a single orchestrated environment.
|
| 26 |
+
|
| 27 |
+
At its core is a Manager Agent (smolagents) that interprets user questions, plans multi-step reasoning, and coordinates two specialized agents:
|
| 28 |
+
|
| 29 |
+
1. Clinical Agent
|
| 30 |
+
|
| 31 |
+
Queries ClinicalTrials.gov with structured filters
|
| 32 |
+
|
| 33 |
+
Retrieves PubMed articles
|
| 34 |
+
|
| 35 |
+
Extracts text from scientific PDFs
|
| 36 |
+
|
| 37 |
+
Produces structured tables, trial summaries, and evidence-based reports
|
| 38 |
+
|
| 39 |
+
2. Online Information Agent
|
| 40 |
+
|
| 41 |
+
Performs Wikipedia and DuckDuckGo searches
|
| 42 |
+
|
| 43 |
+
Visits and extracts content from arbitrary webpages
|
| 44 |
+
|
| 45 |
+
Supports general research, context building, and cross-verification
|
| 46 |
+
|
| 47 |
+
## Summary
|
| 48 |
+
|
| 49 |
+
This MCP server is a full-stack multi-agent research system with:
|
| 50 |
+
|
| 51 |
+
Hierarchical LLM planning
|
| 52 |
+
|
| 53 |
+
Dedicated scientific and clinical tools
|
| 54 |
+
|
| 55 |
+
Real-time execution monitoring
|
| 56 |
+
|
| 57 |
+
FAISS-based custom RAG infrastructure
|
| 58 |
+
|
| 59 |
+
Integrated web search and document extraction
|
| 60 |
+
|
| 61 |
+
A complete interactive UI for researchers or clinicians
|
| 62 |
+
|
| 63 |
+
It is suitable for:
|
| 64 |
+
|
| 65 |
+
Clinical evidence synthesis
|
| 66 |
+
|
| 67 |
+
Scientific research workflows
|
| 68 |
+
|
| 69 |
+
Medical question answering
|
| 70 |
+
|
| 71 |
+
Literature reviews
|
| 72 |
+
|
| 73 |
+
Automated extraction pipelines
|
| 74 |
+
|
| 75 |
+
# Use Gradio Interface
|
| 76 |
+
Use the specialized code agent from gradio interface [track agent reasonning]
|
| 77 |
+
* Must Add NEBIUS_API_KEY secrets to the huggingface space in order to use the Agent
|
| 78 |
+
* Add LANGFUSE secrets to track
|
| 79 |
+
|
| 80 |
+
# Set-up MCP tools for a client (huggingchat)
|
| 81 |
+
|
| 82 |
+
## 1. Server side : Connect to the space and click "Utiliser via API"
|
| 83 |
+

|
| 84 |
+
## 2. Client side : Select "Manage MCP server" on huggingchat
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# --------------------------------------------------------
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
## 3. Server side : Choose communication type (MCP streamable HTTP)
|
| 91 |
+

|
| 92 |
+
## 4. Client side : Click on "Add Server"
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# ---------------------------------------------------------
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
## 5. Server side : Copy the link for the client side (MCP streamable HTTP)
|
| 99 |
+

|
| 100 |
+
## 6. Client side : Paste the link Click in "Server URL"
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# ----------------------------------------------------------
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# Chat with data
|
| 109 |
+
|
| 110 |
+
## 7. Query LLM Without/With MCP tool changes the actions/output
|
| 111 |
+
## 8. The MCP tools can be called multiple times (x6) in a single request
|
| 112 |
+

|
agent.py
CHANGED
|
@@ -19,19 +19,19 @@ from smolagents import (
|
|
| 19 |
|
| 20 |
load_dotenv()
|
| 21 |
|
| 22 |
-
from langfuse import get_client
|
| 23 |
-
langfuse = get_client()
|
| 24 |
-
if langfuse.auth_check():
|
| 25 |
-
|
| 26 |
-
else:
|
| 27 |
-
|
| 28 |
|
| 29 |
|
| 30 |
-
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
|
| 31 |
-
SmolagentsInstrumentor().instrument()
|
| 32 |
|
| 33 |
model = LiteLLMModel(
|
| 34 |
-
model_id="openai/
|
| 35 |
api_key=os.environ.get("NEBIUS_API_KEY"),
|
| 36 |
api_base="https://api.tokenfactory.nebius.com/v1/"
|
| 37 |
)
|
|
@@ -113,6 +113,7 @@ clinical_agent = CodeAgent(
|
|
| 113 |
"Gather general or recent information from online sources. "
|
| 114 |
"Use Wikipedia for overviews, DuckDuckGo for recent data, and VisitWebpageTool for specific URLs. "
|
| 115 |
"Return structured summaries with sources."
|
|
|
|
| 116 |
),
|
| 117 |
tools=[ClinicalTrialsSearchTool()],
|
| 118 |
additional_authorized_imports=["time", "numpy", "pandas"],
|
|
@@ -150,10 +151,11 @@ manager_agent = CodeAgent(
|
|
| 150 |
"Most important task is to provide a complete answer to user questions based on clinical trial data and online information. "
|
| 151 |
"Orchestrate workflow between clinical and online agents. "
|
| 152 |
"Validate outputs, resolve conflicts, and ensure the final answer is complete and accurate."
|
|
|
|
| 153 |
),
|
| 154 |
-
tools=[FinalAnswerTool()],
|
| 155 |
model=model,
|
| 156 |
-
managed_agents=[clinical_agent,search_online_info],
|
| 157 |
# executor_type="modal",
|
| 158 |
provide_run_summary=True,
|
| 159 |
additional_authorized_imports=["time", "numpy", "pandas"],
|
|
|
|
| 19 |
|
| 20 |
load_dotenv()
|
| 21 |
|
| 22 |
+
# from langfuse import get_client
|
| 23 |
+
# langfuse = get_client()
|
| 24 |
+
# if langfuse.auth_check():
|
| 25 |
+
# print("Langfuse client is authenticated and ready!")
|
| 26 |
+
# else:
|
| 27 |
+
# print("Authentication failed. Please check your credentials and host.")
|
| 28 |
|
| 29 |
|
| 30 |
+
# from openinference.instrumentation.smolagents import SmolagentsInstrumentor
|
| 31 |
+
# SmolagentsInstrumentor().instrument()
|
| 32 |
|
| 33 |
model = LiteLLMModel(
|
| 34 |
+
model_id="openai/nvidia/Llama-3_1-Nemotron-Ultra-253B-v1",
|
| 35 |
api_key=os.environ.get("NEBIUS_API_KEY"),
|
| 36 |
api_base="https://api.tokenfactory.nebius.com/v1/"
|
| 37 |
)
|
|
|
|
| 113 |
"Gather general or recent information from online sources. "
|
| 114 |
"Use Wikipedia for overviews, DuckDuckGo for recent data, and VisitWebpageTool for specific URLs. "
|
| 115 |
"Return structured summaries with sources."
|
| 116 |
+
"Use the ClinicalTrialsSearchTool() for any question related to clinical trial"
|
| 117 |
),
|
| 118 |
tools=[ClinicalTrialsSearchTool()],
|
| 119 |
additional_authorized_imports=["time", "numpy", "pandas"],
|
|
|
|
| 151 |
"Most important task is to provide a complete answer to user questions based on clinical trial data and online information. "
|
| 152 |
"Orchestrate workflow between clinical and online agents. "
|
| 153 |
"Validate outputs, resolve conflicts, and ensure the final answer is complete and accurate."
|
| 154 |
+
"rimarily use the managed agent clinical_agent for question related to clinical trials"
|
| 155 |
),
|
| 156 |
+
tools=[FinalAnswerTool(),ClinicalTrialsSearchTool()],
|
| 157 |
model=model,
|
| 158 |
+
# managed_agents=[clinical_agent,search_online_info],
|
| 159 |
# executor_type="modal",
|
| 160 |
provide_run_summary=True,
|
| 161 |
additional_authorized_imports=["time", "numpy", "pandas"],
|
app.py
CHANGED
|
@@ -83,7 +83,7 @@ def answer_question(question, history):
|
|
| 83 |
question_with_history = "Conversation history:\n" + str(history) + "\n\nNew user question:\n " + question
|
| 84 |
for st in safe_agent.run(question_with_history,stream=True,return_full_result=True):
|
| 85 |
if isinstance(st, smolagents.memory.PlanningStep):
|
| 86 |
-
plan = "#
|
| 87 |
for m in plan.split("\n"):
|
| 88 |
thoughts += "\n" + m
|
| 89 |
yield thoughts, final_answer, history
|
|
@@ -103,13 +103,20 @@ def answer_question(question, history):
|
|
| 103 |
yield thoughts, final_answer, history
|
| 104 |
|
| 105 |
elif isinstance(st, smolagents.memory.ActionStep):
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
yield thoughts, final_answer, history
|
| 110 |
-
|
| 111 |
-
thoughts += "\n\n\n********** End fo Step " + str(st.step_number) + " : *********\n" + str(st.token_usage) + "\nStep duration" + str(st.timing) + "\n\n"
|
| 112 |
yield thoughts, final_answer, history
|
|
|
|
|
|
|
|
|
|
| 113 |
elif isinstance(st, smolagents.memory.FinalAnswerStep):
|
| 114 |
final_answer = st.output
|
| 115 |
history.append({"question": question, "answer": final_answer})
|
|
@@ -123,13 +130,6 @@ def answer_question(question, history):
|
|
| 123 |
return "Request cancelled","Submit new request", ""
|
| 124 |
|
| 125 |
|
| 126 |
-
|
| 127 |
-
# def create_rag_files(refs :list[str], VECTOR_DB_PATH:str)-> str:
|
| 128 |
-
# from tool_create_FAISS_vector import create_vector_store_from_list_of_doi
|
| 129 |
-
|
| 130 |
-
# FAISS_VECTOR_PATH = create_vector_store_from_list_of_doi(refs,VECTOR_DB_PATH)
|
| 131 |
-
# return FAISS_VECTOR_PATH
|
| 132 |
-
|
| 133 |
def tool_clinical_trial(query_cond:str=None, query_term:str=None,query_lead:str=None,max_results: str="5") -> str:
|
| 134 |
"""
|
| 135 |
Search Clinical Trials database for trials with 4 arguments.
|
|
@@ -196,6 +196,7 @@ def create_rag(refs :str, VECTOR_DB_PATH:str)-> str:
|
|
| 196 |
"""
|
| 197 |
from tool_create_FAISS_vector import create_vector_store_from_list_of_doi
|
| 198 |
FAISS_VECTOR_PATH = create_vector_store_from_list_of_doi(refs,VECTOR_DB_PATH)
|
|
|
|
| 199 |
return FAISS_VECTOR_PATH
|
| 200 |
|
| 201 |
|
|
@@ -207,11 +208,11 @@ def use_rag(query: str, store_name: str, top_k: int = 5) -> str:
|
|
| 207 |
store_name (str): The path to the FAISS vector store to query.
|
| 208 |
top_k (int): The number of top-k most relevant context documents to retrieve (default: 5).
|
| 209 |
Returns:
|
| 210 |
-
str: A
|
| 211 |
"""
|
| 212 |
from tool_query_FAISS_vector import query_vector_store
|
| 213 |
context_as_dict = query_vector_store(query, store_name, top_k)
|
| 214 |
-
return
|
| 215 |
|
| 216 |
|
| 217 |
def describe_figure(image : Image.Image) -> str:
|
|
@@ -230,14 +231,6 @@ def describe_figure(image : Image.Image) -> str:
|
|
| 230 |
# Create neat interface - Question Analyzer as a Blocks component
|
| 231 |
with gr.Blocks() as interface2:
|
| 232 |
gr.Markdown("# Question Analyzer")
|
| 233 |
-
# gr.Markdown("""Enter a question to analyze. Examples:
|
| 234 |
-
# - Find the name of the sponsor that did the most studies on Alzheimer's disease in the last 10 years.
|
| 235 |
-
# - Provide a summary of recent clinical trials on diabetes and list 3 relevant research articles from PubMed.
|
| 236 |
-
# - What are the scientific paper linked to the clinical study referenced as NCT04516746?
|
| 237 |
-
# - How many clinical studies on cancer were completed in the last 5 years?
|
| 238 |
-
# - Find recent phase 3 trials for lung cancer sponsored by Pfizer
|
| 239 |
-
# """)
|
| 240 |
-
|
| 241 |
with gr.Row():
|
| 242 |
with gr.Column():
|
| 243 |
question_input = gr.Textbox(
|
|
@@ -246,9 +239,9 @@ with gr.Blocks() as interface2:
|
|
| 246 |
lines=3,
|
| 247 |
)
|
| 248 |
gr.Examples(["What is the weather in LA?",
|
| 249 |
-
"What are the
|
| 250 |
-
"How many
|
| 251 |
-
"
|
| 252 |
with gr.Row():
|
| 253 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 254 |
stop_btn = gr.Button("Stop", variant="secondary")
|
|
@@ -334,7 +327,8 @@ with gr.Blocks() as interface3:
|
|
| 334 |
inputs=[ref_input, vector_name_input]
|
| 335 |
)
|
| 336 |
path_output = gr.Textbox(
|
| 337 |
-
label="Path of the vector store"
|
|
|
|
| 338 |
)
|
| 339 |
submit_btn = gr.Button("Create Vector Store")
|
| 340 |
submit_btn.click(
|
|
@@ -353,7 +347,7 @@ demo = gr.TabbedInterface(
|
|
| 353 |
fn=use_rag,
|
| 354 |
inputs=[gr.Textbox(label="Question that needs context to answer", placeholder="What is the dose of medicine to gove an infant under type2 diabetes"),
|
| 355 |
gr.Textbox(label="Name of the vector store to use", placeholder="Diabetes, Sickel_cell_anemia, Prostate_cancer, ..")],
|
| 356 |
-
outputs=gr.Textbox(label="Answer with Rag",placeholder="Your answer will be provided here"),
|
| 357 |
api_name="use_vector_store_to_create_context"),
|
| 358 |
gr.Interface(
|
| 359 |
fn=tool_clinical_trial,
|
|
|
|
| 83 |
question_with_history = "Conversation history:\n" + str(history) + "\n\nNew user question:\n " + question
|
| 84 |
for st in safe_agent.run(question_with_history,stream=True,return_full_result=True):
|
| 85 |
if isinstance(st, smolagents.memory.PlanningStep):
|
| 86 |
+
plan = 20*"# " + "\n# Planning of manager agent" + st.plan.split("## 2. Plan")[-1]
|
| 87 |
for m in plan.split("\n"):
|
| 88 |
thoughts += "\n" + m
|
| 89 |
yield thoughts, final_answer, history
|
|
|
|
| 103 |
yield thoughts, final_answer, history
|
| 104 |
|
| 105 |
elif isinstance(st, smolagents.memory.ActionStep):
|
| 106 |
+
|
| 107 |
+
for chatmessage in st.model_input_messages:
|
| 108 |
+
if chatmessage.role == "assistant":
|
| 109 |
+
managed_agent_plan = chatmessage.content[0]['text'].split("2. Plan")[-1]
|
| 110 |
+
thoughts += "Managed agent plan:\n"
|
| 111 |
+
for l in managed_agent_plan.split("\n"):
|
| 112 |
+
thoughts += l
|
| 113 |
+
thoughts += "\n\n--> Code action from managed agent \n" + st.code_action +"\n\n"
|
| 114 |
yield thoughts, final_answer, history
|
| 115 |
+
thoughts += "\n********** End fo Step " + str(st.step_number) + " : *********\n" + str(st.token_usage) + "\nStep duration" + str(st.timing) + "\n\n"
|
|
|
|
| 116 |
yield thoughts, final_answer, history
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
elif isinstance(st, smolagents.memory.FinalAnswerStep):
|
| 121 |
final_answer = st.output
|
| 122 |
history.append({"question": question, "answer": final_answer})
|
|
|
|
| 130 |
return "Request cancelled","Submit new request", ""
|
| 131 |
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
def tool_clinical_trial(query_cond:str=None, query_term:str=None,query_lead:str=None,max_results: str="5") -> str:
|
| 134 |
"""
|
| 135 |
Search Clinical Trials database for trials with 4 arguments.
|
|
|
|
| 196 |
"""
|
| 197 |
from tool_create_FAISS_vector import create_vector_store_from_list_of_doi
|
| 198 |
FAISS_VECTOR_PATH = create_vector_store_from_list_of_doi(refs,VECTOR_DB_PATH)
|
| 199 |
+
|
| 200 |
return FAISS_VECTOR_PATH
|
| 201 |
|
| 202 |
|
|
|
|
| 208 |
store_name (str): The path to the FAISS vector store to query.
|
| 209 |
top_k (int): The number of top-k most relevant context documents to retrieve (default: 5).
|
| 210 |
Returns:
|
| 211 |
+
str: A TOON formated string containing the retrieved contexts, including the contents, the source and the scores.
|
| 212 |
"""
|
| 213 |
from tool_query_FAISS_vector import query_vector_store
|
| 214 |
context_as_dict = query_vector_store(query, store_name, top_k)
|
| 215 |
+
return str(context_as_dict)
|
| 216 |
|
| 217 |
|
| 218 |
def describe_figure(image : Image.Image) -> str:
|
|
|
|
| 231 |
# Create neat interface - Question Analyzer as a Blocks component
|
| 232 |
with gr.Blocks() as interface2:
|
| 233 |
gr.Markdown("# Question Analyzer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
with gr.Row():
|
| 235 |
with gr.Column():
|
| 236 |
question_input = gr.Textbox(
|
|
|
|
| 239 |
lines=3,
|
| 240 |
)
|
| 241 |
gr.Examples(["What is the weather in LA?",
|
| 242 |
+
"What are the 5 most recent clinical study sponsored by Merck?",
|
| 243 |
+
"How many trials were completed in 2025 by AbbVie?",
|
| 244 |
+
"What are the pmids associated with the study NCT04516746?",],question_input)
|
| 245 |
with gr.Row():
|
| 246 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 247 |
stop_btn = gr.Button("Stop", variant="secondary")
|
|
|
|
| 327 |
inputs=[ref_input, vector_name_input]
|
| 328 |
)
|
| 329 |
path_output = gr.Textbox(
|
| 330 |
+
label="Path of the vector store",
|
| 331 |
+
lines=4
|
| 332 |
)
|
| 333 |
submit_btn = gr.Button("Create Vector Store")
|
| 334 |
submit_btn.click(
|
|
|
|
| 347 |
fn=use_rag,
|
| 348 |
inputs=[gr.Textbox(label="Question that needs context to answer", placeholder="What is the dose of medicine to gove an infant under type2 diabetes"),
|
| 349 |
gr.Textbox(label="Name of the vector store to use", placeholder="Diabetes, Sickel_cell_anemia, Prostate_cancer, ..")],
|
| 350 |
+
outputs=gr.Textbox(label="Answer with Rag",lines=8, placeholder="Your answer will be provided here"),
|
| 351 |
api_name="use_vector_store_to_create_context"),
|
| 352 |
gr.Interface(
|
| 353 |
fn=tool_clinical_trial,
|
test_app.ipynb
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
test_nebius_mode.ipynb
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "371b903a",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"Available models:\n",
|
| 14 |
+
"- meta-llama/Meta-Llama-3.1-8B-Instruct-fast\n",
|
| 15 |
+
"- meta-llama/Meta-Llama-3.1-8B-Instruct\n",
|
| 16 |
+
"- meta-llama/Llama-Guard-3-8B\n",
|
| 17 |
+
"- nvidia/Llama-3_1-Nemotron-Ultra-253B-v1\n",
|
| 18 |
+
"- nvidia/Nemotron-Nano-V2-12b\n",
|
| 19 |
+
"- google/gemma-2-2b-it\n",
|
| 20 |
+
"- google/gemma-2-9b-it-fast\n",
|
| 21 |
+
"- Qwen/Qwen2.5-Coder-7B-fast\n",
|
| 22 |
+
"- BAAI/bge-en-icl\n",
|
| 23 |
+
"- BAAI/bge-multilingual-gemma2\n",
|
| 24 |
+
"- intfloat/e5-mistral-7b-instruct\n",
|
| 25 |
+
"- meta-llama/Llama-3.3-70B-Instruct\n",
|
| 26 |
+
"- meta-llama/Llama-3.3-70B-Instruct-fast\n",
|
| 27 |
+
"- deepseek-ai/DeepSeek-R1-0528\n",
|
| 28 |
+
"- Qwen/Qwen3-235B-A22B-Instruct-2507\n",
|
| 29 |
+
"- Qwen/Qwen3-235B-A22B-Thinking-2507\n",
|
| 30 |
+
"- Qwen/Qwen3-32B\n",
|
| 31 |
+
"- Qwen/Qwen3-32B-fast\n",
|
| 32 |
+
"- google/gemma-3-27b-it\n",
|
| 33 |
+
"- google/gemma-3-27b-it-fast\n",
|
| 34 |
+
"- Qwen/Qwen2.5-VL-72B-Instruct\n",
|
| 35 |
+
"- Qwen/Qwen3-Embedding-8B\n",
|
| 36 |
+
"- deepseek-ai/DeepSeek-V3-0324\n",
|
| 37 |
+
"- zai-org/GLM-4.5\n",
|
| 38 |
+
"- zai-org/GLM-4.5-Air\n",
|
| 39 |
+
"- openai/gpt-oss-120b\n",
|
| 40 |
+
"- openai/gpt-oss-20b\n",
|
| 41 |
+
"- deepseek-ai/DeepSeek-R1-0528-fast\n",
|
| 42 |
+
"- deepseek-ai/DeepSeek-V3-0324-fast\n",
|
| 43 |
+
"- moonshotai/Kimi-K2-Instruct\n",
|
| 44 |
+
"- Qwen/Qwen3-30B-A3B-Thinking-2507\n",
|
| 45 |
+
"- Qwen/Qwen3-30B-A3B-Instruct-2507\n",
|
| 46 |
+
"- Qwen/Qwen3-Coder-30B-A3B-Instruct\n",
|
| 47 |
+
"- Qwen/Qwen3-Coder-480B-A35B-Instruct\n",
|
| 48 |
+
"- NousResearch/Hermes-4-70B\n",
|
| 49 |
+
"- NousResearch/Hermes-4-405B\n",
|
| 50 |
+
"- PrimeIntellect/INTELLECT-3\n",
|
| 51 |
+
"- moonshotai/Kimi-K2-Thinking\n",
|
| 52 |
+
"- black-forest-labs/flux-dev\n",
|
| 53 |
+
"- black-forest-labs/flux-schnell\n"
|
| 54 |
+
]
|
| 55 |
+
}
|
| 56 |
+
],
|
| 57 |
+
"source": [
|
| 58 |
+
"import requests\n",
|
| 59 |
+
"import os\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"api_key = os.environ.get(\"NEBIUS_API_KEY\")\n",
|
| 62 |
+
"headers = {\n",
|
| 63 |
+
" \"Authorization\": f\"Bearer {api_key}\",\n",
|
| 64 |
+
" \"Content-Type\": \"application/json\"\n",
|
| 65 |
+
"}\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"# Try the models endpoint\n",
|
| 68 |
+
"response = requests.get(\n",
|
| 69 |
+
" \"https://api.tokenfactory.nebius.com/v1/models\",\n",
|
| 70 |
+
" headers=headers\n",
|
| 71 |
+
")\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"if response.status_code == 200:\n",
|
| 74 |
+
" models = response.json()\n",
|
| 75 |
+
" print(\"Available models:\")\n",
|
| 76 |
+
" for model in models.get('data', []):\n",
|
| 77 |
+
" print(f\"- {model.get('id')}\")\n",
|
| 78 |
+
"else:\n",
|
| 79 |
+
" print(f\"Error: {response.status_code}\")\n",
|
| 80 |
+
" print(response.text)"
|
| 81 |
+
]
|
| 82 |
+
}
|
| 83 |
+
],
|
| 84 |
+
"metadata": {
|
| 85 |
+
"kernelspec": {
|
| 86 |
+
"display_name": "mcp-birthday",
|
| 87 |
+
"language": "python",
|
| 88 |
+
"name": "python3"
|
| 89 |
+
},
|
| 90 |
+
"language_info": {
|
| 91 |
+
"codemirror_mode": {
|
| 92 |
+
"name": "ipython",
|
| 93 |
+
"version": 3
|
| 94 |
+
},
|
| 95 |
+
"file_extension": ".py",
|
| 96 |
+
"mimetype": "text/x-python",
|
| 97 |
+
"name": "python",
|
| 98 |
+
"nbconvert_exporter": "python",
|
| 99 |
+
"pygments_lexer": "ipython3",
|
| 100 |
+
"version": "3.10.19"
|
| 101 |
+
}
|
| 102 |
+
},
|
| 103 |
+
"nbformat": 4,
|
| 104 |
+
"nbformat_minor": 5
|
| 105 |
+
}
|
test_query_FAISS.ipynb
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tool_create_FAISS_vector.py
CHANGED
|
@@ -369,9 +369,9 @@ from transformers import AutoTokenizer
|
|
| 369 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 370 |
from tqdm import tqdm
|
| 371 |
|
| 372 |
-
def create_vector_store_from_list_of_doi(refs :str,
|
| 373 |
|
| 374 |
-
VECTOR_DB_PATH = "./tmp/vector_stores/" +
|
| 375 |
|
| 376 |
from langchain_community.vectorstores import FAISS
|
| 377 |
|
|
@@ -434,10 +434,25 @@ def create_vector_store_from_list_of_doi(refs :str, VECTOR_DB_PATH:str) -> str:
|
|
| 434 |
print("merge vector store")
|
| 435 |
KNOWLEDGE_VECTOR_DATABASE.merge_from(NEW_KNOWLEDGE_VECTOR_DATABASE)
|
| 436 |
KNOWLEDGE_VECTOR_DATABASE.save_local(VECTOR_DB_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
else:
|
| 438 |
NEW_KNOWLEDGE_VECTOR_DATABASE.save_local(VECTOR_DB_PATH)
|
| 439 |
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
else:
|
| 443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 370 |
from tqdm import tqdm
|
| 371 |
|
| 372 |
+
def create_vector_store_from_list_of_doi(refs :str, VECTOR_DB_NAME:str) -> str:
|
| 373 |
|
| 374 |
+
VECTOR_DB_PATH = "./tmp/vector_stores/" + VECTOR_DB_NAME
|
| 375 |
|
| 376 |
from langchain_community.vectorstores import FAISS
|
| 377 |
|
|
|
|
| 434 |
print("merge vector store")
|
| 435 |
KNOWLEDGE_VECTOR_DATABASE.merge_from(NEW_KNOWLEDGE_VECTOR_DATABASE)
|
| 436 |
KNOWLEDGE_VECTOR_DATABASE.save_local(VECTOR_DB_PATH)
|
| 437 |
+
vector_type={"name":VECTOR_DB_NAME,
|
| 438 |
+
"num_vectors" : KNOWLEDGE_VECTOR_DATABASE.index.ntotal,
|
| 439 |
+
"vector_dim" : KNOWLEDGE_VECTOR_DATABASE.index.d,
|
| 440 |
+
"distance_strategy" : KNOWLEDGE_VECTOR_DATABASE.distance_strategy}
|
| 441 |
else:
|
| 442 |
NEW_KNOWLEDGE_VECTOR_DATABASE.save_local(VECTOR_DB_PATH)
|
| 443 |
|
| 444 |
+
vector_type={"name":VECTOR_DB_NAME,
|
| 445 |
+
"num_vectors" : NEW_KNOWLEDGE_VECTOR_DATABASE.index.ntotal,
|
| 446 |
+
"vector_dim" : NEW_KNOWLEDGE_VECTOR_DATABASE.index.d,
|
| 447 |
+
"distance_strategy" : NEW_KNOWLEDGE_VECTOR_DATABASE.distance_strategy}
|
| 448 |
+
return str(vector_type)
|
| 449 |
|
| 450 |
else:
|
| 451 |
+
if KNOWLEDGE_VECTOR_DATABASE:
|
| 452 |
+
vector_type={"name":VECTOR_DB_NAME,
|
| 453 |
+
"num_vectors" : KNOWLEDGE_VECTOR_DATABASE.index.ntotal,
|
| 454 |
+
"vector_dim" : KNOWLEDGE_VECTOR_DATABASE.index.d,
|
| 455 |
+
"distance_strategy" : KNOWLEDGE_VECTOR_DATABASE.distance_strategy}
|
| 456 |
+
return str(vector_type)
|
| 457 |
+
else:
|
| 458 |
+
return f"could not extract new ref for this vector store"
|
tool_query_FAISS_vector.py
CHANGED
|
@@ -44,14 +44,17 @@ def query_vector_store(query: str, store_name: str, top_k: int = 5) -> dict:
|
|
| 44 |
embedding_model,
|
| 45 |
allow_dangerous_deserialization=True
|
| 46 |
)
|
| 47 |
-
|
| 48 |
-
results = vector_store.similarity_search(query, top_k)
|
| 49 |
-
|
| 50 |
-
context = [r.page_content for r in results]
|
| 51 |
-
sources = [r.metadata["source"] for r in results]
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
"context": context,
|
| 55 |
"sources": sources,
|
| 56 |
-
"
|
| 57 |
-
|
|
|
|
|
|
|
|
|
| 44 |
embedding_model,
|
| 45 |
allow_dangerous_deserialization=True
|
| 46 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
results = vector_store.similarity_search_with_score(query, top_k)
|
| 49 |
+
|
| 50 |
+
context = [r[0].page_content for r in results][::-1]
|
| 51 |
+
sources = [r[0].metadata["source"] for r in results][::-1]
|
| 52 |
+
scores = [r[1] for r in results][::-1]
|
| 53 |
+
|
| 54 |
+
dict_ = {
|
| 55 |
"context": context,
|
| 56 |
"sources": sources,
|
| 57 |
+
"scores": scores,
|
| 58 |
+
"store_name": "s"
|
| 59 |
+
}
|
| 60 |
+
return dict_
|