Skip to content

Commit

Permalink
Restore: "feat(sdk): user feedback scores" (traceloop#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirga authored and 5war00p committed Dec 31, 2023
1 parent 1e3a62c commit 7ad0902
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/sample-app/sample_app/methods_decorated_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,7 @@ def joke_workflow():
signature = generate_signature(pirate_joke)
print(pirate_joke + "\n\n" + signature)

Traceloop.report_score("chat_id", "chat_9876", 1)


joke_workflow()
28 changes: 27 additions & 1 deletion packages/traceloop-sdk/traceloop/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Traceloop:
AUTO_CREATED_URL = str(Path.home() / ".cache" / "traceloop" / "auto_created_url")

__tracer_wrapper: TracerWrapper
__fetcher: Fetcher

@staticmethod
def init(
Expand All @@ -57,7 +58,8 @@ def init(
and not exporter
and not processor
):
Fetcher(base_url=api_endpoint, api_key=api_key).run()
Traceloop.__fetcher = Fetcher(base_url=api_endpoint, api_key=api_key)
Traceloop.__fetcher.run()
print(
Fore.GREEN + "Traceloop syncing configuration and prompts" + Fore.RESET
)
Expand Down Expand Up @@ -158,3 +160,27 @@ def set_correlation_id(correlation_id: str) -> None:

def set_association_properties(properties: dict) -> None:
set_association_properties(properties)

def report_score(
association_property_name: str,
association_property_id: str,
score: float,
):
if not Traceloop.__fetcher:
print(
Fore.RED
+ "Error: Cannot report score. Missing Traceloop API key,"
+ " go to https://https://app.traceloop.com/settings/api-keys to create one"
)
print("Set the TRACELOOP_API_KEY environment variable to the key")
print(Fore.RESET)
return

Traceloop.__fetcher.post(
"score",
{
"entity_name": f"traceloop.association.properties.{association_property_name}",
"entity_id": association_property_id,
"score": score,
},
)
23 changes: 20 additions & 3 deletions packages/traceloop-sdk/traceloop/sdk/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import requests

from threading import Thread, Event
from typing import Optional
from typing import Dict, Optional
from tenacity import (
RetryError,
retry,
Expand Down Expand Up @@ -61,6 +61,9 @@ def run(self):
self._exit_monitor.start()
self._poller_thread.start()

def post(self, api: str, body: Dict[str, str]):
post_url(f"{self._base_url}/v1/traceloop/{api}", self._api_key, body)


class RetryIfServerError(retry_if_exception):
def __init__(
Expand Down Expand Up @@ -100,6 +103,20 @@ def fetch_url(url: str, api_key: str):
return response.json()


def post_url(url: str, api_key: str, body: Dict[str, str]):
response = requests.post(
url,
headers={
"Authorization": f"Bearer {api_key}",
"X-Traceloop-SDK-Version": __version__,
},
json=body,
)

if response.status_code != 200:
raise requests.exceptions.HTTPError(response=response)


def thread_func(
prompt_registry: PromptRegistry,
content_allow_list: ContentAllowList,
Expand All @@ -124,10 +141,10 @@ def refresh_data(
prompt_registry: PromptRegistry,
content_allow_list: ContentAllowList,
):
response = fetch_url(f"{base_url}/v1/prompts", api_key)
response = fetch_url(f"{base_url}/v1/traceloop/prompts", api_key)
prompt_registry.load(response)

response = fetch_url(f"{base_url}/v1/config/pii/tracing-allow-list", api_key)
response = fetch_url(f"{base_url}/v1/traceloop/pii/tracing-allow-list", api_key)
content_allow_list.load(response)


Expand Down

0 comments on commit 7ad0902

Please sign in to comment.