Unit Tests | LangSmith Evaluations - Part 10

LangChain
17 Apr 202407:38

Summary

TLDRランス氏が、コード生成の流れに関する10本目のビデオを紹介しています。この流れでは、大規模なドキュメントやユーザーの質問を入力として、コードソリューションを生成します。生成されたコードは、プリアンブル、インポート、コードの3つの要素を含む構造化されたオブジェクトです。ランス氏は、ユニットテストの重要性を強調し、特にインポートとコードが実行可能であることを確認するテストを提案しています。また、CIフローの一部として実行されるユニットテストの実行方法と、LangSmithを使用してテスト結果を記録する方法についても説明しています。LangSmithのデコレーターを使用することで、テスト結果をLangSmithに記録し、データセットに分類して管理することができます。

Takeaways

  • 📈 ユニットテストは、CI(継続的インテグレーション)の一部として実行される単純なアサーションであることが多いです。
  • 🔍 Lanceは、コード生成のフローに沿って、自然言語の質問を構造化された回答オブジェクトに変換する作業を行っています。
  • 🛠️ ユニットテストは、生成されたコードオブジェクトが正しくインポートとコードを含んでいるかどうかを確認するのに役立ちます。
  • 📄 ユニットテストはオフラインで実行でき、CIフローの一部として実行されることがあります。
  • 🔗 LangSmithは、一般的なフレームワーク(例:pytest)と組み合わせて使用して、テストを計装することができる。
  • 📝 main.pyファイルには、コード生成ロジックが定義されており、テストは独立したファイルとしてtestサブディレクトリに配置されます。
  • 🧩 テストでは、インポートとコードの実行を試み、エラーが発生した場合はpytest.failを使用してテストを失敗させます。
  • 📚 LangSmithのデコレーター@unitを使用すると、ユニットテストをLangSmithに記録できます。
  • 📊 LangSmithを使用してユニットテストを記録すると、テスト結果とメタデータが独立したデータセットに記録されます。
  • 📈 記録されたテスト結果では、レイテンシーやフィードバックスコア、使用されたトークンなどの詳細を確認できます。
  • 🔄 LangSmithを使用してユニットテストを記録することで、CIフローの一部として実行しやすくなります。

Q & A

  • ランスが紹介するlsmith評価シリーズの10番目のビデオは、何について説明していますか?

    -ランスが紹介するlsmith評価シリーズの10番目のビデオは、ユニットテストに焦点を当てています。ユニットテストは、CIの一部としてアプリケーション機能を実行する単純なアサーションになり得ます。

  • コード生成ノードが受け取る入力とは何ですか?

    -コード生成ノードは、大規模なドキュメンテーションセットを入力として受け取ります。たとえば、ラインチェーンのドキュメントやユーザーの質問などが挙げられます。

  • コード生成ノードが生成する構造化された回答オブジェクトには何が含まれますか?

    -コード生成ノードが生成する構造化された回答オブジェクトには、3つの要素が含まれます。これらは、プレアンブル、インポート、およびコードです。

  • ユニットテストで確認すべき内容とは何ですか?

    -ユニットテストでは、インポートとコードが実行可能かどうかを確認することが重要です。これは、生成された構造化コードオブジェクトが正しく、かつ期待通りに機能することを確認するためです。

  • ランスが使用しているデータモデルは何を含んでいますか?

    -ランスが使用しているデータモデルは、プレフィックスインポートとコードを含んでいます。これは、llm(Large Language Model)にバインドされ、構造化出力メソッドを使用して定義されています。

  • テストを実行する際に使用しているフレームワークは何ですか?

    -テストを実行する際に使用しているフレームワークは、pytestです。これは、Pythonで広く使用されるテストフレームワークです。

  • ランスが示すテストコードの例では、何をテストしていますか?

    -ランスが示すテストコードの例では、生成されたコードのインポートとコードブロックの実行可能性をテストしています。

  • lsmithのデコレーターを使用する利点は何ですか?

    -lsmithのデコレーターを使用することで、ユニットテストの結果をlsmithに記録できます。これにより、テスト結果のメタデータが得られ、テストの結果をデータセットとして分離して保管することが可能です。

  • lsmithのデータセットとテスト結果を確認するにはどうすればよいですか?

    -lsmithのデータセットとテスト結果を確認するには、lsmithのウェブインターフェースにアクセスし、データセットとテスト結果を閲覧します。各テスト結果は独立したデータセットとして記録され、詳細な情報を見ることができます。

  • CIフローにユニットテストを含める理由は何ですか?

    -CIフローにユニットテストを含めることで、アプリケーションの各機能が期待通りに動作することを継続的に確認できます。また、コードの変更や追加によって生じる可能性のある問題を早期に検出することができます。

  • ランスが使用しているメインのPythonファイルは何を定義していますか?

    -ランスが使用しているメインのPythonファイル、main.pyは、コード生成ロジックを定義しています。具体的には、テキストを入力として受け取り、インポート、機能的なコードブロック、およびプレアンブルを含む構造化コードオブジェクトを生成するgenerate_code_solution関数を定義しています。

  • lsmithのユニットテストデコレーターの機能は何ですか?

    -lsmithのユニットテストデコレーターは、ユニットテストをlsmithに記録する機能を提供します。これにより、テストが実行されたときの詳細なメタデータとともにテスト結果をlsmithに記録し、後で分析や参照が容易になります。

Outlines

00:00

📝 ユニットテストの重要性と実装

Lanceは、Lang Chainの10番目のビデオで、ユニットテストの重要性を説明しています。ユニットテストは、CI(継続的インテグレーション)の一部として実行される単純なアサーションであり、アプリケーションの機能性に対して行われます。彼は、コード生成のフローを通じて、自然言語の質問を構造化された回答オブジェクトに変換するプロセスを紹介しました。そのオブジェクトには、preamble、imports、コードの3つの要素が含まれています。ユニットテストは、これらの要素が期待どおりに実行されることを確認するために行われます。また、Lang Smithを用いてユニットテストを記録し、CIフローの一部として実行することができると述べています。

05:01

🔍 Lang Smithによるユニットテストの記録

Lanceは、Lang Smithのデコレータを使用して、ユニットテストを記録する方法を紹介しました。テストは標準的なユニットテストであり、pytestを使用して実行されますが、Lang Smithのデコレータを追加することで、テスト結果をLang Smithに記録できます。これにより、テスト結果が独立したデータセットに記録され、メタデータとともに確認・分析が可能になります。また、Lang Smithのデータセットとテスト機能を使用することで、テストの結果を詳細に把握し、CIフローで簡単に使用できる利便性が得られます。

Mindmap

Keywords

💡ユニットテスト

ユニットテストとは、ソフトウェアの個々の部品を分離してテストすることを指します。このビデオでは、ユニットテストがコード生成の流れにおいて、インポートとコードが実行可能であることを確認するため行われます。ユニットテストはCI(継続的インテグレーション)プロセスの一部として実行されることが一般的です。

💡CI(継続的インテグレーション)

CIは、ソフトウェア開発プロセスでコードが定期的にビルド、テストされることを意味します。ビデオでは、ユニットテストがCIフローの一部として実行されると説明されています。CIは、開発者がコードの変更がプロジェクト全体にどのように影響するかを迅速に把握できるように支援するツールです。

💡コード生成

コード生成とは、自然言語の質問やドキュメントからコードを自動的に生成するプロセスです。ビデオでは、コード生成ノードがユーザーの質問とドキュメントに基づいてコードソリューションを生成することが強調されています。これは、開発者の作業を効率化し、エラーを減らすための重要なステップです。

💡pandocオブジェクト

pandocオブジェクトは、プレアンブル、インポート、コードを含む構造化された回答オブジェクトです。ビデオでは、自然言語の質問をpandocオブジェクトに変換することで、ユニットテストがインポートとコードが正しく機能することを確認するのに役立ちます。

💡ランダムテスト

ランダムテストとは、ランダム性に基づいてテストケースを生成する手法です。ビデオでは、ランダムテストがユニットテストと組み合わせて使用され、コード生成プロセスの信頼性を向上させる方法が説明されています。

💡pytest

pytestは、Pythonで書かれたユニットテストを実行するためのフレームワークです。ビデオでは、pytestを使用してユニットテストを実行し、コード生成プロセスが期待どおりに動作することを確認する方法が紹介されています。

💡LangSmith

LangSmithは、ビデオで使用されるテスト結果を記録し、分析するツールです。ユニットテストにLangSmithのデコレーターを追加することで、テスト結果をLangSmithに記録し、テストのメタデータと結果を確認できるようになります。

💡データセット

データセットとは、テスト結果を記録し、分析するために使用されるデータの集合体です。ビデオでは、LangSmithを使用してユニットテストの結果をデータセットとして記録し、テストの詳細を確認することができます。

💡構造化出力

構造化出力とは、データが整理され、特定の形式に従って出力されることを意味します。ビデオでは、コード生成プロセスで生成されたコードが構造化出力としてpandocオブジェクトに変換され、ユニットテストで使用されることが説明されています。

💡ランダム性

ランダム性とは、予測不可能性や偶然性を意味します。ビデオでは、ランダムテストがランダム性に基づいてテストケースを生成し、コード生成プロセスの堅牢性を向上させる方法が説明されています。

💡メタデータ

メタデータとは、データに関するデータです。ビデオでは、LangSmithを使用してユニットテストの結果が記録される際に、テストに関する詳細情報(メタデータ)も一緒に記録されることが強調されています。これにより、テスト結果をより深く分析できます。

Highlights

Unit tests are often simple assertions that can run as part of CI for app functionality.

The motivating example involves code generation that takes in documentation and user questions to produce code solutions.

The code generation process converts a natural language question into a structured answer object with a preamble, imports, and code.

The goal is to instrument unit tests to check if the imports and code are executable and work as expected.

The unit test setup includes a standalone dataset, a heuristic, and a reference for expected code executability.

Unit tests can be run offline as part of a CI flow or online.

LangSmith works with conventional frameworks like pytest for instrumenting tests.

A simple main.py file is set up to instrument the code generation logic.

Independent test files are created in a test subdirectory to implement checks for imports and code execution.

The unit tests use a decorator from LangSmith to log the test results to LangSmith.

Running the unit tests with pytest executes the tests and logs the results to LangSmith datasets.

Each test is logged to an independent dataset, allowing for compartmentalization.

The logged test results in LangSmith include metadata such as latency, feedback score, and tokens used.

The input passed to the unit test is also logged in LangSmith.

Using LangSmith to log unit tests provides a convenient way to monitor test results as part of CI.

The process demonstrates how to integrate LangSmith with standard unit testing frameworks like pytest.

Logging unit test results to LangSmith provides valuable insights into test performance and code quality.

The approach allows developers to easily track unit test results over time and identify trends or issues.

Overall, the integration of LangSmith with unit testing enables more effective test result monitoring and analysis.

Transcripts

play00:00

hi this is Lance from Lang chain this is

play00:02

the 10th video on our lsmith evaluation

play00:04

series focused on unit tests so unit

play00:07

tests are often simple assertions uh for

play00:09

example they can run as part of CI uh

play00:12

for app functionality now let me give a

play00:14

motivating example here so I've done

play00:17

some recent work on code generation and

play00:19

it follows a flow kind of like you see

play00:20

here so I have this code generation node

play00:24

uh that takes in some large set of

play00:26

documentation like for example a bunch

play00:28

of line chain Docs takes in user

play00:30

questions and produces code Solutions

play00:33

based on the user question and the

play00:35

documentation now in this flow I

play00:37

typically do something uh to the output

play00:40

where I convert it into this pantic

play00:43

object that contains three things a

play00:44

preamble Imports and code and I do

play00:47

Downstream stuff with that which you

play00:49

don't have to worry about here the key

play00:51

point is I basically convert a natural

play00:54

language question into a structured

play00:56

answer object that has these three

play00:58

things so how can instrument unit tests

play01:01

for example that check whether like

play01:03

Imports and the code are are executable

play01:06

that's like a very sane unit test you

play01:07

might want to do uh just to convince

play01:09

yourself that you're able to produce

play01:11

like a structured code object correctly

play01:13

and that the Imports and code actually

play01:15

work as expected right so that's like a

play01:17

pretty sane unit test that we we may

play01:19

want and again if we look at our overall

play01:21

framing where does this sit so in this

play01:23

case um we don't necessarily have like a

play01:26

standalone data set of examples we have

play01:29

um you know a heuristic like a

play01:31

hard-coded decision or a hard-coded

play01:34

assertion um and we have some reference

play01:38

um that we expect um for example in this

play01:40

particular case I expect that the code

play01:43

is executable correctly um that's really

play01:46

it and of course unit tests they they

play01:49

can be run offline um you know as part

play01:51

of a CI flow or they can be run online

play01:53

and we're going to talk about the

play01:54

offline case and how you can instrument

play01:56

this with lsmith so what we're going to

play01:58

do is we're going to show the Langs

play02:00

Smith works with conventional Frameworks

play02:01

like Pi test for instrumenting tests and

play02:04

we're going to set up a few things we're

play02:05

just going to set up a very simple

play02:07

main.py file and I'll go over there and

play02:09

show you that right now which is

play02:10

basically going to instrument uh my kind

play02:13

of generation logic and then we're going

play02:15

to set up um a few different test as

play02:18

independent files in this test

play02:19

subdirectory uh that'll implement the

play02:22

various checks we want to do in this

play02:23

case to check that Imports and code are

play02:25

executed correctly so I'm going to hop

play02:27

over here to my VSS code so you can see

play02:31

it now and you can see here I'll move

play02:33

this over um that I'm in this

play02:37

introduction folder so this is in Langs

play02:39

withth cookbooks and I've set up an app

play02:42

so here's my app and main.py you can see

play02:44

right here now this is where the logic

play02:46

of my chain is going to be defined so in

play02:49

this particular case this generate code

play02:51

solution just takes in text and what's

play02:54

going to do is here's my generation

play02:57

prompt which basically says you're an

play02:59

expert in L expression language um

play03:02

here's a document related to lch

play03:04

expression language here and um produce

play03:07

an output that contains Imports a

play03:09

functioning code block and a preamble

play03:14

um which I will uh State somewhere in

play03:19

here um yeah description of the code

play03:22

solution the Imports and then a

play03:25

functioning code block so those are the

play03:26

three pieces now here I'm actually going

play03:29

to Define data model so this is a pantic

play03:32

object that contains prefix Imports and

play03:34

code and I'm going to bind that to my

play03:36

llm using with with structured output um

play03:39

method which is very convenient and this

play03:42

code gen chain should output a solution

play03:44

object that has a prefix import and code

play03:47

so that's kind of step

play03:49

one now this is just a module so this is

play03:53

what I'm calling my app and that's all

play03:54

it has now if I look at my tests here

play03:57

I've defined two tests so one is test

play04:00

code so this is going to take in or it's

play04:03

going to it has one um kind of piece of

play04:07

L transpression language documentation

play04:10

here um so this is like an example input

play04:13

to our

play04:14

function and this text execution simply

play04:16

will take that input right here it'll

play04:19

invoke our gen code solution which we

play04:21

defined over here so that's this guy

play04:23

right here there we go it will um

play04:30

yep it will invoke that it will produce

play04:32

a solution and now we're going to test

play04:34

we're going to try to execute the

play04:36

Imports there and uh basically if that

play04:40

fails we're going to we're going to flag

play04:41

the py test. fail and we're going to

play04:44

return the error so that's the setup

play04:45

here likewise with code execution same

play04:48

flow um in this case though we're just

play04:51

going to grab both the Imports and the

play04:54

code itself we'll try to execute all of

play04:56

it and again we'll return an error so

play04:58

these are our two tests

play05:00

now this looks like standard unit

play05:02

testing there's nothing that's you know

play05:04

Lang Smith specific here but I want to

play05:06

call your attention to one thing this

play05:08

decorator unit which we import from

play05:10

Langs smith. unit allows you then to log

play05:13

this unit test to Langs Smith um so we

play05:16

have that in both cases so there's just

play05:17

a simple decorator on top of this

play05:19

function that's going to perform our

play05:20

unit test with from lsmith we import

play05:23

unit so we do that in both cases and all

play05:27

we have to do then is just say run P

play05:28

test so we're in our Dory we're in this

play05:30

introduction directory right here so I

play05:32

can just show you so we have my app and

play05:35

my test here that's it so we can kick

play05:38

off uh we can run P test and this will

play05:41

kick off the unit test so this is just

play05:42

standard stuff there's nothing that's

play05:44

pretty that that's kind of very specific

play05:47

to lsmith here other than I've added

play05:48

this little decorator unit to my uh unit

play05:52

tests here that's all's going on so it's

play05:55

running both of them you can see this is

play05:56

kind of churning along so that's cool

play05:59

um and here's my other one so I have

play06:02

test Imports test code these are my two

play06:04

unit tests it looks like two PA so this

play06:06

is great this is just using p test and

play06:08

running unit test but there's one nice

play06:10

trick because I've run these with that

play06:12

Langs Smith decorator if I go over to my

play06:14

Langs Smith so if I go to data sets and

play06:17

testing so I can see now I have data

play06:20

sets for the directory name and then my

play06:23

unit test name test Imports test code if

play06:25

I go in here I can actually see that the

play06:28

results of the unit test are logged and

play06:31

what's pretty nice is that um you get

play06:34

all this metadata here which is pretty

play06:36

good

play06:38

um and yep I can see the names of the

play06:41

various runs I can see the result um so

play06:45

this is again pretty convenient

play06:47

basically it's allowing me to log the

play06:48

results of the unit test to Langs Smith

play06:51

and each test then is logged to an

play06:52

independent data set so I can keep them

play06:54

compartmentalized which again is also

play06:56

quite nice um let's actually click on

play06:58

one and kind of see

play07:00

uh actually let's go ahead and use

play07:01

comparison mode to look at a few of them

play07:04

so here we go this is pretty nice so I

play07:06

can look at things like latency I can

play07:07

look at the feedback score I can look at

play07:09

the tokens used again in each case I get

play07:11

logged input so this is the input that I

play07:14

passed to the unit test

play07:16

um

play07:19

and there we go so this is each one of

play07:22

them each each of the unit tests all

play07:26

pass uh and I can see latency and

play07:28

everything so anyway this is a pretty

play07:29

nice and easy way to use Langs Smith to

play07:31

log unit tests um and this is a very

play07:34

convenient thing to to run as part of

play07:35

your CI thanks

Rate This

5.0 / 5 (0 votes)

Related Tags
コード生成ユニットテストCIフローランス言語モデルデータセットテストロギングpandocPythonソフトウェア開発品質保証
Do you need a summary in English?