モニタリング LangChain
LangChain は、大規模な言語モデル(LLM)と外部ツールを使用して、インテリジェントなコンテキスト認識システムを作成するAI駆動型アプリケーションを構築および管理するために設計された Python フレームワークです。 LangChain,、タスク、ツール、LLMを連鎖させてデータを推論し、文書、要約、洞察などの動的出力を生成するインテリジェントなワークフローを開発できる。 LangChain’s 複数のタスクやツールを連鎖させることで、多様なユースケースに適応できる高度なAIシステムを構築することができる。
Instanaは LangChain,、ワークフロー、LLMインタラクション、タスク実行のリアルタイムモニタリングを提供する強力な観測機能を提供する。 Instana の分散トレース、メトリクス収集、エラー検出を統合することで、 LangChain ワークフローがどのようにデータを処理し、外部ツールと相互作用し、タスク実行を最適化するかについて、深い洞察を得ることができます。 Instanaは、AI駆動システムのエンドツーエンドのトレーサビリティを確保し、 LangChain の実装をより透明化、信頼性、拡張性の高いものにする。 この統合により、チームはパフォーマンスを監視し、 LangChain で構築されたAI搭載アプリケーションのシームレスな運用のための異常を検出することができます。
計装 LangChain アプリケーション
LangChain アプリケーションを計測するには、以下の手順を実行します:
あなたの環境がすべての前提条件を満たしていることを確認してください。 詳しくは、 前提条件を参照。
LangChain, の依存関係をインストールするには、以下のコマンドを実行する:
pip3 install langchain-core==0.3.34 langchain-community==0.3.17LangChain サンプル・アプリケーションで使用されている watsonX モデルにアクセスするために、以下の認証情報をエクスポートします:
export WATSONX_URL="<watsonx-url>" export WATSONX_PROJECT_ID="<watsonx-project-id>" export WATSONX_API_KEY="<watsonx-api-key>"以下のコードを実行して、 LangChain サンプル・アプリケーションを生成します:
from langchain_ibm import WatsonxLLM from langchain_core.prompts import PromptTemplate from traceloop.sdk import Traceloop from traceloop.sdk.decorators import task, workflow import os # Initialize Traceloop Traceloop.init(app_name="langchain_watsonx_service") @task(name="initialize_watsonx_model") def initialize_model(): return WatsonxLLM( model_id="ibm/granite-13b-instruct-v2", url=os.getenv("WATSONX_URL"), apikey=os.getenv("WATSONX_API_KEY"), project_id=os.getenv("WATSONX_PROJECT_ID"), params={ "decoding_method": "sample", "max_new_tokens": 512, "min_new_tokens": 1, "temperature": 0.5, "top_k": 50, "top_p": 1, } ) @task(name="create_prompt_template") def create_prompt(): return PromptTemplate( input_variables=["input_text"], template="You are a helpful AI assistant. Respond to the following: {input_text}" ) @task(name="process_llm_query") def process_query(prompt, llm, input_text): chain = prompt | llm return chain.invoke({"input_text": input_text}) @workflow(name="watsonx_conversation_workflow") def run_conversation(): watsonx_llm = initialize_model() prompt_template = create_prompt() input_text = "Explain the concept of quantum computing in simple terms." response = process_query(prompt_template, watsonx_llm, input_text) print("Response:", response) if __name__ == "__main__": run_conversation()以下のコマンドを実行して、 LangChain アプリケーションを実行する:
python3 ./<langchain-sample-application>.py
LangChain サンプル・アプリは、以下の例に示すようなレスポンスを生成する:
Response: Quantum computing is a type of computing that uses quantum-mechanical phenomena such as superposition and quantum entanglement to perform calculations. It is different from classical computing, which uses binary digits (0s and 1s) to represent information. In quantum computing, information is encoded in quantum bits, or "qubits," which can exist in a superposition of both 0 and 1 at the same time. This allows quantum computers to perform certain types of calculations much faster than classical computers.
LangChain モニタリングを設定すると、Instana は LangChain アプリケーションから以下のトレースとメトリクスを収集します:

LangChain, で使用される LLM から収集されたメトリクスを表示するには、 View metrics を参照してください。
トラブルシューティング
LangChain ベースのアプリケーションを監視していると、以下のような問題が発生することがある:
create_react_agentを使用すると、一部のメトリクスが期待される値で表示されないことがある
モデル名を含むメトリクスのいずれかが正しく表示されない場合は、create_react_agentのインポートに使用されているパッケージを確認してください。 langchain.agents ' パッケージからのインポートはサポートされていませんので、代わりに ' langgraph.prebuilt ' パッケージを使用してください。