AnthropicallyHuggingFaces's picture
Add LangSmith client
7678749 verified
Raw History Blame Contribute Delete
1.2 kB
"""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()