使用GCP Typescript客户端
v2重大变化: SDK类已从
MistralGoogleCloud来MistralGCP(和MistralGoogleCloudError来MistralGCPError)以与Python SDK保持一致。
SDK安装
NPM
npm add @mistralai/mistralai-gcp
规划
pnpm add @mistralai/mistralai-gcp
包子
bun add @mistralai/mistralai-gcp
纱
yarn add @mistralai/mistralai-gcp zod
# Note that Yarn does not install peer dependencies automatically. You will need
# to install zod as shown above.
要求
有关支持的JavaScript运行时,请咨询 RUNTIMES.md.
SDK使用示例
创建聊天完成
这个例子展示了如何创建聊天完成。
import { MistralGCP } from "@mistralai/mistralai-gcp";
const sdk = new MistralGCP({
region: "europe-west4",
projectId: process.env["GOOGLE_PROJECT_ID"],
});
async function run() {
const result = await sdk.chat.complete({
model: "mistral-small-latest",
messages: [
{
content:
"Who is the best French painter? Answer in one short sentence.",
role: "user",
},
],
});
// Handle the result
console.log(result);
}
run();
可用资源和操作
闲谈,聊天
鱼翅
- 创建 -影片完成
服务器发送的事件流
服务器发送的事件 是用来从某些流内容
操作。这些操作将把流公开为一个async可迭代对象
可以使用 for await...of 循环。循环将会
当服务器不再有任何事件要发送时终止,并关闭
潜在的联系。
import { MistralGCP } from "@mistralai/mistralai-gcp";
const sdk = new MistralGCP({
region: "europe-west4",
projectId: process.env["GOOGLE_PROJECT_ID"],
});
async function run() {
const result = await sdk.chat.stream({
model: "mistral-small-latest",
messages: [
{
content:
"Who is the best French painter? Answer in one short sentence.",
role: "user",
},
],
});
for await (const event of result) {
// Handle the event
}
}
run();
重试
这个SDK中的一些端点支持重试。如果您在没有任何配置的情况下使用SDK,它将退回到API提供的默认重试策略。但是,可以在每个操作的基础上或在整个SDK上重写默认的重试策略。
要更改单个API调用的默认重试策略,只需为调用提供一个retryConfig对象:
import { MistralGCP } from "@mistralai/mistralai-gcp";
const sdk = new MistralGCP({
region: "europe-west4",
projectId: process.env["GOOGLE_PROJECT_ID"],
});
async function run() {
const result = await sdk.chat.stream(
{
model: "mistral-small-latest",
messages: [
{
content:
"Who is the best French painter? Answer in one short sentence.",
role: "user",
},
],
},
{
retries: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
},
);
for await (const event of result) {
// Handle the event
}
}
run();
如果你想覆盖所有支持重试的操作的默认重试策略,你可以在SDK初始化时提供一个retryConfig:
import { MistralGCP } from "@mistralai/mistralai-gcp";
const sdk = new MistralGCP({
retryConfig: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
region: "europe-west4",
projectId: process.env["GOOGLE_PROJECT_ID"],
});
async function run() {
const result = await sdk.chat.stream({
model: "mistral-small-latest",
messages: [
{
content:
"Who is the best French painter? Answer in one short sentence.",
role: "user",
},
],
});
for await (const event of result) {
// Handle the event
}
}
run();
错误处理
所有SDK方法都返回一个响应对象或抛出一个错误。如果在OpenAPI规范中指定了错误对象,SDK将抛出适当的错误类型。
| 错误的对象 | 状态码 | 内容类型 |
|---|---|---|
| 错误。HTTPValidationError | 422 | application / json |
| 错误。SDKError | 4 xx-5xx | / |
当方法参数或从服务器返回的数据与预期格式不匹配时,也可能发生验证错误。这个 SDKValidationError 将捕获在调用的属性中验证失败的原始值 rawValue. 此外,一个 pretty() 方法可用于记录格式化良好的字符串,因为验证错误可能列出许多问题,并且在调试时普通错误字符串可能难以读取。
import { MistralGCP } from "@mistralai/mistralai-gcp";
import { SDKValidationError } from "@mistralai/mistralai-gcp/models/errors";
const sdk = new MistralGCP({
region: "europe-west4",
projectId: process.env["GOOGLE_PROJECT_ID"],
});
async function run() {
let result;
try {
result = await sdk.chat.complete({
model: "mistral-small-latest",
messages: [
{
content:
"Who is the best French painter? Answer in one short sentence.",
role: "user",
},
],
});
} catch (err) {
switch (true) {
case err instanceof SDKValidationError: {
// Validation errors can be pretty-printed
console.error(err.pretty());
// Raw value may also be inspected
console.error(err.rawValue);
return;
}
case err instanceof errors.HTTPValidationError: {
console.error(err); // handle exception
return;
}
default: {
throw err;
}
}
}
// Handle the result
console.log(result);
}
run();
服务器选择
按名称选择服务器
方法传递服务器名称,可以全局覆盖默认服务器 server 初始化SDK客户端实例时的可选参数。然后,所选服务器将被用作使用它的操作的默认服务器。该表列出了与可用服务器相关的名称:
| 名字 | 服务器 | 变量 |
|---|---|---|
prod | https://api.mistral.ai | 无 |
import { MistralGCP } from "@mistralai/mistralai-gcp";
const sdk = new MistralGCP({
server: "prod",
region: "europe-west4",
projectId: process.env["GOOGLE_PROJECT_ID"],
});
async function run() {
const result = await sdk.chat.stream({
model: "mistral-small-latest",
messages: [
{
content:
"Who is the best French painter? Answer in one short sentence.",
role: "user",
},
],
});
for await (const event of result) {
// Handle the event
}
}
run();
覆盖每个客户端的服务器URL
方法传递一个URL,也可以全局覆盖默认服务器 serverURL 初始化SDK客户端实例时的可选参数。例如:
import { MistralGCP } from "@mistralai/mistralai-gcp";
const sdk = new MistralGCP({
serverURL: "https://api.mistral.ai",
region: "europe-west4",
projectId: process.env["GOOGLE_PROJECT_ID"],
});
async function run() {
const result = await sdk.chat.stream({
model: "mistral-small-latest",
messages: [
{
content:
"Who is the best French painter? Answer in one short sentence.",
role: "user",
},
],
});
for await (const event of result) {
// Handle the event
}
}
run();
自定义HTTP客户机
TypeScript SDK使用 HTTPClient 这就封装了本地代码
获取API。这
客户端是一个薄薄的包装 fetch 并提供附加钩子的能力
围绕请求生命周期,可用于修改请求或处理
错误和响应。
这个 HTTPClient 构造函数接受可选的 fetcher 论证可以是
用于集成第三方HTTP客户端或编写模拟测试时
HTTP客户端和提要在fixture中。
控件的使用方法 "beforeRequest" 钩到加一个
自定义标头和请求超时以及如何使用 "requestError" 钩
要记录错误:
import { MistralGCP } from "@mistralai/mistralai-gcp";
import { HTTPClient } from "@mistralai/mistralai-gcp/lib/http";
const httpClient = new HTTPClient({
// fetcher takes a function that has the same signature as native `fetch`.
fetcher: (request) => {
return fetch(request);
},
});
httpClient.addHook("beforeRequest", (request) => {
const nextRequest = new Request(request, {
signal: request.signal || AbortSignal.timeout(5000),
});
nextRequest.headers.set("x-custom-header", "custom value");
return nextRequest;
});
httpClient.addHook("requestError", (error, request) => {
console.group("Request Error");
console.log("Reason:", `${error}`);
console.log("Endpoint:", `${request.method} ${request.url}`);
console.groupEnd();
});
const sdk = new MistralGCP({ httpClient });
身份验证
每客户端安全方案
本SDK全局支持以下安全方案:
| 名字 | 类型 | 计划 |
|---|---|---|
apiKey | http | HTTP无记名 |
的API进行身份验证 apiKey 参数在初始化SDK客户端实例时必须设置。例如:
import { MistralGCP } from "@mistralai/mistralai-gcp";
const sdk = new MistralGCP({
region: "europe-west4",
projectId: process.env["GOOGLE_PROJECT_ID"],
});
async function run() {
const result = await sdk.chat.stream({
model: "mistral-small-latest",
messages: [
{
content:
"Who is the best French painter? Answer in one short sentence.",
role: "user",
},
],
});
for await (const event of result) {
// Handle the event
}
}
run();
开发
贡献
虽然我们重视对这个SDK的开源贡献,但这个库是通过编程生成的。添加到内部文件的任何手动更改将在下一代中被覆盖。 我们期待听到您的反馈。请随意打开PR或概念验证问题,我们将尽最大努力将其包含在未来的版本中。