Skip to content

Commit

Permalink
moar docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bboynton97 committed Jul 2, 2024
1 parent 990cfb9 commit 1b82054
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def init(
auto_start_session=True,
inherited_session_id: Optional[str] = None,
skip_auto_end_session: Optional[bool] = False,
) -> Union[str, None]:
) -> Union[Session, None]:
"""
Initializes the AgentOps singleton pattern.
Expand Down
4 changes: 3 additions & 1 deletion agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,9 @@ def _safe_get_session(self) -> Session:

elif len(self._sessions) > 1:
raise ValueError(
"If multiple sessions exist, you must use session.function(). Example: session.add_tags(...) instead of agentops.add_tags(...)"
"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"
)

return session
Expand Down
20 changes: 10 additions & 10 deletions agentops/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def __init__(
self.queue = []

self.stop_flag = threading.Event()
self.thread = threading.Thread(target=self.run)
self.thread = threading.Thread(target=self._run)
self.thread.daemon = True
self.thread.start()

self._start_session()

def set_session_video(self, video: str) -> None:
def set_video(self, video: str) -> None:
"""
Sets a url to the video recording of the session.
Expand All @@ -74,7 +74,7 @@ def end_session(

self.stop_flag.set()
self.thread.join(timeout=1)
self.flush_queue()
self._flush_queue()

with self.lock:
payload = {"session": self.__dict__}
Expand Down Expand Up @@ -136,16 +136,16 @@ def record(potato, event: Union[Event, ErrorEvent]):
potato.record(event)
event.trigger_event = None # removes trigger_event from serialization

Check warning on line 137 in agentops/session.py

View check run for this annotation

Codecov / codecov/patch

agentops/session.py#L134-L137

Added lines #L134 - L137 were not covered by tests

potato.add_event(event.__dict__)
potato._add_event(event.__dict__)

def add_event(self, event: dict) -> None:
def _add_event(self, event: dict) -> None:
with self.lock:
self.queue.append(event)

if len(self.queue) >= self.config.max_queue_size:
self.flush_queue()
self._flush_queue()

Check warning on line 146 in agentops/session.py

View check run for this annotation

Codecov / codecov/patch

agentops/session.py#L146

Added line #L146 was not covered by tests

def reauthorize_jwt(self) -> Union[str, None]:
def _reauthorize_jwt(self) -> Union[str, None]:
with self.lock:
payload = {"session_id": self.session_id}
serialized_payload = json.dumps(filter_unjsonable(payload)).encode("utf-8")
Expand Down Expand Up @@ -198,7 +198,7 @@ def _update_session(self) -> None:
jwt=self.jwt,
)

def flush_queue(self) -> None:
def _flush_queue(self) -> None:
with self.lock:
queue_copy = copy.deepcopy(self.queue) # Copy the current items
self.queue = []
Expand All @@ -220,11 +220,11 @@ def flush_queue(self) -> None:
logger.debug(serialized_payload)
logger.debug("</AGENTOPS_DEBUG_OUTPUT>\n")

def run(self) -> None:
def _run(self) -> None:
while not self.stop_flag.is_set():
time.sleep(self.config.max_wait_time / 1000)
if self.queue:
self.flush_queue()
self._flush_queue()

def create_agent(self, name, agent_id):
if agent_id is None:
Expand Down
39 changes: 31 additions & 8 deletions docs/v1/concepts/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ The AgentOps dashboard provides detailed insights at the session level, includin

**There must be an active session in order to use AgentOps.**

<card>
</card>

---

## `Session`

#### Properties
### Properties

Sessions possess the following attributes:
- **ID**: A unique identifier for the session.
Expand All @@ -31,15 +28,41 @@ Optionally, sessions may include:
- **Host Environment**: Automatically gathers basic information about the system on which the session ran.
- **Video**: If applicable, an optional video recording of the session.

### Methods
#### `end_session`
**Params**
- **end_state** (str, enum): Success|Failure|Indeterminate
- **end_state_reason** (optional, str): additional notes on end state

**Returns** (str): Total cost of session in USD

#### `record`
**Params**
- **event** ([Event](/v1/concepts/events#event-class)): The Event to record as part of the session


#### `add_tags`
**Params**
- **tags** (List[str]): a list of tags to assign to append to the current tags

#### `set_tags`
**Params**
- **tags** (List[str]): a list of tags to assign to append to set

_Note: Overrides any current tags_


## Starting a Session
When you call `agentops.init()`, a session is automatically started.
Calling `agentops.init(auto_start_session=False)` will initialize the AgentOps SDK but not start a session.

To start a session later, call `agentops.start_session()` [(reference)](/v1/usage/sdk-reference/#start-session)

Both `agentops.init()` and `agentops.start_session()` works as a factory pattern and returns a `Session` object. The above methods can all be called on this session object.

## Ending a Session
If a process ends without any call to agentops, it will show in the dashboard as `Indeterminate`.
To end with a state, call `agentops.end_session()` [(reference)](/v1/usage/sdk-reference/#end-session)
To end with a state, call either `agentops.end_session(...)` [(reference)](/v1/usage/sdk-reference/#end-session) if only one session is in use. Otherwise use `session.end_session(...)`

## Inherited Sessions
When working with multiple agents running in different processes, it's possible to initialize AgentOps or start a session
Expand All @@ -53,11 +76,11 @@ You can retrieve the current session_id by assigning the returned value from `in
<CodeGroup>
```python python
import agentops
session_id = agentops.init()
# pass session_id to the other process
session = agentops.init()
# pass session.session_id to the other process

# -- other process --
session_id = retrieve_session_id()
session_id = retrieve_session_id() # <-- your function
agentops.init(inherited_session_id=<id>)
```
</CodeGroup>
Expand Down

0 comments on commit 1b82054

Please sign in to comment.