From 8a90a3c67c5e6309f55482b44860951834f5757e Mon Sep 17 00:00:00 2001 From: ethan-tonic Date: Tue, 2 Apr 2024 16:15:22 -0400 Subject: [PATCH] Fixed run uploads --- pyproject.toml | 2 +- tonic_ragas_logger/ragas_validate_api.py | 42 +++++++++++++----------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f2840b0..5d79eb4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "tonic-ragas-logger" -version = "1.0.3" +version = "1.1.0" description = "Uploads results from ragas to Tonic Validate." authors = [] readme = "README.md" diff --git a/tonic_ragas_logger/ragas_validate_api.py b/tonic_ragas_logger/ragas_validate_api.py index a92a501..947acc7 100644 --- a/tonic_ragas_logger/ragas_validate_api.py +++ b/tonic_ragas_logger/ragas_validate_api.py @@ -1,4 +1,4 @@ -from typing import Optional, Dict +from typing import Any, List, Optional, Dict from tonic_validate import Run, RunData from tonic_ragas_logger.config import Config @@ -33,7 +33,11 @@ def __init__( self.client = HttpClient(self.config.TONIC_VALIDATE_BASE_URL, api_key) def upload_results( - self, project_id: str, results: Result, run_metadata: Dict[str, str] = {} + self, + project_id: str, + results: Result, + run_metadata: Dict[str, Any] = {}, + tags: List[str] = [], ) -> str: """Uploads results to a Tonic Validate project. @@ -47,21 +51,15 @@ def upload_results( Metadata to attach to the run. If the values are not strings, then they are converted to strings before making the request. """ - # ensure run_metadata is Dict[str, str] - processed_run_metadata = { - str(key): str(value) for key, value in run_metadata.items() - } - run_response = self.client.http_post(f"/projects/{project_id}/runs") - run_response = self.client.http_put( - f"/projects/{project_id}/runs/{run_response['id']}", - data={"run_metadata": processed_run_metadata}, - ) run = self.__convert_to_run(results) - for run_data in run.run_data: - _ = self.client.http_post( - f"/projects/{project_id}/runs/{run_response['id']}/logs", - data=run_data.to_dict(), - ) + run_response = self.client.http_post( + f"/projects/{project_id}/runs/with_data", + data={ + "run_metadata": run_metadata, + "tags": tags, + "data": [run_data.to_dict() for run_data in run.run_data], + }, + ) return run_response["id"] def __convert_to_run(self, results: Result) -> Run: @@ -74,7 +72,8 @@ def __convert_to_run(self, results: Result) -> Run: """ try: overall_scores = { - str(score): float(value) for score, value in results.items() + str(score): 0 if value is None else float(value) + for score, value in results.items() } except ValueError: raise ValueError( @@ -95,7 +94,7 @@ def __convert_to_run(self, results: Result) -> Run: for i in range(len(results.scores)): try: scores: Dict[str, float | None] = { - str(score): float(value) + str(score): 0 if value is None else float(value) for score, value in results.scores[i].items() } except ValueError: @@ -112,4 +111,9 @@ def __convert_to_run(self, results: Result) -> Run: ) ) - return Run(overall_scores=overall_scores, run_data=run_data, id=None) + return Run( + overall_scores=overall_scores, + run_data=run_data, + llm_evaluator=None, + id=None, + )