AgentOps-AI / agentops

Python SDK for agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks like CrewAI, Langchain, and Autogen
https://agentops.ai
MIT License
1.7k stars 162 forks source link

API server: Error posting event: Error inserting to table='tools'. Postgres 23503: insert or update on table "tools" violates foreign key constraint "tools_agent_id_fkey"' #386

Open anmol-aidora opened 5 days ago

anmol-aidora commented 5 days ago

🐛 Bug Report

🔎 Describe the Bug 🖇 AgentOps: Could not post events - API server: Error posting event: Error inserting to table='tools'. Postgres: json['code']='23503'. json['message']='insert or update on table "tools" violates foreign key constraint "tools_agent_id_fkey"'

🔄 Reproduction Steps Not sure

🙁 Expected Behavior No such error

HowieG commented 5 days ago

Hey @anmol-aidora thanks for reporting. I'm on it - can you provide any code to replicate?

anmol-aidora commented 4 days ago

Sure @HowieG , I believe this error is coming up when two crews are running one after the other. The first one runs without any error, but the second crew kickoff log shows this error. When checking the interface, I observed that Tool calls are not getting logged.

import os
from dotenv import load_dotenv
load_dotenv("/Users/xplr/leave-management/python/.env.local")

from crewai import Agent, Task, Crew
# from crewai_tools import ScrapeWebsiteTool
from utils.custom_tools import ScrapeWebsiteTool
import agentops
agentops.end_all_sessions()
agentops.init(default_tags=["dummy"])

crew_scrape_tool = ScrapeWebsiteTool()

dummy_agent = Agent(
    role="Dummy",
    goal="You are librarian of an online library.",
    backstory=(
        "You are the owner of the website https://books.toscrape.com/ "
    ),
    tools=[crew_scrape_tool],
    allow_delegation=False,
    verbose=True,    
)

from pydantic import BaseModel, Field
from typing import List
class Topics(BaseModel):
    """List of topics"""
    topics: List[str] = Field(description="List of one word topics")

dummy_task = Task(
    description=(
        "The task is to browse the online library website: https://books.toscrape.com/ and "
        "find a list of topics form the topics available in the website."
    ),
    tools=[crew_scrape_tool],
    expected_output="list of topics in JSON format",
    agent=dummy_agent,
    output_json=Topics,
)

dummy_crew = Crew(
    agents=[dummy_agent,],
    tasks=[dummy_task],
    memory=False,
    verbose=True
)
dummy_crew_output = dummy_crew.kickoff()

print(dummy_crew_output.json_dict)

dummy_agent2 = Agent(
    role="Dummy",
    goal="You are librarian of an online library.",
    backstory=(
        "You are the owner of the website https://books.toscrape.com/ "
    ),
    tools=[crew_scrape_tool],
    allow_delegation=False,
    verbose=True,    
)

dummy_task2 = Task(
    description=(
        "The task is to browse the online library website: https://books.toscrape.com/ and find a new book which"
        " is similar to this topic: {topic}, and return its name. If you are not able to find a book similar to "
        "the topic, return any book name from the website you find fascinating."
    ),
    tools=[crew_scrape_tool],
    expected_output="Give the name of the book. Make sure the name is JSON serializable",
    agent=dummy_agent2,
)

dummy_crew2 = Crew(
    agents=[dummy_agent2,],
    tasks=[dummy_task2],
    memory=False,
    verbose=True
)
for topic in dummy_crew_output.json_dict["topics"]:
    agentops.start_session(tags=["dummy"])
    dummy_crew_output2 = dummy_crew2.kickoff(inputs={"topic": topic})
    print(dummy_crew_output2)

I am not getting that error in this code but my code is similar to this. As you can see in the screenshot, tool call is not getting logged:

Screenshot 2024-09-13 at 9 42 38 PM