from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from crewai import Agent, LLM
from services import crud,Email
from fastapi import APIRouter
import os
from dotenv import load_dotenv
router = APIRouter()
load_dotenv()
class PromptRequest(BaseModel):
prompt: str
llm = LLM(
model=“groq/openai/gpt-oss-120b”,
api_key=os.getenv(“GROQ_API_KEY”),
)
agent = Agent(
llm=llm,
name=“Genix AI”,
role=“User Manager”,
inject_date= True,
allow_delegation=True,
goal=“”"
Perform CRUD operations on users:
- Only execute create, update, or delete if the operation is valid.
- Do not attempt to modify the same user twice simultaneously.
- Always confirm the result using the tool response before responding to the user.
- You must not attempt to add, update, or delete the same user twice at the same time.
- When creating a user:
Always send a welcome email to the given email if the user is created successfully.
Send emails when requested:
- Extract the recipient’s email as ‘to’.
- Generate a clear, concise subject based on context.
- Write a professional, polite body with short paragraphs.
- Use a formal, respectful tone.
Respond naturally, warmly, and do not mention internal tools or methods.
“”",
backstory= “”"
You are Genix AI, a smart, friendly, and highly capable virtual assistant.
You handle user management (CRUD) and sending emails professionally.
You must not attempt to modify the same user simultaneously.
Always confirm successful operations with the real tool output.
Respond clearly, helpfully, and naturally without revealing internal tools.
“”",
tools=[
crud.UserCRUDFunctions.create_user,
crud.UserCRUDFunctions.get_user,
crud.UserCRUDFunctions.update_user,
crud.UserCRUDFunctions.delete_user,
Email.EmailTools.email_user
],
max_iter=1,
memory=True,
verbose=True,
reasoning=True,
max_reasoning_attempts=1,
)
@router.post(“/prompt”)
async def run_agent(request: PromptRequest):
try:
user_prompt = request.prompt
response = agent.kickoff(user_prompt)
print(response)
return response.raw
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
I am trying to do a CRUD operation on user’s data. When I am trying to create a user, It creates twice a time for a same user. Even I have set the max_tier to 1, But it running twice.