Building a Stockbroker Agent in LangGraph.js

LangChain
5 Sept 202420:38

Summary

TLDRこのビデオでは、Lang Chainを使用してフルスタック株式ブローカーエージェントを実装する方法が説明されています。バックエンドはLangraバックエンドで、フロントエンドはNext.jsで書かれています。エージェントは、Financial APIsから詳細を取得し、株の購入に関するインフォームドな意思決定を支援します。人間ループ機能により、購入前に確認が必要な場合にダイナミックインタラプトが発生し、購入が承認されるまで実行されません。デプロイメントとフロントエンドの統合方法も学びます。

Takeaways

  • 😀 ビデオでは、Lang Chainを使用してフルスタックの株式ブローカーエージェントを実装する方法が説明されています。
  • 🛠️ 実装には、Lang ChainのバックエンドとNext.jsで書かれたフロントエンドが含まれています。
  • 🌐 バックエンドは、金融APIから企業や株の詳細をフェッチする機能を持っています。
  • 📈 利用されるAPIは、Vera's Financial APIが挙げられます。このAPIを使用して、株の価格や企業の収入報告書などのデータを取得します。
  • 💼 エージェントは株の購入詳細を準備し、購入を実行する前に人間ループ機能で購入確認を求めます。
  • 🔄 購入確認が行われる前に、ダイナミックな割り込みが発生し、購入は承認されるまで中断されます。
  • 💻 購入は実際にはモックであり、本物の株購入は実行されませんが、本番環境での動作と同様のプロセスが模倣されています。
  • 🔗 フロントエンドでは、ユーザーがメッセージを送信すると、バックエンドのLang Chainグラフにイベントが流れます。
  • 📊 購入の確認やキャンセルは、ユーザーインターフェースで行い、状態が更新されるとグラフは続行されます。
  • 🚀 ビデオでは、Lang Chain Studioを使用してグラフをデプロイし、クラウドに配置する方法も説明されています。

Q & A

  • ビデオではどのようなアプリケーションを実装していますか?

    -ビデオでは、フルスタック株式ブローカーエージェントを実装しています。これは、人間ループ機能を備えており、金融APIから企業の詳細や株価を取得することができます。

  • どのような技術スタックを使用していますか?

    -バックエンドはLangChainを使用し、フロントエンドはNext.jsで書かれています。また、LangChain Cloudにデプロイされます。

  • 人間ループ機能とは何ですか?

    -人間ループ機能とは、エージェントが株の購入を実行する前に、購入確認フィールドがtrueに設定されていない場合にダイナミックインタラプトを発生させ、購入を実行する前にユーザーによる確認を必要とする機能です。

  • 購入確認はどのように行われますか?

    -購入確認は、購入確認フィールドがtrueに設定されるまで購入は実行されません。ユーザーはこのフィールドをtrueに設定するか、購入をキャンセルする必要があります。

  • 株の購入は実際に実行されますか?

    -ビデオではモック購入が行われ、実際の購入は実行されません。しかし、本番環境で構築する場合は、購入APIコールを実行する機能と同様に動作します。

  • デプロイメントプロセスにはどのようなステップがありますか?

    -デプロイメントプロセスには、LangChain Studioに移動し、新しいデプロイメントを作成し、Gitリポジトリをリンクし、デプロイメントタイプと環境変数を指定し、デプロイメントを完了するというステップがあります。

  • フロントエンドで何が行われますか?

    -フロントエンドでは、ユーザーがメッセージを送信し、バックエンドのLangChainエージェントと対話することができます。また、購入の確認やキャンセルなどの操作を行うことができます。

  • ビデオのデモではどの企業の株価を取得していますか?

    -ビデオのデモでは、Googleの現在の株価を取得しています。

  • 購入を確認する方法として2要素認証は推奨されますか?

    -本番環境では、購入を確認する方法として2要素認証を導入することが推奨されますが、ビデオのデモではシンプルなブールフィールドを使用しています。

  • デプロイメントが完了した後、どのようにしてフロントエンドに接続しますか?

    -デプロイメントが完了した後、API URLをコピーし、それをフロントエンドの環境変数ファイルに設定して、Next.jsアプリケーションを立ち上げます。

Outlines

00:00

🚀 ビデオ概要とプロジェクトの紹介

このビデオでは、 Brace が Lang Chain と呼ばれるプラットフォームを使用してフルスタック株取引エージェントを実装する方法について説明しています。このエージェントには、人間ループ機能が備わっており、金融APIから公開企業の詳細や株の価格を取得することができます。また、モック購入の詳細を準備し、購入が確認されるまで中断されるというプロセスが説明されています。

05:01

🛠️ 各ノードの機能とコードの詳細

ビデオでは、各ノードの役割とコードの詳細について説明しています。特に、購入確認フィールドが真に設定されていない場合に発生するダイナミック中断、購入詳細の準備、購入実行ノードの動作について詳述されています。また、Lang Smith Studio でのグラフの動作やデプロイメントの方法も紹介されています。

10:02

🌐 Lang Smith でのデプロイメントと設定

デプロイメントプロセスが説明されています。これは、Lang Smith プラットフォーム上で新しいデプロイメントを作成し、Git リポジトリをリンクし、API 構成ファイルのパスを指定するプロセスです。また、環境変数の設定方法やデプロイメントの確認方法についても説明されています。

15:03

🖥️ フロントエンドのセットアップと動作

フロントエンドのセットアップと動作が説明されています。これには、環境変数の設定、Next.js アプリケーションの起動、ユーザーインターフェースでの質問の送信、購入の確認プロセスが含まれています。また、購入が完了した後のメッセージの表示方法や、実際の購入が行われる場合のセキュリティ対策についても触れられています。

20:04

🎉 プロジェクトの完成と期待

最後に、プロジェクトの完成と、ユーザーがどのようにこの技術を利用して独自のアプリケーションを構築できるかについて話されています。また、ビデオの視聴者に対して、今後のプロジェクトや構築物への期待を表しています。

Mindmap

Keywords

💡フルスタック

「フルスタック」とは、システムのすべてのレイヤーやコンポーネントを一か所で管理し、開発できるアーキテクチャを指します。ビデオでは、フルスタックのストックブローカーエージェントを実装するプロセスが説明されています。これは、バックエンドとフロントエンドの両方を含み、ユーザーが金融APIから株の詳細を取得し、取引判断を下すためのツールを提供します。

💡Lang Chain

「Lang Chain」とは、ビデオで使用されるバックエンド技術の一つであり、自然言語処理を通じて複雑なタスクを自動化するフレームワークです。ビデオでは、Lang Chainを使用して、株の価格取得や取引の準備など、金融データを扱うエージェントを構築しています。

💡Human Loop

「Human Loop」とは、人工知能システムの判断や処理に人間が関与するプロセスを指します。ビデオでは、株の購入前に「購入確認」フィールドを通じてHuman Loopが実装されています。これにより、ユーザーは自動化された取引前に最終的な確認を行うことができます。

💡Next.js

「Next.js」とは、Reactに基づくウェブアプリケーションフレームワークで、サーバーサイドレンダリングや静的サイトジェネレーションなど、高性能なウェブサイトの開発を支援します。ビデオでは、Next.jsを使用してフロントエンドを構築し、ユーザーインターフェースを提供しています。

💡API

「API」とは、アプリケーションプログラミングインターフェースの略で、異なるソフトウェア間でデータを交換するための方法やプロトコルを定義します。ビデオでは、金融APIを使用して公開企業の株価や財務レポートなどのデータを取得しています。

💡デプロイ

「デプロイ」とは、開発されたアプリケーションやサービスを運用環境にリリースするプロセスを指します。ビデオでは、Lang Chain Cloudにバックエンドをデプロイし、プロダクションレベルのサービスとして実行する手順が説明されています。

💡Docker

「Docker」とは、アプリケーションをコンテナ化し、異なる環境間で簡単に移植できるようにするプラットフォームです。ビデオでは、Dockerを使用して、バックエンドアプリケーションをパッケージ化し、クラウドにデプロイしています。

💡条件エッジ

「条件エッジ」とは、特定の条件に基づいて異なるノードにフローをルーティングする仕組みです。ビデオでは、購入確認フィールドに基づいて、取引の実行ノードに到達するかどうかを決定する条件エッジが使用されています。

💡トランザクション

「トランザクション」とは、データベースや金融システムにおいて、一連の操作が成功するか失敗するかをグループ化する単位です。ビデオでは、株の購入をモックとして扱い、トランザクションの成功や中断をシミュレートしています。

💡フロントエンド

「フロントエンド」とは、ユーザーが直接交わるアプリケーションのユーザーインターフェースや関連する機能を指します。ビデオでは、Next.jsを使用して構築されたフロントエンドは、ユーザーが金融データを取得し、取引を行うためのチャットインタフェースを提供しています。

Highlights

介绍如何实现全栈股票经纪代理,包括Langchain后端和Next.js前端。

代理具有人机循环功能,可以从金融API获取数据。

使用Vera的金融数据集API来获取不同上市公司和股票价格的数据。

代理可以获取股票当前价格、收入报告等数据,以便做出明智的购买决策。

在执行购买之前,如果购买确认字段未设置为true,则会触发动态中断。

演示了如何通过消息询问Google当前股价,并得到响应。

展示了如何准备购买股票的细节,并在没有用户确认的情况下中断购买流程。

用户可以通过设置购买确认字段为true来执行购买。

即使在新的对话线程中,未设置购买确认也会导致购买无法执行。

介绍了如何通过代码实现每个节点的功能。

展示了如何使用Langchain工具节点和工具调用。

详细解释了准备购买细节节点的逻辑和功能。

说明了执行购买节点如何工作,包括确认购买的逻辑。

介绍了如何配置Langchain JSON文件以部署到Langchain Cloud。

演示了如何在Langchain Cloud上部署图,并连接到前端。

展示了如何在Next.js前端中使用聊天界面与生产服务器交互。

提供了如何在生产环境中实现全栈Langchain和Next.js应用程序的示例。

视频结束时,鼓励观众构建自己的应用程序并探索Langchain的可能性。

Transcripts

play00:00

what's going on everyone it's brace from

play00:01

Lang chain and in this video we're going

play00:03

to be implementing a full stack stock

play00:05

broker human Loop agent including a

play00:07

langra backend um which we're then going

play00:09

to deploy to L graph cloud and

play00:11

production at the end of the video as

play00:13

well as a front end written in xjs so

play00:15

this graph we're going to be building is

play00:17

a stock broker agent which has some

play00:19

human Loop capabilities where our agent

play00:21

can fetch details from Financial apis if

play00:24

you're on Twitter you're probably

play00:25

familiar with verat we're going to be

play00:27

using his financial um data sets API to

play00:31

fetch data about different public

play00:32

companies and then also prices of stocks

play00:35

um so big thank you to him for building

play00:36

out this great financial data API uh so

play00:39

our agent has a series of tools that it

play00:41

can call around getting the current

play00:43

price of a stock getting some you know

play00:45

revenue reports or some uh different

play00:47

data that these public companies have to

play00:49

disclose so that our agent can make

play00:50

informed decisions about purchasing

play00:52

stocks um it can then also prepare

play00:54

purchase details if we ask it to

play00:56

purchase a stock and then the human Loop

play00:58

portion um is before it executes the

play01:01

purchase uh if the purchase confirmed

play01:03

field in our state is not set to true

play01:05

then it will throw a dynamic interrupt

play01:07

interrupt our our graph and we will have

play01:09

to set this Feld to true or else the um

play01:12

purchase will not be able to go through

play01:14

these purchases are just mock stock

play01:16

purchases they're not actually executing

play01:17

purchases um but it works pretty much

play01:19

the same way minus the execute purchase

play01:21

API call you'd make um if you were to

play01:23

build this for production so we can try

play01:25

out a quick message here saying what is

play01:28

Google's current price and on the right

play01:29

we see all the events stream in so we

play01:32

see our LM called the price snapshot

play01:34

tool the price snapshot tool return

play01:36

Google's current price and that got sent

play01:38

back to the LM where it generated a

play01:39

plain text response saying Google's

play01:41

current stock price is

play01:42

$157 we can add a new message saying

play01:45

let's buy four shares when we submit

play01:47

that it called the purchase stock tool

play01:50

that then got routed to the prepare

play01:52

purchase details node where it prepared

play01:53

all the purchase details um and before

play01:56

it could execute the execute purchase

play01:57

node the graph was interrupted and this

play02:00

is because purchase confirmed is not set

play02:02

to true if we inspect the current state

play02:04

we can see that we have our messages in

play02:06

our state our requested stock purchase

play02:08

details object which is what the execute

play02:10

purchase node needs in order to actually

play02:12

execute the purchase however purchase

play02:14

confirmed is not present so if we check

play02:17

this box here and then update it to

play02:19

execute this as the prepare purchase

play02:21

details node so the node that just ran

play02:23

and resubmit then now purchase confirm

play02:26

will be true and it should be able to

play02:27

execute the purchase and the LM should

play02:29

tell us that it executed the purchase so

play02:31

we hit submit we see purchase confirm

play02:33

true and then our llm called the execute

play02:37

purchase node where it return

play02:38

successfully purchase four shares of

play02:40

Google at $157 a share now if we were to

play02:43

create a new thread and say what's

play02:45

Google's current stock price submit it

play02:47

and then once this goes through we can

play02:49

say let's buy four shares or let's not

play02:52

set this to true and instead let's say

play02:54

let's buy four shares we hit

play02:56

submit it then obviously will not be

play02:58

able to execute the execute purchase

play03:00

node because purchase confirmed is not

play03:01

set to True um and then if we try and

play03:03

continue it since purchase confirmed is

play03:05

not set to true it will never be able to

play03:08

execute this node which is exactly what

play03:09

we want so we can see doesn't matter how

play03:11

many times we click continue it is never

play03:13

able to execute the purchase it also

play03:15

doesn't matter if we send in a new

play03:17

message saying execute the purchase uh

play03:19

we have to explicitly set this field to

play03:21

true we can send in a new message and as

play03:23

we can see it did not work even though

play03:25

it updated the state at the end to

play03:27

include that it is just unable to call

play03:29

the node because of our node interrupt

play03:31

uh which we throw if purchase confirmed

play03:33

is not true so now that we have a good

play03:36

highle idea how our graph Works let's go

play03:39

jump into the code and see exactly how

play03:41

each node works so to follow along

play03:43

you're going to want to clone the Lang

play03:45

graph JS examples repository I'll add a

play03:47

link to it in the description and then

play03:48

once you've cloned that navigate into

play03:50

the stock broker directory I already

play03:51

have it cloned so I can open it up and

play03:53

as you can see the stock broker

play03:54

directory is formatted in a monor repo

play03:57

like way where we have two projects back

play03:59

end and front end inside of our

play04:01

directory and then a root package Json

play04:03

and a turbo. Json file because we're

play04:05

using turbo repo to manage our monor

play04:06

repo so to install dependencies for a

play04:08

stock broker you can just run yarn

play04:10

install from the root of the stock

play04:11

broker directory and that will install

play04:13

all the dependencies you need for the

play04:14

back end and the front end and then once

play04:16

you've done that you can navigate into

play04:17

backend and index.ts where we have the

play04:20

code defined for our stock broker agent

play04:23

so as you can see at the top of our file

play04:24

the first thing we Define is our graph

play04:26

annotation this defines the state of our

play04:29

graph and the different fields which we

play04:30

can return and pass through to each of

play04:32

our nodes we are importing the messages

play04:34

annotation uh State object from Lang

play04:36

graph where we spread that and that

play04:38

essentially adds the messages field to

play04:40

our state um and this is of type base

play04:42

message or a list of Base messages um

play04:44

and then when you return a message into

play04:46

the state it gets concatenated uh to the

play04:49

current state with a little more logic

play04:50

around adding and removing messages the

play04:52

next field we have is the requested

play04:54

stock purchase details field and this is

play04:57

of a type ticker uh qu and Max purchase

play05:00

and this contains the fields which our

play05:02

prepare purchase details node generates

play05:05

uh so that when we want to actually

play05:06

execute the purchase we have exactly the

play05:08

fields that we need to execute that

play05:10

purchase finally we have purchase

play05:12

confirmed uh which is a Boolean and this

play05:14

is the field we use to determine whether

play05:16

or not a user has actually confirmed a

play05:18

purchase we're using gbd4 for this but

play05:21

you can use any LM you'd like and then

play05:24

we pass all of our tools to a tool node

play05:26

a tool node is a built-in class that is

play05:28

exported from lingraph where you can

play05:30

pass in a list of tools added as a node

play05:32

and then anytime that node is invoked

play05:34

and it's passed an AI message um from an

play05:37

LM that contains a tool call it will map

play05:40

the generated tool call to one of the

play05:42

tool calls you passed in and then invoke

play05:44

the function on that tool call if we

play05:46

inspect this list we see we have a web

play05:48

search tool purchase stock tool and then

play05:50

a series of other tools from the

play05:51

financial data sets API which allow us

play05:54

to get things like the current price

play05:56

some company facts um and so on we can

play05:58

inspect the per purchase stock tool for

play06:00

example and as we can see it's a simple

play06:02

L chain tool uh that for the function it

play06:05

just returns a string saying please

play06:06

confirm you would like you want to

play06:08

purchase uh this many shares of this

play06:10

ticker at the purchase price per share

play06:12

or the current price the schema contains

play06:16

ticker company name quantity and Max

play06:18

purchase price which are all optional

play06:20

and we'll get to why these are all

play06:22

optional when we inspect the prepare

play06:24

purchase details node um and then all

play06:26

the other tools are built pretty much

play06:28

the same way however the ones which call

play06:30

an API have this call financial data

play06:34

data sets API function inside them where

play06:36

they are actually hitting the financial

play06:38

data sets. a API so that's the um tools

play06:41

which we provide to the model and then

play06:42

the first node we have is call model

play06:45

this call model maps to the agent node

play06:47

if we scroll to the bottom we can see in

play06:48

our graph we've defined the agent node

play06:51

with the call model function um and then

play06:53

every time we start we will always map

play06:54

to the agent node and that's what we can

play06:56

see right here start always goes to the

play06:57

agent node so if we inspect this node we

play07:00

can see it takes in an argument of state

play07:02

which contains all the fields in our

play07:04

state we extract the messages and then

play07:06

we Define a simple system message are

play07:08

just saying things like you're a

play07:09

financial analyst you can do XYZ you

play07:11

have a series of tools um provided to

play07:13

you we then bind all of our tools to the

play07:16

LM so that our LM can actually invoke

play07:17

the tools that will need to be routed to

play07:19

the tool router prepare purchase details

play07:21

node and then we invoke the LM always

play07:23

passing the system message as the first

play07:25

message and then the rest of the message

play07:27

come after finally we return the result

play07:29

to the messages field and then our

play07:31

messages annotation State object will

play07:34

handle concatenating this new AI message

play07:37

to the rest of our messages in our state

play07:39

we can see we've defined a conditional

play07:40

edge here which always occurs after the

play07:43

agent node is invoked um and that's why

play07:45

our agent node has all of these dotted

play07:47

lines coming out of it or dotted arrows

play07:49

sorry these dotted arrows indicate a

play07:51

conditional Edge that this node can take

play07:52

so we can either go to the tools node

play07:54

prepare purchase details node and so on

play07:57

and we can see the different options

play07:58

that this condition Edge can take are

play08:00

the tools node it can end it can call

play08:02

the prepare purchase details node and

play08:04

finally it can call the execute purchase

play08:06

node so if we inspect the should

play08:08

continue function we see it also takes

play08:10

in a state argument the as as the first

play08:12

argument um and the type of it is the

play08:14

graph annotation we defined and then we

play08:16

extract the messages requested stock

play08:18

purchase details fielded from the state

play08:20

and we make sure that the last message

play08:23

is either an AI message or um if it's

play08:26

not an AI message we return and if it is

play08:28

an AI message but is not have tool calls

play08:30

we also return uh so we return this

play08:32

special end variable here and that will

play08:34

essentially map our agent to the end so

play08:36

if there were no tool calls called or if

play08:37

it was say a human message somehow made

play08:39

it to the last message in the list then

play08:42

just return because there's no more

play08:43

actions for the agent to take next if

play08:46

you have populated the request stock

play08:48

purchase details field then we want to

play08:49

map to the execute purchase node um

play08:52

that's because if this field is

play08:53

populated that means that there is a

play08:54

stock purchase which the user uh is

play08:56

trying to put through so we want to map

play08:58

it to this noes so that I can execute

play09:00

the purchase or it will be interrupted

play09:01

if you've not set um purchase confirm to

play09:04

true then we extract the tool calls from

play09:06

the message and throw an error if none

play09:08

exist and then finally we iterate over

play09:10

all the tool calls and we say if the

play09:11

tool name is purchase stock so if they

play09:14

called this purchase stock tool then we

play09:16

want to route to the prepare purchase

play09:17

details node and if it's not route to

play09:19

the tools node um which has access to

play09:21

all the different tools we defined and

play09:23

it can invoke the functions on those

play09:24

tools we want to route to a special node

play09:27

for the prepare purchase details um

play09:29

function because this contains some

play09:31

extra logic that doesn't live inside of

play09:33

our tool so we've seen the call model

play09:35

node we've seen the tool node and we've

play09:37

seen our conditional Edge for the agent

play09:40

next we can inspect the prepare purchase

play09:41

details node and then finally the excute

play09:43

purchase details node so for the prepare

play09:45

purchase details node which is this node

play09:47

we see right here once again it takes in

play09:49

the state it extracts the message and if

play09:51

the last message is not an AI message

play09:53

throw an error because that's unexpected

play09:55

next we look for the purchase stock tool

play09:58

call if it do not exist throw an error

play10:00

because once again this should always

play10:01

exist if this note is being called then

play10:04

we extract the max purchase price

play10:06

company name and ticker fields from that

play10:08

tool call so this tool call is going to

play10:10

use the context in the message history

play10:13

um and or if the user just said I want

play10:15

to buy you know this many shares of this

play10:18

company for this price um it'll extract

play10:21

those and then pass them to the tool

play10:22

where we're get given structured output

play10:24

like we see here then if the user does

play10:27

not define a ticker see we allow for for

play10:29

a company name and a ticker if they do

play10:31

not Define a ticker we then check to see

play10:33

if they Define a company name if they

play10:35

did not Define a company name then we

play10:36

want to return early um and as this

play10:38

comment States right here since the last

play10:40

call is a tool call we will always need

play10:42

to have a tool message follow that tool

play10:44

call and that is because the open AI API

play10:47

simply does not allow for messages which

play10:49

are not tool messages to follow AI

play10:51

messages which called tools so we just

play10:53

return a tool call or tool message

play10:55

saying please provide missing

play10:56

information um and then we provide an AI

play10:59

message saying please provide either the

play11:01

company ticker or the company name to

play11:03

purchase a stock since this is an AI

play11:04

message uh with a string content and no

play11:07

tool calls it'll just get routed to the

play11:08

end and that will you know tell the user

play11:10

you need to provide either the company

play11:11

ticker or the company name however if

play11:14

they did Define a company name then we

play11:16

can pass it to the find company name

play11:18

function essentially what this function

play11:20

does is it takes in the company name it

play11:22

calls the web search tool which is

play11:23

powered by T to search the web for the

play11:26

ticker for this company name and then we

play11:28

pass the search results of the TV search

play11:30

results tool to an llm with a simple

play11:33

structured output um tool bound to it

play11:36

saying extract the ticker from the

play11:38

search results and then we return the

play11:39

ticker next if the user did not Define a

play11:42

Max purchase price then we're going to

play11:43

invoke the price snapshot tool passing

play11:46

in the ticker uh because the per Max

play11:48

purchase price defaults to the current

play11:49

price if it's not specified and then we

play11:52

assign the max purchase price um

play11:54

variable to the result of the price

play11:56

snapshot tool call finally we return the

play11:59

requested stock purchase details object

play12:01

and that's what our exq purchase uh node

play12:04

needs in order to execute the purchase

play12:06

passing in the ticker the quantity uh

play12:08

defaulting to one and then the max

play12:10

purchase price next we see we have the

play12:12

execute purchase node and that's this

play12:14

one the first thing we do is we extract

play12:16

the purchase confirmed Boolean field and

play12:18

the requested stock purchase details

play12:20

field from the state if you have not

play12:22

defined your requested stock purchase

play12:24

details we throw an error this should

play12:25

never happen because this this node

play12:27

should only ever be called after the

play12:28

prepare purchase Det details um but if

play12:30

somehow it is called we want to throw an

play12:31

error because you cannot purchase a

play12:33

stock if you don't know what you're

play12:34

purchasing then we check for the

play12:36

purchase confirmed field this is

play12:38

important because here we're throwing a

play12:39

special type of error node interrupt

play12:42

because if you have not specified

play12:43

purchase confirmed true right if this is

play12:45

false then we need to interrupt the

play12:47

graph saying please confirm the purchase

play12:49

before executing uh in order to allow

play12:51

the user to set purchase confirmed to

play12:53

true or false if they don't want to

play12:55

confirm it this is so we can't just say

play12:57

the LM buy four shares and then it goes

play12:59

through and just buys these shares right

play13:00

away without letting us confirm so if

play13:02

purchase confirmed is not true we're

play13:03

going to interrupt the graph with this

play13:05

special error here which we can throw

play13:06

inside any node um and that will

play13:09

interrupt the graph like we saw in the

play13:10

studio allow us to update the state and

play13:12

then it can

play13:13

continue it's important to note that in

play13:15

the real world in production you're

play13:16

probably going to want to have some sort

play13:18

of two-factor authentication here

play13:19

instead of a simple Boolean field uh

play13:21

where the user you know adds some

play13:23

two-factor authen authentication code

play13:25

and then in your backend you validate

play13:27

that code to ensure that they actually

play13:28

are allowed to confirm purchases but in

play13:32

this case we just have a simple Boolean

play13:33

field because we're not actually

play13:34

purchasing stocks so if all this is

play13:36

defined then we execute the purchase um

play13:38

in our case it's just returning a new

play13:40

message um an AI message saying

play13:42

successfully purchased this many shares

play13:44

at this price and once again since it's

play13:47

a text message it will get routed back

play13:49

to the agent where the agent will say

play13:51

okay it's an AI message and there are no

play13:52

tool calls so route to the end and then

play13:54

that will finish the

play13:56

graph now let's inspect our L graph .

play13:59

Json file this is where we Define all

play14:02

the configuration for running our graph

play14:04

in the studio and also deploying it to

play14:06

the cloud which will do in just a second

play14:08

we can specify things like the node

play14:09

version we can customize our Docker file

play14:11

the dependencies in this case I just

play14:13

said to look inside this directory and

play14:15

install the dependencies inside this

play14:16

directory and then our graphs we only

play14:18

have one graph here stock broker uh but

play14:21

if you are building a larger application

play14:23

you can Define as many graphs you would

play14:24

like and then finally we Define our

play14:27

environment variable file and this tell

play14:29

the studio for local development where

play14:31

to find our secrets so it can actually

play14:33

invoke our not that require these

play14:35

specific API Keys okay so now that we've

play14:38

looked at exactly how our graph works we

play14:41

can go and deploy it to production and

play14:42

then hook it up to our front end and run

play14:44

it and see how it would work with a chat

play14:46

likee interface so to deploy your graph

play14:49

to production you're going to want to go

play14:50

to Lang Smith and then to the

play14:52

deployments tab and click new deployment

play14:55

here you can link your GI up repository

play14:58

if you've not already linked it

play14:59

um and once you've done that you can

play15:00

select a rebook from the drop down in my

play15:02

case it's going to be Lang graph JS

play15:04

examples and then you want to give it a

play15:06

name so we'll use the default name for

play15:08

now um and then after that you want to

play15:10

specify the path to the Lang graph API

play15:12

config file and this is the path or this

play15:14

is the file that we were just looking at

play15:16

a moment ago and in our case it's going

play15:18

to be nested inside of the uh stock

play15:20

broker SLB backend directory so when

play15:23

we've updated that path our Cloud

play15:24

deployment will actually be able to find

play15:26

the proper Lang graph config uh file and

play15:28

all the different configurations we've

play15:29

stated next you can specify a git Branch

play15:32

if you want to customize that in our

play15:33

case main is fine and then a deployment

play15:36

type so if you're going to production

play15:37

you're obviously going to want to select

play15:38

production here but for us uh just

play15:40

development is okay and then your

play15:42

environment variables uh this is where

play15:43

you would add your uh financial data

play15:45

sets API key your T API key and your

play15:48

open AI API key you do not need to add a

play15:51

lang Smith API key because lra Cloud

play15:54

will automatically create a new tracing

play15:56

project just for this deployment and

play15:58

then all the different lsmith runs and

play16:00

events that occur inside this production

play16:01

deployment will automatically get mapped

play16:03

to this uh project and the API keys for

play16:06

this project are in are injected right

play16:08

to your deployment so there's no need to

play16:10

specify any sort of L Smith API keys

play16:12

because we do all that for you when

play16:14

you're done you hit submit and then when

play16:16

your deployment is finished you can open

play16:18

it up like this open up the uh

play16:20

deployment page right here and you see a

play16:22

bunch of different things like your

play16:23

tracing project the repository which

play16:25

this was loaded in from this is the

play16:27

production API URL and it's how you'll

play16:29

actually hit your API um and you can see

play16:31

things like the different uh runs which

play16:34

were occurred or which were invoked

play16:36

using your production endpoint so we can

play16:38

inspect all of these um just like you

play16:40

would if you were say looking at your

play16:43

runs inside your projects from the

play16:44

project page you can also check out the

play16:46

different revisions um a revision is

play16:48

essentially when you make a change to

play16:49

your code you need to trigger a new

play16:51

revision by clicking new revision here

play16:53

um and that will redeploy your app using

play16:55

the new code you can also open it up in

play16:58

L graph Studio and this looks pretty

play16:59

much the same as the langra studio

play17:01

you're running locally however this is

play17:03

connected to your production endpoint um

play17:05

and you can interact with it the exact

play17:07

same way you would using the local

play17:09

version uh but of course this is going

play17:10

to hit the production end point so now

play17:13

this is done we can copy our API URL and

play17:16

then navigate back to our GitHub

play17:19

repository go into the front end

play17:21

directory and then you're going to want

play17:22

to copy this environment variable

play17:24

example file um and create a newm file

play17:27

using your actual secrets so Lang graph

play17:29

API key API URL that's going to be the

play17:31

API URL that was Genera your deployment

play17:34

Lan chain API key you do need this um in

play17:37

order to connect to that deployment and

play17:39

then the next public Lan graph graph ID

play17:42

this is going to be the name of the

play17:43

graph that you defined in your lr. Json

play17:45

so in our case it's going to be stock

play17:47

broker once you've defined all those

play17:49

fields you can run yarn Dev um inside of

play17:52

the front end directory and spin up your

play17:54

uh web server so we can do that right

play17:56

now see I I open up my terminal I want

play17:58

to navigate into the front end directory

play18:00

and then I can run yarn Dev it's going

play18:02

to spit up a new deployment or a new web

play18:05

server sorry at locost 3000 I can open

play18:08

that up and then we'll see our UI is

play18:09

loaded uh we pre-loaded in some

play18:11

questions like what are some facts about

play18:13

Google how much revenue did Apple make

play18:15

last year and so on um but we can just

play18:17

submit a question like how much revenue

play18:19

did Apple make last year this is going

play18:21

to call some tools so we see it called

play18:23

the income statements tool passing in

play18:25

the arguments of Apple's ticker um the

play18:27

period of time in which to fetch the

play18:29

income statement and then the limit and

play18:30

then finally it generated a response

play18:32

saying Apple made $383 billion Revenue

play18:35

last year we can also click show result

play18:37

here and this shows us the result of the

play18:40

tool in this case it's the result from

play18:41

the financial data sets API that was uh

play18:44

invoked by our tool and this is how our

play18:46

LM was able to generate that context now

play18:48

if we want to say um purchase some Apple

play18:51

stock we're going to ask it what the

play18:53

current price is and then say purchase

play18:55

and um since our graph will be

play18:56

interrupted we'll see a similar UI to

play18:58

what we saw saw in langra studio

play18:59

allowing us to say confirm or deny so

play19:02

for purchasing I've created a new

play19:04

session here and we can send a message

play19:05

like let's buy four shares of Apple for

play19:07

the current price we'll hit submit and

play19:09

it should call some tools see it call

play19:11

the price snapshot tool um which got the

play19:13

current price of Apple passing in

play19:15

Apple's ticker and then it called the

play19:17

purchase stock Tool uh passing in

play19:19

Apple's ticker the company name the

play19:21

quantity um for and then the max

play19:23

purchase price uh since our graph was

play19:25

interrupted we get this message here

play19:26

saying graph interrupted the next node

play19:28

is the execute purchase node and then

play19:30

also our input is disabled while our

play19:32

graph is interrupted so if we want to

play19:34

confirm we can say confirm purchase and

play19:36

then update State this is going to make

play19:38

an update State API call to our Lang

play19:41

graph production deployment updating the

play19:43

states so that the purchase confirm

play19:45

field is true for this uh thread and

play19:48

then once this is done we going to

play19:50

continue and this is also similar to the

play19:51

ux you saw in the land graph Studio

play19:53

where you would continue and it

play19:54

continues from where it left off in the

play19:56

graph so we see state was updated and

play19:58

when once we continue it should purchase

play20:00

the stock for us so we say continue and

play20:02

then successfully purchase four shares

play20:03

of Apple at $220 a share so this worked

play20:07

exactly as we intended um and obviously

play20:09

in production you're not going to want

play20:10

to have a simple checkbox the user can

play20:12

check it might want to be like I said

play20:13

earlier some sort of two-factor

play20:14

authentication this is how you'd

play20:16

Implement a full stack L graph and

play20:18

nextjs app uh deploy it to L graph cloud

play20:21

in production and then hit your

play20:23

production API uh using a nextjs front

play20:25

end like this we can interact with our

play20:27

production server you using a chat

play20:29

interface um I hope you guys enjoyed

play20:31

this video and know some more about

play20:33

human Loop applications and Lane graph

play20:35

and I'm excited to see what everyone's

play20:36

builds

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
フルスタック株式ブローカーエージェントデプロイプロダクションフロントエンドバックエンドAPI統合人間ループLangchain
هل تحتاج إلى تلخيص باللغة الإنجليزية؟