> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenRouter

> Use OpenRouter's unified interface for many LLMs with automatic Weave integration

このガイドでは、OpenRouter 経由でアクセスする LLM への Call を Weave で自動的にトレースする方法を説明します。これにより、アプリケーションでのモデルの使用状況を単一のダッシュボードから監視、デバッグ、評価できます。

OpenRouter は多くの LLM に対応する統一インターフェースです。OpenAI GPT-4、Anthropic Claude、Google Gemini などの基盤モデルに加え、LLama-3、Mixtral などのオープンソースモデルも[サポートしています](https://openrouter.ai/models)。一部のモデルは無料で提供されています。

OpenRouter は REST API と [OpenAI SDK との互換性](https://openrouter.ai/docs/guides/community/openai-sdk)を提供しており、Weave はこれを自動的に検出して連携します。詳しくは、OpenRouter の[クイックスタートガイド](https://openrouter.ai/docs/quick-start)をご覧ください。Weave は OpenAI SDK を検出するため、既存の OpenAI コードを設定を少し変更するだけで再利用できます。

OpenAI SDK のコードを OpenRouter に切り替えるには、APIキーを [OpenRouter の APIキー](https://openrouter.ai/docs/api-keys)に変更し、`base_url` を `https://openrouter.ai/api/v1` に設定して、利用可能な[チャットモデル](https://openrouter.ai/docs/models)のいずれかをモデルに指定します。`weave.init()` を呼び出す際には、トレースを保存するプロジェクト名を指定します。指定しない場合、Weave はデフォルトの entity を使用します。デフォルトの entity を確認または更新する方法については、W\&B Models のドキュメントにある[ユーザー設定](/ja/products/wandb/platform/app/settings-page/user-settings#default-team)をご覧ください。次の例を実行すると、Weave は OpenRouter への Call を Weave プロジェクト内のトレースとして取得します。

```python lines {5,10-12} theme={"system"}
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](/ja/products/wandb/weave/guides/integrations/openai#track-your-own-ops) ガイドをご覧ください。


## Related topics

- [インテグレーションの概要](/ja/products/wandb/weave/guides/integrations.md)
