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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain.chat_models import init_chat_model
|
| 3 |
+
from typing import Literal
|
| 4 |
+
from tavily import TavilyClient
|
| 5 |
+
from deepagents import create_deep_agent
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
tavily_client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])
|
| 9 |
+
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
|
| 10 |
+
|
| 11 |
+
def internet_search(
|
| 12 |
+
query: str,
|
| 13 |
+
max_results: int = 5,
|
| 14 |
+
topic: Literal["general", "news", "finance"] = "general",
|
| 15 |
+
include_raw_content: bool = False,
|
| 16 |
+
):
|
| 17 |
+
"""Run a web search"""
|
| 18 |
+
return tavily_client.search(
|
| 19 |
+
query,
|
| 20 |
+
max_results=max_results,
|
| 21 |
+
include_raw_content=include_raw_content,
|
| 22 |
+
topic=topic,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
book_subagent = {
|
| 26 |
+
"name": "book-agent",
|
| 27 |
+
"description": "Handles questions about books, authors, publishing, literary awards, and reading recommendations.",
|
| 28 |
+
"system_prompt": """
|
| 29 |
+
You are a specialized book research assistant.
|
| 30 |
+
|
| 31 |
+
Responsibilities:
|
| 32 |
+
- Search for accurate and up-to-date information about books.
|
| 33 |
+
- Verify publication dates, authors, summaries, awards, and editions.
|
| 34 |
+
- Use the internet_search tool when factual verification is required.
|
| 35 |
+
|
| 36 |
+
Rules:
|
| 37 |
+
- Always prioritize factual accuracy.
|
| 38 |
+
- Use the tool when the answer depends on real-world data.
|
| 39 |
+
- Summarize findings clearly and concisely.
|
| 40 |
+
- Do NOT add opinions unless explicitly requested.
|
| 41 |
+
- Do NOT ask follow-up questions.
|
| 42 |
+
- Return only the answer.
|
| 43 |
+
""",
|
| 44 |
+
"tools": [internet_search],
|
| 45 |
+
"model": "mistral-large-latest"
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
film_subagent = {
|
| 49 |
+
"name": "film-agent",
|
| 50 |
+
"description": "Handles questions about movies, actors, directors, release dates, box office, and streaming availability.",
|
| 51 |
+
"system_prompt": """
|
| 52 |
+
You are a specialized film research assistant.
|
| 53 |
+
|
| 54 |
+
Responsibilities:
|
| 55 |
+
- Search for accurate and current information about films.
|
| 56 |
+
- Verify cast, crew, release dates, ratings, box office, and streaming platforms.
|
| 57 |
+
- Use the internet_search tool when information must be verified.
|
| 58 |
+
|
| 59 |
+
Rules:
|
| 60 |
+
- Prefer tool usage over memory for factual details.
|
| 61 |
+
- Keep responses concise and factual.
|
| 62 |
+
- Do NOT speculate.
|
| 63 |
+
- Do NOT add recommendations unless asked.
|
| 64 |
+
- Do NOT ask follow-up questions.
|
| 65 |
+
- Return only the answer.
|
| 66 |
+
""",
|
| 67 |
+
"tools": [internet_search],
|
| 68 |
+
"model": "mistral-large-latest"
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
food_subagent = {
|
| 72 |
+
"name": "food-agent",
|
| 73 |
+
"description": "Handles questions about food, recipes, ingredients, nutrition, restaurants, and culinary topics.",
|
| 74 |
+
"system_prompt": """
|
| 75 |
+
You are a specialized food research assistant.
|
| 76 |
+
|
| 77 |
+
Responsibilities:
|
| 78 |
+
- Provide accurate information about dishes, ingredients, nutrition facts, and restaurants.
|
| 79 |
+
- Use internet_search for current restaurant data, pricing, or trends.
|
| 80 |
+
- Verify nutritional or location-based details when needed.
|
| 81 |
+
|
| 82 |
+
Rules:
|
| 83 |
+
- Be concise and factual.
|
| 84 |
+
- Avoid unnecessary commentary.
|
| 85 |
+
- Do NOT ask follow-up questions.
|
| 86 |
+
- Do NOT provide cooking tips unless explicitly requested.
|
| 87 |
+
- Return only the answer.
|
| 88 |
+
""",
|
| 89 |
+
"tools": [internet_search],
|
| 90 |
+
"model": "mistral-large-latest"
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
subagents = [book_subagent, film_subagent, food_subagent]
|
| 94 |
+
|
| 95 |
+
# System prompt to steer the agent to be an expert researcher
|
| 96 |
+
instructions = """
|
| 97 |
+
You are the main orchestration agent.
|
| 98 |
+
|
| 99 |
+
Your role:
|
| 100 |
+
- Understand the user question.
|
| 101 |
+
- Decide if it belongs to books, films, or food.
|
| 102 |
+
- Delegate to the appropriate subagent when internet lookup is needed.
|
| 103 |
+
- Return a concise, accurate, fully summarized final answer.
|
| 104 |
+
|
| 105 |
+
Rules:
|
| 106 |
+
- Always prefer subagents when the question requires factual, up-to-date, or specific information.
|
| 107 |
+
- Do NOT answer from memory if current data may be required.
|
| 108 |
+
- Do NOT expose internal reasoning or mention subagents.
|
| 109 |
+
- Do NOT ask follow-up questions.
|
| 110 |
+
- Do NOT add suggestions or extra commentary.
|
| 111 |
+
- Keep the answer direct and to the point.
|
| 112 |
+
- If information is unavailable, state that clearly.
|
| 113 |
+
|
| 114 |
+
Output Style:
|
| 115 |
+
- Plain text only.
|
| 116 |
+
- No preamble.
|
| 117 |
+
- No bullet points unless the question explicitly asks for a list.
|
| 118 |
+
- Maximum clarity and brevity.
|
| 119 |
+
"""
|
| 120 |
+
|
| 121 |
+
agent = create_deep_agent(
|
| 122 |
+
model="mistral-large-latest",
|
| 123 |
+
system_prompt=instructions,
|
| 124 |
+
subagents=subagents
|
| 125 |
+
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# ---- Single Turn Function ----
|
| 129 |
+
def chat_with_agent(user_input):
|
| 130 |
+
result = agent.invoke(
|
| 131 |
+
{
|
| 132 |
+
"messages": [
|
| 133 |
+
{"role": "user", "content": user_input}
|
| 134 |
+
]
|
| 135 |
+
}
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
return result["messages"][-1].content
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# ---- UI ----
|
| 142 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 143 |
+
|
| 144 |
+
gr.Markdown("## 📚🎬🍳 Read-Watch-Eat - Smart Multi-Agent Assistant")
|
| 145 |
+
gr.Markdown(
|
| 146 |
+
"Ask about books, films, or food. The system automatically chooses the correct specialist agent."
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
with gr.Row():
|
| 150 |
+
input_text = gr.Textbox(
|
| 151 |
+
placeholder="It's a snowy morning in Japan, suggest a good book and energetic breakfast.",
|
| 152 |
+
label="Your Question",
|
| 153 |
+
lines=2
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
output_text = gr.Textbox(
|
| 157 |
+
label="Answer",
|
| 158 |
+
lines=6,
|
| 159 |
+
interactive=False
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
submit_btn = gr.Button("Submit")
|
| 163 |
+
clear_btn = gr.Button("Clear")
|
| 164 |
+
|
| 165 |
+
submit_btn.click(
|
| 166 |
+
fn=chat_with_agent,
|
| 167 |
+
inputs=input_text,
|
| 168 |
+
outputs=output_text
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
clear_btn.click(
|
| 172 |
+
lambda: ("", ""),
|
| 173 |
+
outputs=[input_text, output_text]
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
demo.launch(share = True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
langchain
|
| 4 |
+
tavily
|
| 5 |
+
langgraph
|
| 6 |
+
langchain_community
|
| 7 |
+
langchain_mistralai
|
| 8 |
+
mistralai
|
| 9 |
+
typing_extensions
|