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

# Langfuse

> Novita AI と Langfuse をセットアップし、AI アプリケーションを開発、監視、評価、デバッグするためのステップバイステップガイド。

Langfuse を使用すると、チームは Novita AI で構築した LLM アプリケーションを共同でデバッグ、分析、反復改善できます。完全に統合された機能により開発ワークフローが効率化され、生産性が向上し、進捗が加速します。

このガイドでは、Novita AI を Langfuse と統合する方法を説明します。Novita AI のチャット、言語、コード向け API エンドポイントは、OpenAI の API と完全に互換性があります。これにより、Langfuse OpenAI drop-in replacement を使用して、アプリケーションのすべての部分をトレースできます。

# 前提条件

始める前に、以下を準備してください。

* Novita AI LLM API Key:
  * Novita AI のウェブサイトにアクセスしてアカウントを作成します。
  * ログインして Key Management ページに移動し、API Key を生成します。これは Novita AI のモデルを Langfuse と統合するために必須です。

    <Frame>
      <img src="https://mintcdn.com/novitaai/AUzGFUdz_qhrXjaJ/images/third-party/langfuse-1.png?fit=max&auto=format&n=AUzGFUdz_qhrXjaJ&q=85&s=d7d5a07696198c38f4d3872678a80e85" alt="" width="3486" height="1004" data-path="images/third-party/langfuse-1.png" />
    </Frame>
* Langfuse アカウント:
  * 強力な AI アプリケーションの構築を始めるために、公式 [Langfuse](https://langfuse.com)ウェブサイトで Langfuse アカウントを登録します。

# 統合手順

## ステップ 1: 依存関係をインストールする

必要な Python パッケージがすべてインストールされていることを確認してください。

```shell theme={"system"}
pip install openai langfuse
```

## ステップ 2: 環境変数を設定する

```python theme={"system"}
import os

# Get keys for your project from the project settings page
# https://cloud.langfuse.com

os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-..." # DOCS EXAMPLE KEYS
os.environ["LANGFUSE_SECRET_KEY"] = "sk-..." # DOCS EXAMPLE KEYS
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # 🇪🇺 EU region
# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # 🇺🇸 US region


# Get your Novita AI API key from the project settings page
os.environ["NOVITA_API_KEY"] = "..."
```

## ステップ 3: Langfuse OpenAI drop-in Replacement

このステップでは、`from langfuse.openai import openai` をインポートして、ネイティブの [OpenAI drop-in replacement](https://langfuse.com/docs/integrations/openai/python/get-started) を使用します。

OpenAI のクライアントライブラリで Novita AI の使用を開始するには、Novita AI API key を `api_key` オプションに渡し、`base_url` を [`https://api.novita.ai/openai:`](https://api.novita.ai/openai) に変更します。

```python theme={"system"}
# instead of import openai:
from langfuse.openai import openai

client = openai.OpenAI(
  api_key=os.environ.get("NOVITA_API_KEY"),
  base_url="https://api.novita.ai/openai",
)
```

**注:** OpenAI drop-in replacement は、[Langfuse Python SDK](https://langfuse.com/docs/observability/sdk/overview) および [`@observe()` instrumentation](https://langfuse.com/docs/observability/sdk/instrumentation) と完全に互換性があり、アプリケーションのすべての部分をトレースできます。

## ステップ 4: 例を実行する

次のコードセルは、トレースされた OpenAI クライアントを使用して Novita AI のチャットモデルを呼び出す方法を示しています。すべての API 呼び出しは Langfuse によってシームレスにトレースされます。

```python theme={"system"}
client = openai.OpenAI(
  api_key=os.environ.get("NOVITA_API_KEY"),
  base_url="https://api.novita.ai/openai",
)

response = client.chat.completions.create(
  model="meta-llama/llama-3.1-70b-instruct",
  messages=[
    {"role": "system", "content": "Act like you are a helpful assistant."},
    {"role": "user", "content": "What are the famous attractions in San Francisco?"},
  ]
)

print(response.choices[0].message.content)

San Francisco, one of the most iconic cities in the world, is home to a plethora of famous attractions that cater to all interests and ages. Here are some of the most popular attractions in San Francisco:

1. **Golden Gate Bridge**: An engineering marvel and a symbol of San Francisco, the Golden Gate Bridge is a must-visit attraction. Take a walk or bike ride across the bridge for spectacular views of the city and the Bay.
2. **Alcatraz Island**: Explore the infamous former prison turned national park, which once housed notorious inmates like Al Capone. Take a ferry to the island and enjoy a guided tour of the prison and its surroundings.
3. **Fisherman's Wharf**: A bustling waterfront district, Fisherman's Wharf is famous for its seafood restaurants, street performers, and stunning views of the Bay Bridge and Alcatraz Island. Don't miss the sea lions at Pier 39!
4. **Chinatown**: San Francisco's Chinatown is one of the largest and oldest in the United States. Explore the colorful streets, try some authentic Chinese cuisine, and shop for unique souvenirs.
5. **Golden Gate Park**: A sprawling urban park that's home to several attractions, including the de Young Museum, the California Academy of Sciences, and the Japanese Tea Garden.
6. **Cable Cars**: A classic San Francisco experience, the cable cars offer a fun and historic way to explore the city. Take a ride on the Powell-Mason line to Fisherman's Wharf, or the Powell-Hyde line to Lombard Street.
7. **Lombard Street**: Known as the "crookedest street in the world," Lombard Street is a scenic and winding road that offers stunning views of the city.
8. **Union Square**: A vibrant public square in the heart of the city, Union Square is surrounded by shopping, dining, and entertainment options. Catch a show at the historic Curran Theatre or take a stroll through the square.
9. **The Painted Ladies**: A row of colorful Victorian houses on Alamo Square, the Painted Ladies are a iconic symbol of San Francisco's architecture. Take a photo in front of these stunning homes.
10. **The Exploratorium**: A museum of science, art, and human perception, the Exploratorium is a great place to learn and have fun. With interactive exhibits and stunning views of the Bay, it's a must-visit for families and science enthusiasts.
11. **Pier 39**: A popular shopping and dining destination, Pier 39 offers stunning views of the Bay Bridge, Alcatraz Island, and the sea lions that call the pier home.
12. **The de Young Museum**: A fine arts museum located in Golden Gate Park, the de Young Museum features a diverse collection of art and cultural exhibitions from around the world.

These are just a few of the many famous attractions in San Francisco. Whether you're interested in history, culture, science, or entertainment, San Francisco has something for everyone.
```

## ステップ 5: Langfuse でトレースを確認する

サンプルのモデル呼び出しを実行した後、Langfuse でトレースを確認できます。これらのトレースには、Novita AI API 呼び出しに関する詳細情報が含まれます。

* リクエストパラメータ（model、messages、temperature など）
* レスポンス内容
* トークン使用量の統計
* レイテンシ指標

[Langfuse の公開サンプルトレースリンク](https://cloud.langfuse.com/project/cm7ua5l6e05wlad07qr6ce2wn/traces/039cc8b2-dba0-479f-9cd6-63672bc08c71?timestamp=2025-03-06T02%3A15%3A15.184Z)。
