はじめに
Azure AI Foundry 上で提供されている Cohere Rerank モデル(Cohere‑rerank‑v4.0‑pro) を Python から利用しようとした際に、
どのエンドポイントを指定すればよいのか分からずハマったので、検証結果と動作する例をまとめます。
同じところで詰まっている方の参考になれば幸いです。
前提
- Azure AI Foundry で Cohere‑rerank‑v4.0‑pro をデプロイ済み
- Cohere の公式ドキュメントを参照して Python から呼び出す
- cohere Python SDK を利用
- 本記事の内容は Preview 機能 を対象としています(本番利用は要注意)
やりたかったこと
Cohere のドキュメントにある Rerank API のサンプルをもとに、Azure 上にデプロイした Cohere Rerank モデルを Python から呼び出す。
参照した主なドキュメント:
Azure 側のモデル紹介
Cohere 公式ドキュメント(Azure 連携)
まず試したがうまくいかなかった例
Cohere のドキュメントに記載されている形式をそのまま Azure 向けに置き換え、base_url を Foundry ポータルに表示されている値に合わせて指定してみました。
import cohere
co = cohere.Client(
base_url="https://<Resource名>.<Region名>.inference.ai.azure.com/v1/rerank",
api_key="<api_key>",
)
documents = [
{
"Title": "Incorrect Password",
"Content": "Hello, I have been trying to access my account for the past hour and it keeps saying my password is incorrect.",
},
{
"Title": "Return Defective Product",
"Content": "Hello, I have a question about the return policy for this product. I purchased it a few weeks ago and it is defective.",
},
]
response = co.rerank(
documents=documents,
query="What emails have been about returning items?",
model="Cohere-rerank-v4.0-pro",
rank_fields=["Title", "Content"],
top_n=5,
)
結果
getaddrinfo failed
エンドポイントへ到達できないエラーが発生し、実行できませんでした。
Cohere のドキュメント通りやってるのに何でできないんだと憤慨
動作した Python の例
検証の結果、以下の形式のエンドポイントを指定することで Python SDK から正常に実行できることを確認しました。
import cohere
co = cohere.Client(
base_url="https://<Resource名>.services.ai.azure.com/providers/cohere",
api_key="<api-key>",
)
documents = [
{
"Title": "Incorrect Password",
"Content": "Hello, I have been trying to access my account for the past hour and it keeps saying my password is incorrect.",
},
{
"Title": "Return Defective Product",
"Content": "Hello, I have a question about the return policy for this product. I purchased it a few weeks ago and it is defective.",
},
]
response = co.rerank(
documents=documents,
query="What emails have been about returning items?",
model="Cohere-rerank-v4.0-pro",
rank_fields=["Title", "Content"],
top_n=5,
)
curl での実行例(直接 API を叩く場合)
Python SDK を使わず、REST API として直接呼び出す場合は、Foundryに記載のある以下の URL で実行できます。
curl -X POST "https://<Resource名>.services.ai.azure.com/providers/cohere/v2/rerank" \
-H "api-key: <api-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "Cohere-rerank-v4.0-pro",
"query": "What is the capital of France?",
"documents": [
"Paris is the capital and largest city of France.",
"London is the capital of the United Kingdom."
],
"top_n": 3
}'
ハマりポイントまとめ
- Cohere Python SDK を使う場合は
https://<Resource名>.services.ai.azure.com/providers/cohere
を base_url に指定すると動作した - REST API で叩く場合は
https://<Resource名>.services.ai.azure.com/providers/cohere/v2/rerankが有効 - Cohere Rerank モデルは Preview 提供 のため、仕様変更や挙動差分が出る可能性がある
おわりに
Azure AI Foundry × Cohere Rerank はとても便利ですが、エンドポイント周りの表記がやや分かりづらく、ドキュメント通りでも動かない点には注意が必要だと感じました。
同じエラー(404 / 接続失敗)で悩んでいる方の助けになれば幸いです。