File size: 1,200 Bytes
7678749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""LangSmith client - tracing, datasets, evaluation."""
import os
import requests

BASE = os.getenv("LANGSMITH_ENDPOINT", "https://api.smith.langchain.com")
KEY = os.getenv("LANGSMITH_API_KEY")


def _h():
    if not KEY:
        raise RuntimeError("LANGSMITH_API_KEY not set")
    return {"x-api-key": KEY, "Content-Type": "application/json"}


def list_runs(limit=50, **params):
    return requests.get(f"{BASE}/runs", headers=_h(), params={"limit": limit, **params}, timeout=60).json()


def get_run(run_id: str):
    return requests.get(f"{BASE}/runs/{run_id}", headers=_h(), timeout=60).json()


def create_dataset(name, description=""):
    return requests.post(f"{BASE}/datasets", headers=_h(), json={"name": name, "description": description}, timeout=60).json()


def upload_examples(dataset_id, examples):
    return requests.post(f"{BASE}/datasets/{dataset_id}/examples", headers=_h(), json=examples, timeout=120).json()


def evaluate(run_name, evaluator, dataset_id, project_id=None):
    return requests.post(f"{BASE}/eval", headers=_h(), json={
        "evaluators": [evaluator], "runName": run_name,
        "datasetId": dataset_id, "projectId": project_id,
    }, timeout=120).json()