@mistralai/mistralai-azure

中文文档
2.1.0发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证

Azure上用于Mistral AI的TypeScript客户端库

中文使用文档
文档目录20 节
  1. SDK安装
  2. NPM
  3. 规划
  4. 包子
  5. 纱
  6. 要求
  7. SDK使用示例
  8. 创建聊天完成
  9. 可用资源和操作
  10. 闲谈,聊天
  11. 服务器发送的事件流
  12. 重试
  13. 错误处理
  14. 服务器选择
  15. 按名称选择服务器
  16. 覆盖每个客户端的服务器URL
  17. 自定义HTTP客户机
  18. 身份验证
  19. 每客户端安全方案
  20. 贡献

Azure Typescript客户端

SDK安装

NPM

npm add @mistralai/mistralai-azure

规划

pnpm add @mistralai/mistralai-azure

包子

bun add @mistralai/mistralai-azure

纱

yarn add @mistralai/mistralai-azure zod

# Note that Yarn does not install peer dependencies automatically. You will need
# to install zod as shown above.

要求

有关支持的JavaScript运行时,请咨询 RUNTIMES.md.

SDK使用示例

创建聊天完成

这个例子展示了如何创建聊天完成。

import { MistralAzure } from "@mistralai/mistralai-azure";

const mistralAzure = new MistralAzure({
  apiKey: process.env["AZURE_API_KEY"],
  endpoint: process.env["AZURE_ENDPOINT"],
});

async function run() {
  const result = await mistralAzure.chat.complete({
    model: "azureai",
    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 { MistralAzure } from "@mistralai/mistralai-azure";

const mistralAzure = new MistralAzure({
  apiKey: process.env["AZURE_API_KEY"],
  endpoint: process.env["AZURE_ENDPOINT"],
});

async function run() {
  const result = await mistralAzure.chat.stream({
    model: "azureai",
    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 { MistralAzure } from "@mistralai/mistralai-azure";

const mistralAzure = new MistralAzure({
  apiKey: process.env["AZURE_API_KEY"],
  endpoint: process.env["AZURE_ENDPOINT"],
});

async function run() {
  const result = await mistralAzure.chat.stream(
    {
      model: "azureai",
      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 { MistralAzure } from "@mistralai/mistralai-azure";

const mistralAzure = new MistralAzure({
  retryConfig: {
    strategy: "backoff",
    backoff: {
      initialInterval: 1,
      maxInterval: 50,
      exponent: 1.1,
      maxElapsedTime: 100,
    },
    retryConnectionErrors: false,
  },
  apiKey: process.env["AZURE_API_KEY"],
  endpoint: process.env["AZURE_ENDPOINT"],
});

async function run() {
  const result = await mistralAzure.chat.stream({
    model: "azureai",
    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将抛出适当的错误类型。

错误的对象状态码内容类型
错误。HTTPValidationError422application / json
错误。SDKError4 xx-5xx/

当方法参数或从服务器返回的数据与预期格式不匹配时,也可能发生验证错误。这个 SDKValidationError 将捕获在调用的属性中验证失败的原始值 rawValue. 此外,一个 pretty() 方法可用于记录格式化良好的字符串,因为验证错误可能列出许多问题,并且在调试时普通错误字符串可能难以读取。

import { MistralAzure } from "@mistralai/mistralai-azure";
import { SDKValidationError } from "@mistralai/mistralai-azure/models/errors";

const mistralAzure = new MistralAzure({
  apiKey: process.env["AZURE_API_KEY"],
  endpoint: process.env["AZURE_ENDPOINT"],
});

async function run() {
  let result;
  try {
    result = await mistralAzure.chat.complete({
      model: "azureai",
      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客户端实例时的可选参数。然后,所选服务器将被用作使用它的操作的默认服务器。该表列出了与可用服务器相关的名称:

名字服务器变量
prodhttps://api.mistral.ai无
import { MistralAzure } from "@mistralai/mistralai-azure";

const mistralAzure = new MistralAzure({
  server: "prod",
  apiKey: process.env["AZURE_API_KEY"],
  endpoint: process.env["AZURE_ENDPOINT"],
});

async function run() {
  const result = await mistralAzure.chat.stream({
    model: "azureai",
    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 { MistralAzure } from "@mistralai/mistralai-azure";

const mistralAzure = new MistralAzure({
  serverURL: "https://api.mistral.ai",
  apiKey: process.env["AZURE_API_KEY"],
  endpoint: process.env["AZURE_ENDPOINT"],
});

async function run() {
  const result = await mistralAzure.chat.stream({
    model: "azureai",
    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 { MistralAzure } from "@mistralai/mistralai-azure";
import { HTTPClient } from "@mistralai/mistralai-azure/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 MistralAzure({ httpClient });

身份验证

每客户端安全方案

本SDK全局支持以下安全方案:

名字类型计划
apiKeyhttpHTTP无记名

的API进行身份验证 apiKey 参数在初始化SDK客户端实例时必须设置。例如:

import { MistralAzure } from "@mistralai/mistralai-azure";

const mistralAzure = new MistralAzure({
  apiKey: process.env["AZURE_API_KEY"],
  endpoint: process.env["AZURE_ENDPOINT"],
});

async function run() {
  const result = await mistralAzure.chat.stream({
    model: "azureai",
    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或概念验证问题,我们将尽最大努力将其包含在未来的版本中。

版本与发布记录

@mistralai/mistralai-azure 共发布 21 个版本,首次发布于 2024/07/30;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.0latest2026/11/021 天前
2.0.02026/05/255 个月前
1.7.32026/05/125 个月前
1.7.22026/05/125 个月前
1.7.12026/05/125 个月前
1.7.02026/01/219 个月前

关于 @mistralai/mistralai-azure 的常见问题

@mistralai/mistralai-azure 是什么?

Azure上用于Mistral AI的TypeScript客户端库

如何安装 @mistralai/mistralai-azure?

在项目目录运行 npm install @mistralai/mistralai-azure,或使用 pnpm add / yarn add / bun add。

@mistralai/mistralai-azure 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@mistralai/mistralai-azure 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@mistralai/mistralai-azure 可以商用吗?

@mistralai/mistralai-azure 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@mistralai/mistralai-azure 还在维护吗?

最新版本 2.1.0 发布于 2026/11/02(1 天前);至今共发布 21 个版本,其中过去 12 个月发布了 6 个;首次发布于 2024/07/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。