Skip to content

Commit

Permalink
Merge branch 'main' into issue13/add-ollama-support
Browse files Browse the repository at this point in the history
  • Loading branch information
sprajosh authored Jun 14, 2024
2 parents 8213825 + 961ba21 commit dbc0d5c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ pip install git+https://github.com/AgentOps-AI/crewAI.git@main
- [AgentOps integration example](https://docs.agentops.ai/v1/integrations/crewai)
- [Official CrewAI documentation](https://docs.crewai.com/how-to/AgentOps-Observability)

### AutoGen 🤖
With only two lines of code, add full observability and monitoring to Autogen agents. Set an `AGENTOPS_API_KEY` in your environment and call `agentops.init()`

- [Autogen Observability Example](https://microsoft.github.io/autogen/docs/notebooks/agentchat_agentops)
- [Autogen - AgentOps Documentation](https://microsoft.github.io/autogen/docs/ecosystem/agentops)

### Langchain 🦜🔗

AgentOps works seamlessly with applications built using Langchain. To use the handler, install Langchain as an optional dependency:
Expand Down
36 changes: 35 additions & 1 deletion agentops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# agentops/__init__.py
import functools
import os
import logging
from typing import Optional, List, Union
Expand All @@ -18,6 +19,23 @@
except ModuleNotFoundError:
pass

is_initialized = False


def noop(*args, **kwargs):
return


def check_init(child_function):
@functools.wraps(child_function)
def wrapper(*args, **kwargs):
if is_initialized:
return child_function(*args, **kwargs)
else:
return noop(*args, **kwargs)

return wrapper


def init(
api_key: Optional[str] = None,
Expand Down Expand Up @@ -79,6 +97,9 @@ def init(
skip_auto_end_session=skip_auto_end_session,
)

global is_initialized
is_initialized = True

return inherited_session_id or c.current_session_id


Expand Down Expand Up @@ -118,9 +139,19 @@ def start_session(
e.g. ["test_run"].
config: (Configuration, optional): Client configuration object
"""
return Client().start_session(tags, config, inherited_session_id)

try:
sess_result = Client().start_session(tags, config, inherited_session_id)

global is_initialized
is_initialized = True

return sess_result
except Exception:
pass


@check_init
def record(event: Union[Event, ErrorEvent]):
"""
Record an event with the AgentOps service.
Expand All @@ -131,6 +162,7 @@ def record(event: Union[Event, ErrorEvent]):
Client().record(event)


@check_init
def add_tags(tags: List[str]):
"""
Append to session tags at runtime.
Expand All @@ -141,6 +173,7 @@ def add_tags(tags: List[str]):
Client().add_tags(tags)


@check_init
def set_tags(tags: List[str]):
"""
Replace session tags at runtime.
Expand Down Expand Up @@ -169,5 +202,6 @@ def stop_instrumenting():
Client().stop_instrumenting()


@check_init
def create_agent(name: str, agent_id: Optional[str] = None):
return Client().create_agent(name=name, agent_id=agent_id)
2 changes: 1 addition & 1 deletion agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def end_session(
self._session.end_session(end_state, end_state_reason)
token_cost = self._worker.end_session(self._session)

if token_cost == "unknown":
if token_cost is None or token_cost == "unknown":
logger.info("Could not determine cost of run.")
else:
token_cost_d = Decimal(token_cost)
Expand Down
81 changes: 78 additions & 3 deletions docs/v1/integrations/autogen.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,81 @@ title: Autogen
description: "AgentOps provides first class support for Autogen"
---

<Note>
Documentation for this integration is coming soon.
</Note>
AgentOps and Autogen teamed up to make monitoring Autogen agents dead simple.

Autogen has comprehensive [documentation](https://microsoft.github.io/autogen/docs) available as well as a great [quickstart](https://microsoft.github.io/autogen/docs/Getting-Started).

## Adding AgentOps to Autogen agents

<Steps>
<Step title="Install the AgentOps SDK">
<CodeGroup>
```bash pip
pip install agentops
```
```bash poetry
poetry add agentops
```
</CodeGroup>
<Check>
[Give us a star](https://github.com/AgentOps-AI/agentops) on GitHub while you're at it (you may be our 1,000th 😊)
</Check>
</Step>
<Step title="Install Autogen">
<CodeGroup>
```bash pip
pip install pyautogen
```
```bash poetry
poetry add pyautogen
```
</CodeGroup>
</Step>
<Step title="Add 3 lines of code">
1. Before setting up anything in Autogen, call `agentops.init()`
2. At the end of your agent run, call `agentops.end_session("Success")`
<CodeGroup>
```python python
import agentops

# Beginning of program (i.e. main.py, __init__.py)
# IMPORTANT: Must be before using any autogen setup
agentops.init(<INSERT YOUR API KEY HERE>)
...
# End of program (e.g. main.py)
agentops.end_session("Success") # Success|Fail|Indeterminate
```
</CodeGroup>
<Check>
Instantiating the AgentOps client will automatically instrument Autogen, meaning you will be able to see all
of your sessions on the AgentOps Dashboard along with the full LLM chat histories, cost, token counts, etc.
</Check>
<Tip>
For more features see our [Usage](/v1/usage) section.
</Tip>
</Step>
<Step title="Set your API key">
Retrieve an API Key from your Settings > [Projects & API Keys](https://app.agentops.ai/settings/projects) page.
<Frame type="glass" caption="Settings > Projects & API Keys">
<img height="200" src="/images/api-keys.png" />
</Frame>
<Info>
API keys are tied to individual projects.<br></br>
A Default Project has been created for you, so just click Copy API Key
</Info>
Set this API Key in your [environment variables](/v1/usage/environment-variables)
```python .env
AGENTOPS_API_KEY=<YOUR API KEY>
```
</Step>
<Step title="Run your agent">
Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your Autogen Agent! 🕵️
<Tip>
After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard
</Tip>
<div/>{/* Intentionally blank div for newline */}
<Frame type="glass" caption="Clickable link to session">
<img height="200" src="https://github.com/AgentOps-AI/agentops/blob/cf67191f13e0e2a09446a61b7393e1810b3eee95/docs/images/link-to-session.gif?raw=true" />
</Frame>
</Step>
</Steps>
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "agentops"
version = "0.2.2"
version = "0.2.3"
authors = [
{ name="Alex Reibman", email="[email protected]" },
{ name="Shawn Qiu", email="[email protected]" },
Expand Down
1 change: 0 additions & 1 deletion tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def setup_method(self):

def test_session(self, mock_req):
agentops.start_session(config=self.config)
print(self.config.api_key)

agentops.record(ActionEvent(self.event_type))
agentops.record(ActionEvent(self.event_type))
Expand Down

0 comments on commit dbc0d5c

Please sign in to comment.