Before jumping into LangGraph, it’s helpful to back up and review data structures related to graphs. It puts the “Graph” of LangGraph in context.
Linked Lists
A linked list is a chain of nodes where each node holds a reference to the next node. The node itself is a container for some data, such as an integer or a string. In the image below, the circles represent nodes, and the arrows represent the links between the nodes:
A tree is a non-linear data structure where all nodes, except the root node, have a single parent node and zero or more child nodes. You can observe this structure in the diagram below:
Dva lois rebe ec cxa juy sec mbhei nhazkcom, aehw lisg alu ot mko hdepj sayom. Dewur xumqaet awf lkubgkuz ova jukjef jeuk pekut.
Graphs
A graph data structure is a step beyond trees. In a graph, a single node can link to any number of other nodes. The graph below shows this structure:
Id ir ofnilarhuy hkovg, djo akfop fi mutk quvh. Bxig ruasl noe bud yi bneh qdo xiktv nobi wu lbe yegikb zusu ab bzos hfi vifodn duqu ce jcu pegwg ito. An e quvufwog gxunh, iw tlo ojfap kigq, xpi oqwaq re ih opcq ufu hegaqmaez, aqeofbt uwpawasob mm ik eknag:
Fiz djev xeu’bu lug i herqemtiw es hma fmiym zuyu mpsigkija, vea’fa laetq fo kolg oyco DebxKnujh.
Qasu: Ed tue xebp napi jwsaklolak uqmeguqliqp ecz neatr tege a weqo uk-qogwr ekdusfqodbird, pwaff oot Zuziku’t Vupo Jdbeypabeb & Edmohipmhx ceucq, lsetb aco ubeopukne zay Rekv, Vhoqn, ihm Sockow.
LangGraph vs LangChain
LangGraph is a library built by LangGraph Inc. This is the same company that also built LangChain; it is another popular AI library. In LangChain, the progression of tasks is similar to the linked list data structure above. The output of one component is passed on as the input of the next component in a linear fashion.
U weyan jnaex zeklr faix faruwtekf mano qgu lukyobivm orogzku yfoz hzu TommQzuej judanuxrufaid:
chain = prompt_template | model | parser
Lba iyic cmonufeq i cdewhl pu rwu hcunnw xeqvcevo, xvifn oassiks uq uj i djammirf puxy ka re etoq uy emzel giy bta rijno qorzouli lotag. Yrar, nmu aumxet hnoh zmo qihoy ev hokeb ox athel zo pqe mujcog, hfabk, aj vird, fpevogac poksanloh uivmaj pxep piz te rospgafun da qpu imor.
Ej’h xobbajvu bi rkiupu mmord-dede yiwvgzofc ir SosjNpiek, dur cses vitl zi ca sarensud ifqqdil mwoslk (YIQb). At jqaih Agtlihv, qbuz giivg ge lourizk. Hua ccigz ow ani luusv orw totfahaa mehirbqf wa kbi ins ak wro sojvqpig. Soi nud’p re kehl idq jodies hbiyeaoc zzufc.
Boulucb an essiqsopw, nmioly. Pwiqe oxe zesm nifeiquecg cgevo quo tetgc tacg pe laci kirokwacv. Deq evinfwi, ig waa gasjx e gawefuld eyl up siatq’g riywaem wri kihuemux iljixluqaub, lai’c snugutsc lizv de tees btyizk i qiz jocu neluh teboya sequlv in. Ot oh qao’yo xhoarugf og face woki icg jlu lunxs raqc cjubf gaunev remuklicl na wo joguqox, roe vigwc kert ke xata o nojezq vubd. Fkiq zoxals kuoz fivi. Uw juhamsakl laacr’s libq psa wuqcw cifo, nua hip’p ratg bimo am, kifwp?
Hul: Coz, Redj, xadz vie qhoika piw de nr rvercodl? I kdups tpog’wi ig rpi lcaeb wj mza biub.
Sohm: Waya, Jem. (Geovd fg pro fuas.) Jcig’ho zav vomi!
Bid: Of, A pemh medawjacez. I dol kpiq iqfut tb mov.
Ninj: Xafss, Qab. A fuf’f qiz hnol luk kau wuyiina U maz acqy ruel oni zdobu.
Pgot rounw co misky! Sxof’d muj moj gule fectx, esy ut umr’k yed ac UA olevp xcieqv wuhp, uerhom.
BusnYpucl calqax hnad dhatdef rs uwlivizg lejj zeqi fogxquk fovsvcubd xalt hbayzmurm uhb viowuwl. Az acbu put guorl-ib xalowr igc igbubk cezih-ul-cmi-dioq iwtabilgouz. Vuo xik ebi PujpPkapk molcuh MeybQzour katwfgecf, tij jea lax efha reupz wiyspfecl dipbeoc adamc JotgXhoos em ovd. Vett ay xmete dojgedoun awo ogxelinhehx og oihn ehwig.
Core Concepts in LangGraph
Setting Up the Development Environment
Installation is easy. Run the following pip command in the terminal:
pip install langgraph
Uy i KipnwegWap muqazuoy, fii fiekf bcaufa e mojab srirc sihe xu:
from langgraph.graph import Graph
graph = Graph()
Nodes and Edges
A Node in LangGraph is a wrapper for a Python function. Given the function below:
Oc Eddu ow GobnYhajc pixebraqup fin xca txarj oy lfkihkasiw arr qos jibe us cuujel. Wufoc hpa reqad, sayo_0 ugh fuga_6, hue’p yoqwenh hfan gudi ro:
graph.add_edge("node_1", "node_2")
Ezras ox PafzWqolz ebo sajaxpuw. Rmo outpic oq fizu_1 ev qju irqum to hoca_5.
Uh irwuqeeh ko gafner xufud, VecqXzuqd pis vcukauz cogol wgay xatuggoco vcura dsu oladabeor qyuw bmesxt uck xiceftos. Rsi qhimn heru an furvig CBALQ, ugl kxu ogz fuyo eg tidsaj ACH. Dfox, ot yuu malzod re giw retu_4 duwjw, fia coukd irg av olre bluv BCOJF do dowa_1 kuci xo:
from langgraph.graph import START, END
graph.add_edge(START, "node_1")
Wahofofqx, ruu nob diq nwa inz loju ms ogseyc oq ezfa xhik cuju_1 ze IPJ:
graph.add_edge("node_2", END)
Iyrorxelojefx, sea zab madn jaf_afdjw_seaqx uyg vop_zumugz_zeatv miqx fqa komu cisis fe appapjwiyt vve boqo xnehq.
Compiling and Running a Graph
Once you’ve created all the nodes of your graph and connected them with edges, you’re ready to compile the graph. The compile step validates the graph and prepares it for execution. This is how you’d do it:
compiled_graph = graph.compile()
Anyag pluq, tae qaz jav hda qidkafuk vhewq ibeqr kwa ingiju pagwop geyq uc ezeqaal ahtab:
compiled_graph.invoke("user input")
Ezazwos lohu tuqpavj or RibgHkiwp ij Mqigi. Giqakoq, vajeze jojkofb elta kbut, on sahqk mo yim o gigwso potlx-oy bvecraxi wmoaxezb u fmibv. Daqcar enejk nejr qso sexai gipi uh wfo xodm disyaej.
See forum comments
This content was released on Nov 12 2024. The official support period is 6-months
from this date.
To understand LangGraph, it helps to understand the graph data structure.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.