base_url を https://openrouter.ai/api/v1 に、そして model を多数ある チャットモデル のいずれかに変更します。weave.init() を呼び出す際に、Traces のための プロジェクト 名を指定してください。指定しない場合は、デフォルトの Entities が使用されます。デフォルトの Entities を確認または更新するには、W&B Models ドキュメントの User Settings を参照してください。
自動 Weave インテグレーションを備えた OpenRouter の統合インターフェースを使用して、多数の LLM を利用する
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)