Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI
Python agentagentopsagents-sdkaianthropicautogencost-estimationcrewaievalsevaluation-metricsgroqlangchainllmmistralollamaopenaiopenai-agents
NoSessionException when user does not init() (#307)
Howard Gila2f2f7f0dc8f
diff --git a/agentops/__init__.py b/agentops/__init__.pyindex 5fcefb9c..5c7ca5cb 100755--- a/agentops/__init__.py+++ b/agentops/__init__.py@@ -11,6 +11,8 @@ from .decorators import record_functionfrom .agent import track_agentfrom .log_config import loggerfrom .session import Session+from .state import get_state, set_state+try:from .partners.langchain_callback_handler import (@@ -20,8 +22,6 @@ try:except ModuleNotFoundError:pass-is_initialized = False-def noop(*args, **kwargs):return@@ -30,7 +30,7 @@ def noop(*args, **kwargs):def check_init(child_function):@functools.wraps(child_function)def wrapper(*args, **kwargs):- if is_initialized:+ if get_state("is_initialized"): # is initialized in state.py is not workingreturn child_function(*args, **kwargs)else:return noop(*args, **kwargs)@@ -103,8 +103,7 @@ def init(tags=tags, config=c.config, inherited_session_id=inherited_session_id)- global is_initialized- is_initialized = True+ set_state("is_initialized", True)return session@@ -156,8 +155,7 @@ def start_session(try:sess_result = Client().start_session(tags, config, inherited_session_id)- global is_initialized- is_initialized = True+ set_state("is_initialized", True)return sess_resultexcept Exception:diff --git a/agentops/agent.py b/agentops/agent.pyindex 11b95134..8ed41174 100644--- a/agentops/agent.py+++ b/agentops/agent.py@@ -4,6 +4,7 @@ from .log_config import loggerfrom uuid import uuid4from agentops import Clientfrom inspect import isclass, isfunction+from .state import get_statedef track_agent(name: Union[str, None] = None):@@ -17,6 +18,10 @@ def track_agent(name: Union[str, None] = None):def new_init(self, *args, **kwargs):try:original_init(self, *args, **kwargs)++ if not get_state("is_initialized"):+ return+self.agent_ops_agent_id = str(uuid4())session = kwargs.get("session", None)diff --git a/agentops/client.py b/agentops/client.pyindex 5e9bd773..32705f8c 100644--- a/agentops/client.py+++ b/agentops/client.py@@ -34,6 +34,7 @@ from .config import ClientConfigurationfrom .llm_tracker import LlmTrackerfrom termcolor import coloredfrom typing import Tuple+from .state import get_state, set_state@conditional_singleton@@ -178,6 +179,8 @@ class Client(metaclass=MetaClient):returnsession = self._safe_get_session()+ if session is None:+ returnsession.add_tags(tags=tags)@@ -191,11 +194,13 @@ class Client(metaclass=MetaClient):tags (List[str]): The list of tags to set."""- try:- session = self._safe_get_session()- session.set_tags(tags=tags)- except NoSessionException:+ session = self._safe_get_session()++ if session is None:self._tags_for_future_session = tags+ return++ session.set_tags(tags=tags)def record(self, event: Union[Event, ErrorEvent]) -> None:"""@@ -206,6 +211,9 @@ class Client(metaclass=MetaClient):"""session = self._safe_get_session()+ if session is None:+ logger.error("Could not record event. No session.")+ returnsession.record(event)def _record_event_sync(self, func, event_name, *args, **kwargs):@@ -382,7 +390,7 @@ class Client(metaclass=MetaClient):end_state_reason: Optional[str] = None,video: Optional[str] = None,is_auto_end: Optional[bool] = None,- ) -> Decimal:+ ) -> Optional[Decimal]:"""End the current session with the AgentOps service.@@ -397,6 +405,8 @@ class Client(metaclass=MetaClient):"""session = self._safe_get_session()+ if session is None:+ returnsession.end_state = end_statesession.end_state_reason = end_state_reason@@ -424,7 +434,11 @@ class Client(metaclass=MetaClient):"This run's cost ${}".format("{:.2f}".format(token_cost_d)if token_cost_d == 0- else "{:.6f}".format(token_cost_d.quantize(Decimal('0.000001'), rounding=ROUND_HALF_UP))+ else "{:.6f}".format(+ token_cost_d.quantize(+ Decimal("0.000001"), rounding=ROUND_HALF_UP+ )+ )))@@ -453,6 +467,8 @@ class Client(metaclass=MetaClient):else:# if no session passed, assume single sessionsession = self._safe_get_session()+ if session is None:+ returnsession.create_agent(name=name, agent_id=agent_id)return agent_id@@ -553,7 +569,7 @@ class Client(metaclass=MetaClient):)] = session- def _safe_get_session(self) -> Session:+ def _safe_get_session(self) -> Optional[Session]:for s in self._sessions:if s.end_state is not None:self._sessions.remove(s)@@ -563,15 +579,19 @@ class Client(metaclass=MetaClient):session = self._sessions[0]if len(self._sessions) == 0:- raise NoSessionException("No session exists")+ if get_state("is_initialized"):+ return Noneelif len(self._sessions) > 1:- raise MultiSessionException(- "If multiple sessions exist, you must use session.function(). Example: session.add_tags(...) instead "- "of agentops.add_tags(...). More info: "- "https://docs.agentops.ai/v1/concepts/core-concepts#session-management"+ calling_function = inspect.stack()[+ 2+ ].function # Using index 2 because we have a wrapper at index 1+ logger.warning(+ f"Multiple sessions detected. You must use session.{calling_function}(). More info: https://docs.agentops.ai/v1/concepts/core-concepts#session-management")+ return+return sessiondef end_all_sessions(self):diff --git a/agentops/meta_client.py b/agentops/meta_client.pyindex 6a188369..51f1630d 100644--- a/agentops/meta_client.py+++ b/agentops/meta_client.py@@ -55,11 +55,10 @@ def handle_exceptions(method):logger.warning(f"Error: {e}")config = getattr(self, "config", None)if config is not None:- type(self).send_exception_to_server(- e,- self.config._api_key,- self._sessions[0], # TODO: find which session caused exception- )+ session = None+ if len(self._sessions) > 0:+ session = self._sessions[0]+ type(self).send_exception_to_server(e, self.config._api_key, session)raise ereturn wrapperdiff --git a/agentops/state.py b/agentops/state.pynew file mode 100644index 00000000..d29b093f--- /dev/null+++ b/agentops/state.py@@ -0,0 +1,9 @@+state_dict = {}+++def set_state(key: str, value: any):+ state_dict[key] = value+++def get_state(key: str) -> any:+ return state_dict.get(key)diff --git a/examples/crew/job_posting.ipynb b/examples/crew/job_posting.ipynbindex 5a2f641f..f6f75eeb 100644--- a/examples/crew/job_posting.ipynb+++ b/examples/crew/job_posting.ipynb@@ -6,7 +6,16 @@"metadata": {},"outputs": [],"source": [- "%pip install -U agentops\n",+ "# %pip install -e ../.."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "%pip install -U 'crewai[tools]'\n","%pip install -U 'crewai[agentops]'"]},@@ -18,9 +27,9 @@"source": ["import os\n","\n",- "os.environ[\"SERPER_API_KEY\"] = \"YOUR_SERPER_API_KEY\"\n",- "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY\"\n",- "os.environ[\"AGENTOPS_API_KEY\"] = \"YOUR_AGENTOPS_API_KEY\""+ "os.environ[\"SERPER_API_KEY\"] = \"...\"\n",+ "os.environ[\"OPENAI_API_KEY\"] = \"...\"\n",+ "os.environ[\"AGENTOPS_API_KEY\"] = \"...\""]},{@@ -30,21 +39,22 @@"outputs": [],"source": ["from crewai import Agent\n",- "from crewai_tools.tools import WebsiteSearchTool, SeperDevTool, FileReadTool\n",+ "from crewai_tools.tools import WebsiteSearchTool, SerperDevTool, FileReadTool\n","\n","web_search_tool = WebsiteSearchTool()\n",- "seper_dev_tool = SeperDevTool()\n",+ "serper_dev_tool = SerperDevTool()\n","file_read_tool = FileReadTool(\n"," file_path=\"job_description_example.md\",\n"," description=\"A tool to read the job description example file.\",\n",")\n","\n",+ "\n","class Agents:\n"," def research_agent(self):\n"," return Agent(\n"," role=\"Research Analyst\",\n"," goal=\"Analyze the company website and provided description to extract insights on culture, values, and specific needs.\",\n",- " tools=[web_search_tool, seper_dev_tool],\n",+ " tools=[web_search_tool, serper_dev_tool],\n"," backstory=\"Expert in analyzing company cultures and identifying key values and needs from various sources, including websites and brief descriptions.\",\n"," verbose=True,\n"," )\n",@@ -53,7 +63,7 @@" return Agent(\n"," role=\"Job Description Writer\",\n"," goal=\"Use insights from the Research Analyst to create a detailed, engaging, and enticing job posting.\",\n",- " tools=[web_search_tool, seper_dev_tool, file_read_tool],\n",+ " tools=[web_search_tool, serper_dev_tool, file_read_tool],\n"," backstory=\"Skilled in crafting compelling job descriptions that resonate with the company's values and attract the right candidates.\",\n"," verbose=True,\n"," )\n",@@ -62,7 +72,7 @@" return Agent(\n"," role=\"Review and Editing Specialist\",\n"," goal=\"Review the job posting for clarity, engagement, grammatical accuracy, and alignment with company values and refine it to ensure perfection.\",\n",- " tools=[web_search_tool, seper_dev_tool, file_read_tool],\n",+ " tools=[web_search_tool, serper_dev_tool, file_read_tool],\n"," backstory=\"A meticulous editor with an eye for detail, ensuring every piece of content is clear, engaging, and grammatically perfect.\",\n"," verbose=True,\n"," )"@@ -77,6 +87,7 @@"from textwrap import dedent\n","from crewai import Task\n","\n",+ "\n","class Tasks:\n"," def research_company_culture_task(self, agent, company_description, company_domain):\n"," return Task(\n",@@ -157,7 +168,6 @@"outputs": [],"source": ["from crewai import Crew\n",- "\n","import agentops\n","\n","agentops.init(tags=[\"crew-job-posting-example\"])\n",@@ -213,6 +223,11 @@"\n","agentops.end_session(\"Success\")"]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": []}],"metadata": {diff --git a/examples/crew/job_posting.py b/examples/crew/job_posting.pynew file mode 100644index 00000000..e4d7d9c4--- /dev/null+++ b/examples/crew/job_posting.py@@ -0,0 +1,180 @@+# TO TEST: comment and uncomment agentops code+++import os++os.environ["SERPER_API_KEY"] = "..."+os.environ["OPENAI_API_KEY"] = "..."++from crewai import Agent+from crewai_tools.tools import WebsiteSearchTool, SerperDevTool, FileReadTool++web_search_tool = WebsiteSearchTool()+serper_dev_tool = SerperDevTool()+file_read_tool = FileReadTool(+ file_path="job_description_example.md",+ description="A tool to read the job description example file.",+)+++class Agents:+ def research_agent(self):+ return Agent(+ role="Research Analyst",+ goal="Analyze the company website and provided description to extract insights on culture, values, and specific needs.",+ tools=[web_search_tool, serper_dev_tool],+ backstory="Expert in analyzing company cultures and identifying key values and needs from various sources, including websites and brief descriptions.",+ verbose=True,+ )++ def writer_agent(self):+ return Agent(+ role="Job Description Writer",+ goal="Use insights from the Research Analyst to create a detailed, engaging, and enticing job posting.",+ tools=[web_search_tool, serper_dev_tool, file_read_tool],+ backstory="Skilled in crafting compelling job descriptions that resonate with the company's values and attract the right candidates.",+ verbose=True,+ )++ def review_agent(self):+ return Agent(+ role="Review and Editing Specialist",+ goal="Review the job posting for clarity, engagement, grammatical accuracy, and alignment with company values and refine it to ensure perfection.",+ tools=[web_search_tool, serper_dev_tool, file_read_tool],+ backstory="A meticulous editor with an eye for detail, ensuring every piece of content is clear, engaging, and grammatically perfect.",+ verbose=True,+ )+++from textwrap import dedent+from crewai import Task+++class Tasks:+ def research_company_culture_task(self, agent, company_description, company_domain):+ return Task(+ description=dedent(+ f"""\+ Analyze the provided company website and the hiring manager's company's domain {company_domain}, description: "{company_description}". Focus on understanding the company's culture, values, and mission. Identify unique selling points and specific projects or achievements highlighted on the site.+ Compile a report summarizing these insights, specifically how they can be leveraged in a job posting to attract the right candidates."""+ ),+ expected_output=dedent(+ """\+ A comprehensive report detailing the company's culture, values, and mission, along with specific selling points relevant to the job role. Suggestions on incorporating these insights into the job posting should be included."""+ ),+ agent=agent,+ )++ def research_role_requirements_task(self, agent, hiring_needs):+ return Task(+ description=dedent(+ f"""\+ Based on the hiring manager's needs: "{hiring_needs}", identify the key skills, experiences, and qualities the ideal candidate should possess for the role. Consider the company's current projects, its competitive landscape, and industry trends. Prepare a list of recommended job requirements and qualifications that align with the company's needs and values."""+ ),+ expected_output=dedent(+ """\+ A list of recommended skills, experiences, and qualities for the ideal candidate, aligned with the company's culture, ongoing projects, and the specific role's requirements."""+ ),+ agent=agent,+ )++ def draft_job_posting_task(+ self, agent, company_description, hiring_needs, specific_benefits+ ):+ return Task(+ description=dedent(+ f"""\+ Draft a job posting for the role described by the hiring manager: "{hiring_needs}". Use the insights on "{company_description}" to start with a compelling introduction, followed by a detailed role description, responsibilities, and required skills and qualifications. Ensure the tone aligns with the company's culture and incorporate any unique benefits or opportunities offered by the company.+ Specfic benefits: "{specific_benefits}"""+ ),+ expected_output=dedent(+ """\+ A detailed, engaging job posting that includes an introduction, role description, responsibilities, requirements, and unique company benefits. The tone should resonate with the company's culture and values, aimed at attracting the right candidates."""+ ),+ agent=agent,+ )++ def review_and_edit_job_posting_task(self, agent, hiring_needs):+ return Task(+ description=dedent(+ f"""\+ Review the draft job posting for the role: "{hiring_needs}". Check for clarity, engagement, grammatical accuracy, and alignment with the company's culture and values. Edit and refine the content, ensuring it speaks directly to the desired candidates and accurately reflects the role's unique benefits and opportunities. Provide feedback for any necessary revisions."""+ ),+ expected_output=dedent(+ """\+ A polished, error-free job posting that is clear, engaging, and perfectly aligned with the company's culture and values. Feedback on potential improvements and final approval for publishing. Formated in markdown."""+ ),+ agent=agent,+ output_file="job_posting.md",+ )++ def industry_analysis_task(self, agent, company_domain, company_description):+ return Task(+ description=dedent(+ f"""\+ Conduct an in-depth analysis of the industry related to the company's domain: "{company_domain}". Investigate current trends, challenges, and opportunities within the industry, utilizing market reports, recent developments, and expert opinions. Assess how these factors could impact the role being hired for and the overall attractiveness of the position to potential candidates.+ Consider how the company's position within this industry and its response to these trends could be leveraged to attract top talent. Include in your report how the role contributes to addressing industry challenges or seizing opportunities."""+ ),+ expected_output=dedent(+ """\+ A detailed analysis report that identifies major industry trends, challenges, and opportunities relevant to the company's domain and the specific job role. This report should provide strategic insights on positioning the job role and the company as an attractive choice for potential candidates."""+ ),+ agent=agent,+ )+++from crewai import Crew+import agentops++agentops.init(tags=["crew-job-posting-example"])++tasks = Tasks()+agents = Agents()++company_description = input("What is the company description?\n")+company_domain = input("What is the company domain?\n")+hiring_needs = input("What are the hiring needs?\n")+specific_benefits = input("What are specific_benefits you offer?\n")++# Create Agents+researcher_agent = agents.research_agent()+writer_agent = agents.writer_agent()+review_agent = agents.review_agent()++# Define Tasks for each agent+research_company_culture_task = tasks.research_company_culture_task(+ researcher_agent, company_description, company_domain+)+industry_analysis_task = tasks.industry_analysis_task(+ researcher_agent, company_domain, company_description+)+research_role_requirements_task = tasks.research_role_requirements_task(+ researcher_agent, hiring_needs+)+draft_job_posting_task = tasks.draft_job_posting_task(+ writer_agent, company_description, hiring_needs, specific_benefits+)+review_and_edit_job_posting_task = tasks.review_and_edit_job_posting_task(+ review_agent, hiring_needs+)++# Instantiate the crew with a sequential process+crew = Crew(+ agents=[researcher_agent, writer_agent, review_agent],+ tasks=[+ research_company_culture_task,+ industry_analysis_task,+ research_role_requirements_task,+ draft_job_posting_task,+ review_and_edit_job_posting_task,+ ],+)++# Kick off the process+result = crew.kickoff()++print("Job Posting Creation Process Completed.")+print("Final Job Posting:")+print(result)++agentops.end_session("Success")diff --git a/pyproject.toml b/pyproject.tomlindex 10ec0334..1090a5e0 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"[project]name = "agentops"-version = "0.3.2"+version = "0.3.3"authors = [{ name="Alex Reibman", email="[email protected]" },{ name="Shawn Qiu", email="[email protected]" },diff --git a/tests/test_session.py b/tests/test_session.pyindex 6593de8c..3ab52f53 100644--- a/tests/test_session.py+++ b/tests/test_session.py@@ -175,8 +175,8 @@ class TestSingleSessions:assert session is Nonedef test_safe_get_session_no_session(self, mock_req):- with pytest.raises(NoSessionException):- session = Client()._safe_get_session()+ session = Client()._safe_get_session()+ assert session is Nonedef test_safe_get_session_with_session(self, mock_req):agentops.start_session(config=self.config)@@ -187,8 +187,8 @@ class TestSingleSessions:agentops.start_session(config=self.config)agentops.start_session(config=self.config)- with pytest.raises(MultiSessionException):- session = Client()._safe_get_session()+ session = Client()._safe_get_session()+ assert session is Noneclass TestMultiSessions: