Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
get the run_id from the http header and log run_id by default for eve…
Browse files Browse the repository at this point in the history
…ry event (#77)

* wyvern run_id from the header

* add run_id to all the events

* string for the log event run_id

* v0.0.23
  • Loading branch information
wintonzheng authored Oct 3, 2023
1 parent 0db764d commit 5cfc424
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "wyvern-ai"
version = "0.0.22"
version = "0.0.23"
description = ""
authors = ["Wyvern AI <[email protected]>"]
readme = "README.md"
Expand Down
5 changes: 4 additions & 1 deletion wyvern/components/candidates/candidate_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ async def execute(
current_span = tracer.current_span()
if current_span:
current_span.set_tag("candidate_size", len(input.scored_candidates))
url_path = request_context.ensure_current_request().url_path
wyvern_request = request_context.ensure_current_request()
url_path = wyvern_request.url_path
run_id = wyvern_request.run_id

def candidate_events_generator() -> List[CandidateEvent]:
timestamp = datetime.utcnow()
candidate_events = [
CandidateEvent(
request_id=input.request.request_id,
run_id=run_id,
api_source=url_path,
event_timestamp=timestamp,
event_data=CandidateEventData(
Expand Down
12 changes: 11 additions & 1 deletion wyvern/components/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
from enum import Enum
from typing import Generic, Optional, TypeVar

from pydantic import BaseModel
from pydantic import BaseModel, Field
from pydantic.generics import GenericModel

from wyvern import request_context

EVENT_DATA = TypeVar("EVENT_DATA", bound=BaseModel)


def generate_run_id() -> str:
curr_wyvern_request = request_context.current()
if curr_wyvern_request is None:
return str(0)
return str(curr_wyvern_request.run_id)


class EventType(str, Enum):
"""Enum for the different types of events that can be logged."""

Expand Down Expand Up @@ -37,6 +46,7 @@ class LoggedEvent(GenericModel, Generic[EVENT_DATA]):
event_timestamp: Optional[datetime]
event_type: EventType
event_data: EVENT_DATA
run_id: str = Field(default_factory=generate_run_id)


class EntityEventData(BaseModel):
Expand Down
5 changes: 4 additions & 1 deletion wyvern/components/features/feature_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ async def execute(
self, input: FeatureEventLoggingRequest[REQUEST_ENTITY], **kwargs
) -> None:
"""Logs feature events."""
url_path = request_context.ensure_current_request().url_path
wyvern_request = request_context.ensure_current_request()
url_path = wyvern_request.url_path
run_id = wyvern_request.run_id

def feature_event_generator():
"""Generates feature events. This is a generator function that's called by the event logger. It's never called directly.
Expand All @@ -76,6 +78,7 @@ def feature_event_generator():
return [
FeatureEvent(
request_id=input.request.request_id,
run_id=run_id,
api_source=url_path,
event_timestamp=timestamp,
event_data=FeatureLogEventData(
Expand Down
5 changes: 4 additions & 1 deletion wyvern/components/impressions/impression_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ async def execute(
current_span = tracer.current_span()
if current_span:
current_span.set_tag("impression_size", len(input.scored_impressions))
url_path = request_context.ensure_current_request().url_path
wyvern_request = request_context.ensure_current_request()
url_path = wyvern_request.url_path
run_id = wyvern_request.run_id

def impression_events_generator() -> List[ImpressionEvent]:
timestamp = datetime.utcnow()
impression_events = [
ImpressionEvent(
request_id=input.request.request_id,
run_id=run_id,
api_source=url_path,
event_timestamp=timestamp,
event_data=ImpressionEventData(
Expand Down
3 changes: 3 additions & 0 deletions wyvern/components/models/model_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async def execute(self, input: INPUT_TYPE, **kwargs) -> MODEL_OUTPUT:
api_source = wyvern_request.url_path
request_id = self._get_request_id(input)
model_output = await self.inference(input, **kwargs)
run_id = wyvern_request.run_id

if self.cache_output:
wyvern_request.cache_model_output(self.name, model_output.data)
Expand All @@ -119,6 +120,7 @@ def events_generator() -> List[ModelEvent]:
all_events.append(
ModelEvent(
request_id=request_id,
run_id=run_id,
api_source=api_source,
event_timestamp=timestamp,
event_data=ModelEventData(
Expand All @@ -134,6 +136,7 @@ def events_generator() -> List[ModelEvent]:
all_events.append(
ModelEvent(
request_id=request_id,
run_id=run_id,
api_source=api_source,
event_timestamp=timestamp,
event_data=ModelEventData(
Expand Down
2 changes: 2 additions & 0 deletions wyvern/event_logging/event_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ def log_custom_events(events: List[ENTITY_EVENT_DATA_TYPE]) -> None:
request = request_context.ensure_current_request()
api_source = request.url_path
request_id = request.request_id
run_id = request.run_id

def event_generator() -> List[LoggedEvent[Any]]:
timestamp = datetime.utcnow()
return [
CustomEvent(
request_id=request_id,
run_id=run_id,
api_source=api_source,
event_timestamp=timestamp,
event_data=event,
Expand Down
2 changes: 2 additions & 0 deletions wyvern/experimentation/providers/eppo_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ def event_generator() -> List[LoggedEvent[ExperimentationEventData]]:
timestamp = datetime.utcnow()
request_id = request.request_id
api_source = request.url_path
run_id = request.run_id

return [
ExperimentationEvent(
request_id=request_id,
run_id=run_id,
api_source=api_source,
event_timestamp=timestamp,
event_data=ExperimentationEventData(
Expand Down
10 changes: 8 additions & 2 deletions wyvern/web_frameworks/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import logging
import time
from contextlib import asynccontextmanager
from typing import Dict, Type, Union
from typing import Annotated, Dict, Type, Union

import uvicorn
from fastapi import BackgroundTasks, FastAPI, HTTPException, Request
from fastapi import BackgroundTasks, FastAPI, Header, HTTPException, Request
from fastapi.responses import JSONResponse
from pydantic import ValidationError

Expand Down Expand Up @@ -131,6 +131,10 @@ async def post(
data: root_component.REQUEST_SCHEMA_CLASS, # type: ignore
fastapi_request: Request,
background_tasks: BackgroundTasks,
x_wyvern_run_id: Annotated[
int,
Header(),
] = 0,
) -> root_component.RESPONSE_SCHEMA_CLASS: # type: ignore
"""
The main entrypoint for the route component. This will parse the request payload, set the WyvernRequest in
Expand All @@ -153,10 +157,12 @@ async def post(
request_id = None
if isinstance(data, BaseWyvernRequest):
request_id = data.request_id

wyvern_req = WyvernRequest.parse_fastapi_request(
json=data,
req=fastapi_request,
request_id=request_id,
run_id=str(x_wyvern_run_id),
)
request_context.set(wyvern_req)

Expand Down
3 changes: 3 additions & 0 deletions wyvern/wyvern_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class WyvernRequest:
]

request_id: Optional[str] = None
run_id: str = "0"

# TODO: params

Expand All @@ -69,6 +70,7 @@ def parse_fastapi_request(
cls,
json: BaseModel,
req: fastapi.Request,
run_id: str = "0",
request_id: Optional[str] = None,
) -> WyvernRequest:
"""
Expand All @@ -93,6 +95,7 @@ def parse_fastapi_request(
feature_map=FeatureMap(feature_map={}),
model_output_map={},
request_id=request_id,
run_id=run_id,
)

def cache_model_output(
Expand Down

0 comments on commit 5cfc424

Please sign in to comment.