0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Langfuse を Amazon Bedrock と Integration して体験してみる

Posted at

Amazon Bedrock Integration

Langfuse を Amazon Bedrock と組み合わせることで、各リクエストの詳細なトレースやメトリクスを簡単に取得でき、アプリのパフォーマンスや動作を分析できます。

トレーシング機能に加え、プレイグラウンド、LLM を用いた評価、プロンプト実験などの UI 機能も Amazon Bedrock と完全に互換性があり、プロジェクト設定で Bedrock の設定を追加するだけで利用できます。

Integration には以下の 3 つが書かれていました。

  • フレームワークの利用
    • Langchain
    • Llama Index
    • Haystack
    • Vercel AI SDK
  • LiteLLM による経由
  • Bedrock SDK を Langfuse Decorator でラッピング

Langfuse を始める

1. Langfuse リポジトリのクローン

git clone https://github.com/langfuse/langfuse.git
cd langfuse

2. Langfuse の起動

docker compose up

3. Langfuse UI にサインイン

http://localhost:3000 を開き、Langfuse UI にアクセスする。初回は、Sign up でアカウントを作成し、Sign in します。
スクリーンショット 2025-02-28 19.03.14.png

Sign in 後最初に表示される画面は以下の通りです。
スクリーンショット 2025-02-28 19.07.55.png

4.Organization と Project の作成

最初に Organization と Project を作成しておきます
スクリーンショット 2025-02-28 19.09.06.png

スクリーンショット 2025-02-28 19.09.45.png

Langfuse をアプリケーションに実装

1.Langfuse から secret_key と public_key を確認する

「Settings」の「API Keys」から作成、確認でききます。
スクリーンショット 2025-02-28 20.32.24.png

2. アプリケーションコードの修正

アプリケーションコードで、コメントアウトしている箇所を追記します。
「Settings」の「API Keys」は 1 つ前の手順で確認した「API Keys」を利用します。

import streamlit as st
from langchain_aws import ChatBedrock
from langchain_core.messages import HumanMessage, SystemMessage
# インポート
from langfuse.callback import CallbackHandler

# CallbackHandler の設定
langfuse_handler = CallbackHandler(
    secret_key="<secret_key>",
    public_key="<public_key>",
    host="http://localhost:3000"
)

modeId = "anthropic.claude-3-5-sonnet-20241022-v2:0"

llm = ChatBedrock(
    model_id = modeId,
    model_kwargs={
        "max_tokens": 2000,
        "temperature": 0.1,
        "top_k": 250,
        "top_p": 0.9,
    },
    streaming=True,
    # 呼び出し
    callbacks=[langfuse_handler]
)

systemMsg = "あなたはユーザーの質問に答える日本人のアシスタントです。"

messages = [
    SystemMessage(content= systemMsg),
]

st.title("Bedrock チャットボット")

if prompt := st.chat_input("質問内容を入力して下さい"):
    messages.append(HumanMessage(content=prompt))

    with st.chat_message("user"):
        st.markdown(prompt)
        
    with st.chat_message("assistant"):
        st.write_stream(llm.stream(messages))

動作確認

1. 質問する

とりあえず、適当な質問をしておきます。
スクリーンショット 2025-02-28 20.29.05.png

2. トレースを確認する

Langfuse からトレースを確認してみます。

Tracing > Traces はこんな感じです。
スクリーンショット 2025-02-28 20.41.35.png

更にトレースの中身はこんな感じでした。
スクリーンショット 2025-02-28 20.46.06.png

最後にダッシュボードも用意されているので、監視の可視化と言う面でとても便利ですね。
スクリーンショット 2025-02-28 20.46.46.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?