1
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?

LangSmith で Amazon Bedrock のトレースを体験してみる

Last updated at Posted at 2025-03-06

LangSmith とは

LangSmith は、LangChain の開発元である LangChain Inc. が提供する、LLM(大規模言語モデル)アプリケーションのデバッグ、監視、評価を行うためのツールです。

LangSmith の主な機能

  • トレース
  • 評価
  • プロンプト管理

動かしてみる

登録

LangSmith に登録しサインインする。

セットアップ

既に試した後の画面ですが、サインイン後の「Set up tracing」を参考に行なっていきます。
スクリーンショット 2025-03-06 19.05.05.png

API キーの作成

「Genarate API key」をクリックし、作成します。
スクリーンショット 2025-03-06 19.09.41.png

LangSmith のインストール

LangSmith をインストールします。

pip install -U langsmith

実装

次のコードでコメントしている箇所が、LangSmith の実装です。

<API_KEY>は先程の「API キーの作成」の手順で作成したものを指定し、
<LANGCHAIN_PROJECT>は今回のプロジェクト名を入力しておきます。
私の場合は今回「bedrock-tracing」というプロジェクト名を入れておきました。

import os
import boto3
from langchain_aws import ChatBedrock
from langchain.schema import HumanMessage
from langsmith import traceable

# LangSmith の設定
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "<API_KEY>"
os.environ["LANGCHAIN_PROJECT"] = "<LANGCHAIN_PROJECT>"

boto3_bedrock = boto3.client("bedrock-runtime")

llm = ChatBedrock(
    model_id="anthropic.claude-3-5-sonnet-20241022-v2:0", 
    client=boto3_bedrock
    )

# LangSmith トレースを有効化
@traceable(name="Bedrock Chat Example") 

def chat_with_bedrock():
    messages = [HumanMessage(content="LangSmith でトレースする方法を教えてください。")]
    response = llm.invoke(messages)
    print(response.content)
    return response

# 実行
chat_with_bedrock()

動作確認

上記のコードを実行後、LangSmith を確認してみます。

画面左の「Tracing Projects」をクリックすると、「Tracing Projects」というプロジェクトが作成されていますのでクリックします。
スクリーンショット 2025-03-06 19.17.37.png

中身を確認していくと、このような形でトレースされていることが確認できました。
スクリーンショット 2025-03-06 19.25.52.png

1
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
1
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?