Skip to content

Commit

Permalink
Merge pull request #1003 from parea-ai/refetch-project-uuid
Browse files Browse the repository at this point in the history
refetch project uuid if missing
  • Loading branch information
jalexanderII authored Jul 22, 2024
2 parents bbca12c + defb0e1 commit c217ea7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
21 changes: 16 additions & 5 deletions parea/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,34 @@ class Parea:

def __attrs_post_init__(self):
self._client.set_api_key(self.api_key)
parea_logger.set_client(self._client)

if self.api_key:
try:
project_api_response: CreateGetProjectResponseSchema = self._create_or_get_project(self.project_name)
if project_api_response.was_created:
print(f"Created project {project_api_response.name}")
self._project = structure(asdict(project_api_response), ProjectSchema)
parea_logger.set_project_uuid(self.project_uuid)
parea_logger.set_project_uuid(self._project.uuid, self.project_name)
except httpx.HTTPStatusError as e:
if e.response.status_code == 502:
logger.error("Error creating Parea project please try again")
else:
raise
parea_logger.set_client(self._client)
else:
logger.warning("No API key found. Parea client will not be able to send data to the Parea API.")

def _get_project_uuid(self) -> Optional[str]:
if not (self._project and self._project.uuid):
project_api_response: CreateGetProjectResponseSchema = self._create_or_get_project(self.project_name or "default")
self._project = structure(asdict(project_api_response), ProjectSchema)
parea_logger.set_project_uuid(self._project.uuid, self.project_name)
try:
return self._project.uuid
except Exception as e:
logger.error(f"Parea: Error getting project uuid for project {self.project_name}: {e}")
return None

def wrap_openai_client(self, client: "OpenAI", integration: Optional[str] = None) -> None:
"""Only necessary for instance client with OpenAI version >= 1.0.0"""
from parea.wrapper import OpenAIWrapper
Expand Down Expand Up @@ -156,12 +167,12 @@ def integrate_with_sglang(self):

def _add_project_uuid_to_data(self, data) -> dict:
data_dict = asdict(data)
data_dict["project_uuid"] = self._project.uuid
data_dict["project_uuid"] = self.project_uuid
return data_dict

@property
def project_uuid(self) -> str:
return self._project.uuid
return self._get_project_uuid()

def completion(self, data: Completion) -> CompletionResponse:
data = self._update_data_and_trace(data)
Expand Down Expand Up @@ -431,7 +442,7 @@ def _update_data_and_trace(self, data: Completion) -> Completion:
data = serialize_metadata_values(data)
inference_id = gen_trace_id()
data.inference_id = inference_id
data.project_uuid = self._project.uuid
data.project_uuid = self.project_uuid

try:
parent_trace_id = get_current_trace_id()
Expand Down
35 changes: 29 additions & 6 deletions parea/parea_logger.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from typing import Any, Dict
from typing import Any, Dict, Optional

import json
import logging
import os

from attrs import asdict, define, field
from cattrs import structure

from parea.api_client import HTTPClient
from parea.constants import PAREA_OS_ENV_EXPERIMENT_UUID
from parea.helpers import serialize_metadata_values
from parea.schemas.log import TraceIntegrations
from parea.schemas.models import TraceLog, UpdateLog
from parea.schemas.models import CreateGetProjectResponseSchema, TraceLog, UpdateLog
from parea.utils.universal_encoder import json_dumps

logger = logging.getLogger()

LOG_ENDPOINT = "/trace_log"
VENDOR_LOG_ENDPOINT = "/trace_log/{vendor}"

Expand All @@ -20,12 +24,31 @@
class PareaLogger:
_client: HTTPClient = field(init=False, default=None)
_project_uuid: str = field(init=False, default=None)
_project_name: str = field(init=False, default=None)

def set_client(self, client: HTTPClient) -> None:
self._client = client

def set_project_uuid(self, project_uuid: str) -> None:
def set_project_uuid(self, project_uuid: str, project_name: str) -> None:
self._project_uuid = project_uuid
self._project_name = project_name

def _get_project_uuid(self) -> Optional[str]:
if not self._project_uuid:
self._project_uuid = self._create_or_get_project(self._project_name or "default").uuid
try:
return self._project_uuid
except Exception as e:
logger.error(f"PareaLogger: Error getting project uuid for project {self._project_name}: {e}")
return None

def _create_or_get_project(self, name: str) -> CreateGetProjectResponseSchema:
r = self._client.request(
"POST",
"/project",
data={"name": name},
)
return structure(r.json(), CreateGetProjectResponseSchema)

def update_log(self, data: UpdateLog) -> None:
data = serialize_metadata_values(data)
Expand All @@ -37,7 +60,7 @@ def update_log(self, data: UpdateLog) -> None:

def record_log(self, data: TraceLog) -> None:
data = serialize_metadata_values(data)
data.project_uuid = self._project_uuid
data.project_uuid = self._get_project_uuid()
self._client.request(
"POST",
LOG_ENDPOINT,
Expand All @@ -60,7 +83,7 @@ def default_log(self, data: TraceLog) -> None:
self.record_log(data)

def record_vendor_log(self, data: Dict[str, Any], vendor: TraceIntegrations) -> None:
data["project_uuid"] = self._project_uuid
data["project_uuid"] = self._get_project_uuid()
if experiment_uuid := os.getenv(PAREA_OS_ENV_EXPERIMENT_UUID, None):
data["experiment_uuid"] = experiment_uuid
self._client.add_integration("langchain")
Expand All @@ -71,7 +94,7 @@ def record_vendor_log(self, data: Dict[str, Any], vendor: TraceIntegrations) ->
)

async def arecord_vendor_log(self, data: Dict[str, Any], vendor: TraceIntegrations) -> None:
data["project_uuid"] = self._project_uuid
data["project_uuid"] = self._get_project_uuid()
if experiment_uuid := os.getenv(PAREA_OS_ENV_EXPERIMENT_UUID, None):
data["experiment_uuid"] = experiment_uuid
self._client.add_integration("langchain")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "parea-ai"
packages = [{ include = "parea" }]
version = "0.2.187"
version = "0.2.188"
description = "Parea python sdk"
readme = "README.md"
authors = ["joel-parea-ai <[email protected]>"]
Expand Down

0 comments on commit c217ea7

Please sign in to comment.