跳到主要内容

快速入门

提示

正在寻找 La Plateforme (平台)?请访问 console.mistral.ai

账户设置

  • 首先,在 console.mistral.ai 创建 Mistral 账户或登录。
  • 然后,导航至“工作区”和“账单”以添加您的支付信息并激活账户的支付功能。
  • 之后,前往“API 密钥”页面,点击“创建新密钥”生成新的 API 密钥。请务必复制该 API 密钥,安全保存,不要分享给任何人。

Mistral AI API 入门

Open In Colab

Mistral AI API 为开发者提供了一种无缝的方式,只需几行代码即可将 Mistral 最先进的模型集成到其应用和生产工作流中。我们的 API 目前通过 La Plateforme (平台) 提供。您需要激活账户的支付功能才能启用您的 API 密钥。稍等片刻后,您就可以使用我们的 chat (聊天) 端点

import os
from mistralai import Mistral

api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"

client = Mistral(api_key=api_key)

chat_response = client.chat.complete(
model= model,
messages = [
{
"role": "user",
"content": "What is the best French cheese?",
},
]
)
print(chat_response.choices[0].message.content)

要使用 Mistral AI 的嵌入(embeddings)API 生成文本嵌入,我们可以向 API 端点发送请求,并指定嵌入模型 mistral-embed,同时提供输入文本列表。API 将随后返回相应的嵌入作为数值向量,这些向量可用于 NLP 应用中的进一步分析或处理。

import os
from mistralai import Mistral

api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-embed"

client = Mistral(api_key=api_key)

embeddings_response = client.embeddings.create(
model=model,
inputs=["Embed this sentence.", "As well as this one."]
)

print(embeddings_response)

有关 API 上提供的模型的完整描述,请参阅模型文档