Skip to content

Commit

Permalink
rename record_function to record_action (#335)
Browse files Browse the repository at this point in the history
* rename record_function

* doc updats

* bypass request_mock
  • Loading branch information
bboynton97 authored Aug 7, 2024
1 parent 4723fb0 commit beaecb9
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 52 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ Initialize the AgentOps client and automatically get analytics on every LLM call
import agentops

# Beginning of program's code (i.e. main.py, __init__.py)
agentops.init(<INSERT YOUR API KEY HERE>)
agentops.init( < INSERT YOUR API KEY HERE >)

...


# (optional: record specific functions)
@agentops.record_function('sample function being record')
@agentops.record_action('sample function being record')
def sample_function(...):
...


# End of program
agentops.end_session('Success')
# Woohoo You're done 🎉
Expand Down
2 changes: 1 addition & 1 deletion agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .client import Client
from .event import Event, ActionEvent, LLMEvent, ToolEvent, ErrorEvent
from .decorators import record_function, track_agent, record_tool
from .decorators import record_action, track_agent, record_tool, record_function
from .helpers import check_agentops_update
from .log_config import logger
from .session import Session
Expand Down
11 changes: 9 additions & 2 deletions agentops/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@


def record_function(event_name: str):
logger.warning(
"DEPRECATION WARNING: record_function has been replaced with record_action and will be removed in the next minor version. Also see: record_tool"
)
return record_action(event_name)


def record_action(event_name: str):
"""
Decorator to record an event before and after a function call.
Usage:
Expand All @@ -32,7 +39,7 @@ async def async_wrapper(*args, session: Optional[Session] = None, **kwargs):
if session is None:
if Client().is_multi_session:
raise ValueError(
"If multiple sessions exists, `session` is a required parameter in the function decorated by @record_function"
"If multiple sessions exists, `session` is a required parameter in the function decorated by @record_action"
)
func_args = inspect.signature(func).parameters
arg_names = list(func_args.keys())
Expand Down Expand Up @@ -93,7 +100,7 @@ def sync_wrapper(*args, session: Optional[Session] = None, **kwargs):
if session is None:
if Client().is_multi_session:
raise ValueError(
"If multiple sessions exists, `session` is a required parameter in the function decorated by @record_function"
"If multiple sessions exists, `session` is a required parameter in the function decorated by @record_action"
)
func_args = inspect.signature(func).parameters
arg_names = list(func_args.keys())
Expand Down
16 changes: 11 additions & 5 deletions agentops/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import json
import inspect
from typing import Union
import requests
import http.client
import json
from importlib.metadata import version, PackageNotFoundError

from .log_config import logger
Expand Down Expand Up @@ -152,10 +153,15 @@ def get_agentops_version():


def check_agentops_update():
response = requests.get("https://pypi.org/pypi/agentops/json")

if response.status_code == 200:
latest_version = response.json()["info"]["version"]
# using http.client to avoid this call being caught by requests_mock on tests
conn = http.client.HTTPSConnection("pypi.org")
conn.request("GET", "/pypi/agentops/json")
response = conn.getresponse()
data = response.read().decode()
json_data = json.loads(data)

if response.status == 200:
latest_version = json_data["info"]["version"]

try:
current_version = version("agentops")
Expand Down
8 changes: 4 additions & 4 deletions docs/v1/concepts/events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ The `ActionEvent` is a generic event for recording events that do not fit into t
| logs | str | None | "Executed action successfully" | Logs generated during the action event |
| screenshot | str | None | "/path/to/screenshot.png" | Path to screenshot captured during the action event |

An action event can be used the [same way as other events](/v1/usage/recording-events) but also with the `record_function`
decorator.
An action or tool event can be used the [same way as other events](/v1/usage/recording-events) but also with the `record_action` or `record_tool`
decorators.

<CodeGroup>
```python python
from agentops import record_function
from agentops import record_action

@record_function()
@record_action()
def some_action(params):
return "some response"
```
Expand Down
4 changes: 2 additions & 2 deletions docs/v1/examples/notebooks/openai-gpt.html
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ <h1 id="events">Events</h1>
</div>
<div id="b45754a57148eed1" class="cell code" data-collapsed="false">
<div class="sourceCode" id="cb7"><pre
class="sourceCode python"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> agentops <span class="im">import</span> record_function</span>
class="sourceCode python"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> agentops <span class="im">import</span> record_action</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="at">@record_function</span>(<span class="st">&quot;add numbers&quot;</span>)</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="at">@record_action</span>(<span class="st">&quot;add numbers&quot;</span>)</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> add(x, y):</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> x <span class="op">+</span> y</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a></span>
Expand Down
6 changes: 3 additions & 3 deletions docs/v1/quickstart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ import EnvTooltip from '/snippets/add-env-tooltip.mdx'
<CardGroup cols={1}>

<Card icon="code" title="Decorate Functions">
You can instrument other functions inside your code with the handy `@record_function`
You can instrument other functions inside your code with the handy `@record_action`
decorator, which will record an `action_type`, the parameters, and the returns. You
will see these function calls alongside your LLM calls from instantiating the AgentOps client.
```python python
# (record specific functions)
@agentops.record_function('sample function being record')
@agentops.record_action('sample function being record')
def sample_function(...):
...
```
Expand Down Expand Up @@ -94,7 +94,7 @@ import agentops
agentops.init(<INSERT YOUR API KEY HERE>)

# (record specific functions)
@agentops.record_function('sample function being record')
@agentops.record_action('sample function being record')
def sample_function(...):
...

Expand Down
27 changes: 22 additions & 5 deletions docs/v1/usage/recording-events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,40 @@ description: "Log events such as agent actions, LLM calls, tool calls, and error
To get the most out of AgentOps, it is best to carefully consider what events to record -
not simply record whatever your agent is logging. AgentOps offers two ways to record events:

## `@record_function` Decorator
## `@record_action` Decorator

- **`event_type`** (str): Type of the event.

To make AgentOps easier to integrate, we also provide a function decorator to automatically creates
and records an event for your function.
To make AgentOps easier to integrate, we also provide a function decorator to automatically create
and record an event for your function.

```python python
from agentops import record_function
from agentops import record_action

@record_function('sample function being record')
@record_action('sample function being record')
def sample_function(...):
...
```

The decorator will record the function's parameters, returns, and the time duration. We suggest using this on functions that take a long time and contain nested functions. For example, if you decorate a function that makes several openai calls, then each openai call will show in the replay graph as a child of the decorated function.

## `@record_tool` Decorator

- **`tool_name`** (str): The name of the tool represented by the python function

Additionally, we provide a function decorator to automatically create tool events for python functions.

```python python
from agentops import record_tool

@record_tool('SampleToolName')
def sample_tool(...):
...
```

The decorator will record the function's parameters, returns, and the time duration. We suggest using this on functions that take a long time and contain nested functions. For example, if you decorate a function that makes several openai calls, then each openai call will show in the replay graph as a child of the decorated function.


## `record()` Method

From this point, simply call the .record() method in the AgentOps client:
Expand Down
16 changes: 10 additions & 6 deletions examples/openai-gpt.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"metadata": {
"collapsed": false
},
"id": "5d424a02e30ce7f4"
"id": "5d424a02e30ce7f4",
"execution_count": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -104,7 +105,8 @@
"metadata": {
"collapsed": false
},
"id": "2704d6d625efa77f"
"id": "2704d6d625efa77f",
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -125,7 +127,8 @@
"metadata": {
"collapsed": false
},
"id": "537abd77cd0e0d25"
"id": "537abd77cd0e0d25",
"execution_count": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -159,7 +162,8 @@
"metadata": {
"collapsed": false
},
"id": "544c8f1bdb8c6e4b"
"id": "544c8f1bdb8c6e4b",
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -178,10 +182,10 @@
},
"outputs": [],
"source": [
"from agentops import record_function\n",
"from agentops import record_action\n",
"\n",
"\n",
"@record_function(\"add numbers\")\n",
"@record_action(\"add numbers\")\n",
"def add(x, y):\n",
" return x + y\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/recording-events.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
},
"outputs": [],
"source": [
"from agentops import record_function\n",
"from agentops import record_action\n",
"\n",
"\n",
"@record_function(\"add numbers\")\n",
"@record_action(\"add numbers\")\n",
"def add(x, y):\n",
" return x + y\n",
"\n",
Expand Down
6 changes: 3 additions & 3 deletions tests/openai_handlers/_test_llm_tracker_ge_1_async.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from openai import AsyncOpenAI
import asyncio
import agentops
from agentops import record_function
from agentops import record_action
from dotenv import load_dotenv

load_dotenv()

agentops.init()


@record_function("openai v1 async no streaming")
@record_action("openai v1 async no streaming")
async def call_openai_v1_async_no_streaming():
client = AsyncOpenAI()

Expand All @@ -25,7 +25,7 @@ async def call_openai_v1_async_no_streaming():
# raise ValueError("This is an intentional error for testing.")


@record_function("openai v1 async with streaming")
@record_action("openai v1 async with streaming")
async def call_openai_v1_async_streaming():
client = AsyncOpenAI() # Using the async client

Expand Down
6 changes: 3 additions & 3 deletions tests/openai_handlers/_test_llm_tracker_ge_1_sync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from openai import OpenAI
import agentops
from agentops import record_function
from agentops import record_action
from packaging.version import parse
from importlib import import_module
import sys
Expand All @@ -19,7 +19,7 @@
print("openai version: ", module_version)


@record_function("openai v1 sync no streaming")
@record_action("openai v1 sync no streaming")
def call_openai_v1_sync_no_streaming():
client = OpenAI()
chat_completion = client.chat.completions.create(
Expand All @@ -34,7 +34,7 @@ def call_openai_v1_sync_no_streaming():
# raise ValueError("This is an intentional error for testing.")


@record_function("openai v1 sync with streaming")
@record_action("openai v1 sync with streaming")
def call_openai_v1_sync_streaming():
client = OpenAI()
chat_completion = client.chat.completions.create(
Expand Down
6 changes: 3 additions & 3 deletions tests/openai_handlers/_test_llm_tracker_lt_1_sync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from openai import ChatCompletion
import agentops
from agentops import record_function
from agentops import record_action
from packaging.version import parse
from importlib import import_module
import sys
Expand All @@ -19,7 +19,7 @@
print("openai version: ", module_version)


@record_function("openai v0 sync no streaming")
@record_action("openai v0 sync no streaming")
def call_openai_v0_sync_no_streaming():
chat_completion = ChatCompletion.create(
model="gpt-3.5-turbo",
Expand All @@ -33,7 +33,7 @@ def call_openai_v0_sync_no_streaming():
# raise ValueError("This is an intentional error for testing.")


@record_function("openai v0 sync with streaming")
@record_action("openai v0 sync with streaming")
def call_openai_v0_sync_with_streaming():
chat_completion = ChatCompletion.create(
model="gpt-3.5-turbo",
Expand Down
Loading

0 comments on commit beaecb9

Please sign in to comment.