1414
1515from ragas .callbacks import ChainRunEncoder , parse_run_traces
1616from ragas .cost import CostCallbackHandler
17+ from ragas .exceptions import UploadException
1718from ragas .messages import AIMessage , HumanMessage , ToolCall , ToolMessage
18- from ragas .utils import RAGAS_API_URL , safe_nanmean
19+ from ragas .sdk import RAGAS_API_URL , RAGAS_APP_URL , upload_packet
20+ from ragas .utils import safe_nanmean
1921
2022if t .TYPE_CHECKING :
2123 from pathlib import Path
@@ -509,8 +511,6 @@ def total_cost(
509511 def upload (self , base_url : str = RAGAS_API_URL , verbose : bool = True ) -> str :
510512 from datetime import datetime , timezone
511513
512- import requests
513-
514514 timestamp = datetime .now (timezone .utc ).isoformat ()
515515 root_trace = [
516516 trace for trace in self .ragas_traces .values () if trace .parent_run_id is None
@@ -523,19 +523,28 @@ def upload(self, base_url: str = RAGAS_API_URL, verbose: bool = True) -> str:
523523 },
524524 cls = ChainRunEncoder ,
525525 )
526-
527- response = requests .post (
528- f"{ base_url } /alignment/evaluation" ,
529- data = packet ,
530- headers = {"Content-Type" : "application/json" },
526+ response = upload_packet (
527+ path = "/alignment/evaluation" ,
528+ data_json_string = packet ,
529+ base_url = base_url ,
531530 )
532531
533- if response .status_code != 200 :
534- raise Exception (f"Failed to upload results: { response .text } " )
535-
532+ # check status codes
536533 evaluation_endpoint = (
537- f"https://app.ragas.io /alignment/evaluation/{ root_trace .run_id } "
534+ f"{ RAGAS_APP_URL } /alignment/evaluation/{ root_trace .run_id } "
538535 )
536+ if response .status_code == 409 :
537+ # this evalution already exists
538+ if verbose :
539+ print (f"Evaluation run already exists. View at { evaluation_endpoint } " )
540+ return evaluation_endpoint
541+ elif response .status_code != 200 :
542+ # any other error
543+ raise UploadException (
544+ status_code = response .status_code ,
545+ message = f"Failed to upload results: { response .text } " ,
546+ )
547+
539548 if verbose :
540549 print (f"Evaluation results uploaded! View at { evaluation_endpoint } " )
541550 return evaluation_endpoint
@@ -563,15 +572,13 @@ def __getitem__(self, key):
563572
564573
565574class MetricAnnotation (BaseModel ):
566-
567575 root : t .Dict [str , t .List [SampleAnnotation ]]
568576
569577 def __getitem__ (self , key ):
570578 return SingleMetricAnnotation (name = key , samples = self .root [key ])
571579
572580 @classmethod
573581 def from_json (cls , path , metric_name : t .Optional [str ]) -> "MetricAnnotation" :
574-
575582 dataset = json .load (open (path ))
576583 if metric_name is not None and metric_name not in dataset :
577584 raise ValueError (f"Split { metric_name } not found in the dataset." )
@@ -613,7 +620,6 @@ def select(self, indices: t.List[int]) -> "SingleMetricAnnotation":
613620
614621 @classmethod
615622 def from_json (cls , path ) -> "SingleMetricAnnotation" :
616-
617623 dataset = json .load (open (path ))
618624
619625 return cls (
@@ -622,7 +628,6 @@ def from_json(cls, path) -> "SingleMetricAnnotation":
622628 )
623629
624630 def filter (self , function : t .Optional [t .Callable ] = None ):
625-
626631 if function is None :
627632 function = lambda x : True # noqa: E731
628633
0 commit comments