@mistralai/mistralai-azure

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

TypeScript client library for Mistral AI on Azure

原始使用文档
文档目录20 节
  1. SDK Installation
  2. NPM
  3. PNPM
  4. Bun
  5. Yarn
  6. Requirements
  7. SDK Example Usage
  8. Create Chat Completions
  9. Available Resources and Operations
  10. chat
  11. Server-sent event streaming
  12. Retries
  13. Error Handling
  14. Server Selection
  15. Select Server by Name
  16. Override Server URL Per-Client
  17. Custom HTTP Client
  18. Authentication
  19. Per-Client Security Schemes
  20. Contributions

Mistral on Azure Typescript Client

SDK Installation

NPM

npm add @mistralai/mistralai-azure

PNPM

pnpm add @mistralai/mistralai-azure

Bun

bun add @mistralai/mistralai-azure

Yarn

yarn add @mistralai/mistralai-azure zod

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

Requirements

For supported JavaScript runtimes, please consult RUNTIMES.md.

SDK Example Usage

Create Chat Completions

This example shows how to create chat completions.

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();

Available Resources and Operations

chat

Server-sent event streaming

Server-sent events are used to stream content from certain operations. These operations will expose the stream as an async iterable that can be consumed using a for await...of loop. The loop will terminate when the server no longer has any events to send and closes the underlying connection.

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();

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a retryConfig object to the call:

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();

If you'd like to override the default retry strategy for all operations that support retries, you can provide a retryConfig at SDK initialization:

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();

Error Handling

All SDK methods return a response object or throw an error. If Error objects are specified in your OpenAPI Spec, the SDK will throw the appropriate Error type.

Error ObjectStatus CodeContent Type
errors.HTTPValidationError422application/json
errors.SDKError4xx-5xx/

Validation errors can also occur when either method arguments or data returned from the server do not match the expected format. The SDKValidationError that is thrown as a result will capture the raw value that failed validation in an attribute called rawValue. Additionally, a pretty() method is available on this error that can be used to log a nicely formatted string since validation errors can list many issues and the plain error string may be difficult read when debugging.

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 Selection

Select Server by Name

You can override the default server globally by passing a server name to the server optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the names associated with the available servers:

NameServerVariables
prodhttps://api.mistral.aiNone
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();

Override Server URL Per-Client

The default server can also be overridden globally by passing a URL to the serverURL optional parameter when initializing the SDK client instance. For example:

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();

Custom HTTP Client

The TypeScript SDK makes API calls using an HTTPClient that wraps the native Fetch API. This client is a thin wrapper around fetch and provides the ability to attach hooks around the request lifecycle that can be used to modify the request or handle errors and response.

The HTTPClient constructor takes an optional fetcher argument that can be used to integrate a third-party HTTP client or when writing tests to mock out the HTTP client and feed in fixtures.

The following example shows how to use the "beforeRequest" hook to to add a custom header and a timeout to requests and how to use the "requestError" hook to log errors:

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 });

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

NameTypeScheme
apiKeyhttpHTTP Bearer

To authenticate with the API the apiKey parameter must be set when initializing the SDK client instance. For example:

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();

Development

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

版本与发布记录

@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 和提交记录判断。