Spaces:
Sleeping
Sleeping
Update app/MultiAgent.py
Browse files- app/MultiAgent.py +187 -187
app/MultiAgent.py
CHANGED
|
@@ -1,187 +1,187 @@
|
|
| 1 |
-
from agents import Agent, ModelSettings, Runner, RunConfig,OpenAIResponsesModel ,AsyncOpenAI,function_tool,OpenAIChatCompletionsModel
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from datetime import datetime, date
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
load_dotenv(dotenv_path="./.env.local")
|
| 6 |
-
import asyncio
|
| 7 |
-
from typing import List
|
| 8 |
-
from .VectorDBManagers import VectorDBManager
|
| 9 |
-
from chatkit.agents import AgentContext
|
| 10 |
-
import os
|
| 11 |
-
import nest_asyncio
|
| 12 |
-
nest_asyncio.apply()
|
| 13 |
-
from .function_tool import suggestion_ragtool
|
| 14 |
-
kimi_model = OpenAIResponsesModel(
|
| 15 |
-
model="
|
| 16 |
-
openai_client=AsyncOpenAI(
|
| 17 |
-
base_url="https://api.groq.com/openai/v1",
|
| 18 |
-
api_key=os.getenv("GROQ_API_KEY"),
|
| 19 |
-
)
|
| 20 |
-
)
|
| 21 |
-
google_model = OpenAIChatCompletionsModel(
|
| 22 |
-
model="google/gemini-2.5-flash", # Google Gemini via OpenRouter
|
| 23 |
-
openai_client=AsyncOpenAI(
|
| 24 |
-
base_url="https://openrouter.ai/api/v1",
|
| 25 |
-
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 26 |
-
)
|
| 27 |
-
)
|
| 28 |
-
deepseek_model = OpenAIChatCompletionsModel(
|
| 29 |
-
model="deepseek/deepseek-chat", # DeepSeek via OpenRouter
|
| 30 |
-
openai_client=AsyncOpenAI(
|
| 31 |
-
base_url="https://openrouter.ai/api/v1",
|
| 32 |
-
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 33 |
-
)
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
sumary_model = OpenAIResponsesModel(
|
| 37 |
-
model="
|
| 38 |
-
openai_client=AsyncOpenAI(
|
| 39 |
-
base_url="https://api.groq.com/openai/v1",
|
| 40 |
-
api_key=os.getenv("GROQ_API_KEY")
|
| 41 |
-
)
|
| 42 |
-
)
|
| 43 |
-
def build_sugguestion_information_agent()-> Agent[AgentContext]:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
current_time = datetime.now()
|
| 47 |
-
current_date = date.today()
|
| 48 |
-
current_day = datetime.today().strftime("%A")
|
| 49 |
-
information_agent = Agent[AgentContext](
|
| 50 |
-
name="company_suggestion_information",
|
| 51 |
-
instructions=(
|
| 52 |
-
"You are an information agent and customer service representative for the company. "
|
| 53 |
-
"Your goal is to provide clear, concise answers using ONLY the suggestion_ragtool tool. "
|
| 54 |
-
"For greeting do not call tool reply by your self add company name "
|
| 55 |
-
"Always speak as the company using 'we'. Do NOT guess or assume. "
|
| 56 |
-
"If information is not found, reply politely: "
|
| 57 |
-
"phraphse according to your intellgence 'No information is available regarding to this . You may book an appointment or speak to our sales agent for more details.' praphrase it "
|
| 58 |
-
|
| 59 |
-
"Make only ONE suggestion_ragtool query that fully represents the user’s request. "
|
| 60 |
-
"All company-related answers must come strictly from the suggestion_ragtool tool. "
|
| 61 |
-
"Do not create or assume any details. Always use the correct company name. "
|
| 62 |
-
"The suggestion_ragtool result is your official answer. "
|
| 63 |
-
|
| 64 |
-
"Respond in under not more than 80 words, in a friendly customer-service tone. "
|
| 65 |
-
"Never leave incomplete replies and never ignore earlier conversation context. "
|
| 66 |
-
|
| 67 |
-
"As a customer support agent, always answer using official company information found through the suggestion_ragtool tool. for partcular question liek greeting and user info if you have so do not use tool reply by self "
|
| 68 |
-
f"Current system time : {current_time}, date: {current_date}, day: {current_day}. "
|
| 69 |
-
),
|
| 70 |
-
model=deepseek_model,
|
| 71 |
-
tools=[
|
| 72 |
-
suggestion_ragtool
|
| 73 |
-
],
|
| 74 |
-
model_settings=ModelSettings(
|
| 75 |
-
temperature=1,
|
| 76 |
-
top_p=1,
|
| 77 |
-
max_tokens=2048,
|
| 78 |
-
),
|
| 79 |
-
)
|
| 80 |
-
return information_agent
|
| 81 |
-
|
| 82 |
-
def build_summarizer_agent() -> Agent[AgentContext]:
|
| 83 |
-
"""
|
| 84 |
-
Creates a summarizer agent that condenses chat history
|
| 85 |
-
into a short, factual summary for context preservation.
|
| 86 |
-
"""
|
| 87 |
-
summarizer_agent = Agent[AgentContext](
|
| 88 |
-
name="Summarizer Agent",
|
| 89 |
-
instructions="""
|
| 90 |
-
You are a summarization assistant.
|
| 91 |
-
Your job is to take several user and assistant messages and produce a concise,
|
| 92 |
-
factual summary that captures key intents, facts, and outcomes.
|
| 93 |
-
|
| 94 |
-
Guidelines:
|
| 95 |
-
- Keep the summary under 80 words.
|
| 96 |
-
- Focus on what the user is asking for and the assistant's key responses.
|
| 97 |
-
- Do NOT add new information.
|
| 98 |
-
- Preserve important context like customer concerns, preferences, or goals.
|
| 99 |
-
- Write in plain English.
|
| 100 |
-
""",
|
| 101 |
-
model=sumary_model, # or use default_model if configured in your environment
|
| 102 |
-
model_settings=ModelSettings(
|
| 103 |
-
temperature=0.3,
|
| 104 |
-
top_p=0.9,
|
| 105 |
-
max_tokens=300,
|
| 106 |
-
),
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
return summarizer_agent
|
| 110 |
-
def build_kimi_information_agent()-> Agent[AgentContext]:
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
current_time = datetime.now()
|
| 114 |
-
current_date = date.today()
|
| 115 |
-
current_day = datetime.today().strftime("%A")
|
| 116 |
-
information_agent = Agent[AgentContext](
|
| 117 |
-
name="company_suggestion_information",
|
| 118 |
-
instructions=(
|
| 119 |
-
"You are an information agent and customer service representative for the company. "
|
| 120 |
-
"Your goal is to provide clear, concise answers using ONLY the suggestion_ragtool tool. "
|
| 121 |
-
"For greeting do not call tool reply by your self add company name "
|
| 122 |
-
"Always speak as the company using 'we'. Do NOT guess or assume. "
|
| 123 |
-
"If information is not found, reply politely: "
|
| 124 |
-
"phraphse according to your intellgence 'No information is available regarding to this . You may book an appointment or speak to our sales agent for more details.' praphrase it "
|
| 125 |
-
|
| 126 |
-
"Make only ONE suggestion_ragtool query that fully represents the user’s request. "
|
| 127 |
-
"All company-related answers must come strictly from the suggestion_ragtool tool. "
|
| 128 |
-
"Do not create or assume any details. Always use the correct company name. "
|
| 129 |
-
"The suggestion_ragtool result is your official answer. "
|
| 130 |
-
|
| 131 |
-
"Respond in under not more than 80 words, in a friendly customer-service tone. "
|
| 132 |
-
"Never leave incomplete replies and never ignore earlier conversation context. "
|
| 133 |
-
|
| 134 |
-
"As a customer support agent, always answer using official company information found through the suggestion_ragtool tool. for partcular question liek greeting and user info if you have so do not use tool reply by self "
|
| 135 |
-
f"Current system time : {current_time}, date: {current_date}, day: {current_day}. "
|
| 136 |
-
),
|
| 137 |
-
model=kimi_model,
|
| 138 |
-
tools=[
|
| 139 |
-
suggestion_ragtool
|
| 140 |
-
],
|
| 141 |
-
model_settings=ModelSettings(
|
| 142 |
-
temperature=1,
|
| 143 |
-
top_p=1,
|
| 144 |
-
max_tokens=2048,
|
| 145 |
-
),
|
| 146 |
-
)
|
| 147 |
-
return information_agent
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
def build_google_information_agent()-> Agent[AgentContext]:
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
current_time = datetime.now()
|
| 154 |
-
current_date = date.today()
|
| 155 |
-
current_day = datetime.today().strftime("%A")
|
| 156 |
-
information_agent = Agent[AgentContext](
|
| 157 |
-
name="company_suggestion_information",
|
| 158 |
-
instructions=(
|
| 159 |
-
"You are an information agent and customer service representative for the company. "
|
| 160 |
-
"Your goal is to provide clear, concise answers using ONLY the suggestion_ragtool tool. "
|
| 161 |
-
"For greeting do not call tool reply by your self add company name "
|
| 162 |
-
"Always speak as the company using 'we'. Do NOT guess or assume. "
|
| 163 |
-
"If information is not found, reply politely: "
|
| 164 |
-
"phraphse according to your intellgence 'No information is available regarding to this . You may book an appointment or speak to our sales agent for more details.' praphrase it "
|
| 165 |
-
|
| 166 |
-
"Make only ONE suggestion_ragtool query that fully represents the user’s request. "
|
| 167 |
-
"All company-related answers must come strictly from the suggestion_ragtool tool. "
|
| 168 |
-
"Do not create or assume any details. Always use the correct company name. "
|
| 169 |
-
"The suggestion_ragtool result is your official answer. "
|
| 170 |
-
|
| 171 |
-
"Respond in under not more than 80 words, in a friendly customer-service tone. "
|
| 172 |
-
"Never leave incomplete replies and never ignore earlier conversation context. "
|
| 173 |
-
|
| 174 |
-
"As a customer support agent, always answer using official company information found through the suggestion_ragtool tool. for partcular question liek greeting and user info if you have so do not use tool reply by self "
|
| 175 |
-
f"Current system time : {current_time}, date: {current_date}, day: {current_day}. "
|
| 176 |
-
),
|
| 177 |
-
model=google_model,
|
| 178 |
-
tools=[
|
| 179 |
-
suggestion_ragtool
|
| 180 |
-
],
|
| 181 |
-
model_settings=ModelSettings(
|
| 182 |
-
temperature=1,
|
| 183 |
-
top_p=1,
|
| 184 |
-
max_tokens=2048,
|
| 185 |
-
),
|
| 186 |
-
)
|
| 187 |
-
return information_agent
|
|
|
|
| 1 |
+
from agents import Agent, ModelSettings, Runner, RunConfig,OpenAIResponsesModel ,AsyncOpenAI,function_tool,OpenAIChatCompletionsModel
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from datetime import datetime, date
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
load_dotenv(dotenv_path="./.env.local")
|
| 6 |
+
import asyncio
|
| 7 |
+
from typing import List
|
| 8 |
+
from .VectorDBManagers import VectorDBManager
|
| 9 |
+
from chatkit.agents import AgentContext
|
| 10 |
+
import os
|
| 11 |
+
import nest_asyncio
|
| 12 |
+
nest_asyncio.apply()
|
| 13 |
+
from .function_tool import suggestion_ragtool
|
| 14 |
+
kimi_model = OpenAIResponsesModel(
|
| 15 |
+
model="moonshotai/kimi-k2-instruct-0905", # Valid Groq model
|
| 16 |
+
openai_client=AsyncOpenAI(
|
| 17 |
+
base_url="https://api.groq.com/openai/v1",
|
| 18 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
| 19 |
+
)
|
| 20 |
+
)
|
| 21 |
+
google_model = OpenAIChatCompletionsModel(
|
| 22 |
+
model="google/gemini-2.5-flash", # Google Gemini via OpenRouter
|
| 23 |
+
openai_client=AsyncOpenAI(
|
| 24 |
+
base_url="https://openrouter.ai/api/v1",
|
| 25 |
+
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 26 |
+
)
|
| 27 |
+
)
|
| 28 |
+
deepseek_model = OpenAIChatCompletionsModel(
|
| 29 |
+
model="deepseek/deepseek-chat", # DeepSeek via OpenRouter
|
| 30 |
+
openai_client=AsyncOpenAI(
|
| 31 |
+
base_url="https://openrouter.ai/api/v1",
|
| 32 |
+
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 33 |
+
)
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
sumary_model = OpenAIResponsesModel(
|
| 37 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct", # Valid Groq model
|
| 38 |
+
openai_client=AsyncOpenAI(
|
| 39 |
+
base_url="https://api.groq.com/openai/v1",
|
| 40 |
+
api_key=os.getenv("GROQ_API_KEY")
|
| 41 |
+
)
|
| 42 |
+
)
|
| 43 |
+
def build_sugguestion_information_agent()-> Agent[AgentContext]:
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
current_time = datetime.now()
|
| 47 |
+
current_date = date.today()
|
| 48 |
+
current_day = datetime.today().strftime("%A")
|
| 49 |
+
information_agent = Agent[AgentContext](
|
| 50 |
+
name="company_suggestion_information",
|
| 51 |
+
instructions=(
|
| 52 |
+
"You are an information agent and customer service representative for the company. "
|
| 53 |
+
"Your goal is to provide clear, concise answers using ONLY the suggestion_ragtool tool. "
|
| 54 |
+
"For greeting do not call tool reply by your self add company name "
|
| 55 |
+
"Always speak as the company using 'we'. Do NOT guess or assume. "
|
| 56 |
+
"If information is not found, reply politely: "
|
| 57 |
+
"phraphse according to your intellgence 'No information is available regarding to this . You may book an appointment or speak to our sales agent for more details.' praphrase it "
|
| 58 |
+
|
| 59 |
+
"Make only ONE suggestion_ragtool query that fully represents the user’s request. "
|
| 60 |
+
"All company-related answers must come strictly from the suggestion_ragtool tool. "
|
| 61 |
+
"Do not create or assume any details. Always use the correct company name. "
|
| 62 |
+
"The suggestion_ragtool result is your official answer. "
|
| 63 |
+
|
| 64 |
+
"Respond in under not more than 80 words, in a friendly customer-service tone. "
|
| 65 |
+
"Never leave incomplete replies and never ignore earlier conversation context. "
|
| 66 |
+
|
| 67 |
+
"As a customer support agent, always answer using official company information found through the suggestion_ragtool tool. for partcular question liek greeting and user info if you have so do not use tool reply by self "
|
| 68 |
+
f"Current system time : {current_time}, date: {current_date}, day: {current_day}. "
|
| 69 |
+
),
|
| 70 |
+
model=deepseek_model,
|
| 71 |
+
tools=[
|
| 72 |
+
suggestion_ragtool
|
| 73 |
+
],
|
| 74 |
+
model_settings=ModelSettings(
|
| 75 |
+
temperature=1,
|
| 76 |
+
top_p=1,
|
| 77 |
+
max_tokens=2048,
|
| 78 |
+
),
|
| 79 |
+
)
|
| 80 |
+
return information_agent
|
| 81 |
+
|
| 82 |
+
def build_summarizer_agent() -> Agent[AgentContext]:
|
| 83 |
+
"""
|
| 84 |
+
Creates a summarizer agent that condenses chat history
|
| 85 |
+
into a short, factual summary for context preservation.
|
| 86 |
+
"""
|
| 87 |
+
summarizer_agent = Agent[AgentContext](
|
| 88 |
+
name="Summarizer Agent",
|
| 89 |
+
instructions="""
|
| 90 |
+
You are a summarization assistant.
|
| 91 |
+
Your job is to take several user and assistant messages and produce a concise,
|
| 92 |
+
factual summary that captures key intents, facts, and outcomes.
|
| 93 |
+
|
| 94 |
+
Guidelines:
|
| 95 |
+
- Keep the summary under 80 words.
|
| 96 |
+
- Focus on what the user is asking for and the assistant's key responses.
|
| 97 |
+
- Do NOT add new information.
|
| 98 |
+
- Preserve important context like customer concerns, preferences, or goals.
|
| 99 |
+
- Write in plain English.
|
| 100 |
+
""",
|
| 101 |
+
model=sumary_model, # or use default_model if configured in your environment
|
| 102 |
+
model_settings=ModelSettings(
|
| 103 |
+
temperature=0.3,
|
| 104 |
+
top_p=0.9,
|
| 105 |
+
max_tokens=300,
|
| 106 |
+
),
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
return summarizer_agent
|
| 110 |
+
def build_kimi_information_agent()-> Agent[AgentContext]:
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
current_time = datetime.now()
|
| 114 |
+
current_date = date.today()
|
| 115 |
+
current_day = datetime.today().strftime("%A")
|
| 116 |
+
information_agent = Agent[AgentContext](
|
| 117 |
+
name="company_suggestion_information",
|
| 118 |
+
instructions=(
|
| 119 |
+
"You are an information agent and customer service representative for the company. "
|
| 120 |
+
"Your goal is to provide clear, concise answers using ONLY the suggestion_ragtool tool. "
|
| 121 |
+
"For greeting do not call tool reply by your self add company name "
|
| 122 |
+
"Always speak as the company using 'we'. Do NOT guess or assume. "
|
| 123 |
+
"If information is not found, reply politely: "
|
| 124 |
+
"phraphse according to your intellgence 'No information is available regarding to this . You may book an appointment or speak to our sales agent for more details.' praphrase it "
|
| 125 |
+
|
| 126 |
+
"Make only ONE suggestion_ragtool query that fully represents the user’s request. "
|
| 127 |
+
"All company-related answers must come strictly from the suggestion_ragtool tool. "
|
| 128 |
+
"Do not create or assume any details. Always use the correct company name. "
|
| 129 |
+
"The suggestion_ragtool result is your official answer. "
|
| 130 |
+
|
| 131 |
+
"Respond in under not more than 80 words, in a friendly customer-service tone. "
|
| 132 |
+
"Never leave incomplete replies and never ignore earlier conversation context. "
|
| 133 |
+
|
| 134 |
+
"As a customer support agent, always answer using official company information found through the suggestion_ragtool tool. for partcular question liek greeting and user info if you have so do not use tool reply by self "
|
| 135 |
+
f"Current system time : {current_time}, date: {current_date}, day: {current_day}. "
|
| 136 |
+
),
|
| 137 |
+
model=kimi_model,
|
| 138 |
+
tools=[
|
| 139 |
+
suggestion_ragtool
|
| 140 |
+
],
|
| 141 |
+
model_settings=ModelSettings(
|
| 142 |
+
temperature=1,
|
| 143 |
+
top_p=1,
|
| 144 |
+
max_tokens=2048,
|
| 145 |
+
),
|
| 146 |
+
)
|
| 147 |
+
return information_agent
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def build_google_information_agent()-> Agent[AgentContext]:
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
current_time = datetime.now()
|
| 154 |
+
current_date = date.today()
|
| 155 |
+
current_day = datetime.today().strftime("%A")
|
| 156 |
+
information_agent = Agent[AgentContext](
|
| 157 |
+
name="company_suggestion_information",
|
| 158 |
+
instructions=(
|
| 159 |
+
"You are an information agent and customer service representative for the company. "
|
| 160 |
+
"Your goal is to provide clear, concise answers using ONLY the suggestion_ragtool tool. "
|
| 161 |
+
"For greeting do not call tool reply by your self add company name "
|
| 162 |
+
"Always speak as the company using 'we'. Do NOT guess or assume. "
|
| 163 |
+
"If information is not found, reply politely: "
|
| 164 |
+
"phraphse according to your intellgence 'No information is available regarding to this . You may book an appointment or speak to our sales agent for more details.' praphrase it "
|
| 165 |
+
|
| 166 |
+
"Make only ONE suggestion_ragtool query that fully represents the user’s request. "
|
| 167 |
+
"All company-related answers must come strictly from the suggestion_ragtool tool. "
|
| 168 |
+
"Do not create or assume any details. Always use the correct company name. "
|
| 169 |
+
"The suggestion_ragtool result is your official answer. "
|
| 170 |
+
|
| 171 |
+
"Respond in under not more than 80 words, in a friendly customer-service tone. "
|
| 172 |
+
"Never leave incomplete replies and never ignore earlier conversation context. "
|
| 173 |
+
|
| 174 |
+
"As a customer support agent, always answer using official company information found through the suggestion_ragtool tool. for partcular question liek greeting and user info if you have so do not use tool reply by self "
|
| 175 |
+
f"Current system time : {current_time}, date: {current_date}, day: {current_day}. "
|
| 176 |
+
),
|
| 177 |
+
model=google_model,
|
| 178 |
+
tools=[
|
| 179 |
+
suggestion_ragtool
|
| 180 |
+
],
|
| 181 |
+
model_settings=ModelSettings(
|
| 182 |
+
temperature=1,
|
| 183 |
+
top_p=1,
|
| 184 |
+
max_tokens=2048,
|
| 185 |
+
),
|
| 186 |
+
)
|
| 187 |
+
return information_agent
|