完整的Galileo AI平台 TypeScript/JS SDK 参考,用于评估、观察和保护 GenAI 应用。适用于构建 Node.js 或 TypeScript 项目。
Galileo TypeSDK.是一项面向实际任务的技能,主要用于Galileo TypeScript SDK( galileo) 为GenAI 应用程序在Node.js和TypeScript. 它支持日志 LLM 调用, r。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
Galileo TypeScript SDK(galileo)为 Node.js 和 TypeScript 环境下的 GenAI 应用提供评估(evaluation)与可观测性(observability)工作流。它支持记录 LLM 调用、检索器(retriever)操作、工具(tool)调用以及多步工作流,并内置评分能力。
其他参考资料:
npm install galileo
或使用 yarn / pnpm:
yarn add galileo pnpm add galileo
import { wrapOpenAI, init, flush } from "galileo";
import OpenAI from "openai";
await init({ projectName: "my-project", logstream: "my-log-stream" });
const openai = wrapOpenAI(new OpenAI());
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "用一句话解释量子计算。" }],
});
console.log(response.choices[0].message.content);
await flush();
在 .env 文件或 Shell 中设置以下环境变量:
GALILEO_API_KEY="your-api-key" # 必需 — 来自 Galileo 控制台 GALILEO_CONSOLE_URL="https://app.galileo.ai" # 控制台 URL(或自托管地址)
也可通过用户名/密码方式认证:
GALILEO_USERNAME="your-username" GALILEO_PASSWORD="your-password"
最简单的全量追踪 OpenAI 调用方式:封装客户端后,所有调用将自动记录:
import { wrapOpenAI, init, flush } from "galileo";
import OpenAI from "openai";
await init({ projectName: "my-project", logstream: "production" });
const openai = wrapOpenAI(new OpenAI());
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "RAG 是什么?" }],
});
await flush();
Azure OpenAI 同样受支持,可通过 wrapAzureOpenAI 封装。
log() 函数封装器可封装任意函数,将其执行记录为一个 span。支持同步、异步及生成器(generator)函数:
import { log, init, flush } from "galileo";
await init({ projectName: "my-project", logstream: "production" });
const retrieveDocuments = log(
{ spanType: "retriever", name: "vector-search" },
async (query: string) => {
const results = await vectorDb.search(query, { k: 5 });
return results.map((r) => r.content);
}
);
const generateResponse = log(
{ spanType: "llm", name: "gpt-4o-call" },
async (query: string, context: string[]) => {
const openai = new OpenAI();
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: `Context: ${context.join("\n")}\n\nQuestion: ${query}` }],
});
return response.choices[0].message.content;
}
);
const ragPipeline = log(
{ spanType: "workflow", name: "rag-pipeline" },
async (query: string) => {
const docs = await retrieveDocuments(query);
return generateResponse(query, docs);
}
);
await ragPipeline("RAG 有哪些优势?");
await flush();
支持的 span 类型包括:workflow、llm、retriever、tool、agent。
如需更精细的控制,可直接使用 GalileoLogger 显式构建 trace 和 spans:
import { GalileoLogger } from "galileo";
const logger = new GalileoLogger({
projectName: "my-project",
logStreamName: "production",
});
logger.startTrace({ input: "计算 15 * 42" });
logger.addToolSpan({
input: "15 * 42",
output: "630",
durationNs: 50000000,
});
logger.addLlmSpan({
input: "数学工具返回了 630。请向用户作答。",
output: "15 乘以 42 等于 630。",
durationNs: 800000000,
model: "gpt-4o",
});
logger.conclude({ output: "15 乘以 42 等于 630。" });
await logger.flush();
可用的 span 方法包括:addLlmSpan、addRetrieverSpan、addToolSpan、addWorkflowSpan、addAgentSpan、addProtectSpan。
使用 galileoContext 进行作用域化的生命周期管理:
import { galileoContext } from "galileo";
await galileoContext.init({ projectName: "my-project", logstream: "production" });
// ... 追踪你的调用 ...
await galileoContext.flush();
await galileoContext.reset();
将相关 trace 分组为 session,适用于多轮对话场景:
import { init, flush, startSession, setSession, clearSession } from "galileo";
await init({ projectName: "my-project", logstream: "production" });
const sessionId = await startSession({ name: "user-conversation-123" });
// 在 setSession 与 clearSession 之间创建的所有 trace 均被归入该 session
setSession(sessionId);
// ... 记录你的 trace ...
clearSession();
await flush();
使用 runExperiment 对 LLM 工作流在数据集上进行自动化评分评估:
import { runExperiment, GalileoMetrics } from "galileo";
const result = await runExperiment({
name: "qa-eval-run",
datasetName: "my-test-dataset",
metrics: [GalileoMetrics.contextAdherence, GalileoMetrics.completeness, GalileoMetrics.inputToxicity],
projectName: "eval-project",
function: async (input) => {
const response = await callYourLLM(input.question);
return response;
},
});
console.log("实验链接:", result.link);
import { runExperiment, GalileoMetrics } from "galileo";
const result = await runExperiment({
name: "rag-eval",
dataset: [
{ question: "什么是机器学习?", expected: "机器学习是..." },
{ question: "解释人工智能", expected: "人工智能是..." },
],
metrics: [GalileoMetrics.contextAdherence, GalileoMetrics.chunkAttributionUtilization, GalileoMetrics.completeness],
projectName: "eval-project",
function: async (input) => {
const docs = await retrieve(input.question);
return generateAnswer(input.question, docs);
},
});
import { runExperiment, GalileoMetrics } from "galileo";
const result = await runExperiment({
name: "prompt-eval",
datasetName: "my-test-dataset",
promptTemplate: { id: "your-prompt-template-id" },
promptSettings: { model_alias: "GPT-4o", temperature: 0.7 },
metrics: [GalileoMetrics.correctness, GalileoMetrics.instructionAdherence],
projectName: "eval-project",
});
更多内容请参阅「高级评估模式」。
import { GalileoLogger } from "galileo";
const logger = new GalileoLogger({
projectName: "rag-app",
logStreamName: "production",
});
logger.startTrace({ input: "光合作用是如何工作的?" });
logger.addRetrieverSpan({
input: "光合作用是如何工作的?",
output: ["光合作用是植物通过……的过程。"],
});
logger.addLlmSpan({
input: "结合上下文,解释光合作用。",
output: "光合作用是植物所使用的一种过程……",
durationNs: 1500000000,
model: "gpt-4o",
});
logger.conclude({ output: "光合作用是植物所使用的一种过程……" });
await logger.flush();
import { GalileoLogger } from "galileo";
const logger = new GalileoLogger({
projectName: "agent-app",
logStreamName: "production",
});
logger.startTrace({ input: "调研并总结量子计算" });
logger.addToolSpan({
input: "search: 量子计算概述",
output: "搜索结果……",
durationNs: 200000000,
});
logger.addRetrieverSpan({
input: "量子计算",
output: ["文档1:量子比特……", "文档2:叠加态……"],
});
logger.addLlmSpan({
input: "总结以下关于量子计算的研究……",
output: "量子计算利用量子力学现象……",
durationNs: 2500000000,
model: "gpt-4o",
});
logger.conclude({
output: "量子计算利用量子力学现象……",
});
await logger.flush();
init() 或创建 GalileoLogger 实例。flush(),以确保 trace 上传至 Galileo;在 Web 服务中,应在每个请求处理器末尾执行 flush()。wrapOpenAI。log() 封装函数作为 span —— 它可自动处理同步、异步和生成器函数。GalileoLogger。runExperiment —— 它自动完成数据集加载、评分及结果上传。.env 文件,而非硬编码 API 密钥。durationNs 值,以保障延迟指标的有效性。GalileoObserveWorkflow 和 GalileoEvaluateWorkflow 已弃用,但为保持向后兼容仍导出。请改用 GalileoLogger(或 wrapOpenAI / log())与 runExperiment。