Spaces:
Sleeping
Sleeping
File size: 2,120 Bytes
b3cdb41 | 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 | import os
from dotenv import load_dotenv
from langchain_groq import ChatGroq
# -----------------------------------
# Load environment variables
# -----------------------------------
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
raise ValueError(
"Missing GROQ_API_KEY"
)
# -----------------------------------
# Shared LLM
# -----------------------------------
llm = ChatGroq(
groq_api_key=GROQ_API_KEY,
model_name="llama-3.1-8b-instant",
temperature=0.3,
max_tokens=500
)
# -----------------------------------
# Research Agent
# -----------------------------------
def research_agent(topic):
prompt = f"""
You are a Research Agent.
Research the following topic deeply:
Topic:
{topic}
Provide:
- key facts
- important concepts
- useful insights
"""
response = llm.invoke(prompt)
return response.content
# -----------------------------------
# Planner Agent
# -----------------------------------
def planner_agent(topic):
prompt = f"""
You are a Planner Agent.
Create a step-by-step action plan for:
{topic}
Make the plan practical and structured.
"""
response = llm.invoke(prompt)
return response.content
# -----------------------------------
# Summary Agent
# -----------------------------------
def summary_agent(research, plan):
prompt = f"""
You are a Summary Agent.
Summarize the following research and plan.
RESEARCH:
{research}
PLAN:
{plan}
Produce:
- concise summary
- key takeaways
- final recommendations
"""
response = llm.invoke(prompt)
return response.content
# -----------------------------------
# Coordinator Agent
# -----------------------------------
def run_multi_agent_system(user_query):
# Step 1
research = research_agent(user_query)
# Step 2
plan = planner_agent(user_query)
# Step 3
summary = summary_agent(
research,
plan
)
return {
"research": research,
"plan": plan,
"summary": summary
} |