LoginSignup
3
3

久々にLangChainのドキュメント見ていたら、LangSmithなるものに気づきました(遅い)。

It lets you debug, test, evaluate, and monitor chains and intelligent agents built on any LLM framework and seamlessly integrates with LangChain, the go-to open source framework for building with LLMs.

LangSmithによって、いかなるLLMフレームワークで構築したインテリジェントなチェーンやエージェントをデバッグ、テスト、評価、監視できるようになり、LLMで開発するための頼りになるオープンソースフレームワークであるLangChainとシームレスに連携できます。

LangSmithでサインアップして、APIキーを取得します。
Screenshot 2024-02-24 at 16.36.02.png
Screenshot 2024-02-24 at 16.36.19.png

LangChainのクイックスタートの前半を流します。

%pip install langchain
%pip install langchain-openai
dbutils.library.restartPython()

APIキーを設定します。

# OpenAIのセットアップ
import os
import openai

OPENAI_API_TOKEN = dbutils.secrets.get("demo-token-takaaki.yayoi", "openai_api_key")
os.environ["OPENAI_API_KEY"] = OPENAI_API_TOKEN
os.environ["LANGCHAIN_TRACING_V2"]="true"
os.environ["LANGCHAIN_API_KEY"]="<LangChainのAPIキー>"
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(openai_api_key=OPENAI_API_TOKEN)
llm.invoke("how can langsmith help with testing?")

AIMessage(content='Langsmith can help with testing in the following ways:\n\n1. Automated testing: Langsmith can be used to write scripts for automated testing of software applications, which can help in identifying bugs and errors in the code.\n\n2. Test data generation: Langsmith can be used to generate test data for various scenarios, which can help in testing the functionality and performance of the software.\n\n3. Integration testing: Langsmith can be used to write scripts for integration testing of different components of the software, ensuring that they work together seamlessly.\n\n4. Performance testing: Langsmith can be used to write scripts for performance testing of the software, helping to identify bottlenecks and optimize the code for better performance.\n\n5. Regression testing: Langsmith can be used to write scripts for regression testing, ensuring that new code changes do not introduce any new bugs or issues in the software.\n\nOverall, Langsmith can be a valuable tool for testing software applications, helping to improve the quality and reliability of the code.')

日本語で。

llm.invoke("langsmithはどのようにテストの役に立ちますか?")

AIMessage(content='Langsmithは、テストの役に立ついくつかの方法があります。まず、Langsmithは言語処理技術を使用して、テストの質問や回答を分析し、正確な情報を提供します。また、Langsmithはテストの準備や練習に役立つ質問や課題を提供することもできます。さらに、Langsmithは特定のテストに関する情報や戦略を提供することもできます。そのため、Langsmithを使用することで、効果的なテストの準備や対策を行うことができます。')

プロンプトを組み合わせます。

from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
    ("system", "あなたは世界クラスの技術ドキュメントのライターです。"),
    ("user", "{input}")
])

この記法は初めて見ました。シェルみたいですね。

chain = prompt | llm 
chain.invoke({"input": "langsmithはどのようにテストの役に立ちますか?"})

より技術書っぽくなりました。

AIMessage(content='Langsmithは、テストの準備、実行、結果の分析、およびレポート作成のための包括的なツールです。以下に、Langsmithがテストにどのように役立つかについていくつかの利点を挙げてみましょう。\n\n1. 自動化されたテストケースの作成:Langsmithは、自動化されたテストケースを作成するための強力な機能を提供します。これにより、テストケースの網羅性や正確性が向上し、手動でのテスト作業の負担が軽減されます。\n\n2. テストの効率化:Langsmithは、テストケースの自動実行や複数のテストケースの一括実行など、テストの効率化をサポートします。これにより、テストの時間と労力を節約することができます。\n\n3. テスト結果の分析:Langsmithは、テスト結果の詳細な分析を提供し、問題の特定や修正が容易になります。テスト結果のダッシュボードやレポートを通じて、全体的なテストの状況を把握することができます。\n\n4. 連携性:Langsmithは、他の開発ツールやCI/CDツールとの連携が可能です。これにより、テストの自動化や統合がスムーズに行えます。\n\n5. 品質向上:Langsmithを使用することで、テストの網羅性や正確性が向上し、バグの早期発見や品質向上につながります。また、継続的なテストの実施により、ソフトウェアの品質管理が強化されます。\n\nこれらの利点により、Langsmithはテストプロセス全体を効率的に管理し、ソフトウェアの品質を向上させるのに役立ちます。')

アウトプットパーサーを組み合わせます。

from langchain_core.output_parsers import StrOutputParser

output_parser = StrOutputParser()

この記法は直感的ですね。

chain = prompt | llm | output_parser
chain.invoke({"input": "langsmithはどのようにテストの役に立ちますか?"})

テキストのみを取得できました。

'Langsmithは、テストにおいて以下のような方法で役立ちます:\n\n1. 多言語対応のテスト: Langsmithは複数の言語に対応しているため、異なる言語でのテストを容易に行うことができます。これにより、グローバルな製品やサービスにおいて、複数の言語でのテストを効率的に実施することができます。\n\n2. 言語ごとのテストケースの管理: Langsmithを使用することで、異なる言語でのテストケースを簡単に管理することができます。言語ごとにテストケースを整理し、効果的なテスト計画を策定することが可能です。\n\n3. 多言語翻訳テスト: Langsmithを活用することで、翻訳されたテキストのテストを容易に行うことができます。異なる言語間でのテキストの一貫性や正確性を確認するために、Langsmithを使用することが重要です。\n\n4. 翻訳品質の向上: Langsmithを使用することで、翻訳された内容の品質を向上させることができます。正確な翻訳が行われているかどうかをテストし、ユーザーにとってわかりやすい言語表現が使用されているかを確認することができます。\n\n総じて、Langsmithは多言語環境においてテストを効果的に実施し、製品やサービスの品質向上に貢献します。'

で、LangSmithの画面にアクセスしてみます。LLMの入出力が確認できます。これは便利。
Screenshot 2024-02-24 at 16.47.56.png
Screenshot 2024-02-24 at 16.48.07.png

メトリクスも追跡できます。
Screenshot 2024-02-24 at 16.52.35.png

Databricksの推論テーブルモデルサービングエンドポイントと近い機能ですね。でも、こちらの方が(当たり前ですが)LangChainとの連携が深いです。チェーンのコンポーネントを追跡できます。
Screenshot 2024-02-24 at 17.07.49.png

LLMをベースにしたシステム開発がどんどん本格化している感じです。

はじめてのDatabricks

はじめてのDatabricks

Databricks無料トライアル

Databricks無料トライアル

3
3
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
3
3