Cerebras 节点 API 库
这个库提供了从服务器侧的TypeScript或JavaScript对 Cerebras REST API的方便访问.
REST API 文档可见于 推论-docs.cerebras.ai。此库的全部 API 可以在 日记。 。 。 .
它的产生与 无污。 。 。 .
[注]
这个SDK有一个机制 发送一些请求给/v1/tcp_warming用于减少TTFT。 如果这样做不可取, 设置warmTCPConnection=false在建筑师.如果你正在反复重建 SDK 实例, 会导致不良的性能。 建议您建造一次SDK,并在可能时重新使用实例.
关于脑管
在Cerebras,我们开发出世界上最大和最快的AI处理器"瓦费尔-比例引擎-3"(WSE-3). 由WSE-3提供动力的Cerebras CS-3系统代表了AI超级计算机的一门新类,它为基因AI训练设定了标准,并以无与伦比的性能和可扩展性进行推论.
以Cerebras作为你的推论提供者,你可以:
- AI推断工作量达到前所未有的速度
- 以高吞吐量进行商业建设
- 用我们无缝的集群技术,不费吹灰之力地扩大你的人工智能工作量
我们的CS-3系统可以快速而容易地被集群,以创造出世界上最大的AI超级计算机,使得放置和运行最大的模型变得简单. 主要公司、研究机构和政府已经在使用Cerebras解决方案开发专有模式并培训受欢迎的开源模式.
想要体验脑管的力量吗? 看看我们 网站 寻找更多的资源,探索通过脑云或现场部署获得我们技术的选项!
安装
npm install @cerebras/cerebras_cloud_sdk
API 密钥
从中获取 API 密钥 云. 脑.ai 并添加到您的环境变量中:
export CEREBRAS_API_KEY="your-api-key-here"
使用量
此库的全部 API 可在 日记。 。 。 .
聊天补全
import Cerebras from '@cerebras/cerebras_cloud_sdk';
const client = new Cerebras({
apiKey: process.env['CEREBRAS_API_KEY'], // This is the default and can be omitted
});
async function main() {
const chatCompletion = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Why is fast inference important?' }],
model: 'gpt-oss-120b',
});
console.log(chatCompletion?.choices[0]?.message);
}
main();
文本补全
import Cerebras from '@cerebras/cerebras_cloud_sdk';
const client = new Cerebras({
apiKey: process.env['CEREBRAS_API_KEY'], // This is the default and can be omitted
});
async function main() {
const completion = await client.completions.create({
prompt: "It was a dark and stormy ",
model: 'gpt-oss-120b',
});
console.log(completion?.choices[0]?.text);
}
main();
B. 精简答复
我们支持使用服务器发送事件(SSE)进行流化响应.
注意 当流出时, usage 财务报告和审定财务报表 time_info 信息将只包含在最后块中.
聊天补全
import Cerebras from '@cerebras/cerebras_cloud_sdk';
const client = new Cerebras({
apiKey: process.env['CEREBRAS_API_KEY'], // This is the default and can be omitted
});
async function main() {
const stream = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Why is fast inference important?' }],
model: 'gpt-oss-120b',
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
}
main();
文本补全
import Cerebras from '@cerebras/cerebras_cloud_sdk';
const client = new Cerebras({
apiKey: process.env['CEREBRAS_API_KEY'], // This is the default and can be omitted
});
async function main() {
const stream = await client.completions.create({
prompt: "It was a dark and stormy ",
model: 'gpt-oss-120b',
max_tokens: 10,
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.text || '');
}
}
main();
如果您需要取消一条溪流, 您可以 break 从循环
或打电话 stream.controller.abort()。 。 。 .
请求响应类型( A)
这个库包括所有请求参数和响应字段的TypeScript定义. 你可以这样导入和使用它们:
import Cerebras from '@cerebras/cerebras_cloud_sdk';
const client = new Cerebras({
apiKey: process.env['CEREBRAS_API_KEY'], // This is the default and can be omitted
});
const params: Cerebras.Chat.ChatCompletionCreateParams = {
messages: [{ role: 'user', content: 'Why is fast inference important?' }],
model: 'gpt-oss-120b',
};
const chatCompletion: Cerebras.Chat.ChatCompletion = await client.chat.completions.create(params);
每种方法的文件、请求参数和响应字段都以道克字符串形式提供,并将出现在大多数现代编辑中.
处理错误
当库无法连接到API时,
或者如果API返回一个非成功状态代码(即4xx或5xx回复),
子类 APIError 将扔出 :
import Cerebras from '@cerebras/cerebras_cloud_sdk';
const client = new Cerebras({
apiKey: process.env['CEREBRAS_API_KEY'], // This is the default and can be omitted
});
async function main() {
const chatCompletion = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'This should cause an error!' }],
model: 'some-model-that-doesnt-exist' as any, // Ask TS to ignore the obviously invalid model name... Do not do this!
})
.catch(async (err) => {
if (err instanceof Cerebras.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
console.log(err); // Full exception
} else {
throw err;
}
});
}
main();
出错代码如下:
| 身份代码 | 错误类型 |
|---|---|
| 400个 | BadRequestError |
| 401 (中文(简体) ) | AuthenticationError |
| 403 (韩语) | PermissionDeniedError |
| 404 (韩语) | NotFoundError |
| 422个 | UnprocessableEntityError |
| 429个 | RateLimitError |
| >=500 | InternalServerError |
| 不详 | APIConnectionError |
检索
某些出错默认会自动重试2次,并有短暂的指数后退. 连接出错(例如由于网络连接问题) 408 请求超时, 409 相冲突, 429 限制费率,和 QQ500 内部出错都会默认重审.
你可以用这个 maxRetries 选项以配置或禁用此选项:
import Cerebras from '@cerebras/cerebras_cloud_sdk';
// Configure the default for all requests:
const client = new Cerebras({
maxRetries: 0, // default is 2
});
// Or, configure per-request:
await client.chat.completions.create({ messages: [{ role: 'user', content: 'Why is fast inference important?' }], model: 'gpt-oss-120b' }, {
maxRetries: 5,
});
超时
默认1分钟后请求暂停 。 您可以用一个 timeout 选项 :
import Cerebras from '@cerebras/cerebras_cloud_sdk';
// Configure the default for all requests:
const client = new Cerebras({
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
});
// Override per-request:
await client.chat.completions.create({ messages: [{ role: 'user', content: 'Why is fast inference important?' }], model: 'gpt-oss-120b' }, {
timeout: 5 * 1000,
});
超时,一个 APIConnectionTimeoutError 被抛出.
请注意,哪些时间是 默认重审两次。 。 。 .
高级使用
获取原始响应数据(例如信头)
那个"行" Response 返回时间 fetch() 可通过 .asResponse() 方法 APIPromise 键入所有方法返回.
你也可以使用这个 .withResponse() 获取原始数据的方法 Response 与解析数据一起.
import Cerebras from '@cerebras/cerebras_cloud_sdk';
const client = new Cerebras();
const response = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'Why is fast inference important?' }],
model: 'gpt-oss-120b',
})
.asResponse();
console.log(response.headers.get('X-My-Header'));
console.log(response.statusText); // access the underlying Response object
const { data: chatCompletion, response: raw } = await client.chat.completions
.create({
messages: [{ role: 'user', content: 'Why is fast inference important?' }],
model: 'gpt-oss-120b',
})
.withResponse();
console.log(raw.headers.get('X-My-Header'));
console.log(chatCompletion);
提出自定义/无文件记录的请求
此库的打入是为了方便地访问有文献记载的API. 如果您需要访问无证 端点,参数,或响应属性,库仍然可以被使用.
无记录的终点
要向无记录的端点提出请求, 您可以使用 client.get, (中文(简体) ), client.post,以及其他HTTP动词.
在提出这些请求时,将尊重客户端的选项,例如复试.
await client.post('/some/path', {
body: { some_prop: 'foo' },
query: { some_query_arg: 'bar' },
});
无文件请求段落
要使用无证参数提出请求, 您可以使用 // @ts-expect-error 关于无证件者
参数。 此库不会在运行时验证请求是否与类型相匹配, 所以任何额外的值
发送将按.
client.foo.create({
foo: 'my_param',
bar: 12,
// @ts-expect-error baz is not yet public
baz: 'undocumented option',
});
申请与 GET 动词,任何额外的参数都会在查询中,所有其他请求都会发送
身体里有额外的参数.
如果你想明确发送一个额外的参数,你可以这样做与 query, (中文(简体) ), body,以及 headers 请求
选项.
无文件回复属性
要访问无证响应属性, 您可以访问响应对象 // @ts-expect-error 打开
,或者将响应对象投放到必要的类型。 像请求的段落,我们不
验证或从 API 的响应中脱去额外的属性.
自定义获取客户端
默认情况下,此库使用 node-fetch 在节点,并期望一个全局 fetch 函数在其他环境中.
如果您更愿意使用符合全球网络标准的网络标准 fetch 即使在节点环境中也能函数,
(例如,如果您正在以 --experimental-fetch 组合填充的下一个JS undici, (中文(简体) ),
在您第一次导入前添加以下导入 from "Cerebras"数字 :
// Tell TypeScript and the package to use the global web fetch instead of node-fetch.
// Note, despite the name, this does not add any polyfills, but expects them to be provided if needed.
import '@cerebras/cerebras_cloud_sdk/shims/web';
import Cerebras from '@cerebras/cerebras_cloud_sdk';
倒转时加 import "@cerebras/cerebras_cloud_sdk/shims/node" (它的确导入多填充).
如果您获取错误的类型脚本类型, 此功能也会有所帮助 Response (单位:千美元)更多细节) (中文(简体) ).
日志和中间软件
您也可以提供习惯 fetch 函数当即决客户端时,
可用于检查或修改 Request 或者说 Response 每次请求之前/之后:
import { fetch } from 'undici'; // as one example
import Cerebras from '@cerebras/cerebras_cloud_sdk';
const client = new Cerebras({
fetch: async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
console.log('About to make a request', url, init);
const response = await fetch(url, init);
console.log('Got response', response);
return response;
},
});
请注意,如果 DEBUG=true 环境变量,此库将自动记录所有请求和响应 .
这样做只是为了调试目的,今后可能会不通知而改变.
配置 HTTP( S) 代理( 例如, 代理)
默认情况下,这个库会对所有 http/https 请求使用一个稳定的代理来再利用 TCP 连接,消除了许多 TCP & TLS 握手并刮去大多数请求的大约100ms.
如果您想要禁用或自定义此行为, 例如在代理后使用 API, 您可以通过 httpAgent 用于所有请求(无论是http还是https),例如:
import http from 'http';
import { HttpsProxyAgent } from 'https-proxy-agent';
// Configure the default for all requests:
const client = new Cerebras({
httpAgent: new HttpsProxyAgent(process.env.PROXY_URL),
});
// Override per-request:
await client.chat.completions.create(
{
messages: [{ role: 'user', content: 'Why is fast inference important?' }],
model: 'gpt-oss-120b',
},
{
httpAgent: new http.Agent({ keepAlive: false }),
},
);
语义版本
这套套件一般沿用 交换器 虽然某些向后兼容的修改可以作为小版本发布:
- 仅影响静态类型的变化,而不突破运行时的行为.
- 图书馆内部的改动,技术上是公开的,但并非打算或记录外部使用的. (请打开一个 GitHub 问题让我们知道, 如果你依赖这样的内部。 )
- 我们预计不会在实践中影响到绝大多数用户的变化.
我们认真对待倒向相容性,并努力地确保你能够依靠平稳的升级经验.
我们很期待你的反馈,请打开 问题 包括问题、错误或建议.
所需资源
TypeScript QQ 4.5被支持.
支持以下运行时间:
- 网页浏览器(最新Chrome、Firefox、Safari、Edge等)
- 节点.js 18 LTS 或以后(非欧洲)版本.
- 德诺v1.28.0以上.
- 1.0号或以后.
- 云浮活计.
- Vercel Edge 运行时段.
- 28岁以上
"node"环境"jsdom"目前不支持). - Nitro v 2.6 或 更强.
注意React Industrial此时不支持.
如果您对其它运行时间环境感兴趣, 请在 GitHub 上打开或投票 .
捐款
见 提供的文件。 。 。 .