@anvia/openai (英语)
连接 Anvia 代理到 OpenAI 或 OpenAI 兼容的端点 。 使用一个客户端 聊天、打字决定、嵌入、图像、讲话和抄本.
Azure OpenAI 和 Azure AI 创建,使用
@anvia/azure 用于Azure端点和认证配置.
安装
pnpm add @anvia/openai @anvia/core
快速启动
设定 OPENAI_API_KEY 在运行此示例之前.
import { Agent } from "@anvia/core";
import { OpenAIClient } from "@anvia/openai";
const client = new OpenAIClient({
apiKey: process.env.OPENAI_API_KEY!,
});
const agent = new Agent({
id: "assistant",
model: client.completionModel({ modelId: "gpt-5.6", api: "responses" }),
instructions: "Answer clearly and concisely.",
});
const result = await agent.generate({
prompt: "What should I check before launching a new product?",
});
if (result.type === "response") {
console.log(result.output);
}
类型决定
import { OpenAIClient, GPT_6_LUNA } from "@anvia/openai";
import { check, decide } from "@anvia/core/decision";
const model = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! }).decisionModel({
modelId: GPT_6_LUNA,
});
const result = await decide({
model,
state: { message: "Please refund my duplicate charge." },
questions: { refund: check({ instructions: "Is a refund requested?" }) },
});
console.log(result.answers.refund.probability);
使用带有OpenAI SDK 7.30.0+的专用决定API. 支持文字和结构化的JSON;
结构状态被序列化为文本。 拒绝投掷 DecisionRefusalError 从核心.
见地图绘制、限制、批次和拒绝处理的使用指南.
你能建造的东西
- 聊天补全和响应 API,流化,工具,和结构化输出.
- 为所支持的模型打入了推理控制.
- 输入分类,多标签检查,和通过数字评分
decisionModel()财务报告和审定财务报表decide()。 。 。 . - 嵌入,图像生成,语音生成,抄录,和模型上市.
- 自定义端点通过
baseUrl。 。 。 .
模型处理默认为聊天补全. 该示例明确选择响应.
Bun用户可以安装相同的软件包 bun add。 。 。 .
学多点
兼容模型元数据
listModels() 广告保护地 supported_parameters 作为 supportedParameters
当它是一系列字符串,并且对象值 reasoning 作为特定供应商
元数据。 使用前验证推理字段;其方案因服务而异.
对象值 pricing 以同样的方式保存;在使用前验证其字段.