Evaluation Quickstart 快速上手
运行同步 RAG 评测,提交异步评测 job,并读取报告。
这个快速上手使用内联测试用例,所以无需提前准备数据集也能验证 Evaluation API。下面的 API 片段是 AI 生成的 Python、JavaScript 和 Java 客户端示例。
1. 查看引擎和指标
import osimport requestsBASE_URL = os.getenv("CORTEX_URL", "http://127.0.0.1:8080")TOKEN = os.getenv("CORTEX_TOKEN", "replace_with_token")def auth_headers(): return {"Authorization": f"Bearer {TOKEN}"}for path in ["/v1/eval/engines", "/v1/eval/metrics"]: response = requests.get(f"{BASE_URL}{path}", headers=auth_headers()) response.raise_for_status() print(path, response.json())const BASE_URL = process.env.CORTEX_URL ?? "http://127.0.0.1:8080";const TOKEN = process.env.CORTEX_TOKEN ?? "replace_with_token";const authHeaders = { Authorization: `Bearer ${TOKEN}`,};for (const path of ["/v1/eval/engines", "/v1/eval/metrics"]) { const response = await fetch(`${BASE_URL}${path}`, { headers: authHeaders }); if (!response.ok) throw new Error(await response.text()); console.log(path, await response.json());}import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;public class CortexExample { static final String BASE_URL = System.getenv().getOrDefault("CORTEX_URL", "http://127.0.0.1:8080"); static final String TOKEN = System.getenv().getOrDefault("CORTEX_TOKEN", "replace_with_token"); static final HttpClient HTTP = HttpClient.newHttpClient(); static void print(HttpResponse<String> response) { System.out.println(response.statusCode()); System.out.println(response.body()); } public static void main(String[] args) throws Exception { for (String path : new String[] {"/v1/eval/engines", "/v1/eval/metrics"}) { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL + path)) .header("Authorization", "Bearer " + TOKEN) .GET() .build(); print(HTTP.send(request, HttpResponse.BodyHandlers.ofString())); } }}2. 运行同步 RAG 评测
import osimport requestsBASE_URL = os.getenv("CORTEX_URL", "http://127.0.0.1:8080")TOKEN = os.getenv("CORTEX_TOKEN", "replace_with_token")def auth_headers(): return {"Authorization": f"Bearer {TOKEN}"}payload = { "name": "quickstart-rag-eval", "eval_type": "rag", "engine_id": "deepeval", "input": { "type": "inline_test_cases", "test_cases": [ { "user_input": "What does Cortex Parse do?", "actual_output": "Cortex Parse converts URLs and files into normalized Markdown.", "expected_output": "Parse should mention URLs, files, and Markdown.", "retrieval_contexts": [ "Cortex Parse accepts URLs and files and returns normalized Markdown with metadata." ], "metadata": { "case_id": "rag-001" } } ] }, "target": { "type": "existing_outputs" }, "metrics": [ { "metric_key": "rag.answer_relevance", "threshold": 0.65 }, { "metric_key": "rag.faithfulness", "threshold": 0.65 }, { "metric_key": "rag.contextual_relevance", "threshold": 0.6 } ], "output": { "persist_report_object": True, "include_sample_results": True }}response = requests.post( f"{BASE_URL}/v1/eval/sync", headers={**auth_headers(), "Content-Type": "application/json"}, json=payload,)response.raise_for_status()data = response.json()print(data)const BASE_URL = process.env.CORTEX_URL ?? "http://127.0.0.1:8080";const TOKEN = process.env.CORTEX_TOKEN ?? "replace_with_token";const authHeaders = { Authorization: `Bearer ${TOKEN}`,};const payload = { "name": "quickstart-rag-eval", "eval_type": "rag", "engine_id": "deepeval", "input": { "type": "inline_test_cases", "test_cases": [ { "user_input": "What does Cortex Parse do?", "actual_output": "Cortex Parse converts URLs and files into normalized Markdown.", "expected_output": "Parse should mention URLs, files, and Markdown.", "retrieval_contexts": [ "Cortex Parse accepts URLs and files and returns normalized Markdown with metadata." ], "metadata": { "case_id": "rag-001" } } ] }, "target": { "type": "existing_outputs" }, "metrics": [ { "metric_key": "rag.answer_relevance", "threshold": 0.65 }, { "metric_key": "rag.faithfulness", "threshold": 0.65 }, { "metric_key": "rag.contextual_relevance", "threshold": 0.6 } ], "output": { "persist_report_object": true, "include_sample_results": true }};const response = await fetch(`${BASE_URL}/v1/eval/sync`, { method: "POST", headers: { ...authHeaders, "Content-Type": "application/json" }, body: JSON.stringify(payload),});if (!response.ok) throw new Error(await response.text());const data = await response.json();console.log(data);import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;public class CortexExample { static final String BASE_URL = System.getenv().getOrDefault("CORTEX_URL", "http://127.0.0.1:8080"); static final String TOKEN = System.getenv().getOrDefault("CORTEX_TOKEN", "replace_with_token"); static final HttpClient HTTP = HttpClient.newHttpClient(); static void print(HttpResponse<String> response) { System.out.println(response.statusCode()); System.out.println(response.body()); } public static void main(String[] args) throws Exception { String json = """ { \"name\": \"quickstart-rag-eval\", \"eval_type\": \"rag\", \"engine_id\": \"deepeval\", \"input\": { \"type\": \"inline_test_cases\", \"test_cases\": [ { \"user_input\": \"What does Cortex Parse do?\", \"actual_output\": \"Cortex Parse converts URLs and files into normalized Markdown.\", \"expected_output\": \"Parse should mention URLs, files, and Markdown.\", \"retrieval_contexts\": [ \"Cortex Parse accepts URLs and files and returns normalized Markdown with metadata.\" ], \"metadata\": { \"case_id\": \"rag-001\" } } ] }, \"target\": { \"type\": \"existing_outputs\" }, \"metrics\": [ { \"metric_key\": \"rag.answer_relevance\", \"threshold\": 0.65 }, { \"metric_key\": \"rag.faithfulness\", \"threshold\": 0.65 }, { \"metric_key\": \"rag.contextual_relevance\", \"threshold\": 0.6 } ], \"output\": { \"persist_report_object\": true, \"include_sample_results\": true } } """; HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL + "/v1/eval/sync")) .header("Authorization", "Bearer " + TOKEN) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(json)) .build(); print(HTTP.send(request, HttpResponse.BodyHandlers.ofString())); }}3. 用异步方式提交同一请求
import osimport requestsBASE_URL = os.getenv("CORTEX_URL", "http://127.0.0.1:8080")TOKEN = os.getenv("CORTEX_TOKEN", "replace_with_token")def auth_headers(): return {"Authorization": f"Bearer {TOKEN}"}payload = { "name": "quickstart-rag-eval", "eval_type": "rag", "engine_id": "deepeval", "input": { "type": "inline_test_cases", "test_cases": [ { "user_input": "What does Cortex Parse do?", "actual_output": "Cortex Parse converts URLs and files into normalized Markdown.", "expected_output": "Parse should mention URLs, files, and Markdown.", "retrieval_contexts": [ "Cortex Parse accepts URLs and files and returns normalized Markdown with metadata." ], "metadata": { "case_id": "rag-001" } } ] }, "target": { "type": "existing_outputs" }, "metrics": [ { "metric_key": "rag.answer_relevance", "threshold": 0.65 }, { "metric_key": "rag.faithfulness", "threshold": 0.65 }, { "metric_key": "rag.contextual_relevance", "threshold": 0.6 } ], "output": { "persist_report_object": True, "include_sample_results": True }}response = requests.post( f"{BASE_URL}/v1/eval/jobs", headers={**auth_headers(), "Content-Type": "application/json"}, json=payload,)response.raise_for_status()data = response.json()print(data)const BASE_URL = process.env.CORTEX_URL ?? "http://127.0.0.1:8080";const TOKEN = process.env.CORTEX_TOKEN ?? "replace_with_token";const authHeaders = { Authorization: `Bearer ${TOKEN}`,};const payload = { "name": "quickstart-rag-eval", "eval_type": "rag", "engine_id": "deepeval", "input": { "type": "inline_test_cases", "test_cases": [ { "user_input": "What does Cortex Parse do?", "actual_output": "Cortex Parse converts URLs and files into normalized Markdown.", "expected_output": "Parse should mention URLs, files, and Markdown.", "retrieval_contexts": [ "Cortex Parse accepts URLs and files and returns normalized Markdown with metadata." ], "metadata": { "case_id": "rag-001" } } ] }, "target": { "type": "existing_outputs" }, "metrics": [ { "metric_key": "rag.answer_relevance", "threshold": 0.65 }, { "metric_key": "rag.faithfulness", "threshold": 0.65 }, { "metric_key": "rag.contextual_relevance", "threshold": 0.6 } ], "output": { "persist_report_object": true, "include_sample_results": true }};const response = await fetch(`${BASE_URL}/v1/eval/jobs`, { method: "POST", headers: { ...authHeaders, "Content-Type": "application/json" }, body: JSON.stringify(payload),});if (!response.ok) throw new Error(await response.text());const data = await response.json();console.log(data);import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;public class CortexExample { static final String BASE_URL = System.getenv().getOrDefault("CORTEX_URL", "http://127.0.0.1:8080"); static final String TOKEN = System.getenv().getOrDefault("CORTEX_TOKEN", "replace_with_token"); static final HttpClient HTTP = HttpClient.newHttpClient(); static void print(HttpResponse<String> response) { System.out.println(response.statusCode()); System.out.println(response.body()); } public static void main(String[] args) throws Exception { String json = """ { \"name\": \"quickstart-rag-eval\", \"eval_type\": \"rag\", \"engine_id\": \"deepeval\", \"input\": { \"type\": \"inline_test_cases\", \"test_cases\": [ { \"user_input\": \"What does Cortex Parse do?\", \"actual_output\": \"Cortex Parse converts URLs and files into normalized Markdown.\", \"expected_output\": \"Parse should mention URLs, files, and Markdown.\", \"retrieval_contexts\": [ \"Cortex Parse accepts URLs and files and returns normalized Markdown with metadata.\" ], \"metadata\": { \"case_id\": \"rag-001\" } } ] }, \"target\": { \"type\": \"existing_outputs\" }, \"metrics\": [ { \"metric_key\": \"rag.answer_relevance\", \"threshold\": 0.65 }, { \"metric_key\": \"rag.faithfulness\", \"threshold\": 0.65 }, { \"metric_key\": \"rag.contextual_relevance\", \"threshold\": 0.6 } ], \"output\": { \"persist_report_object\": true, \"include_sample_results\": true } } """; HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL + "/v1/eval/jobs")) .header("Authorization", "Bearer " + TOKEN) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(json)) .build(); print(HTTP.send(request, HttpResponse.BodyHandlers.ofString())); }}然后轮询通用 job endpoint,并读取 Evaluation 结果:
import osimport requestsBASE_URL = os.getenv("CORTEX_URL", "http://127.0.0.1:8080")TOKEN = os.getenv("CORTEX_TOKEN", "replace_with_token")def auth_headers(): return {"Authorization": f"Bearer {TOKEN}"}response = requests.get(f"{BASE_URL}/v1/jobs/job_xxx", headers=auth_headers())response.raise_for_status()data = response.json()print(data)const BASE_URL = process.env.CORTEX_URL ?? "http://127.0.0.1:8080";const TOKEN = process.env.CORTEX_TOKEN ?? "replace_with_token";const authHeaders = { Authorization: `Bearer ${TOKEN}`,};const response = await fetch(`${BASE_URL}/v1/jobs/job_xxx`, { headers: authHeaders,});if (!response.ok) throw new Error(await response.text());const data = await response.json();console.log(data);import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;public class CortexExample { static final String BASE_URL = System.getenv().getOrDefault("CORTEX_URL", "http://127.0.0.1:8080"); static final String TOKEN = System.getenv().getOrDefault("CORTEX_TOKEN", "replace_with_token"); static final HttpClient HTTP = HttpClient.newHttpClient(); static void print(HttpResponse<String> response) { System.out.println(response.statusCode()); System.out.println(response.body()); } public static void main(String[] args) throws Exception { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL + "/v1/jobs/job_xxx")) .header("Authorization", "Bearer " + TOKEN) .GET() .build(); print(HTTP.send(request, HttpResponse.BodyHandlers.ofString())); }}import osimport requestsBASE_URL = os.getenv("CORTEX_URL", "http://127.0.0.1:8080")TOKEN = os.getenv("CORTEX_TOKEN", "replace_with_token")def auth_headers(): return {"Authorization": f"Bearer {TOKEN}"}response = requests.get(f"{BASE_URL}/v1/eval/jobs/job_xxx/result", headers=auth_headers())response.raise_for_status()data = response.json()print(data)const BASE_URL = process.env.CORTEX_URL ?? "http://127.0.0.1:8080";const TOKEN = process.env.CORTEX_TOKEN ?? "replace_with_token";const authHeaders = { Authorization: `Bearer ${TOKEN}`,};const response = await fetch(`${BASE_URL}/v1/eval/jobs/job_xxx/result`, { headers: authHeaders,});if (!response.ok) throw new Error(await response.text());const data = await response.json();console.log(data);import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;public class CortexExample { static final String BASE_URL = System.getenv().getOrDefault("CORTEX_URL", "http://127.0.0.1:8080"); static final String TOKEN = System.getenv().getOrDefault("CORTEX_TOKEN", "replace_with_token"); static final HttpClient HTTP = HttpClient.newHttpClient(); static void print(HttpResponse<String> response) { System.out.println(response.statusCode()); System.out.println(response.body()); } public static void main(String[] args) throws Exception { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL + "/v1/eval/jobs/job_xxx/result")) .header("Authorization", "Bearer " + TOKEN) .GET() .build(); print(HTTP.send(request, HttpResponse.BodyHandlers.ofString())); }}当输入用例很多、需要调用外部 target API,或要路由到性能压测 worker 时,建议使用异步模式。