Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parent key support #117

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion agentops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# agentops/__init__.py
from os import environ
from typing import Optional, List

from .client import Client
Expand All @@ -12,13 +13,21 @@


def init(api_key: Optional[str] = None,
parent_key: Optional[str] = None,
tags: Optional[List[str]] = None,
endpoint: Optional[str] = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai'),
max_wait_time: Optional[int] = 1000,
max_queue_size: Optional[int] = 100,
override=True,
auto_start_session=True):
Client(api_key, tags, endpoint, max_wait_time, max_queue_size, override, auto_start_session)

if not parent_key:
parent_key = environ.get('AGENTOPS_PARENT_KEY', None)

if not endpoint:
endpoint = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai')
bboynton97 marked this conversation as resolved.
Show resolved Hide resolved

Client(api_key, parent_key, tags, endpoint, max_wait_time, max_queue_size, override, auto_start_session)


def end_session(end_state: str = Field("Indeterminate",
Expand Down
4 changes: 3 additions & 1 deletion agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Client(metaclass=MetaClient):
"""

def __init__(self, api_key: Optional[str] = None,
parent_key: Optional[str] = environ.get('AGENTOPS_PARENT_KEY', None),
tags: Optional[List[str]] = None,
endpoint: Optional[str] = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai'),
max_wait_time: Optional[int] = 1000,
Expand All @@ -64,14 +65,15 @@ def __init__(self, api_key: Optional[str] = None,
return

self.config = Configuration(api_key or environ.get('AGENTOPS_API_KEY'),
parent_key,
endpoint,
max_wait_time,
max_queue_size)

self._handle_unclean_exits()

if auto_start_session:
self.start_session(tags)
self.start_session(tags, self.config)

if override:
if 'openai' in sys.modules:
Expand Down
10 changes: 10 additions & 0 deletions agentops/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class Configuration:
"""

def __init__(self, api_key: str,
parent_key: Optional[str],
endpoint: Optional[str] = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai'),
max_wait_time: Optional[int] = 1000, max_queue_size: Optional[int] = 100):
self._api_key: str = api_key
self._endpoint = endpoint
self._max_wait_time = max_wait_time
self._max_queue_size = max_queue_size
self._parent_key: Optional[str] = parent_key

@property
def api_key(self) -> str:
Expand Down Expand Up @@ -107,3 +109,11 @@ def max_queue_size(self, value: int):
value (int): The new maximum size of the event queue.
"""
self._max_queue_size = value

@property
def parent_key(self):
return self._parent_key

@parent_key.setter
def parent_key(self, value: str):
self._parent_key = value
8 changes: 6 additions & 2 deletions agentops/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,20 @@ def get_status(code: int) -> HttpStatus:
class HttpClient:

@staticmethod
def post(url: str, payload: bytes, api_key: Optional[str] = None, header=None) -> Response:
def post(url: str, payload: bytes, api_key: Optional[str] = None, parent_key: Optional[str] = None,
header=None) -> Response:
result = Response()
try:
# Create request session with retries configured
request_session = requests.Session()
request_session.mount(url, HTTPAdapter(max_retries=retry_config))

if api_key != None:
if api_key is not None:
JSON_HEADER["X-Agentops-Auth"] = api_key

if parent_key is not None:
JSON_HEADER["X-Agentops-Parent-Key"] = parent_key

res = request_session.post(url, data=payload,
headers=JSON_HEADER, timeout=20)

Expand Down
14 changes: 9 additions & 5 deletions agentops/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def flush_queue(self) -> None:
serialized_payload = safe_serialize(payload).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/events',
serialized_payload,
self.config.api_key)
self.config.api_key,
self.config.parent_key)

def start_session(self, session: Session) -> None:
self._session = session
Expand All @@ -50,7 +51,8 @@ def start_session(self, session: Session) -> None:
serialized_payload = json.dumps(filter_unjsonable(payload)).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/sessions',
serialized_payload,
self.config.api_key)
self.config.api_key,
self.config.parent_key)

def end_session(self, session: Session) -> None:
self.stop_flag.set()
Expand All @@ -66,7 +68,8 @@ def end_session(self, session: Session) -> None:
HttpClient.post(f'{self.config.endpoint}/sessions',
json.dumps(filter_unjsonable(
payload)).encode("utf-8"),
self.config.api_key)
self.config.api_key,
self.config.parent_key)

def update_session(self, session: Session) -> None:
with self.lock:
Expand All @@ -78,7 +81,7 @@ def update_session(self, session: Session) -> None:
json.dumps(filter_unjsonable(
payload)).encode("utf-8"),
self.config.api_key,
self.config.org_key)
self.config.parent_key)

def create_agent(self, agent_id, name):
payload = {
Expand All @@ -91,7 +94,8 @@ def create_agent(self, agent_id, name):
safe_serialize(payload).encode("utf-8")
HttpClient.post(f'{self.config.endpoint}/agents',
serialized_payload,
self.config.api_key)
self.config.api_key,
self.config.parent_key)

def run(self) -> None:
while not self.stop_flag.is_set():
Expand Down