Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
siyangqiu committed Jul 30, 2024
1 parent f3912cc commit adaf6c7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
16 changes: 15 additions & 1 deletion agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def init(
Initializes the AgentOps singleton pattern.
Args:
api_key (str, optional): API Key for AgentOps services. If none is provided, key will
be read from the AGENTOPS_API_KEY environment variable.
parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization. If none is provided, key will
Expand Down Expand Up @@ -108,6 +107,21 @@ def configure(
auto_start_session: Optional[bool] = None,
skip_auto_end_session: Optional[bool] = None,
):
"""
Configure the AgentOps Client
Args:
api_key (str, optional): API Key for AgentOps services.
parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization.
endpoint (str, optional): The endpoint for the AgentOps service.
max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue.
max_queue_size (int, optional): The maximum size of the event queue
default_tags (List[str], optional): Default tags for the sessions that can be used for grouping or sorting later (e.g. ["GPT-4"]).
instrument_llm_calls (bool, optional): Whether to instrument LLM calls and emit LLMEvents.
auto_start_session (bool, optional): Whether to start a session automatically when the client is created.
skip_auto_end_session (bool, optional): Don't automatically end session based on your framework's decision-making
(i.e. Crew determining when tasks are complete and ending the session)
"""
Client().configure(
api_key=api_key,
parent_key=parent_key,
Expand Down
23 changes: 13 additions & 10 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from .session import Session, active_sessions
from .host_env import get_host_env
from .log_config import logger
from .log_config import logger, unsuppress_logs
from .meta_client import MetaClient
from .config import Configuration
from .llm_tracker import LlmTracker
Expand Down Expand Up @@ -82,15 +82,7 @@ def configure(
)

def initialize(self) -> Union[Session, None]:
logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL", "INFO")
log_levels = {
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"DEBUG": logging.DEBUG,
}
logger.setLevel(log_levels.get(logging_level, "INFO"))
unsuppress_logs()

for message in Client()._pre_init_messages:
logger.warning(message)
Expand Down Expand Up @@ -180,6 +172,15 @@ def add_default_tags(self, tags: List[str]) -> None:
"""
self._config.default_tags.update(tags)

def get_default_tags(self) -> List[str]:
"""
Append default tags at runtime.
Args:
tags (List[str]): The list of tags to set.
"""
return list(self._config.default_tags)

def record(self, event: Union[Event, ErrorEvent]) -> None:
"""
Record an event with the AgentOps service.
Expand Down Expand Up @@ -377,6 +378,8 @@ def _update_session(self, session: Session):
] = session

def _safe_get_session(self) -> Optional[Session]:
if not self.is_initialized:
return None
if len(self._sessions) == 1:
return self._sessions[0]

Expand Down
10 changes: 7 additions & 3 deletions agentops/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def post(
except requests.exceptions.Timeout:
result.code = 408
result.status = HttpStatus.TIMEOUT
raise ApiServerException("Could not reach API server - connection timed out")
raise ApiServerException(
"Could not reach API server - connection timed out"
)
except requests.exceptions.HTTPError as e:
try:
result.parse(e.response)
Expand All @@ -105,10 +107,12 @@ def post(
raise ApiServerException(f"RequestException: {e}")

if result.code == 401:
raise ApiServerException(f"API server: invalid API key: {api_key}. Find your API key at https://app.agentops.ai/settings/projects")
raise ApiServerException(
f"API server: invalid API key: {api_key}. Find your API key at https://app.agentops.ai/settings/projects"
)
if result.code == 400:
if "message" in result.body:
raise ApiServerException(f"API server: {result.body["message"]}")
raise ApiServerException(f"API server: {result.body['message']}")
else:
raise ApiServerException(f"API server: {result.body}")
if result.code == 500:
Expand Down
12 changes: 12 additions & 0 deletions agentops/log_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ def format(self, record):
file_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)


def unsuppress_logs():
logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL", "INFO")
log_levels = {
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"DEBUG": logging.DEBUG,
}
logger.setLevel(log_levels.get(logging_level, "INFO"))
2 changes: 1 addition & 1 deletion tach.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ modules:
- path: agentops.http_client
depends_on:
- path: agentops.exceptions
- path: agentops.log_config
- path: agentops.langchain_callback_handler
depends_on: []
- path: agentops.llm_tracker
Expand All @@ -75,6 +74,7 @@ modules:
- path: agentops.log_config
- path: agentops.session
depends_on:
- path: agentops.client
- path: agentops.config
- path: agentops.enums
- path: agentops.event
Expand Down

0 comments on commit adaf6c7

Please sign in to comment.