跳到主要内容

IBM watsonx.ai

介绍

Mistral AI 的 Large 模型作为完全托管的解决方案以及本地部署选项,可在 IBM watsonx.ai 平台上使用。

入门

以下解决方案概述了在 IBM watsonx.ai 的 SaaS 版本上查询 Mistral Large 的步骤。

先决条件

需要以下各项

  • 一个 IBM watsonx 项目 (IBM_CLOUD_PROJECT_ID)
  • 一个具有启用 Watson Machine Learning 服务访问策略的服务 ID。

要启用对 API 的访问,您必须确保

  • 您的服务 ID 已作为 EDITOR 添加到项目中,
  • 您已为您的服务 ID 生成了一个 API 密钥 (IBM_CLOUD_API_KEY)。

查询模型(聊天补全)

您可以使用 IBM 的 SDK 或纯 HTTP 调用来查询 Mistral Large。

警告

下面的示例利用 mistral-common Python 包使用特殊 token 正确格式化用户消息。强烈建议避免直接传递原始字符串并手动处理特殊 token:这可能导致隐式的分词错误,从而严重降低模型输出的质量。

您需要在包含以下软件包的虚拟环境中运行您的代码

  • httpx(已测试版本 0.27.2
  • ibm-watsonx-ai(已测试版本 1.1.11
  • mistral-common(已测试版本 1.4.4

在下面的代码片段中,您的 API 密钥将用于生成 IAM token,然后使用此 token 进行认证以调用模型。

from ibm_watsonx_ai import Credentials
from ibm_watsonx_ai.foundation_models import ModelInference
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.request import ChatCompletionRequest
from mistral_common.protocol.instruct.messages import UserMessage

import os
import httpx

IBM_CLOUD_REGIONS = {
"dallas": "us-south",
"london": "eu-gb",
"frankfurt": "eu-de",
"tokyo": "jp-tok"
}

IBM_CLOUD_PROJECT_ID = "xxx-xxx-xxx" # Replace with your project id


def get_iam_token(api_key: str) -> str:
"""
Return an IAM access token generated from an API key.
"""

headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = f"apikey={api_key}&grant_type=urn:ibm:params:oauth:grant-type:apikey"
resp = httpx.post(
url="https://iam.cloud.ibm.com/identity/token",
headers=headers,
data=data,
)
token = resp.json().get("access_token")
return token


def format_user_message(raw_user_msg: str) -> str:
"""
Return a formatted prompt using the official Mistral tokenizer.
"""

tokenizer = MistralTokenizer.v3() # Use v3 for Mistral Large
tokenized = tokenizer.encode_chat_completion(
ChatCompletionRequest(
messages=[UserMessage(content=raw_user_msg)], model="mistral-large"
)
)
return tokenized.text


region = "frankfurt" # Define the region of your choice here
api_key = os.environ["IBM_API_KEY"]
access_token = get_iam_token(api_key=api_key)
credentials = Credentials(url=f"https://{IBM_CLOUD_REGIONS[region]}.ml.cloud.ibm.com",
token=access_token)

params = {GenParams.MAX_NEW_TOKENS: 256, GenParams.TEMPERATURE: 0.0}
model_inference = ModelInference(
project_id=IBM_CLOUD_PROJECT_ID,
model_id="mistralai/mistral-large",
params=params,
credentials=credentials,
)
user_msg_content = "Who is the best French painter? Answer in one short sentence."
resp = model_inference.generate_text(prompt=format_user_message(user_msg_content))
print(resp)

进一步了解

欲了解更多信息和示例,请查看