Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-update-training-api-26.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Openrouter.ai は、多くの LLM のための統合インターフェースです。OpenAI GPT-4、Anthropic Claude、Google Gemini などの基盤モデルだけでなく、LLama-3 や Mixtral などのオープンソースモデル、さらには 他にも多数 のモデルをサポートしており、一部のモデルは無料で提供されています。
OpenRouter は REST API と OpenAI SDK 互換性 (ドキュメント) を提供しており、Weave はこれを自動的に検出してインテグレーションします(詳細は OpenRouter の クイックスタート を参照してください)。
OpenAI SDK のコードを OpenRouter に切り替えるには、APIキー を OpenRouter API キーに、base_url を https://openrouter.ai/api/v1 に、そして model を多数ある チャットモデル のいずれかに変更します。weave.init() を呼び出す際に、Traces のための プロジェクト 名を指定してください。指定しない場合は、デフォルトの Entities が使用されます。デフォルトの Entities を確認または更新するには、W&B Models ドキュメントの User Settings を参照してください。
import os
import openai
import weave
weave.init('openrouter-weave')
system_content = "You are a travel agent. Be descriptive and helpful."
user_content = "Tell me about San Francisco"
client = openai.OpenAI(
api_key=os.environ.get("OPENROUTER_API_KEY"),
base_url="https://openrouter.ai/api/v1",
)
chat_completion = client.chat.completions.create(
extra_headers={
"HTTP-Referer": $YOUR_SITE_URL, # オプション。openrouter.ai のランキングにあなたのアプリを含める場合に使用します。
"X-Title": $YOUR_APP_NAME, # オプション。openrouter.ai のランキングに表示されます。
},
model="meta-llama/llama-3.1-8b-instruct:free",
messages=[
{"role": "system", "content": system_content},
{"role": "user", "content": user_content},
],
temperature=0.7,
max_tokens=1024,
)
response = chat_completion.choices[0].message.content
print("Model response:\n", response)
これは開始するための簡単な例ですが、より複雑な ユースケース で Weave を独自の関数と統合する方法については、OpenAI ガイドを参照してください。