YouTube2Text

Learn RAG From Scratch – Python AI Tutorial from a LangChain Engineer — Transcript

by freeCodeCamp.org · 27,073 words · 3,910 segments · language en · Watch on YouTube

Full transcript

  1. 0:00in this course Lance Martin will teach
  2. 0:02you how to implement rag from scratch
  3. 0:06Lance is a software engineer at Lang
  4. 0:08chain and Lang chain is one of the most
  5. 0:10common ways to implement rag Lance will
  6. 0:14help you understand how to use rag to
  7. 0:16combine custom data with llms hi this is
  8. 0:19Lance Martin I'm a software engineer at
  9. 0:21Lang chain I'm going to be giving a
  10. 0:23short course focused on rag or retrieval
  11. 0:26augmented generation which is one of the
  12. 0:28most popular kind of ideas and
  13. 0:30in llms
  14. 0:32today so really the motivation for this
  15. 0:34is that most of the world's data is
  16. 0:37private um whereas llms are trained on
  17. 0:41publicly available data so you can kind
  18. 0:43of see on the bottom on the x-axis the
  19. 0:45number of tokens using pre-training
  20. 0:47various llms so it kind of varies from
  21. 0:50say 1.5 trillion tokens in the case of
  22. 0:52smaller models like
  23. 0:5452 out to some very large number that we
  24. 0:56actually don't know for proprietary
  25. 0:58models like GPT 4 CLA
  26. 1:00three but what's really interesting is
  27. 1:03that the context window or the ability
  28. 1:06to feed external information into these
  29. 1:08LMS is actually getting larger so about
  30. 1:11a year ago context windows were between
  31. 1:134 and 8,000 tokens you know that's like
  32. 1:16maybe a dozen pages of text we've
  33. 1:18recently seen models all the way out to
  34. 1:20a million tokens which is thousands of
  35. 1:22pages of text so while these llms are
  36. 1:24trained on large scale public data it's
  37. 1:28increasingly feasible to feed them
  38. 1:30this huge mass of private data that
  39. 1:33they've never seen that private data can
  40. 1:35be your kind of personal data it can be
  41. 1:37corporate data or you know other
  42. 1:40information that you want to pass to an
  43. 1:42LM that's not natively in his training
  44. 1:45set and so this is kind of the main
  45. 1:48motivation for rag it's really the idea
  46. 1:52that llms one are kind of the the center
  47. 1:54of a new kind of operating
  48. 1:56system and two it's increasingly
  49. 1:59critical to be able to feed information
  50. 2:01from external sources such as private
  51. 2:04data into llms for processing so that's
  52. 2:07kind of the overarching motivation for
  53. 2:10Rag and now rag refers to retrieval
  54. 2:13augmented generation and you can think
  55. 2:14of it in three very general steps
  56. 2:17there's a process of indexing of
  57. 2:19external data so you can think about
  58. 2:22this as you know building a database for
  59. 2:24example um many companies already have
  60. 2:27large scale databases in different forms
  61. 2:29they could be SQL DBS relational DBS um
  62. 2:32they could be Vector Stores um or
  63. 2:35otherwise but the point is that
  64. 2:38documents are indexed such that they can
  65. 2:40be retrieved based upon some heuristics
  66. 2:43relative to an input like a question and
  67. 2:45those relevant documents can be passed
  68. 2:47to an llm and the llm can produce
  69. 2:50answers that are grounded in that
  70. 2:52retrieved information so that's kind of
  71. 2:54the centerpiece or central idea behind
  72. 2:56Rag and why it's really powerful
  73. 2:58technology because it's really uniting
  74. 3:01the the knowledge and processing
  75. 3:03capacity of llms with large scale
  76. 3:06private external data source for which
  77. 3:09most of the important data in the world
  78. 3:11still
  79. 3:13lives and in the following short videos
  80. 3:16we're going to kind of build up a
  81. 3:17complete understanding of the rag
  82. 3:20landscape and we're going to be covering
  83. 3:22a bunch of interesting papers and
  84. 3:24techniques that explain kind of how to
  85. 3:25do rag and I've really broken it down
  86. 3:28into a few different sections
  87. 3:30so starting with a question on the left
  88. 3:32the first kind of section is what I call
  89. 3:35query trans translation so this captures
  90. 3:37a bunch of different methods to take a
  91. 3:39question from a user and modify it in
  92. 3:42some way to make it better suited for
  93. 3:44retrieval from you know one of these
  94. 3:45indexes we've talked
  95. 3:47about that can use methods like query
  96. 3:50writing it can be decomposing the query
  97. 3:52into you know constituent sub
  98. 3:55questions then there's a question of
  99. 3:57routing so taking that decomposed a
  100. 4:00Rewritten question and routing it to the
  101. 4:02right place you might have multiple
  102. 4:03Vector stores a relational DB graph DB
  103. 4:06and a vector store so it's the challenge
  104. 4:08of getting a question to the right
  105. 4:10Source then there's a there's kind of
  106. 4:12the challenge of query construction
  107. 4:15which is basically taking natural
  108. 4:16language and converting it into the DSL
  109. 4:19necessary for whatever data source you
  110. 4:21want to work with a classic example here
  111. 4:23is text a SQL which is kind of a very
  112. 4:25kind of well studied process but text a
  113. 4:28cipher for graph DV is very interesting
  114. 4:31text to metadata filters for Vector DBS
  115. 4:34is also a very big area of
  116. 4:36study um then there's indexing so that's
  117. 4:40the process of taking your documents and
  118. 4:42processing them in some way so they can
  119. 4:44be easily retrieved and there's a bunch
  120. 4:46of techniques for that we'll talk
  121. 4:47through we'll talk through different
  122. 4:48embedding methods we'll talk about
  123. 4:50different indexing
  124. 4:51strategies after
  125. 4:53retrieval there are different techniques
  126. 4:56to rerank or filter retrieve documents
  127. 4:59um and then finally we'll talk about
  128. 5:00generation and kind of an interesting
  129. 5:02new set of methods to do what we might
  130. 5:05call as active rag so in that retrieval
  131. 5:08or generation stage grade documents
  132. 5:12grade answers um grade for relevance to
  133. 5:15the question grade for faithfulness to
  134. 5:18the documents I.E check for
  135. 5:19hallucinations and if either fail
  136. 5:22feedback uh re- retrieve or rewrite the
  137. 5:25question uh regenerate the qu regenerate
  138. 5:28the answer and so forth so there's a
  139. 5:30really interesting set of methods we're
  140. 5:31going to talk through that cover that
  141. 5:33like retrieval and generation with
  142. 5:37feedback and you know in terms of
  143. 5:39General outline we'll cover the basics
  144. 5:41first it'll go through indexing
  145. 5:43retrieval and generation kind of in the
  146. 5:44Bare Bones and then we'll talk through
  147. 5:47more advanced techniques that we just
  148. 5:48saw on the prior slide career
  149. 5:50Transformations routing uh construction
  150. 5:52and so forth hi this is Lance from Lang
  151. 5:55chain this the second video in our
  152. 5:57series rack from scratch focused on
  153. 5:59indexing
  154. 6:01so in the past video you saw the main
  155. 6:04kind of overall components of rag
  156. 6:06pipelines indexing retrieval and
  157. 6:09generation and here we're going to kind
  158. 6:10of Deep dive on indexing and give like
  159. 6:13just a quick overview of it so the first
  160. 6:16aspect of indexing is we have some
  161. 6:18external documents that we actually want
  162. 6:20to load and put into what we're trying
  163. 6:22to call Retriever and the goal of this
  164. 6:25retriever is simply given an input
  165. 6:27question I want to fish out doents that
  166. 6:30are related to my question in some
  167. 6:32way now the way to establish that
  168. 6:35relationship or relevance or similarity
  169. 6:37is typically done using some kind of
  170. 6:39numerical representation of documents
  171. 6:42and the reason is that it's very easy to
  172. 6:44compare vectors for example of numbers
  173. 6:47uh relative to you know just free form
  174. 6:50text and so a lot of approaches have
  175. 6:53been a developed over the years to take
  176. 6:56text documents and compress them down
  177. 6:58into a numerical rep presentation that
  178. 7:01then can be very easily
  179. 7:03searched now there's a few ways to do
  180. 7:06that so Google and others came up with
  181. 7:08many interesting statistical methods
  182. 7:11where you take a document you look at
  183. 7:13the frequency of words and you build
  184. 7:15what they call sparse vectors such that
  185. 7:18the vector locations are you know a
  186. 7:20large vocabulary of possible words each
  187. 7:23value represents the number of
  188. 7:24occurrences of that particular word and
  189. 7:27it's sparse because there's of course
  190. 7:28many zeros it's a very large vocabulary
  191. 7:31relative to what's present in the
  192. 7:32document and there's very good search
  193. 7:34methods over this this type of numerical
  194. 7:37representation now a bit more recently
  195. 7:40uh embedding methods that are machine
  196. 7:41learned so you take a document and you
  197. 7:43build a compressed fixed length
  198. 7:45representation of that
  199. 7:47document um have been developed with
  200. 7:51correspondingly very strong search
  201. 7:53methods over
  202. 7:54embeddings um so the intuition here is
  203. 7:59that we take documents and we typically
  204. 8:01split them because embedding models
  205. 8:04actually have limited context windows so
  206. 8:07you know on the order of maybe 512
  207. 8:09tokens up to 8,000 tokens or Beyond but
  208. 8:12they're not infinitely large so
  209. 8:14documents are split and each document is
  210. 8:16compressed into a vector and that Vector
  211. 8:20captures a semantic meaning of the
  212. 8:22document itself the vectors are indexed
  213. 8:25questions can be embedded in the exactly
  214. 8:27same way and then numerical kind of
  215. 8:30comparison in some form you know using
  216. 8:33very different types of methods can be
  217. 8:34performed on these vectors to fish out
  218. 8:37relevant documents relative to my
  219. 8:41question um and let's just do a quick
  220. 8:43code walk through on some of these
  221. 8:46points so I have my notebook here I've
  222. 8:51installed here um now I've set a few API
  223. 8:55keys for lsmith which are very useful
  224. 8:58for tracing which we'll see
  225. 9:00shortly um previously I walked through
  226. 9:03this this kind of quick start that just
  227. 9:04showed overall how to lay out these rag
  228. 9:07pipelines and here what I'll do is I'll
  229. 9:09Deep dive a little bit more on indexing
  230. 9:11and I'm going to take a question and a
  231. 9:13document and first I'm just going to
  232. 9:16compute the number of tokens in for
  233. 9:18example the question and this is
  234. 9:19interesting because embedding models in
  235. 9:22llms more generally operate on tokens
  236. 9:24and so it's kind of nice to understand
  237. 9:27how large the documents are that I'm
  238. 9:28trying to feed in in this case it's
  239. 9:30obviously a very small in this case
  240. 9:33question now I'm going to specify open
  241. 9:35eye embeddings I specify an embedding
  242. 9:38model here and I just say embed embed
  243. 9:40query I can pass my question my document
  244. 9:43and what you can see here is that runs
  245. 9:47and this is mapped to now a vector of
  246. 9:49length 1536 and that fixed length Vector
  247. 9:53representation will be computed for both
  248. 9:56documents and really for any document so
  249. 9:58you're always is kind of computing this
  250. 10:00fix length Vector that encodes the
  251. 10:02semantics of the text that you've passed
  252. 10:05now I can do things like cosine
  253. 10:06similarity to compare
  254. 10:08them and as we'll see here I can load
  255. 10:13some documents this is just like we saw
  256. 10:16previously I can split
  257. 10:18them and I can index them here just like
  258. 10:22we did before but we can see under the
  259. 10:23hood really what we're doing is we're
  260. 10:25taking each split we're embedding it
  261. 10:27using open eye embeddings into this this
  262. 10:29kind of this Vector representation and
  263. 10:31that's stored with a link to the rod
  264. 10:33document itself in our Vector store and
  265. 10:36next we'll see how to actually do
  266. 10:38retrieval using this Vector store hi
  267. 10:41this is Lance from Lang chain and this
  268. 10:43is the third video in our series rag
  269. 10:45from scratch building up a lot of the
  270. 10:47motivations for rag uh from the very
  271. 10:50basic
  272. 10:51components um so we're going to be
  273. 10:54talking about retrieval today in the
  274. 10:56last two uh short videos I outlined
  275. 10:59indexing and gave kind of an overview of
  276. 11:01this flow which starts with indexing of
  277. 11:04our documents retrieval of documents
  278. 11:06relevant to our question and then
  279. 11:07generation of answers based on the
  280. 11:09retriev
  281. 11:11documents and so we saw that the
  282. 11:13indexing process basically makes
  283. 11:15documents easy to retrieve and it goes
  284. 11:18through a flow that basically looks like
  285. 11:20you take our documents you split them in
  286. 11:22some way into these smaller chunks that
  287. 11:25can be easily embedded um those
  288. 11:28embeddings are then numerical
  289. 11:30representations of those documents that
  290. 11:31are easily
  291. 11:32searchable and they're stored in an
  292. 11:35index when given a question that's also
  293. 11:38embedded the index performs a similarity
  294. 11:41search and returns splits that are
  295. 11:43relevant to the
  296. 11:45question now if we dig a little bit more
  297. 11:47under the hood we can think about it
  298. 11:49like this if we take a document and
  299. 11:52embed it let's imagine that embedding
  300. 11:54just had three dimensions so you know
  301. 11:56each document is projected into some
  302. 11:58point in this 3D
  303. 12:00space now the point is that the location
  304. 12:03in space is determined by the semantic
  305. 12:06meaning or content in that document so
  306. 12:09to follow that then documents in similar
  307. 12:12locations in space contain similar
  308. 12:15semantic information and this very
  309. 12:17simple idea is really the Cornerstone
  310. 12:19for a lot of search and retrieval
  311. 12:21methods that you'll see with modern
  312. 12:22Vector stores so in particular we take
  313. 12:25our documents we embed them into this in
  314. 12:27this case a toy 3D space
  315. 12:30we take our question do the
  316. 12:32same we can then do a search like a
  317. 12:35local neighborhood search you can think
  318. 12:37about in this 3D space around our
  319. 12:39question to say hey what documents are
  320. 12:42nearby and these nearby neighbors are
  321. 12:45then retrieved because they can they
  322. 12:47have similar semantics relative to our
  323. 12:50question and that's really what's going
  324. 12:53on here so again we took our documents
  325. 12:56we split them we embed them and now they
  326. 12:58exist in this high dimensional space
  327. 13:00we've taken our question embedded it
  328. 13:02projected in that same space and we just
  329. 13:04do a search around the question from
  330. 13:07nearby documents and grab ones that are
  331. 13:09close and we can pick some number we can
  332. 13:12say we want one or two or three or n
  333. 13:14documents close to my question in this
  334. 13:17embedding space and there's a lot of
  335. 13:19really interesting methods that
  336. 13:20implement this very effectively I I link
  337. 13:22one
  338. 13:24here um and we have a lot of really nice
  339. 13:28uh Integrations to play with this
  340. 13:30general idea so many different embedding
  341. 13:32models many different indexes lots of
  342. 13:35document loaders um and lots of
  343. 13:37Splitters that can be kind of recombined
  344. 13:39to test different ways of doing this
  345. 13:41kind of indexing or
  346. 13:43retrieval um so now I'll show a bit of a
  347. 13:45code walkth through so here we defined
  348. 13:50um we kind of had walked through this
  349. 13:52previously this is our notebook we've
  350. 13:54installed a few packages we've set a few
  351. 13:57environment variables using lsmith
  352. 14:00and we showed this previously this is
  353. 14:02just an overview showing how to run rag
  354. 14:04like kind of end to end in the last uh
  355. 14:07short talk we went through
  356. 14:09indexing um and what I'm going to do
  357. 14:11very simply is I'm just going to reload
  358. 14:14our
  359. 14:15documents so now I have our documents
  360. 14:19I'm going to resplit
  361. 14:21them and we saw before how we can build
  362. 14:23our
  363. 14:24index now here let's actually do the
  364. 14:27same thing but in the slide we actually
  365. 14:29showed kind of that notion of search in
  366. 14:31that 3D
  367. 14:33space and a nice parameter to think
  368. 14:35about in building your your retriever is
  369. 14:38K so K tells you the number of nearby
  370. 14:41neighbors to fetch when you do that
  371. 14:42retrieval process and we talked about
  372. 14:44you know in that 3D space do I want one
  373. 14:47nearby neighbor or two or three so here
  374. 14:49we can specify k equals 1 for example
  375. 14:53now we're building our index so we're
  376. 14:54taking every split embedding it storing
  377. 14:57it now what's nice is I asked a a
  378. 14:59question what is Task decomposition this
  379. 15:01is related to the blog post and I'm
  380. 15:03going to run get relevant documents so I
  381. 15:05run that and now how many documents do I
  382. 15:08get back I get one as expected based
  383. 15:10upon k equals 1 so this retrieve
  384. 15:12document should be related to my
  385. 15:14question now I can go to lsmith and we
  386. 15:17can open it up and we can look at our
  387. 15:19Retriever and we can see here was our
  388. 15:21question here's the one document we got
  389. 15:23back and okay so that makes sense this
  390. 15:27document pertains to task ke
  391. 15:29decomposition in particular and it kind
  392. 15:31of lays out a number of different
  393. 15:33approaches that can be used to do that
  394. 15:35this all kind of makes sense and this
  395. 15:37shows kind of in practice how you can
  396. 15:38implement this this NE this kind of KNN
  397. 15:42or k nearest neighbor search uh really
  398. 15:45easily uh just using a few lines of code
  399. 15:48and next we're going to talk about
  400. 15:50generation
  401. 15:52thanks hey this is Lance from Lang chain
  402. 15:55this is the fourth uh short video in our
  403. 15:57rack from scratch series
  404. 15:59that's going to be focused on
  405. 16:01generation now in the past few videos we
  406. 16:04walked through the general flow uh for
  407. 16:07kind of basic rag starting with indexing
  408. 16:10Fall by
  409. 16:11retrieval then
  410. 16:13generation of an answer based upon the
  411. 16:15documents that we retrieved that are
  412. 16:17relevant to our question this is kind of
  413. 16:19the the very basic
  414. 16:21flow now an important consideration in
  415. 16:25generation is really what's happening is
  416. 16:28we're taking the documents you retrieve
  417. 16:30and we're stuffing them into the llm
  418. 16:32context window so if we kind of walk
  419. 16:34back through the process we take
  420. 16:37documents we split them for convenience
  421. 16:39or embedding we then embed each split
  422. 16:43and we store that in a vector store as
  423. 16:45this kind of easily searchable numerical
  424. 16:47representation or vector and we take a
  425. 16:50question embed it to produce a similar
  426. 16:52kind of numerical representation we can
  427. 16:55then search for example using something
  428. 16:57like KN andn in this kind of dimensional
  429. 17:00space for documents that are similar to
  430. 17:02our question based on their proximity or
  431. 17:05location in this space in this case you
  432. 17:07can see 3D is a toy kind of toy
  433. 17:10example now we've recovered relevant
  434. 17:12splits to our question we pack those
  435. 17:15into the context window and we produce
  436. 17:17our
  437. 17:19answer now this introduces the notion of
  438. 17:22a prompt so the prompt is kind of a you
  439. 17:25can think have a placeholder that has
  440. 17:27for example you know in our case B keys
  441. 17:30so those keys can be like context and
  442. 17:33question so they basically are like
  443. 17:35buckets that we're going to take those
  444. 17:37retrieve documents and Slot them in
  445. 17:40we're going to take our question and
  446. 17:41also slot it in and if you kind of walk
  447. 17:44through this flow you can kind of see
  448. 17:45that we can build like a dictionary from
  449. 17:48our retrieve documents and from our
  450. 17:50question and then we can basically
  451. 17:52populate our prompt template with the
  452. 17:54values from the dict and then becomes a
  453. 17:57prompt value which can be passed to llm
  454. 17:59like a chat model resulting in chat
  455. 18:01messages which we then parse into a
  456. 18:03string and get our answer so that's like
  457. 18:06the basic workflow that we're going to
  458. 18:07see and let's just walk through that in
  459. 18:09code very quickly to kind of give you
  460. 18:11like a Hands-On intuition so we had our
  461. 18:14notebook we walk through previously
  462. 18:16install a few packages I'm setting a few
  463. 18:19lsmith environment variables we'll see
  464. 18:21it's it's nice for uh kind of observing
  465. 18:23and debugging our
  466. 18:25traces um previously we did this quick
  467. 18:27start we're going to skip that over
  468. 18:30um and what I will do is I'm going to
  469. 18:33build our retriever so again I'm going
  470. 18:36to take documents and load them uh and
  471. 18:38then I'm going to split them here we've
  472. 18:40kind of done this previously so I'll go
  473. 18:42through this kind of quickly and then
  474. 18:43we're going to embed them and store them
  475. 18:45in our index so now we have this
  476. 18:47retriever object here now I'm going to
  477. 18:50jump down here now here's where it's
  478. 18:51kind of fun this is the generation bit
  479. 18:54and you can see here I'm defining
  480. 18:55something new this is a prompt template
  481. 18:58and what my prompt template is something
  482. 18:59really simple it's just going to say
  483. 19:00answer the following question based on
  484. 19:02this context it's going to have this
  485. 19:04context variable and a question so now
  486. 19:06I'm building my prompt so great now I
  487. 19:08have this prompt let's define an llm
  488. 19:11I'll choose
  489. 19:1235 now this introdu the notion of a
  490. 19:15chain so in Lang chain we have an
  491. 19:17expression language called L Cel Lang
  492. 19:20chain expression language which lets you
  493. 19:22really easily compose things like
  494. 19:24prompts LMS parsers retrievers and other
  495. 19:27things but the very simple kind of you
  496. 19:30know example here is just let's just
  497. 19:32take our prompt which you defined right
  498. 19:33here and connect it to an LM which you
  499. 19:35defined right here into this chain so
  500. 19:37there's our chain now all we're doing is
  501. 19:39we're invoking that chain so every L
  502. 19:42expression language chain has a few
  503. 19:44common methods like invoke bat stream in
  504. 19:47this case we just invoke it with a dict
  505. 19:50so context and question that maps to the
  506. 19:54expected Keys here in our template
  507. 19:58and so if we run invoke what we see is
  508. 20:01it's just going to execute that chain
  509. 20:02and we get our answer now if we zoom
  510. 20:05over to Langs Smith we should see that
  511. 20:07it's been populated so yeah we see a
  512. 20:09very simple runable
  513. 20:11sequence here was our
  514. 20:14document um and here's our output and
  515. 20:18here is our prompt answer the following
  516. 20:21question based on the context here's the
  517. 20:24document we passed in here is the
  518. 20:26question and then we get our answer so
  519. 20:28that's pretty nice um now there's a lot
  520. 20:32of other options for rag prompts I'll
  521. 20:34pull one in from our prompt tub this
  522. 20:36one's like kind of a popular prompt so
  523. 20:39it just like has a little bit more
  524. 20:41detail but you know it's the main the
  525. 20:43main intuition is the same um you're
  526. 20:47passing in documents you're asking them
  527. 20:48to reason about the documents given a
  528. 20:50question produce an answer and now here
  529. 20:53I'm going to find a rag chain which will
  530. 20:55automatically do the retrieval for us
  531. 20:57and all I have to do is specify here's
  532. 20:59my retriever which we defined
  533. 21:01before here's our question we which we
  534. 21:03invoke with the question gets passed
  535. 21:06through to the key question in our dict
  536. 21:10and it automatically will trigger the
  537. 21:12retriever which will return documents
  538. 21:14which get passed into our context so
  539. 21:16it's exactly what we did up here except
  540. 21:18before we did this
  541. 21:20manually and
  542. 21:22now um this is all kind of automated for
  543. 21:25us we pass that dick which is autop
  544. 21:28populated
  545. 21:29into our prompt llm out to parser now
  546. 21:31let invoke it and that should all just
  547. 21:34run and great we get an answer and we
  548. 21:37can look at the
  549. 21:39trace and we can see everything that
  550. 21:41happened so we can see our retriever was
  551. 21:44run these documents were
  552. 21:46retrieved they get passed into our
  553. 21:49LM and we get our final answer so this
  554. 21:53kind of the end of our overview um where
  555. 21:56we talked about I'll go back to the
  556. 21:58slide here quickly we talked about
  557. 22:00indexing retrieval and now
  558. 22:02generation and follow-up short videos
  559. 22:04we'll kind of dig into some of the more
  560. 22:06com complex or detailed themes that
  561. 22:09address some limitations that can arise
  562. 22:11in this very simple pipeline
  563. 22:14thanks hi my from Lang chain over the
  564. 22:17next few videos we're going to be
  565. 22:18talking about career
  566. 22:20translation um and in this first video
  567. 22:23we're going to cover the topic of
  568. 22:24multi-query
  569. 22:26so query translation sits kind of at the
  570. 22:29first stage of an advanced rag Pipeline
  571. 22:34and the goal of career translation is
  572. 22:36really to take an input user question
  573. 22:39and to translate in some way in order to
  574. 22:42improve
  575. 22:44retrieval so the problem statement is
  576. 22:47pretty intuitive user queries um can be
  577. 22:51ambiguous and if the query is poorly
  578. 22:53written because we're typically doing
  579. 22:56some kind of semantic similarity search
  580. 22:57between the query and our documents if
  581. 23:01the query is poorly written or ill
  582. 23:02opposed we won't retrieve the proper
  583. 23:05documents from our
  584. 23:07index so there's a few approaches to
  585. 23:10attack this problem and you can kind of
  586. 23:13group them in a few different ways so
  587. 23:15here's one way I like to think about it
  588. 23:17a few approaches has involveed query
  589. 23:19rewriting so taking a query and
  590. 23:22reframing it like writing from a
  591. 23:23different perspective um and that's what
  592. 23:26we're going to talk about a little bit
  593. 23:27here in depth using approaches like
  594. 23:29multi-query or rag Fusion which we'll
  595. 23:32talk about in the next video you can
  596. 23:34also do things like take a question and
  597. 23:36break it down to make it less abstract
  598. 23:38like into sub questions and there's a
  599. 23:40bunch of interesting papers focused on
  600. 23:42that like least to most from
  601. 23:44Google you can also take the opposite
  602. 23:46approach of take a question to make it
  603. 23:48more abstract uh and there's actually
  604. 23:50approach we're going to talk about later
  605. 23:51in a future video called stepback
  606. 23:53prompting that focuses on like kind of
  607. 23:56higher a higher level question from the
  608. 23:59input so the intuition though for this
  609. 24:03multier approach is we're taking a
  610. 24:05question and we're going to break it
  611. 24:06down into a few differently worded
  612. 24:08questions uh from different
  613. 24:10perspectives and the intuition here is
  614. 24:13simply that um it is possible that the
  615. 24:17way a question is initially worded once
  616. 24:21embedded it is not well aligned or in
  617. 24:24close proximity in this High dimensional
  618. 24:26embedding space to a document that we
  619. 24:27want to R that's actually related so the
  620. 24:30thinking is that by kind of rewriting it
  621. 24:32in a few different ways you actually
  622. 24:34increase the likel of actually
  623. 24:36retrieving the document that you really
  624. 24:37want to um because of nuances in the way
  625. 24:41that documents and questions are
  626. 24:43embedded this kind of more shotgun
  627. 24:46approach of taking a question Fanning it
  628. 24:47out into a few different perspectives
  629. 24:50May improve and increase the reliability
  630. 24:51of retrieval that's like the intuition
  631. 24:54really um and of course we can com
  632. 24:56combine this with retrieval so we can
  633. 24:59take our our kind of fan out questions
  634. 25:01do retrieval on each one and combine
  635. 25:03them in some way and perform rag so
  636. 25:06that's kind of the overview and now
  637. 25:07let's what let's go over to um our code
  638. 25:11so this is a notebook and we're going to
  639. 25:13share all
  640. 25:14this um we're just installing a few
  641. 25:18packages we're setting a lsmith API Keys
  642. 25:21which we'll see why that's quite useful
  643. 25:22here shortly there's our diagram now
  644. 25:25first I'm going to Index this blog post
  645. 25:27on agents I'm going to split it um well
  646. 25:31I'm going to load it I'm going to split
  647. 25:32it and then I'm going to index it in
  648. 25:34chroma locally so this is a vector store
  649. 25:37we've done this previously so now I have
  650. 25:38my index defined so here is where I'm
  651. 25:41defining my prompt for multiquery which
  652. 25:44is your your assistant your task is to
  653. 25:47basically reframe this question into a
  654. 25:48few different sub
  655. 25:50questions um so there's our
  656. 25:53prompt um right here we'll pass that to
  657. 25:56an llm part it um into a string and then
  658. 26:01split the string by new lines and so
  659. 26:02we'll get a list of questions out of
  660. 26:04this chain that's really all we're doing
  661. 26:06here now all we're doing is here's a
  662. 26:09sample input question there's our
  663. 26:11generate queries chain which we defined
  664. 26:13we're going to take that list and then
  665. 26:15simply apply each question to retriever
  666. 26:19so we'll do retrieval per question and
  667. 26:21this little function here is just going
  668. 26:22to take the unique Union of documents uh
  669. 26:25across all those retrievals so let's run
  670. 26:27this and see what happens so we're going
  671. 26:29to run this and we're going to get some
  672. 26:32set of questions uh or documents back so
  673. 26:36let's go to Langs Smith now we can
  674. 26:37actually see what happened under the
  675. 26:39hood so here's the key
  676. 26:42point we ran our initial chain to
  677. 26:44generate a set of of reframed questions
  678. 26:48from our input and here was that prompt
  679. 26:51and here is that set of questions that
  680. 26:52we generated now what happened is for
  681. 26:55every one of those questions we did an
  682. 26:57independent retrieval that's what we're
  683. 26:58showing here so that's kind of the first
  684. 27:00step which is great now I can go back to
  685. 27:03the notebook and we can show this
  686. 27:05working end to end so now we're going to
  687. 27:07take that retrieval chain we'll pass it
  688. 27:09into context of our final rag prompt
  689. 27:12we'll also pass through the question
  690. 27:14we'll pass that to our rag prompt here
  691. 27:16pass it to an LM and then Pary output
  692. 27:19now let's let's kind of see how that
  693. 27:21works so again that's okay there it is
  694. 27:24so let's actually go into langth and see
  695. 27:26what happened under the hood so this was
  696. 27:28our final chain so this is great we took
  697. 27:31our input question we broke it out to
  698. 27:33these like five rephrase questions for
  699. 27:36every one of those we did a retrieval
  700. 27:38that's all great we then took the unique
  701. 27:41Union of documents and you can see in
  702. 27:42our final llm prompt answer the
  703. 27:45following cont following question based
  704. 27:46on the context this is the final set of
  705. 27:50unique documents that we retrieved from
  706. 27:52all of our sub
  707. 27:54questions um here's our initial question
  708. 27:57there's our answer so that kind of shows
  709. 27:59you how you can set this up really
  710. 28:00easily how you can use l Smith to kind
  711. 28:02of investigate what's going on and in
  712. 28:04particular use l Smith to investigate
  713. 28:06those intermediate questions that you
  714. 28:08generate in that like kind of question
  715. 28:11generation phase and in a future talks
  716. 28:13we're going to go through um some of
  717. 28:15these other methods that we kind of
  718. 28:17introduced at the start of this one
  719. 28:18thank
  720. 28:20you last L chain this is the second
  721. 28:23video of our Deep dive on query
  722. 28:25translation in our rag from scratch
  723. 28:27series focused on a method called rag
  724. 28:30Fusion so as we kind of showed before
  725. 28:33career translation you can think of as
  726. 28:35the first stage in an advanced rag
  727. 28:38pipeline we're taking an input user
  728. 28:40question and We're translating it some
  729. 28:42way in order to improve
  730. 28:44retrievable now we showed this General
  731. 28:48mapping of approaches previously so
  732. 28:50again you have kind of like rewriting so
  733. 28:52you can take a question and like kind of
  734. 28:54break it down into uh differently worded
  735. 28:58are different different perspectives of
  736. 29:00the same question so that's kind of
  737. 29:02rewriting there's sub questions where
  738. 29:04you take a question break it down into
  739. 29:06smaller problems solve each one
  740. 29:08independently and then there step back
  741. 29:10where you take a question and kind of go
  742. 29:11more abstract where you kind of ask a
  743. 29:14higher level question as a precondition
  744. 29:16to answer the user question so those are
  745. 29:18the approaches and we're going to dig
  746. 29:20into one of the particular approaches
  747. 29:22for rewriting called rat Fusion now this
  748. 29:25is really similar to what we just saw
  749. 29:27with multiquery
  750. 29:28the difference being we actually apply a
  751. 29:31a kind of a clever rank ranking step of
  752. 29:33our retriev documents um which you call
  753. 29:36reciprocal rank Fusion that's really the
  754. 29:38only difference the the input stage of
  755. 29:41taking a question breaking it out into a
  756. 29:44few kind of differently worded questions
  757. 29:47retrieval on each one is all the same
  758. 29:50and we're going to see that in the code
  759. 29:51here shortly so let's just hop over
  760. 29:54there and then look at this so again
  761. 29:56here is a notebook that we introduced
  762. 29:59previously here's the packages we've
  763. 30:01installed we've set a few API keys for
  764. 30:04lsmith which we see why is quite
  765. 30:07useful um and you can kind of go down
  766. 30:11here to a rag Fusion
  767. 30:13section and the first thing you'll note
  768. 30:16is what our prompt is so it looks really
  769. 30:18similar to The Prompt we just saw with
  770. 30:19multiquery and simply your helpful
  771. 30:22assistant that generates multiple search
  772. 30:23queries based upon user input and here's
  773. 30:26the question output for queries so let's
  774. 30:30define our prompt and here was our query
  775. 30:33Generation chain again this looks a lot
  776. 30:34like we just saw we take our prompt Plum
  777. 30:37that into an llm and then basically
  778. 30:39parse by new lines and that'll basically
  779. 30:42split out these questions into a list
  780. 30:46that's all it's going to happen here so
  781. 30:47that's cool now here's where the novelty
  782. 30:51comes
  783. 30:52in each time we do retrieval from one of
  784. 30:56those questions we're going to get back
  785. 30:58a list of documents from our Retriever
  786. 31:01and so we do it over that we generate
  787. 31:03four questions here based on our prompt
  788. 31:06we do the over four questions well like
  789. 31:07a list of lists
  790. 31:09basically now reciprocal rank Fusion is
  791. 31:12really well suited for this exact
  792. 31:13problem we want to take this list to
  793. 31:15list and build a single Consolidated
  794. 31:17list and really all that's going on is
  795. 31:20it's looking at the documents in each
  796. 31:22list and kind of aggregating them into a
  797. 31:24final output ranking um and that's
  798. 31:28really the intuition around what's
  799. 31:29happening
  800. 31:33here um so let's go ahead
  801. 31:39and so
  802. 31:46let's so let's go ahead and look at that
  803. 31:49in some detail so we can see we
  804. 31:53run
  805. 31:55retrieval that's great now let's go over
  806. 31:57to Lang Smith and have a look at what's
  807. 32:00going on here so we can see that here
  808. 32:05was our prompt to your helpful assistant
  809. 32:06that generates multiple search queries
  810. 32:08based on a single input and here is our
  811. 32:10search queries and then here are our
  812. 32:14four retrievals so that's that's really
  813. 32:16good so we know that all is
  814. 32:19working um and then those retrievals
  815. 32:23simply went into this rank
  816. 32:25function and our correspondingly ranked
  817. 32:29to a final list of six unique rank
  818. 32:31documents that's really all we
  819. 32:34did so let's actually put that all
  820. 32:37together into an a full rag chain that's
  821. 32:41going to run
  822. 32:43retrieval return that final list of rank
  823. 32:47documents and pass it to our context
  824. 32:50pass through our question send that to a
  825. 32:53rag prompt pass it to an LM parse it to
  826. 32:56an output and let's run all that
  827. 32:58together and see that
  828. 33:01working cool so there's our final
  829. 33:07answer now let's have a look in lsmith
  830. 33:10we can see here was our four questions
  831. 33:13here's our retrievals and then our final
  832. 33:15rag prompt plumed through the final list
  833. 33:18of ranked six questions which we can see
  834. 33:22laid out here and our final answer so
  835. 33:25this can be really convenient
  836. 33:26particularly if we're operating across
  837. 33:29like maybe different Vector stores uh or
  838. 33:32we want to do like retrieval across a
  839. 33:34large number of of kind of differently
  840. 33:36worded questions this reciprocal rank
  841. 33:38Fusion step is really nice um for
  842. 33:41example if we wanted to only take the
  843. 33:43top three documents or something um it
  844. 33:46can be really nice to build that
  845. 33:47Consolidated ranking across all these
  846. 33:49independent retrievals then pass that to
  847. 33:52for the final generation so that's
  848. 33:54really the intuition about what's
  849. 33:55happening here thanks
  850. 33:58hi this is Lance from Lang chain this is
  851. 34:00our third video focused on query
  852. 34:02translation in the rag from scratch
  853. 34:04series and we're going to be talking
  854. 34:06about
  855. 34:07decomposition so query translation in
  856. 34:09general is a set of approaches that sits
  857. 34:11kind of towards the front of this
  858. 34:13overall rag Pipeline and the objective
  859. 34:16is to modify or rewrite or otherwise
  860. 34:18decompose an input question from a user
  861. 34:21in order improve
  862. 34:24retrieval so we can talk through some of
  863. 34:26these approaches previously in
  864. 34:28particular various ways to do query
  865. 34:30writing like rag fusion and multiquery
  866. 34:32there's a separate set of techniques
  867. 34:34that become pretty popular and are
  868. 34:35really interesting for certain problems
  869. 34:37which we might call like kind of
  870. 34:39breaking down or decomposing an input
  871. 34:41question into a set of sub
  872. 34:43questions um so some of the papers here
  873. 34:46that are are pretty cool are for example
  874. 34:49this work from
  875. 34:50Google um and the objective really is
  876. 34:54first to take an input question and
  877. 34:57decompose it into a set of sub problems
  878. 35:00so this particular example from the
  879. 35:02paper was the problem of um last letter
  880. 35:07concatenation and so it took the inut
  881. 35:10question of three words think machine
  882. 35:13learning and broke it down into three
  883. 35:15sub problems think think machine think
  884. 35:17machine learning as the third sub
  885. 35:19problem and then you can see in this
  886. 35:20bottom panel it solves each one
  887. 35:23individually so it shows for example in
  888. 35:25green solving the problem think machine
  889. 35:28where you can catenate the last letter
  890. 35:30of k with the last letter of machine or
  891. 35:32last letter think K less machine e can
  892. 35:36concatenate those to K and then for the
  893. 35:39overall problem taking that solution and
  894. 35:43then and basically building on it to get
  895. 35:45the overall solution of keg so that's
  896. 35:47kind of one concept of decomposing into
  897. 35:50sub problems solving them
  898. 35:52sequentially now a related work called
  899. 35:55IRC or in leap retrieval combines
  900. 35:58retrieval with Chain of Thought
  901. 36:01reasoning and so you can kind of put
  902. 36:03these together into one approach which
  903. 36:05you can think of as kind of dynamically
  904. 36:08retrieval um to solve a set of sub
  905. 36:11problems kind of that retrieval kind of
  906. 36:13interleaving with Chain of Thought as
  907. 36:15noted in the second paper and a set of
  908. 36:19decomposed questions based on your
  909. 36:21initial question from the first work
  910. 36:23from Google so really the idea here is
  911. 36:25we're taking one sub question we're
  912. 36:28answering it we're taking that answer
  913. 36:29and using it to help answer the second
  914. 36:31sub question and so forth so let's
  915. 36:34actually just walk through this in code
  916. 36:35to show how this might
  917. 36:37work so this is The Notebook we've been
  918. 36:40working with from some of the other uh
  919. 36:42videos you can see we already have a
  920. 36:43retriever to find uh up here at the top
  921. 36:47and what we're going to do
  922. 36:49is we're first going to find a prompt
  923. 36:52that's basically going to say given an
  924. 36:54input question let's break it down to
  925. 36:57set of sub problems or sub question
  926. 36:59which can be solved individually so we
  927. 37:01can do that and this blog post is
  928. 37:03focused on agents so let's ask a
  929. 37:05question about what are the main
  930. 37:06components of an LM powerered autonomous
  931. 37:08agent system so let's run this and
  932. 37:13see what the decomposed questions are so
  933. 37:16you can see the decomposed questions are
  934. 37:18what is LM technology how does it work
  935. 37:21um what are components and then how the
  936. 37:23components interact so it's kind of a
  937. 37:25sane way to kind of break down this
  938. 37:27problem into a few sub problems which
  939. 37:28you might attack individually now here's
  940. 37:32where um we Define a prompt that very
  941. 37:35simply is going to take our question
  942. 37:38we'll take any prior questions we've
  943. 37:40answered and we'll take our retrieval
  944. 37:42and basically just combine them and we
  945. 37:45can Define this very simple
  946. 37:47chain um actually let's go back and make
  947. 37:49sure retriever is defined up at the
  948. 37:52top so now we are building our
  949. 37:56retriever good we have that now so we
  950. 37:59can go back down here and let's run this
  951. 38:02so
  952. 38:03now we are
  953. 38:06running and what's happening is we're
  954. 38:10trying to solve each of these questions
  955. 38:12individually using retrieval and using
  956. 38:15any prior question answers so okay very
  957. 38:18good looks like that's been done and we
  958. 38:20can see here's our answer now let's go
  959. 38:23over to langth and actually see what
  960. 38:25happened under the hood so here's what's
  961. 38:27kind of of interesting and helpful to
  962. 38:28see for the first question so here's our
  963. 38:31first one it looks like it just does
  964. 38:33retrieval which is we expect and then it
  965. 38:36uses that to answer this initial
  966. 38:37question now for the second question
  967. 38:40should be a little bit more interesting
  968. 38:42because if you look at our prompt here's
  969. 38:44our question now here is our background
  970. 38:47available question answer pair so this
  971. 38:49was the answer question answer pair from
  972. 38:51the first question which we add to our
  973. 38:52prompt and then here's the retrieval for
  974. 38:55this particular question so we're kind
  975. 38:56of building up up the solution because
  976. 38:58we're pending the question answer pair
  977. 39:00from question one and then likewise with
  978. 39:03question three it should combine all of
  979. 39:05that so we can look at here here's our
  980. 39:08question here's question one here's
  981. 39:10question two great now here's additional
  982. 39:14retrieval related to this particular
  983. 39:16question and we get our final answer so
  984. 39:18that's like a really nice way you can
  985. 39:20kind of build up Solutions um using this
  986. 39:23kind of
  987. 39:24interleaved uh retrieval and
  988. 39:26concatenating question answer pairs I do
  989. 39:29want to mention very briefly that we can
  990. 39:31also take a different approach where we
  991. 39:33can just answer these all individually
  992. 39:36and then just concatenate all those
  993. 39:37answers to produce a final answer and
  994. 39:39I'll show that really quickly here um
  995. 39:43it's like a little bit less interesting
  996. 39:44maybe because you're not using answers
  997. 39:46from each uh question to inform the next
  998. 39:50one you're just answering them all in
  999. 39:51parallel this might be better for cases
  1000. 39:53where it's not really like a sub
  1001. 39:55question decomposition but maybe it's
  1002. 39:57like like a set of set of several in
  1003. 40:00independent questions whose answers
  1004. 40:02don't depend on each other that might be
  1005. 40:04relevant for some
  1006. 40:05problems um and we can go ahead and run
  1007. 40:08okay so this ran as well we can look at
  1008. 40:11our
  1009. 40:12trace and in this
  1010. 40:14case um yeah we can see that this
  1011. 40:16actually just kind of concatenates all
  1012. 40:19of our QA pairs to produce the final
  1013. 40:22answer so this gives you a sense for how
  1014. 40:24you can use quer decomposition employ
  1015. 40:27IDE IDE from uh from two different
  1016. 40:28papers that are pretty cool thanks hi
  1017. 40:32this is Lance from Lang chain this is
  1018. 40:34the fourth video uh in our Deep dive on
  1019. 40:37queer translation in the rag from
  1020. 40:39scratch series and we're going to be
  1021. 40:40focused on step back
  1022. 40:42prompting so queer translation as we
  1023. 40:45said in some of the prior videos kind of
  1024. 40:48sits at the the kind of first stage of
  1025. 40:51kind of a a a rag pipeline or flow and
  1026. 40:56the main aim is to take an question and
  1027. 40:58to translate it or modify in such a way
  1028. 41:00that it improves
  1029. 41:02retrieval now we talked through a few
  1030. 41:04different ways to approach this problem
  1031. 41:07so one General approach involves
  1032. 41:08rewriting a question and we talk about
  1033. 41:10two ways to do that rag fusion
  1034. 41:12multiquery and again this is this is
  1035. 41:14really about taking a question and
  1036. 41:16modifying it to capture a few different
  1037. 41:19perspectives um which may improve the
  1038. 41:21retrieval
  1039. 41:22process now another approach is to take
  1040. 41:25a question and kind of make it less
  1041. 41:26abstract like break it down into sub
  1042. 41:29questions um and then solve each of
  1043. 41:31those independently so that's what we
  1044. 41:32saw with like least to most prompting um
  1045. 41:35and a bunch of other variants kind of in
  1046. 41:37that in that vein of sub problem solving
  1047. 41:42and then consolidating those Solutions
  1048. 41:44into a final
  1049. 41:45answer now a different approach
  1050. 41:48presented um by again Google as well is
  1051. 41:52stepback prompting so stepback prompting
  1052. 41:55kind of takes the the the opposite
  1053. 41:57approach where it tries to ask a more
  1054. 42:00abstract question so the paper talks a
  1055. 42:04lot
  1056. 42:05about um using F shot
  1057. 42:09prompting to produce what they call the
  1058. 42:12stepback or more abstract questions and
  1059. 42:15the way it does it is it provides a
  1060. 42:17number of
  1061. 42:19examples of stepb back questions given
  1062. 42:22your original question so like this is
  1063. 42:24like this is for example they like for
  1064. 42:26prompt temp
  1065. 42:27you're an expert World Knowledge I asked
  1066. 42:29you a question your response should be
  1067. 42:30comprehensive not contradict with the
  1068. 42:32following um and this is kind of where
  1069. 42:35you provide your like original and then
  1070. 42:38step back so here's like some example um
  1071. 42:43questions so like um
  1072. 42:46like uh at year saw the creation of the
  1073. 42:50region where the country is located
  1074. 42:53which region of the
  1075. 42:54country um is the county of of herir
  1076. 42:58related um Janell was born in what
  1077. 43:01country what is janell's personal
  1078. 43:04history so that that's maybe a more
  1079. 43:05intuitive example so it's like you ask a
  1080. 43:07very specific question about like the
  1081. 43:09country someone's born the more abstract
  1082. 43:11question is like just give me the
  1083. 43:12general history of this individual
  1084. 43:14without worrying about that particular
  1085. 43:16um more specific question um so let's
  1086. 43:21actually just walk through how this can
  1087. 43:22be done in practice um so again here's
  1088. 43:25kind of like a a diagram of uh the
  1089. 43:28various approaches um from less
  1090. 43:31abstraction to more
  1091. 43:32abstraction now here is where we're
  1092. 43:35formulating our prompt using a few of
  1093. 43:38the few shot examples from the
  1094. 43:40paper um so again like input um yeah
  1095. 43:45something about like the police perform
  1096. 43:46wful arrests and what what camp members
  1097. 43:48of the police do so like it it basically
  1098. 43:51gives the model a few examples um we
  1099. 43:54basically formulate this into a prompt
  1100. 43:56that's really all going on here again we
  1101. 43:58we repeat um this overall prompt which
  1102. 44:02we saw from the paper your expert World
  1103. 44:04Knowledge your test is to step back and
  1104. 44:06paraphrase a question generate more a
  1105. 44:08generic step back question which is
  1106. 44:10easier to answer here are some examples
  1107. 44:12so it's like a very intuitive prompt so
  1108. 44:16okay let's start with the question what
  1109. 44:18is Task composition for llm agents and
  1110. 44:21we're going to say generate stack
  1111. 44:23question okay so this is pretty
  1112. 44:24intuitive right what is a process of
  1113. 44:26task compos I so like not worrying as
  1114. 44:28much about agents but what is that
  1115. 44:30process of task composition in general
  1116. 44:33and then hopefully that can be
  1117. 44:36independently um retrieved we we can
  1118. 44:39independently retrieve documents related
  1119. 44:41to the stepb back question and in
  1120. 44:43addition retrieve documents related to
  1121. 44:45the the actual question and combine
  1122. 44:47those to produce kind of final answer so
  1123. 44:49that's really all that's going on um and
  1124. 44:52here's the response template where we're
  1125. 44:53Plumbing in the stepback context and our
  1126. 44:57question context and so what we're going
  1127. 45:00to do here is we're going to take our
  1128. 45:01input question and perform retrieval on
  1129. 45:04that we're also going to generate our
  1130. 45:06stepb back question and perform
  1131. 45:07retrieval on that we're going to plumb
  1132. 45:10those into the prompt as here's our very
  1133. 45:13here's our basically uh our prompt Keys
  1134. 45:15normal question step back question um
  1135. 45:19and our overall question again we
  1136. 45:21formulate those as a dict we Plum those
  1137. 45:23into our response prompt um and then we
  1138. 45:29go ahead and attempt to answer our
  1139. 45:31overall question so we're going to run
  1140. 45:33that that's
  1141. 45:35running and okay we have our answer now
  1142. 45:38I want to hop over to Langs Smith and
  1143. 45:41attempt to show you um kind of what that
  1144. 45:45looked like under the hood so let's see
  1145. 45:48let's like go into each of these steps
  1146. 45:51so here was our prompt right you're an
  1147. 45:53expert World Knowledge your test to to
  1148. 45:55step back and paraph as a question
  1149. 45:58um so um here were our few shot prompts
  1150. 46:03and this was our this was our uh stepb
  1151. 46:06question so what is the process of task
  1152. 46:08composition um good from the input what
  1153. 46:12is Tas composition for LM agents we
  1154. 46:14perform retrieval on both what is
  1155. 46:16process
  1156. 46:17composition uh and what is for LM agents
  1157. 46:20we perform both retrievals we then
  1158. 46:23populate our prompt with both
  1159. 46:27uh original question answer and then
  1160. 46:29here's the context retrieve from both
  1161. 46:31the question and the stepb back question
  1162. 46:33here was our final answer so again this
  1163. 46:35is kind of a nice technique um probably
  1164. 46:38depends on a lot of the types of like
  1165. 46:41the type of domain you want to perform
  1166. 46:43retrieval on um but in some domains
  1167. 46:47where for example there's a lot of kind
  1168. 46:48of conceptual knowledge that underpins
  1169. 46:52questions you expect users to ask this
  1170. 46:54stepback approach could be really
  1171. 46:55convenient to automatically formulate a
  1172. 46:59higher level question um to for example
  1173. 47:02try to improve retrieval I can imagine
  1174. 47:04if you're working with like kind of
  1175. 47:05textbooks or like technical
  1176. 47:07documentation where you make independent
  1177. 47:10chapters focused on more highlevel kind
  1178. 47:12of like Concepts and then other chapters
  1179. 47:15on like more detailed uh like
  1180. 47:18implementations this kind of like stepb
  1181. 47:19back approach and independent retrieval
  1182. 47:22could be really helpful thanks hi this
  1183. 47:25is Lance from Lang chain
  1184. 47:27this is the fifth video focused on queer
  1185. 47:29translation in our rack from scratch
  1186. 47:31series we're going to be talking about a
  1187. 47:33technique called
  1188. 47:34hide so again queer translation sits
  1189. 47:37kind of at the front of the overall rag
  1190. 47:39flow um and the objective is to take an
  1191. 47:41input question and translate it in some
  1192. 47:43way that improves
  1193. 47:46retrieval now hide is an interesting
  1194. 47:49approach that takes advantage of a very
  1195. 47:51simple idea the basic rag flow takes a
  1196. 47:55question and embeds it takes a document
  1197. 47:57and embeds it and looks for similarity
  1198. 48:00between an embedded document and
  1199. 48:02embedded question but questions and
  1200. 48:04documents are very different text
  1201. 48:06objects so documents can be like very
  1202. 48:08large chunks taken from dense um
  1203. 48:11Publications or other sources whereas
  1204. 48:14questions are short kind of tur
  1205. 48:16potentially ill worded from users and
  1206. 48:19the intuition behind hide is take
  1207. 48:22questions and map them into document
  1208. 48:25space using a hypothetical document or
  1209. 48:28by generating a hypothetical document um
  1210. 48:31that's the basic intuition and the idea
  1211. 48:34kind of shown here visually is that in
  1212. 48:36principle for certain cases a
  1213. 48:38hypothetical document is closer to a
  1214. 48:41desired document you actually want to
  1215. 48:42retrieve in this you know High
  1216. 48:45dimensional embedding space than the
  1217. 48:47sparse raw input question itself so
  1218. 48:50again it's just kind of means of trans
  1219. 48:52translating raw questions into these
  1220. 48:54hypothetical documents that are better
  1221. 48:57suited for
  1222. 48:58retrieval so let's actually do a Code
  1223. 49:00walkthrough to see how this works and
  1224. 49:02it's actually pretty easy to implement
  1225. 49:04which is really nice so first we're just
  1226. 49:06starting with a prompt and we're using
  1227. 49:08the same notebook that we've used for
  1228. 49:09prior videos we have a blog post on
  1229. 49:12agents r index um so what we're going to
  1230. 49:15do is Define a prompt to generate a
  1231. 49:17hypothetical documents in this case
  1232. 49:19we'll say write a write a paper passage
  1233. 49:22uh to answer a given question so let's
  1234. 49:25just run this and see what happens again
  1235. 49:26we're taking our prompt piping it to to
  1236. 49:29open Ai chck gpte and then using string
  1237. 49:32Opa parer and so here's a hypothetical
  1238. 49:34document section related to our question
  1239. 49:38okay and this is derived of course lm's
  1240. 49:40kind of embedded uh kind of World
  1241. 49:43Knowledge which is you know a sane place
  1242. 49:45to generate hypothetical documents now
  1243. 49:48let's now take that hypothetical
  1244. 49:50document and basically we're going to
  1245. 49:52pipe that into a retriever so this means
  1246. 49:54we're going to fetch documents from our
  1247. 49:56index related to this hypothetical
  1248. 49:59document that's been embedded and you
  1249. 50:01can see we get a few qu a few retrieved
  1250. 50:04uh chunks that are related to uh this
  1251. 50:08hypothetical document that's all we've
  1252. 50:11done um and then let's take the final
  1253. 50:15step where we take those retrieve
  1254. 50:17documents here which we
  1255. 50:19defined and our question we're going to
  1256. 50:22pipe that into this rag prompt and then
  1257. 50:25we're going to run our kind of rag chain
  1258. 50:27right here which you've seen before and
  1259. 50:29we get our answer so that's really it we
  1260. 50:32can go to lsmith and we can actually
  1261. 50:33look at what happened um so here for
  1262. 50:37example this was our final um rag prompt
  1263. 50:43answer the following question based on
  1264. 50:44this context and here is the retrieve
  1265. 50:47documents that we passed in so that
  1266. 50:48part's kind of straightforward we can
  1267. 50:50also look at um okay this is our
  1268. 50:55retrieval okay now this is this is
  1269. 50:57actually what we we generated a
  1270. 51:01hypothetical document here um okay so
  1271. 51:05this is our hypothetical document so
  1272. 51:08we've run chat open AI we generated this
  1273. 51:10passage with our hypothetical document
  1274. 51:12and then we've run
  1275. 51:13retrieval here so this is basically
  1276. 51:15showing hypothetical document generation
  1277. 51:17followed by retrieval um so again here
  1278. 51:21was our passage which we passed in and
  1279. 51:24then here's our retrieve documents from
  1280. 51:25the retriever which are related to the
  1281. 51:27passage content so again in this
  1282. 51:30particular index case it's possible that
  1283. 51:32the input question was sufficient to
  1284. 51:34retrieve these documents in fact given
  1285. 51:36prior examples uh I know that some of
  1286. 51:38these same documents are indeed
  1287. 51:39retrieved just from the raw question but
  1288. 51:42in other context it may not be the case
  1289. 51:44so folks have reported nice performance
  1290. 51:46using Hyde uh for certain domains and
  1291. 51:49the Really convenient thing is that you
  1292. 51:52can take this this document generation
  1293. 51:55prompt you can tune this arbitrarily for
  1294. 51:57your domain of Interest so it's
  1295. 51:59absolutely worth experimenting with it's
  1296. 52:01a it's a need approach uh that can
  1297. 52:03overcome some of the challenges with
  1298. 52:04retrieval uh thanks very much hi this is
  1299. 52:08Lance from Lang chain this is the 10th
  1300. 52:10video in our rack from scratch series
  1301. 52:12focused on
  1302. 52:13routing so we talk through query
  1303. 52:16translation which is the process of
  1304. 52:17taking a question and translating in
  1305. 52:19some way it could be decomposing it
  1306. 52:21using stepback prompting or otherwise
  1307. 52:24but the idea here was take our question
  1308. 52:26change it into a form that's better
  1309. 52:27suited for retrieval now routing is the
  1310. 52:30next step which is basically routing
  1311. 52:32that potentially decomposed question to
  1312. 52:35the right source and in many cases that
  1313. 52:37could be a different database so let's
  1314. 52:38say in this toy example we have a vector
  1315. 52:40store a relational DB and a graph DB the
  1316. 52:43what we redo with routing is we simply
  1317. 52:45route the question based upon the cont
  1318. 52:47of the question to the relevant data
  1319. 52:50source so there's a few different ways
  1320. 52:52to do that one is what we call logical
  1321. 52:54routing in this case we basically give
  1322. 52:56an llm knowledge of the various data
  1323. 52:59sources that we have at our disposal and
  1324. 53:02we let the llm kind of Reason about
  1325. 53:04which one to apply the question to so
  1326. 53:07it's kind of like the the LM is applying
  1327. 53:09some logic to determine you which which
  1328. 53:11data sour for example to to use
  1329. 53:13alternatively you can use semantic
  1330. 53:15routing which is where we take a
  1331. 53:17question we embed it and for example we
  1332. 53:19embed prompts we then compute the
  1333. 53:22similarity between our question and
  1334. 53:24those prompts and then we choose a
  1335. 53:27prompt based upon the similarity so the
  1336. 53:29general idea is in our diagram we talk
  1337. 53:31about routing to for example a different
  1338. 53:33database but it can be very general can
  1339. 53:35be routing to different prompt it can be
  1340. 53:37you know really arbitrarily taking this
  1341. 53:40question and sending it at different
  1342. 53:41places be at different prompts be at
  1343. 53:43different Vector
  1344. 53:44stores so let's walk through the code a
  1345. 53:46little bit so you can see just like
  1346. 53:48before we've done a few pip installs we
  1347. 53:51set up lsmith and let's talk through uh
  1348. 53:54logical routing first so so in this toy
  1349. 53:57example let's say we had for example uh
  1350. 54:00three different docs like we had python
  1351. 54:02docs we had JS docs we had goang docs
  1352. 54:05what we want to do is take a question
  1353. 54:07route it to one of those three so what
  1354. 54:10we're actually doing is we're setting up
  1355. 54:11a data model which is basically going to
  1356. 54:15U be bound to our llm and allow the llm
  1357. 54:19to Output one of these three options as
  1358. 54:23a structured object so you really think
  1359. 54:26about this as like
  1360. 54:27classification classification plus
  1361. 54:29function calling to produce a structured
  1362. 54:31output which is constrained to these
  1363. 54:33three
  1364. 54:34possibilities so the way we do that is
  1365. 54:37let's just zoom in here a little bit we
  1366. 54:39can Define like a structured object that
  1367. 54:41we want to get out from our llm like in
  1368. 54:44this case we want for example you know
  1369. 54:46one of these three data sources to be
  1370. 54:49output we can take this and we can
  1371. 54:52actually convert it into open like open
  1372. 54:54for example function schema
  1373. 54:57and then we actually pass that in and
  1374. 54:58bind it to our llm so what happens is we
  1375. 55:01ask a question our llm invokes this
  1376. 55:04function on the output to produce an
  1377. 55:07output that adheres to the schema that
  1378. 55:09we specify so in this case for example
  1379. 55:12um we output like you know in this toy
  1380. 55:15example let's say we wanted like you
  1381. 55:17know an output to be data source Vector
  1382. 55:19store or SQL database the output will
  1383. 55:22contain a data source object and it'll
  1384. 55:23be you know one of the options we
  1385. 55:25specify as a Json string we also
  1386. 55:28instantiate a parser from this object to
  1387. 55:32parse that Json string to an output like
  1388. 55:35a pantic object for example so that's
  1389. 55:37just one toy example and let's show one
  1390. 55:39up here so in this case again we had our
  1391. 55:41three doc sources um we bind that to our
  1392. 55:46llm so you can see we do with structured
  1393. 55:49output basically under the hood that's
  1394. 55:51taking that object definition turning
  1395. 55:54into function schema and binding that
  1396. 55:55function schema to our llm and we call
  1397. 55:58our prompt you're an expert at routing a
  1398. 56:00user question based on you know
  1399. 56:03programming language um that user
  1400. 56:05referring to so let's define our router
  1401. 56:08here now what we're going to do is we'll
  1402. 56:10ask a question that is python code so
  1403. 56:14we'll call that and now it's done and
  1404. 56:16you see the object we get out is indeed
  1405. 56:18it's a route query object so it's
  1406. 56:20exactly it aderes to this data model
  1407. 56:22we've set up and in this case it's it's
  1408. 56:26it's correct so it's calling this python
  1409. 56:27doc so you can we can extract that right
  1410. 56:29here as a string now once we have this
  1411. 56:33you can really easily set up like a
  1412. 56:35route so this could be like our full
  1413. 56:37chain where we take this router we
  1414. 56:39should defined here and then this choose
  1415. 56:41route function can basically take that
  1416. 56:44output and do something with it so for
  1417. 56:46example if python docs this could then
  1418. 56:49apply the question to like a retriever
  1419. 56:51full of python information uh or JS same
  1420. 56:55thing so this is where you would hook
  1421. 56:57basically that question up to different
  1422. 56:59chains that are like you know retriever
  1423. 57:02chain one for python retriever chain two
  1424. 57:04for JS and so forth so this is kind of
  1425. 57:06like the routing mechanism but this is
  1426. 57:08really doing the heavy lifting of taking
  1427. 57:10an input question and turning into a
  1428. 57:12structured object that restricts the
  1429. 57:14output to one of a few output types that
  1430. 57:18we care about in our like routing
  1431. 57:20problem so that's really kind of the way
  1432. 57:22this all hooks
  1433. 57:23together now semantic outing is actually
  1434. 57:26maybe even a little bit more
  1435. 57:27straightforward based on what we've seen
  1436. 57:29previously so in that case let's say we
  1437. 57:32have two prompts we have a physics
  1438. 57:33prompt we have a math
  1439. 57:35prompt we can embed those prompts no
  1440. 57:38problem we do that here now let's say we
  1441. 57:41have an input question from a user like
  1442. 57:43in this case what is a black hole we
  1443. 57:45pass that through we then apply this
  1444. 57:47runnable Lambda function which is
  1445. 57:49defined right here what we're doing here
  1446. 57:51is we're embedding the question we're
  1447. 57:53Computing similarity between the
  1448. 57:54question and the prompts uh we're taking
  1449. 57:57the most similar and then we're
  1450. 58:00basically choosing the prompt based on
  1451. 58:02that similarity and you can see let's
  1452. 58:03run that and try it
  1453. 58:05out and we're using the physics prompt
  1454. 58:07and there we go black holes region and
  1455. 58:09space so that just shows you kind of how
  1456. 58:11you can use semantic routing uh to
  1457. 58:15basically embed a question embed for
  1458. 58:17example various prompts pick the prompt
  1459. 58:19based on sematic similarity so that
  1460. 58:22really gives you just two ways to do
  1461. 58:23routing one is logical routing with
  1462. 58:25function in uh can be used very
  1463. 58:27generally in this case we applied it to
  1464. 58:29like different coding languages but
  1465. 58:31imagine these could be swapped out for
  1466. 58:33like you know my python uh my like
  1467. 58:36vector store versus My Graph DB versus
  1468. 58:38my relational DB and you could just very
  1469. 58:42simply have some description of what
  1470. 58:43each is and you know then not only will
  1471. 58:46the llm do reasoning but it'll also
  1472. 58:49return an object uh that can be parsed
  1473. 58:52very cleanly to produce like one of a
  1474. 58:54few very specific types which then you
  1475. 58:56can reason over like we did here in your
  1476. 58:59routing function so that kind of gives
  1477. 59:01you the general idea and these are
  1478. 59:02really very useful tools and I encourage
  1479. 59:05you to experiment with them
  1480. 59:07thanks hi this is Lance from Lang chain
  1481. 59:10this is the 11th part of our rag from
  1482. 59:12scratch video series focused on query
  1483. 59:15construction so we previously talked
  1484. 59:17through uh query translation which is
  1485. 59:19the process of taking a question and
  1486. 59:21converting it or translating it into a
  1487. 59:24question that's better optimized for
  1488. 59:25retrieval then we talked about routing
  1489. 59:27which is the process of going taking
  1490. 59:29that question routing it to the right
  1491. 59:30Source be it a given Vector store graph
  1492. 59:33DB um or SQL DB for example now we're
  1493. 59:37going to talk about the process of query
  1494. 59:38construction which is basically taking
  1495. 59:40natural language and converting it into
  1496. 59:42particular domain specific language uh
  1497. 59:45for one of these sources now we're going
  1498. 59:47to talk specifically about the process
  1499. 59:49of going from natural language to uh
  1500. 59:52meditated filters for Vector
  1501. 59:54Stores um the problem statement is
  1502. 59:56basically this let's imagine we had an
  1503. 59:58index of Lang Chain video transcripts um
  1504. 1:00:01you might want to ask a question give me
  1505. 1:00:04you know or find find me videos on chat
  1506. 1:00:06Lang chain published after 2024 for
  1507. 1:00:09example um the the process of query
  1508. 1:00:13structuring basically converts this
  1509. 1:00:15natural language question into a
  1510. 1:00:17structured query that can be applied to
  1511. 1:00:19the metadata uh filters on your vector
  1512. 1:00:22store so most Vector stores will have
  1513. 1:00:24some kind of meditative filters that can
  1514. 1:00:26do kind of structur querying on top of
  1515. 1:00:29uh the chunks that are indexed um so for
  1516. 1:00:32example this type of query will retrieve
  1517. 1:00:34all chunks uh that talk about the topic
  1518. 1:00:36of chat Lang chain uh published after
  1519. 1:00:39the date 2024 that's kind of the problem
  1520. 1:00:41statement and to do this we're going to
  1521. 1:00:43use function calling um in this case you
  1522. 1:00:46can use for example open AI or other
  1523. 1:00:48providers to do that and we're going to
  1524. 1:00:50do is at a high level take the metadata
  1525. 1:00:53fields that are present in our Vector
  1526. 1:00:55store and divide them to the model as
  1527. 1:00:57kind of information and the model then
  1528. 1:01:00can take those and produce queries that
  1529. 1:01:03adhere to the schema provided um and
  1530. 1:01:06then we can parse those out to a
  1531. 1:01:07structured object like a identic object
  1532. 1:01:10which again which can then be used in
  1533. 1:01:12search so that's kind of the problem
  1534. 1:01:14statement and let's actually walk
  1535. 1:01:15through
  1536. 1:01:17code um so here's our notebook which
  1537. 1:01:20we've kind of gone through previously
  1538. 1:01:22and I'll just show you as an example
  1539. 1:01:24let's take a example YouTube video and
  1540. 1:01:26let's look at the metadata that you get
  1541. 1:01:28with the transcript so you can see you
  1542. 1:01:30get stuff like description uh URL um
  1543. 1:01:34yeah publish date length things like
  1544. 1:01:36that now let's say we had an index that
  1545. 1:01:39had um basically a that had a number of
  1546. 1:01:42different metadata fields and filters uh
  1547. 1:01:46that allowed us to do range filtering on
  1548. 1:01:47like view count publication date the
  1549. 1:01:49video length um or unstructured search
  1550. 1:01:52on contents and title so those are kind
  1551. 1:01:54of like the imagine we had an index that
  1552. 1:01:56had uh those kind of filters available
  1553. 1:02:00to us what we can do is capture that
  1554. 1:02:03information about the available filters
  1555. 1:02:05in an object so we're calling that this
  1556. 1:02:06tutorial search object kind of
  1557. 1:02:08encapsulates that information about the
  1558. 1:02:10available searches that we can do and so
  1559. 1:02:12we basically enumerate it here content
  1560. 1:02:14search and title search or semantic
  1561. 1:02:15searches that can be done over those
  1562. 1:02:19fields um and then these filters then
  1563. 1:02:22are various types of structure searches
  1564. 1:02:24we can do on like the length um The View
  1565. 1:02:28count and so forth and so we can just
  1566. 1:02:30kind of build that object now we can set
  1567. 1:02:33this up really easily with a basic
  1568. 1:02:35simple prompt that says you know you're
  1569. 1:02:36an expert can bring natural language
  1570. 1:02:38into database queries you have access to
  1571. 1:02:40the database tutorial videos um given a
  1572. 1:02:43question return a database query
  1573. 1:02:44optimize retrieval so that's kind of it
  1574. 1:02:47now here's the key point though when you
  1575. 1:02:49call this LM with structured output
  1576. 1:02:51you're binding this pantic object which
  1577. 1:02:53contains all the information about our
  1578. 1:02:55index to the llm which is exactly what
  1579. 1:02:58we talked about previously it's really
  1580. 1:03:00this process right here you're taking
  1581. 1:03:02this object you're converting it to a
  1582. 1:03:04function schema for example open AI
  1583. 1:03:05you're binding that to your model and
  1584. 1:03:07then you're going to be able to get um
  1585. 1:03:10structured object out versus a Json
  1586. 1:03:13string from a natural language question
  1587. 1:03:15which can then be parsed into a pantic
  1588. 1:03:18object which you get out so that's
  1589. 1:03:19really the flow and it's taking
  1590. 1:03:21advantage of function calling as we said
  1591. 1:03:23so if we go back down we set up our
  1592. 1:03:26query analyzer chain right here now
  1593. 1:03:28let's try to run that just on a on a
  1594. 1:03:30purely semantic input so rag from
  1595. 1:03:32scratch let's run that and you can see
  1596. 1:03:35this just does like a Content search and
  1597. 1:03:36a title search that's exactly what you
  1598. 1:03:38would expect now if we pass a question
  1599. 1:03:41that includes like a date filter let's
  1600. 1:03:43just see if that would work
  1601. 1:03:45and there we go so you kind of still get
  1602. 1:03:48that semantic search um but you also get
  1603. 1:03:52um search over for example publish date
  1604. 1:03:54earliest and latest publish date kind of
  1605. 1:03:56as as you would expect let's try another
  1606. 1:03:58one here so videos focus on the topic of
  1607. 1:04:01chat Lang chain they're published before
  1608. 1:04:022024 this is just kind of a rewrite of
  1609. 1:04:04this question in slightly different way
  1610. 1:04:06using a different date filter and then
  1611. 1:04:08you can see we can get we get content
  1612. 1:04:10search title search and then we can get
  1613. 1:04:12kind of a date search so this is a very
  1614. 1:04:14general strategy that can be applied
  1615. 1:04:16kind of broadly to um different kinds of
  1616. 1:04:19querying you want to do it's really the
  1617. 1:04:21process of going from an unstructured
  1618. 1:04:23input to a structured query object out
  1619. 1:04:26following an arbitrary schema that you
  1620. 1:04:29provide and so as noted really this
  1621. 1:04:32whole thing we created here this
  1622. 1:04:33tutorial search is based upon the
  1623. 1:04:35specifics of our Vector store of
  1624. 1:04:37interest and if you want to learn more
  1625. 1:04:39about this I link to some documentation
  1626. 1:04:41here that talks a lot about different uh
  1627. 1:04:44types of of Integrations we have with
  1628. 1:04:46different Vector store providers to do
  1629. 1:04:47exactly this so it's a very useful trick
  1630. 1:04:50um it allows you to do kind of query uh
  1631. 1:04:54uh say metadata filter filtering on the
  1632. 1:04:56fly from a natural language question
  1633. 1:04:58it's a very convenient trick uh that
  1634. 1:05:01works with many different Vector DBS so
  1635. 1:05:03encourage you to play with it
  1636. 1:05:06thanks this is Lance from Lang chain I'm
  1637. 1:05:09going to talk about indexing uh and
  1638. 1:05:11mulation indexing in particular for the
  1639. 1:05:1412th part of our rag from scratch series
  1640. 1:05:17here so we previously talked about a few
  1641. 1:05:20different major areas we talk about
  1642. 1:05:22query translation which takes a question
  1643. 1:05:24and translates it in some way to
  1644. 1:05:26optimize for retrieval we talk about
  1645. 1:05:28routing which is the process of taking a
  1646. 1:05:30question routing it to the right data
  1647. 1:05:32source be it a vector store graph DB uh
  1648. 1:05:35SQL DB we talked about queer
  1649. 1:05:37construction we dug into uh basically
  1650. 1:05:39queer construction for Vector stores but
  1651. 1:05:41of course there's also text SQL text to
  1652. 1:05:44Cipher um so now we're going to talk
  1653. 1:05:46about indexing a bit in particular we're
  1654. 1:05:48going to talk about indexing indexing
  1655. 1:05:50techniques for Vector Stores um and I
  1656. 1:05:53want to highlight one particular method
  1657. 1:05:54today called multi-representation
  1658. 1:05:57indexing so the high LEL idea here is
  1659. 1:06:01derived a bit from a paper called
  1660. 1:06:03proposition indexing which kind of makes
  1661. 1:06:05a simple
  1662. 1:06:06observation you can think about
  1663. 1:06:10decoupling raw documents and the unit
  1664. 1:06:13you use for
  1665. 1:06:15retrieval so in the typical case you
  1666. 1:06:18take a document you split it up in some
  1667. 1:06:21way to index it and then you embed the
  1668. 1:06:24split directly
  1669. 1:06:26um this paper talks about actually
  1670. 1:06:29taking a document splitting it in some
  1671. 1:06:32way but then using an llm to produce
  1672. 1:06:35what they call a proposition which you
  1673. 1:06:37can think of as like kind of a
  1674. 1:06:38distillation of that split so it's kind
  1675. 1:06:41of like using an llm to modify that
  1676. 1:06:43split in some way to distill it or make
  1677. 1:06:45it like a crisper uh like summary so to
  1678. 1:06:49speak that's better optimized for
  1679. 1:06:51retrieval so that's kind of one
  1680. 1:06:52highlight one piece of intuition so we
  1681. 1:06:55actually taken that idea and we've kind
  1682. 1:06:57of built on it a bit in kind of a really
  1683. 1:06:59nice way that I think is very well
  1684. 1:07:01suited actually for long context llms so
  1685. 1:07:04the idea is pretty simple you take a
  1686. 1:07:07document and you you actually distill it
  1687. 1:07:10or create a proposition like they show
  1688. 1:07:11in the prior paper I kind of typically
  1689. 1:07:14think of this as just produce a summary
  1690. 1:07:16of the document and you embed that
  1691. 1:07:18summary so that summary is meant to be
  1692. 1:07:21optimized for retrieval so might contain
  1693. 1:07:23a bunch of keywords from the document or
  1694. 1:07:25like the big
  1695. 1:07:26ideas such that when you embed the
  1696. 1:07:29summary you embed a question you do
  1697. 1:07:31search you basically can find that
  1698. 1:07:34document based upon this highly
  1699. 1:07:35optimized summary for retrieval so
  1700. 1:07:38that's kind of represented here in your
  1701. 1:07:39vector store but here's the catch you
  1702. 1:07:42independently store the raw document in
  1703. 1:07:44a dock store and when you when you
  1704. 1:07:47basically retrieve the summary in the
  1705. 1:07:50vector store you return the full
  1706. 1:07:52document for the llm to perform
  1707. 1:07:54generation and this is a nice trick
  1708. 1:07:56because at generation time now with long
  1709. 1:07:59condex LMS for example the LM can handle
  1710. 1:08:02that entire document you don't need to
  1711. 1:08:03worry about splitting it or anything you
  1712. 1:08:05just simply use the summary to prod like
  1713. 1:08:08to create a really nice representation
  1714. 1:08:10for fishing out that full dock use that
  1715. 1:08:13full dock in generation there might be a
  1716. 1:08:14lot of reasons you want to do that you
  1717. 1:08:16want to make sure the LM has the full
  1718. 1:08:17context to actually answer the question
  1719. 1:08:19so that's the big idea it's a nice trick
  1720. 1:08:22and let's walk through some code
  1721. 1:08:23here we have a notebook all set up uh
  1722. 1:08:26just like before we done some pip
  1723. 1:08:28installs um set to maybe I Keys here for
  1724. 1:08:32lsmith um kind of here's a diagram now
  1725. 1:08:35let me show an example let's just load
  1726. 1:08:37two different uh blog posts uh one is
  1727. 1:08:39about agents one is about uh you know
  1728. 1:08:41human data quality um and what we're
  1729. 1:08:45going to do is let's create a summary of
  1730. 1:08:46each of those so this is kind of the
  1731. 1:08:48first step of that process where we're
  1732. 1:08:50going from like the raw documents to
  1733. 1:08:51summaries let's just have a look and
  1734. 1:08:53make sure those ran So Okay cool so the
  1735. 1:08:57first DOC discusses you know building
  1736. 1:08:58autonomous agents the second doc
  1737. 1:09:00contains the importance of high quality
  1738. 1:09:01human data and training okay so that's
  1739. 1:09:03pretty nice we have our summaries now
  1740. 1:09:06we're going to go through a process
  1741. 1:09:07that's pretty
  1742. 1:09:08simple first we Define a vector store
  1743. 1:09:10that's going to index those
  1744. 1:09:12summaries now we're going to Define what
  1745. 1:09:14we call like our our document storage is
  1746. 1:09:16going to store the full documents okay
  1747. 1:09:19so this multiv Vector retriever kind of
  1748. 1:09:21just pulls those two things together we
  1749. 1:09:23basically add our Dock Store we had this
  1750. 1:09:25bite store is basically the the the full
  1751. 1:09:28document store uh the vector store is
  1752. 1:09:30our Vector store um and now this ID is
  1753. 1:09:32what we're going to use to reference
  1754. 1:09:34between the chunks or the summaries and
  1755. 1:09:37the full documents that's really it so
  1756. 1:09:40now for every document we'll Define a
  1757. 1:09:42new Doc ID um and then we're basically
  1758. 1:09:44going to like take our summary documents
  1759. 1:09:47um and we're going to extract um for
  1760. 1:09:51each of our summaries we're going to get
  1761. 1:09:53the associated doc ID so we go um so
  1762. 1:09:58let's go ahead and do that so we have
  1763. 1:10:01our summary docs which we add to the
  1764. 1:10:03vector store we have our full documents
  1765. 1:10:06uh our doc IDs and the full raw
  1766. 1:10:08documents which are added to our doc
  1767. 1:10:10store and then let's just do a query
  1768. 1:10:12Vector store like a similarity search on
  1769. 1:10:14our Vector store so memory and agents
  1770. 1:10:16and we can see okay so we can extract
  1771. 1:10:19you know from the summaries we can get
  1772. 1:10:22for example the summary that pertains to
  1773. 1:10:24um a agents so that's a good thing now
  1774. 1:10:27let's go ahead and run a query get
  1775. 1:10:30relevant documents on our retriever
  1776. 1:10:32which basically combines the summaries
  1777. 1:10:35uh which we use for retrieval then the
  1778. 1:10:38doc store which we use to get the full
  1779. 1:10:40doc back so we're going to apply our
  1780. 1:10:42query we're going to basically run this
  1781. 1:10:45and here's the key Point we've gotten
  1782. 1:10:48back the entire
  1783. 1:10:49article um and we can actually if you
  1784. 1:10:53want to look at the whole thing we we
  1785. 1:10:55can just go ahead and do this here we go
  1786. 1:10:58so this is the entire article that we
  1787. 1:10:59get back from that search so it's a
  1788. 1:11:02pretty nice trick again we query with
  1789. 1:11:04just memory and agents um and we can
  1790. 1:11:07kind of go back to our diagram here we
  1791. 1:11:09quered for memory and agents it started
  1792. 1:11:11our summaries it found the summary
  1793. 1:11:13related to memory and agents it uses
  1794. 1:11:15that doc ID to reference between the
  1795. 1:11:17vector store and the doc store it fishes
  1796. 1:11:19out the right full doc returns us the
  1797. 1:11:21full document in this case the full web
  1798. 1:11:23page that's really it simple idea nice
  1799. 1:11:27way to go from basically like nice
  1800. 1:11:30simple proposition style or summary
  1801. 1:11:32style indexing to full document
  1802. 1:11:34retrieval which is very useful
  1803. 1:11:36especially with long contact LMS thank
  1804. 1:11:40you hi this is Lance from Lang chain
  1805. 1:11:43this is the 13th part of our rag from
  1806. 1:11:45scratch series focused on a technique
  1807. 1:11:47called
  1808. 1:11:48Raptor so Raptor sits within kind of an
  1809. 1:11:51array of different indexing techniques
  1810. 1:11:54that can be applied on Vector Stores um
  1811. 1:11:57we just talked about
  1812. 1:11:58multi-representation indexing um we I
  1813. 1:12:01priv a link to a video that's very good
  1814. 1:12:04talking about the different means of
  1815. 1:12:05chunking so I encourage you to look at
  1816. 1:12:06that and we're going to talk today about
  1817. 1:12:09a technique called Raptor which you can
  1818. 1:12:10kind of think of it as a technique for
  1819. 1:12:12hierarchical
  1820. 1:12:14indexing so the highle intuition is
  1821. 1:12:17this some questions require very
  1822. 1:12:20detailed information from a corpus to
  1823. 1:12:23answer like pertain to a single document
  1824. 1:12:25or single chunk so like we can call
  1825. 1:12:28those low-level
  1826. 1:12:29questions some questions require
  1827. 1:12:31consolidation across kind broad swast of
  1828. 1:12:34a document so across like many documents
  1829. 1:12:37or many chunks within a document and you
  1830. 1:12:39can call those like higher level
  1831. 1:12:41questions and so there's kind of this
  1832. 1:12:44challenge in retrieval and that
  1833. 1:12:46typically we do like K nearest neighbors
  1834. 1:12:48retrieval like we've been talking about
  1835. 1:12:50you're fishing out some number of chunks
  1836. 1:12:53but what if you have a question that
  1837. 1:12:55requires information across like five
  1838. 1:12:57six you know or a number of different
  1839. 1:12:59chunks which may exceed you know the K
  1840. 1:13:02parameter in your retrieval so again
  1841. 1:13:04when you typically do retrieval you
  1842. 1:13:06might set a k parameter of three which
  1843. 1:13:08means you're retrieving three chunks
  1844. 1:13:10from your vector store um and maybe you
  1845. 1:13:12have a high very high level question
  1846. 1:13:14that could benefit from infation across
  1847. 1:13:16more than three so this technique called
  1848. 1:13:18raptor is basically a way to build a
  1849. 1:13:21hierarchical index of document summaries
  1850. 1:13:25and the intuition is this you start with
  1851. 1:13:27a set of documents as your Leafs here on
  1852. 1:13:30the left you cluster them and then you
  1853. 1:13:33Summarize each cluster so each cluster
  1854. 1:13:36of similar documents um will consult
  1855. 1:13:39information from across your context
  1856. 1:13:42which is you know your context could be
  1857. 1:13:44a bunch of different splits or could
  1858. 1:13:45even be across a bunch of different
  1859. 1:13:47documents you're basically capturing
  1860. 1:13:49similar ones and you're consolidating
  1861. 1:13:51the information across them in a summary
  1862. 1:13:53and here's the interesting thing you do
  1863. 1:13:54that
  1864. 1:13:55recursively until either you hit like a
  1865. 1:13:57limit or you end up with one single
  1866. 1:13:59cluster that's a kind of very high level
  1867. 1:14:01summary of all of your
  1868. 1:14:03documents and what the paper shows is
  1869. 1:14:05that if you basically just collapse all
  1870. 1:14:08these and index them together as a big
  1871. 1:14:10pool you end up with a really nice array
  1872. 1:14:12of chunks that span the abstraction
  1873. 1:14:15hierarchy like you have a bunch of
  1874. 1:14:17chunks from Individual documents that
  1875. 1:14:19are just like more detailed chunks
  1876. 1:14:21pertaining to that you know single
  1877. 1:14:23document but you also have chunks from
  1878. 1:14:25these summaries or I would say like you
  1879. 1:14:27know maybe not chunks but in this case
  1880. 1:14:29the summary is like a distillation so
  1881. 1:14:31you know raw chunks on the left that
  1882. 1:14:33represent your leavs are kind of like
  1883. 1:14:35the rawest form of information either
  1884. 1:14:37raw chunks or raw documents and then you
  1885. 1:14:40have these higher level summaries which
  1886. 1:14:42are all indexed together so if you have
  1887. 1:14:44higher level questions they should
  1888. 1:14:46basically be more similar uh in sematic
  1889. 1:14:49search for example to these higher level
  1890. 1:14:50summary chunks if you have lower level
  1891. 1:14:53questions then they'll retrieve these
  1892. 1:14:55more lower level chunks and so you have
  1893. 1:14:56better semantic coverage across like the
  1894. 1:14:59abstraction hierarchy of question types
  1895. 1:15:01that's the intuition they do a bunch of
  1896. 1:15:02nice studies to show that this works
  1897. 1:15:04pretty well um I actually did a deep
  1898. 1:15:07dive video just on this which I link
  1899. 1:15:09below um I did want to cover it briefly
  1900. 1:15:12just at a very high level um so let's
  1901. 1:15:15actually just do kind of a code walkr
  1902. 1:15:17and I've added it to this rack from
  1903. 1:15:19scratch course notebook but I link over
  1904. 1:15:21to my deep dive video as well as the
  1905. 1:15:23paper and the the full code notebook
  1906. 1:15:27which is already checked in is discussed
  1907. 1:15:28at more length in the Deep dive the
  1908. 1:15:31technique is a little bit detailed so I
  1909. 1:15:33only want to give you very high levels
  1910. 1:15:35kind of overview here and you can look
  1911. 1:15:37at the Deep dive video if you want to go
  1912. 1:15:39in more depth again we talked through
  1913. 1:15:41this abstraction
  1914. 1:15:43hierarchy um I applied this to a large
  1915. 1:15:46set of Lang chain documents um so this
  1916. 1:15:49is me loading basically all of our Lang
  1917. 1:15:51chain expression language docs so this
  1918. 1:15:53is on the order of 30 documents you can
  1919. 1:15:55see I do a histogram here of the token
  1920. 1:15:57counts per document some are pretty big
  1921. 1:16:00most are fairly small less than you know
  1922. 1:16:024,000 tokens um and what I did is I
  1923. 1:16:05indexed all of them um individually so
  1924. 1:16:09the all those raw documents you can kind
  1925. 1:16:11of Imagine are here on the left and then
  1926. 1:16:14I do um I do embedding I do clustering
  1927. 1:16:18summarization and I do that recursively
  1928. 1:16:21um until I end up with in this case I
  1929. 1:16:24believe I only set like three levels of
  1930. 1:16:27recursion and then I save them all my
  1931. 1:16:29Vector store so that's like the highle
  1932. 1:16:31idea I'm applying this Raptor technique
  1933. 1:16:33to a whole bunch of Lang chain documents
  1934. 1:16:36um that have fairly large number of
  1935. 1:16:38tokens um so I do that um and yeah I use
  1936. 1:16:45actually use both CLA as well as open AI
  1937. 1:16:48here um this talks through the
  1938. 1:16:50clustering method which they that they
  1939. 1:16:52use which is pretty interesting you can
  1940. 1:16:54kind of dig into that on your own if if
  1941. 1:16:55you're really um interested this is a
  1942. 1:16:58lot of their code um which I cite
  1943. 1:17:00accordingly um this is basically
  1944. 1:17:02implementing the clustering method that
  1945. 1:17:03they use um and this is just simply the
  1946. 1:17:08document embedding stage um this is like
  1947. 1:17:12basically embedding uh and clustering
  1948. 1:17:15that's really it uh some text formatting
  1949. 1:17:19um summarizing of the clusters right
  1950. 1:17:22here um and then this is just running
  1951. 1:17:24that whole process recursively that's
  1952. 1:17:26really it um this is tree building so
  1953. 1:17:30basically I have the RO the rod docs
  1954. 1:17:33let's just go back and look at Doc texts
  1955. 1:17:35so this should be all my raw documents
  1956. 1:17:37uh so that's right you can see it here
  1957. 1:17:39doc text is basically just the text in
  1958. 1:17:41all those Lang chain documents that I
  1959. 1:17:43pulled
  1960. 1:17:45um and so I run this process on them
  1961. 1:17:49right
  1962. 1:17:50here uh so this is that recursive
  1963. 1:17:52embedding cluster basically runs and
  1964. 1:17:54produces is that tree here's the results
  1965. 1:17:57um this is me just going through the
  1966. 1:17:59results and basically adding the result
  1967. 1:18:02text to this list of uh texts um oh okay
  1968. 1:18:07so here's what I do this Leaf text is
  1969. 1:18:09all the raw documents and I'm appending
  1970. 1:18:12to that all the summaries that's all
  1971. 1:18:14it's going on and then I'm indexing them
  1972. 1:18:16all together that's the key Point rag
  1973. 1:18:19chain and there you have it that's
  1974. 1:18:21really all you do um so anyway I
  1975. 1:18:23encourage you to look at this in depth
  1976. 1:18:24it's a pretty interesting technique it
  1977. 1:18:26works well long with long contexts so
  1978. 1:18:29for example one of the arguments I made
  1979. 1:18:30is that it's kind of a nice approach to
  1980. 1:18:32consult information across like a span
  1981. 1:18:35of large
  1982. 1:18:36documents like in this particular case
  1983. 1:18:38my individual documents were lch
  1984. 1:18:40expression language docs uh each each
  1985. 1:18:42being somewhere in the order of you know
  1986. 1:18:44in this case like you know most of them
  1987. 1:18:46are less than 4,000 tokens some pretty
  1988. 1:18:48big but I index them all I cluster them
  1989. 1:18:51without any splits uh embed them cluster
  1990. 1:18:54them build this tree um and go from
  1991. 1:18:56there and it all works because we now
  1992. 1:18:58have llms that can go out to you know
  1993. 1:19:01100 or 200,000 up to million tokens and
  1994. 1:19:03Contex so you can actually just do this
  1995. 1:19:05process for big swats of documents in
  1996. 1:19:08place without any without any splitting
  1997. 1:19:10uh it's a pretty nice approach so I
  1998. 1:19:12encourage you to think about it look at
  1999. 1:19:13it watch the deep that video If you
  2000. 1:19:14really want to go deeper on this um
  2001. 1:19:20thanks hi this is Lance from Lang chain
  2002. 1:19:22this is the 14th part of our rag from
  2003. 1:19:24scratch series we're going to I'm going
  2004. 1:19:26to be talking about an approach called
  2005. 1:19:28cold
  2006. 1:19:29bear um so we've talked about a few
  2007. 1:19:34different approaches for indexing and
  2008. 1:19:37just as kind of a refresher indexing
  2009. 1:19:38Falls uh kind of right down here in our
  2010. 1:19:41flow we started initially with career
  2011. 1:19:43translation taking a question
  2012. 1:19:45translating it in some way to optimize
  2013. 1:19:47retrieval we talked about routing it to
  2014. 1:19:49a particular database we then talked
  2015. 1:19:51about query construction so going from
  2016. 1:19:53natural language to the DSL or domain
  2017. 1:19:56specific language for E any of the
  2018. 1:19:59databases that you want to work with
  2019. 1:20:01those are you know metadata filters for
  2020. 1:20:02Vector stores or Cipher
  2021. 1:20:05for graph DB or SQL for relational DB so
  2022. 1:20:09that's kind of the flow we talked about
  2023. 1:20:10today we talked about some indexing
  2024. 1:20:12approaches like multi-representation
  2025. 1:20:14indexing we gave a small shout out to
  2026. 1:20:16greet camer in the series on chunking uh
  2027. 1:20:19we talked about hierarchical indexing
  2028. 1:20:22and I want to include one Advanced kind
  2029. 1:20:24embedding approach so we talked a lot
  2030. 1:20:26about embeddings are obviously very
  2031. 1:20:28Central to semantic similarity search um
  2032. 1:20:31and
  2033. 1:20:32retrieval so one of the interesting
  2034. 1:20:35points that's been brought up is that
  2035. 1:20:38embedding models of course take a
  2036. 1:20:39document you can see here on the top and
  2037. 1:20:42embed it basically compress it to a
  2038. 1:20:45vector so it's kind of a compression
  2039. 1:20:48process you representing all the
  2040. 1:20:49semantics of that document in a single
  2041. 1:20:51Vector you're doing the same to your
  2042. 1:20:53question you're doing similarity search
  2043. 1:20:55between the question embedding and the
  2044. 1:20:57document embedding um in order to
  2045. 1:20:59perform retrieval you're typically
  2046. 1:21:01taking the you know K most similar um
  2047. 1:21:05document abetting is given a question
  2048. 1:21:07and that's really how you're doing it
  2049. 1:21:10now a lot of people said well hey the
  2050. 1:21:12compressing a full document with all
  2051. 1:21:13this Nuance to single Vector seems a
  2052. 1:21:15little bit um overly restrictive right
  2053. 1:21:18and this is a fair question to ask um
  2054. 1:21:21there's been some interesting approaches
  2055. 1:21:22to try to address that and one is this
  2056. 1:21:24this this approach method called Co bear
  2057. 1:21:28so the intuition is actually pretty
  2058. 1:21:30straightforward there's a bunch of good
  2059. 1:21:31articles I link down here this is my
  2060. 1:21:33little cartoon to explain it which I
  2061. 1:21:35think is hopefully kind of helpful but
  2062. 1:21:37here's the main idea instead of just
  2063. 1:21:39taking a document and compressing it
  2064. 1:21:41down to a single Vector basically single
  2065. 1:21:44uh what we might call embedding Vector
  2066. 1:21:46we take the document we break it up into
  2067. 1:21:49tokens so tokens are just like you know
  2068. 1:21:51units of of content it depends on the
  2069. 1:21:54token areas you use we talked about this
  2070. 1:21:56earlier so you basically tokenize it and
  2071. 1:21:59you produce basically an embedding or
  2072. 1:22:01vector for every token and there's some
  2073. 1:22:03kind of positional uh waiting that
  2074. 1:22:05occurs when you do this process so you
  2075. 1:22:08obviously you look to look at the
  2076. 1:22:09implementation understand the details
  2077. 1:22:10but the intuition is that you're
  2078. 1:22:12producing some kind of representation
  2079. 1:22:14for every token okay and you're doing
  2080. 1:22:17the same thing for your question so
  2081. 1:22:19you're taking your question you're
  2082. 1:22:20breaking into a tokens and you have some
  2083. 1:22:22representation or vector per token
  2084. 1:22:25and then what you're doing is for every
  2085. 1:22:27token in the question you're Computing
  2086. 1:22:29the similarity across all the tokens in
  2087. 1:22:33the document and you're finding the max
  2088. 1:22:36you're taking the max you're storing
  2089. 1:22:38that and you're doing that process for
  2090. 1:22:42all the tokens in the question so again
  2091. 1:22:45token two you compare it to every token
  2092. 1:22:48in the in the document compute the
  2093. 1:22:51Max and then the final score is in this
  2094. 1:22:54case the sum of the max similarities uh
  2095. 1:22:57between every question token and any
  2096. 1:23:00document token so it's an interesting
  2097. 1:23:03approach uh it reports very strong
  2098. 1:23:06performance latency is definitely a
  2099. 1:23:08question um so kind of production
  2100. 1:23:11Readiness is something you should look
  2101. 1:23:12into but it's a it's an approach that's
  2102. 1:23:14worth mentioning here uh because it's
  2103. 1:23:16pretty
  2104. 1:23:18interesting um and let's walk through
  2105. 1:23:20the
  2106. 1:23:21code
  2107. 1:23:22so there's actually nice Library called
  2108. 1:23:25rouille which makes it very easy to play
  2109. 1:23:27with Co bear um she's pip install it
  2110. 1:23:30here I've already done that and we can
  2111. 1:23:33use one of their pre-train models to
  2112. 1:23:35mediate this process so I'm basically
  2113. 1:23:36following their documentation this is
  2114. 1:23:38kind of what they recommended um so I'm
  2115. 1:23:40running this
  2116. 1:23:41now hopefully this runs somewhat quickly
  2117. 1:23:43I'm not sure I I previously have loaded
  2118. 1:23:45this model so hopefully it won't take
  2119. 1:23:47too long and yeah you can see it's
  2120. 1:23:48pretty quick uh I'm on a Mac M2 with 32
  2121. 1:23:51gigs um so just as like a context in
  2122. 1:23:54terms of my my system um this is from
  2123. 1:23:56their documentation we're just grabbing
  2124. 1:23:57a Wikipedia page this is getting a full
  2125. 1:23:59document on Miyazaki so that's cool
  2126. 1:24:03we're going to grab that now this is
  2127. 1:24:05just from their docs this is basically
  2128. 1:24:06how we create an index so we provide the
  2129. 1:24:09you know some index name the collection
  2130. 1:24:11um the max document length and yeah you
  2131. 1:24:13should look at their documentation for
  2132. 1:24:15these flags these are just the defaults
  2133. 1:24:16so I'm going to create my index um so I
  2134. 1:24:19get some logging here so it it's working
  2135. 1:24:21under the hood um and by the way I
  2136. 1:24:24actually have their documentation open
  2137. 1:24:26so you can kind of follow along um
  2138. 1:24:30so
  2139. 1:24:31um let's see yeah right about here so
  2140. 1:24:35you can kind of follow this indexing
  2141. 1:24:36process to create an index you need to
  2142. 1:24:37load a train uh a trained model this can
  2143. 1:24:41be either your own pre-train model or
  2144. 1:24:42one of ours from The Hub um and this is
  2145. 1:24:44kind of the process we're doing right
  2146. 1:24:45now create index is just a few lines of
  2147. 1:24:48code and this is exactly what we're
  2148. 1:24:49doing um so this is the you know my
  2149. 1:24:52documents and this is the indexing step
  2150. 1:24:54that we just we just kind of walk
  2151. 1:24:56through and it looks like it's done um
  2152. 1:24:58so you get a bunch of logging here
  2153. 1:25:00that's fine um now let's actually see if
  2154. 1:25:02this works so we're going to run drag
  2155. 1:25:04search what an emotion Studio did Miaki
  2156. 1:25:06found set our K parameter and we get
  2157. 1:25:09some results okay so it's running and
  2158. 1:25:13cool we get some documents out so you
  2159. 1:25:15know it seems to work now what's nice is
  2160. 1:25:17you can run this within lighting chain
  2161. 1:25:18as a liting chain retriever so that
  2162. 1:25:20basically wraps this as a lighting chain
  2163. 1:25:22Retriever and then you can use it freely
  2164. 1:25:24as a retriever within Lang chain it
  2165. 1:25:25works with all the other different LMS
  2166. 1:25:27and all the other components like
  2167. 1:25:28rankers and so forth that we talk
  2168. 1:25:30through so you can use this directly as
  2169. 1:25:32a retriever let's try this out and boom
  2170. 1:25:35nice and fast um and we get our
  2171. 1:25:38documents again this is a super simple
  2172. 1:25:40test example you should run this maybe
  2173. 1:25:42on more complex cases but it's pretty
  2174. 1:25:44pretty easy spin up it's a really
  2175. 1:25:45interesting alternative indexing
  2176. 1:25:47approach um using again like we talked
  2177. 1:25:49through um a very different algorithm
  2178. 1:25:53for computing do similarity that may
  2179. 1:25:55work better I think an interesting
  2180. 1:25:57regime to consider this would be longer
  2181. 1:25:59documents so if you want like longer um
  2182. 1:26:02yeah if if you basically want kind of
  2183. 1:26:04long context embedding I think you
  2184. 1:26:06should look into for example the uh Max
  2185. 1:26:09token limits for this approach because
  2186. 1:26:11it partitions the document into into
  2187. 1:26:13each token um I would be curious to dig
  2188. 1:26:16into kind of what the overall context
  2189. 1:26:18limits are for this approach of coar but
  2190. 1:26:21it's really interesting to consider and
  2191. 1:26:22it reports very strong performance so
  2192. 1:26:24again I encourage you to play with it
  2193. 1:26:26and this is just kind of an intro to how
  2194. 1:26:27to get set up and to start experimenting
  2195. 1:26:29with it really quickly
  2196. 1:26:32thanks hi this is Lance from Lang chain
  2197. 1:26:35I'm going to be talking about using
  2198. 1:26:36langra to build a diverse and
  2199. 1:26:39sophisticated rag
  2200. 1:26:40flows so just to set the stage the basic
  2201. 1:26:44rag flow you can see here starts with a
  2202. 1:26:47question retrieval of relevant documents
  2203. 1:26:49from an index which are passed into the
  2204. 1:26:52context window of an llm for generation
  2205. 1:26:54of an answer ground in your documents
  2206. 1:26:57that's kind of the basic
  2207. 1:26:59outline and we can see it's like a very
  2208. 1:27:01linear
  2209. 1:27:02path um in practice though you often
  2210. 1:27:06encounter a few different types of
  2211. 1:27:08questions like when do we actually want
  2212. 1:27:10to
  2213. 1:27:11retrieve based upon the context of the
  2214. 1:27:13question um are the retrieve documents
  2215. 1:27:16actually good or not and if they're not
  2216. 1:27:18good should we discard them and then how
  2217. 1:27:21do we loot back and retry retrieval with
  2218. 1:27:23for example and improved
  2219. 1:27:25question so these types of questions
  2220. 1:27:29motivate an idea of active rag which is
  2221. 1:27:32a process where an llm actually decides
  2222. 1:27:34when and where to retrieve based upon
  2223. 1:27:36like existing
  2224. 1:27:37retrievals or existing
  2225. 1:27:40Generations now when you think about
  2226. 1:27:43this there's a few different levels of
  2227. 1:27:45control that you have over an llm in a
  2228. 1:27:48rag
  2229. 1:27:49application the base case like we saw
  2230. 1:27:52with our chain is just use an llm to
  2231. 1:27:55choose a single steps output so for
  2232. 1:27:58example in traditional rag you feed it
  2233. 1:28:00documents and it decides to generation
  2234. 1:28:04so it's just kind of one step now a lot
  2235. 1:28:07of rag workflows will use the idea of
  2236. 1:28:09routing so like given a question should
  2237. 1:28:12I route it to a vector store or a graph
  2238. 1:28:16DB um and we have seen this quite a
  2239. 1:28:20bit now this newer idea that I want to
  2240. 1:28:23introduce
  2241. 1:28:24is how do we build more sophisticated
  2242. 1:28:28logical
  2243. 1:28:29flows um in a rag
  2244. 1:28:32pipeline um that you let the llm choose
  2245. 1:28:36between different steps but specify all
  2246. 1:28:41the transitions that are
  2247. 1:28:43available and this is known as we call a
  2248. 1:28:45state
  2249. 1:28:46machine now there's a few different
  2250. 1:28:50architectures that have emerged uh to
  2251. 1:28:53build different types of rag
  2252. 1:28:54chains and of course chains are
  2253. 1:28:58traditionally used just for like very
  2254. 1:28:59basic rag but this notion of State
  2255. 1:29:02machine is a bit newer and Lang graph
  2256. 1:29:06which we recently released provides a
  2257. 1:29:07really nice way to build State machines
  2258. 1:29:10for Rag and for other
  2259. 1:29:13things and the general idea here is that
  2260. 1:29:16you can lay out more diverse and
  2261. 1:29:18complicated rag
  2262. 1:29:20flows and then Implement them as graphs
  2263. 1:29:24and it kind of motivates this more broad
  2264. 1:29:26idea of of like flow engineering and
  2265. 1:29:28thinking through the actual like
  2266. 1:29:30workflow that you want and then
  2267. 1:29:32implementing it um and we're gonna
  2268. 1:29:35actually do that right now so I'm GNA Pi
  2269. 1:29:39a recent paper called CAG corrective rag
  2270. 1:29:42which is really a nice method um for
  2271. 1:29:46active rag that incorporates a few
  2272. 1:29:48different
  2273. 1:29:50ideas um so first you retrieve documents
  2274. 1:29:54and then you grade
  2275. 1:29:55them now if at least one document
  2276. 1:29:59exceeds the threshold for
  2277. 1:30:01relevance you go to generation you
  2278. 1:30:04generate your
  2279. 1:30:06answer um and it does this knowledge
  2280. 1:30:10refinement stage after that but let's
  2281. 1:30:13not worry about that for right now it's
  2282. 1:30:15kind of not essential for understanding
  2283. 1:30:16the basic flow here so again you do a
  2284. 1:30:20grade for relevance for every document
  2285. 1:30:23if any is relevant you
  2286. 1:30:25generate now if they're all ambiguous or
  2287. 1:30:29incorrect based upon your
  2288. 1:30:31grader you retrieve from an external
  2289. 1:30:34Source they use web
  2290. 1:30:36search and then they pass that as their
  2291. 1:30:39context for answer
  2292. 1:30:42generation so it's a really neat
  2293. 1:30:44workflow where you're doing retrieval
  2294. 1:30:46just like with basic rag but then you're
  2295. 1:30:48reasoning about the documents if they're
  2296. 1:30:50relevant go ahead and at least one is
  2297. 1:30:53relevant go ahead and generate if
  2298. 1:30:54they're not retrieve from alternative
  2299. 1:30:57source and then pack that into the
  2300. 1:30:59context and generate your
  2301. 1:31:02answer so let's see how we would
  2302. 1:31:04implement this as a estate machine using
  2303. 1:31:09Lang
  2304. 1:31:10graph um we'll make a few
  2305. 1:31:13simplifications
  2306. 1:31:15um we're going to first decide if any
  2307. 1:31:19documents are relevant we'll go ahead
  2308. 1:31:22and do the the web search
  2309. 1:31:25um to supplement the output so that's
  2310. 1:31:28just like kind of one minor
  2311. 1:31:29modification um we'll use tab search for
  2312. 1:31:32web search um we use Query writing to
  2313. 1:31:35optimize the search for uh to optimize
  2314. 1:31:38the web search but it follows a lot of
  2315. 1:31:40the the intuitions of the main paper uh
  2316. 1:31:44small note here we set the Tav API key
  2317. 1:31:48and another small mode I've already set
  2318. 1:31:50my lsmith API key um with which we'll
  2319. 1:31:54see is useful a bit later for observing
  2320. 1:31:57the resulting
  2321. 1:31:59traces now I'm going to index three blog
  2322. 1:32:02posts that I
  2323. 1:32:03like um I'm going to use chroma DB I'm G
  2324. 1:32:06use open ey embeddings I'm going to run
  2325. 1:32:09this right now this will create a vector
  2326. 1:32:11store for me from these three blog
  2327. 1:32:15posts and then what I'm going to do is
  2328. 1:32:19Define
  2329. 1:32:20State now this is kind of the core
  2330. 1:32:23object that going to be passed around my
  2331. 1:32:25graph that I'm going to
  2332. 1:32:27modify and right here is where I Define
  2333. 1:32:29it and the key point to note right now
  2334. 1:32:32is it's just a dictionary and it can
  2335. 1:32:35contain things that are relevant for rag
  2336. 1:32:37like question documents generation and
  2337. 1:32:40we'll see how we update that in in in a
  2338. 1:32:42little bit but the first thing to note
  2339. 1:32:44is we Define our state and this is
  2340. 1:32:47what's going to be modified in every Noe
  2341. 1:32:49of our
  2342. 1:32:50graph now here's really the Crux of it
  2343. 1:32:53and this is the thing I want to zoom in
  2344. 1:32:54on a little bit um
  2345. 1:32:58so when you kind of move from just
  2346. 1:33:00thinking about promps to thinking about
  2347. 1:33:02overall flows it it's like kind of a fun
  2348. 1:33:05and interesting exercise I kind of think
  2349. 1:33:07about this as it's been mentioned on
  2350. 1:33:09Twitter a little bit more like flow
  2351. 1:33:12engineering so let's think through what
  2352. 1:33:15was actually done in the paper and what
  2353. 1:33:19modifications to our state are going to
  2354. 1:33:21happen in each stage so we start with a
  2355. 1:33:24question you can see that on the far
  2356. 1:33:25left and this kind of state is represent
  2357. 1:33:28as a dictionary like we have we start
  2358. 1:33:29with a question we perform retrieval
  2359. 1:33:31from our Vector store which we just
  2360. 1:33:33created that's going to give us
  2361. 1:33:35documents so that's one node we made an
  2362. 1:33:38an adjustment to our state by adding
  2363. 1:33:41documents that's step
  2364. 1:33:43one now we have a second node where
  2365. 1:33:45we're going to grade the documents and
  2366. 1:33:48in this node we might filter some out so
  2367. 1:33:50we are making a modification to state
  2368. 1:33:52which is why it's a node so we're going
  2369. 1:33:54to have a
  2370. 1:33:55greater then we're going to have what
  2371. 1:33:58we're going to call a conditional Edge
  2372. 1:34:00so we saw we went from question to
  2373. 1:34:02retrieval retrieval always goes to
  2374. 1:34:05grading and now we have a
  2375. 1:34:07decision if any document is
  2376. 1:34:10irrelevant we're going to go ahead and
  2377. 1:34:14do web search to
  2378. 1:34:15supplement and if they're all relevant
  2379. 1:34:17will go to generation it's a minor kind
  2380. 1:34:20of a minor kind of logical uh decision
  2381. 1:34:23ision that we're going to
  2382. 1:34:25make um if any are not relevant we'll
  2383. 1:34:28transform the query and we'll do web
  2384. 1:34:31search and we'll use that for Generation
  2385. 1:34:33so that's really it and that's how we
  2386. 1:34:35can kind of think about our flow and how
  2387. 1:34:37our States can be modified throughout
  2388. 1:34:39this
  2389. 1:34:40flow now all we then need to do and I I
  2390. 1:34:44kind of found
  2391. 1:34:46spending 10 minutes thinking carefully
  2392. 1:34:48through your flow
  2393. 1:34:50engineering is really valuable because
  2394. 1:34:52from here it's really implementation
  2395. 1:34:55details um and it's pretty easy as
  2396. 1:34:58you'll see so basically I'm going to run
  2397. 1:35:02this code block but then we can like
  2398. 1:35:03walk through some of it I won't show you
  2399. 1:35:04everything so it'll get a little bit
  2400. 1:35:06boring but really all we're doing is
  2401. 1:35:09we're finding functions for every node
  2402. 1:35:12that take in the state and modify in
  2403. 1:35:15some way that's all it's going on so
  2404. 1:35:17think about retrieval we run retrieval
  2405. 1:35:19we take in state remember it's a dict we
  2406. 1:35:22get our state dick like this
  2407. 1:35:24we extract one keyy question from our
  2408. 1:35:26dick we pass that to a retriever we get
  2409. 1:35:29documents and we write back out State
  2410. 1:35:32now with documents key added that's
  2411. 1:35:36all generate going to be similar we take
  2412. 1:35:39in state now we have our question and
  2413. 1:35:41documents we pull in a prompt we Define
  2414. 1:35:44an llm we do minor post processing on
  2415. 1:35:47documents we set up a chain for
  2416. 1:35:49retrieval uh or sorry for Generation
  2417. 1:35:51which is just going to be take our
  2418. 1:35:52prompt pump Plum that to an llm
  2419. 1:35:55partially output a string and we run it
  2420. 1:35:57right here invoking our documents in our
  2421. 1:36:01question to get our answer we write that
  2422. 1:36:04back to State that's
  2423. 1:36:06it and you can kind of follow here for
  2424. 1:36:09every node we just Define a function
  2425. 1:36:12that performs the state modification
  2426. 1:36:13that we want to do on that
  2427. 1:36:15node grading documents is going to be
  2428. 1:36:17the same um in this case I do a little
  2429. 1:36:21thing extra here because I actually
  2430. 1:36:23Define a identic data model for my
  2431. 1:36:25grader so that the output of that
  2432. 1:36:28particular grading chain is a binary yes
  2433. 1:36:31or no you can look at the code make sure
  2434. 1:36:33it's all shared um and that just makes
  2435. 1:36:36sure that our output is is very
  2436. 1:36:38deterministic so that we then can down
  2437. 1:36:41here perform logical filtering so what
  2438. 1:36:45you can see here is um we Define this
  2439. 1:36:49search value no and we iterate through
  2440. 1:36:53our documents we grade them if any
  2441. 1:36:56document uh is graded as not relevant we
  2442. 1:37:00flag this search thing to yes that means
  2443. 1:37:04we're going to perform web search we
  2444. 1:37:06then add that to our state dict at the
  2445. 1:37:08end so run web search now that value is
  2446. 1:37:10true that's
  2447. 1:37:12it and you can kind of see we go through
  2448. 1:37:14some other nodes here there's web search
  2449. 1:37:16node um now here is where our one
  2450. 1:37:20conditional Edge we Define right here
  2451. 1:37:23this is where where we decide to
  2452. 1:37:24generate or not based on that search key
  2453. 1:37:27so we again get our state let's extract
  2454. 1:37:30the various values so we have this
  2455. 1:37:32search value now if search is yes we
  2456. 1:37:37return the next no that we want to go to
  2457. 1:37:40so in this case it'll be transform query
  2458. 1:37:42which will then go to web search else we
  2459. 1:37:46go to
  2460. 1:37:47generate so what we can see is we laid
  2461. 1:37:51out our graph which you can kind of see
  2462. 1:37:53up
  2463. 1:37:55here and now we Define functions for all
  2464. 1:37:57those nodes as well as the conditional
  2465. 1:38:01Edge and now we scroll down all we have
  2466. 1:38:05to do is just lay that out here again as
  2467. 1:38:08our flow and this is kind of what you
  2468. 1:38:10might think of as like kind of flow
  2469. 1:38:11engineering where you're just laying out
  2470. 1:38:13the graph as you drew it where we have
  2471. 1:38:17set our entry point as retrieve we're
  2472. 1:38:19adding an edge between retrieve and
  2473. 1:38:21grade documents so we went retrieval
  2474. 1:38:23grade documents we add our conditional
  2475. 1:38:25Edge depending on the grade either
  2476. 1:38:28transform the query go to web search or
  2477. 1:38:31just go to generate we create an edge
  2478. 1:38:34between transform the query and web
  2479. 1:38:35search then web search to generate and
  2480. 1:38:38then we also have an edge generate to
  2481. 1:38:40end and that's our whole graph that's it
  2482. 1:38:42so we can just run
  2483. 1:38:44this and now I'm going to ask a question
  2484. 1:38:47so let's just say um how does agent
  2485. 1:38:51memory work for example let's just try
  2486. 1:38:53that and what this is going to do is
  2487. 1:38:55going to print out what's going on as we
  2488. 1:38:58run through this graph so um first we
  2489. 1:39:01going to see output from
  2490. 1:39:03retrieve this is going to be all of our
  2491. 1:39:05documents that we retrieved so that's
  2492. 1:39:07that's fine this just from our our
  2493. 1:39:08retriever then you can see that we're
  2494. 1:39:11doing a relevance check across our
  2495. 1:39:13documents and this is kind of
  2496. 1:39:15interesting right you can see we grading
  2497. 1:39:17them here one is grade as not
  2498. 1:39:20relevant um and okay you can see the
  2499. 1:39:23documents are now filtered because we
  2500. 1:39:24removed the one that's not relevant and
  2501. 1:39:26because one is not relevant we decide
  2502. 1:39:29okay we're going to just transform the
  2503. 1:39:31query and run web
  2504. 1:39:33search and um you can see after query
  2505. 1:39:37transformation we rewrite the question
  2506. 1:39:39slightly we then run web
  2507. 1:39:42search um and you can see from web
  2508. 1:39:44search it searched from some additional
  2509. 1:39:47sources um which you can actually see
  2510. 1:39:50here it's
  2511. 1:39:51appended as a so here it is so here it's
  2512. 1:39:55a new document appended from web search
  2513. 1:39:57which is from memory knowledge
  2514. 1:39:59requirements so it it basically looked
  2515. 1:40:01up some AI architecture related to
  2516. 1:40:03memory uh web results so that's fine
  2517. 1:40:06that's exactly what we want to
  2518. 1:40:08do and then um we generate a
  2519. 1:40:11response so that's great and this is
  2520. 1:40:14just showing you everything in kind of
  2521. 1:40:15gory detail but I'm going to show you
  2522. 1:40:18one other thing that's that's really
  2523. 1:40:19nice about this if I go to lsmith
  2524. 1:40:24I have my AP I ke set so all my
  2525. 1:40:26Generations are just logged to to lsmith
  2526. 1:40:29and I can see my Lang graph run here now
  2527. 1:40:33what's really cool is this shows me all
  2528. 1:40:36of my nodes so remember we had retrieve
  2529. 1:40:42grade we evaluated the grade because one
  2530. 1:40:45was irrelevant we then went ahead and
  2531. 1:40:47transformed the query we did a web
  2532. 1:40:50search we pended that to our context you
  2533. 1:40:52can see all those steps are laid out
  2534. 1:40:54here in fact you can even look at every
  2535. 1:40:56single uh grader and its output I will
  2536. 1:40:59move this up
  2537. 1:41:01slightly um so you can see the the
  2538. 1:41:04different scores for grades okay so this
  2539. 1:41:06particular retrieval was graded as as
  2540. 1:41:09not relevant so that's fine that that
  2541. 1:41:11can happen in some cases and because of
  2542. 1:41:15that um we did a query transformation so
  2543. 1:41:18we modified the question slightly how
  2544. 1:41:21does memory how does the memory system
  2545. 1:41:23an artificial agents function so it's
  2546. 1:41:25just a minor rephrasing of the question
  2547. 1:41:27we did this Tav web search this is where
  2548. 1:41:30it queried from this particular blog
  2549. 1:41:33post from medium so it's like a sing web
  2550. 1:41:35query we can like sanity check it and
  2551. 1:41:37then what's need is we can go to our
  2552. 1:41:39generate step look at open Ai and here's
  2553. 1:41:41our full prompt how does the memory
  2554. 1:41:43system in our official agents function
  2555. 1:41:46and then here's all of our documents so
  2556. 1:41:49this is the this is the web search as
  2557. 1:41:51well as we still have the Rel chunks
  2558. 1:41:54that were retrieved from our blog posts
  2559. 1:41:57um and then here's our answer so that's
  2560. 1:42:01really it you can see how um really
  2561. 1:42:05moving from the notion of just like I'll
  2562. 1:42:09actually go back to the original um
  2563. 1:42:12moving
  2564. 1:42:14from uh I will try to open this up a
  2565. 1:42:17little
  2566. 1:42:18bit um
  2567. 1:42:24yeah I can see my face
  2568. 1:42:26still um the transition from laying out
  2569. 1:42:33simple
  2570. 1:42:35chains to
  2571. 1:42:38flows is a really interesting and
  2572. 1:42:40helpful way of thinking about why graphs
  2573. 1:42:42are really interesting because you can
  2574. 1:42:45encode more sophisticated logical
  2575. 1:42:48reasoning
  2576. 1:42:49workflows but in a
  2577. 1:42:51very like clean and well-engineered way
  2578. 1:42:56where you can specify all the
  2579. 1:42:58transitions that you actually want to
  2580. 1:43:00have
  2581. 1:43:01executed um and I actually find this way
  2582. 1:43:05of thinking and building kind of logical
  2583. 1:43:08uh like workflows really
  2584. 1:43:10intuitive um we have a blog post coming
  2585. 1:43:13out uh tomorrow that discusses both
  2586. 1:43:17implementing self rag as well as C rag
  2587. 1:43:20for two different active rag approaches
  2588. 1:43:22using using uh this idea of of State
  2589. 1:43:25machines and Lang graph um so I
  2590. 1:43:28encourage you to play with it uh I found
  2591. 1:43:31it really uh intuitive to work with um I
  2592. 1:43:35also found uh inspection of traces to be
  2593. 1:43:38quite intuitive using Lang graph because
  2594. 1:43:43every node is enumerated pretty clearly
  2595. 1:43:46for you which is not always the case
  2596. 1:43:48when you're using other types of of more
  2597. 1:43:50complex reasoning approaches for example
  2598. 1:43:52like agents so in any case um I hope
  2599. 1:43:56this was helpful and I definitely
  2600. 1:43:58encourage you to check out um kind of
  2601. 1:44:00this notion of like flow engineering
  2602. 1:44:02using Lang graph and in the context of
  2603. 1:44:04rag it can be really powerful hopefully
  2604. 1:44:06as you've seen here thank
  2605. 1:44:09you hey this is Lance from Lang chain I
  2606. 1:44:12want to talk to a recent paper that I
  2607. 1:44:14saw called adaptive rag which brings
  2608. 1:44:16together some interesting ideas that
  2609. 1:44:17have kind of been covered in other
  2610. 1:44:18videos but this actually ties them all
  2611. 1:44:21together in kind of a fun way so the the
  2612. 1:44:23two big ideas to talk about here are one
  2613. 1:44:27of query analysis so we've actually done
  2614. 1:44:30kind of a whole rag from scratch series
  2615. 1:44:31that walks through each of these things
  2616. 1:44:32in detail but this is a very nice
  2617. 1:44:35example of how this comes together um
  2618. 1:44:37with some other ideas we've been talking
  2619. 1:44:39about so query analysis is typically the
  2620. 1:44:41process of taking an input question and
  2621. 1:44:44modifying in some way uh to better
  2622. 1:44:46optimize retrieval there's a bunch of
  2623. 1:44:48different methods for this it could be
  2624. 1:44:50decomposing it into sub questions it
  2625. 1:44:52could be using some clever techniques
  2626. 1:44:54like stepb back prompting um but that's
  2627. 1:44:57kind of like the first stage of query
  2628. 1:44:59analysis then typically you can do
  2629. 1:45:01routing so you route a question to one
  2630. 1:45:03of multiple potential sources it could
  2631. 1:45:05be one or two different Vector stores it
  2632. 1:45:08could be relational DB versus Vector
  2633. 1:45:10store it could be web search it could
  2634. 1:45:12just be like an llm fallback right so
  2635. 1:45:15this is like one kind of big idea query
  2636. 1:45:17analysis right it's kind of like the
  2637. 1:45:18front end of your rag pipeline it's
  2638. 1:45:20taking your question it's modifying it
  2639. 1:45:22in some way it's sending it to the right
  2640. 1:45:24place be it a web search be it a vector
  2641. 1:45:26store be it a relational DB so that's
  2642. 1:45:29kind of topic one now topic two is
  2643. 1:45:32something that's been brought up in a
  2644. 1:45:34few other videos um of what I kind of
  2645. 1:45:36call Flow engineering or adaptive rag
  2646. 1:45:40which is the idea of doing tests in your
  2647. 1:45:43rag pipeline or in your rag inference
  2648. 1:45:45flow uh to do things like check
  2649. 1:45:47relevance documents um check whether or
  2650. 1:45:51not the answer contains hallucinations
  2651. 1:45:53so this recent blog post from Hamil
  2652. 1:45:55Hussein actually covers evaluation in in
  2653. 1:45:58some really nice detail and one of the
  2654. 1:46:00things he highlighted
  2655. 1:46:01explicitly is actually this topic so he
  2656. 1:46:04talks about unit tests and in particular
  2657. 1:46:06he says something really interesting
  2658. 1:46:08here he says you know unlike typical
  2659. 1:46:09unit tests you want to organize these
  2660. 1:46:11assertions in places Beyond typical unit
  2661. 1:46:14testing such as data cleaning and here's
  2662. 1:46:17the key Point automatic retries during
  2663. 1:46:19model inference that's the key thing I
  2664. 1:46:21want to like draw your attention to to
  2665. 1:46:23it's a really nice approach we've talked
  2666. 1:46:25about some other papers that do that
  2667. 1:46:26like corrective rag self rag but it's
  2668. 1:46:28also cool to see it here and kind of
  2669. 1:46:30encapsulated in this way the main idea
  2670. 1:46:33is that you're using kind of unit tests
  2671. 1:46:34in your flow to make Corrections like if
  2672. 1:46:38your retrieval is bad you can correct
  2673. 1:46:39from that if your generation has
  2674. 1:46:41hallucinations you can correct from that
  2675. 1:46:43so I'm going to kind of draw out like a
  2676. 1:46:45cartoon diagram of what we're going to
  2677. 1:46:47do here and you can kind of see it here
  2678. 1:46:50we're starting with a question we talked
  2679. 1:46:52about query analysis we're going to take
  2680. 1:46:53our question and we're going to decide
  2681. 1:46:55where it needs to go and for this
  2682. 1:46:57particular toy example I'm going to say
  2683. 1:46:59either send it to a vector store send it
  2684. 1:47:01to web search or just have the llm
  2685. 1:47:03answer it right so that's like kind of
  2686. 1:47:05my fallback
  2687. 1:47:06Behavior then we're going to bring in
  2688. 1:47:08that idea of kind of online flow
  2689. 1:47:10engineering or unit testing where I'm
  2690. 1:47:13going to have my retrieval either from
  2691. 1:47:15the VOR store or web search I'm then
  2692. 1:47:17going to ask is this actually relevant
  2693. 1:47:19to the question if it isn't I'm actually
  2694. 1:47:21going to kick back to web sech so this
  2695. 1:47:23is a little bit more relevant in the
  2696. 1:47:24case if I've routed to to the vector
  2697. 1:47:27store done retrieval documents aren't
  2698. 1:47:29relevant I'll have a fallback
  2699. 1:47:32mechanism um then I'm going to generate
  2700. 1:47:34I check for hallucinations in my
  2701. 1:47:36generation and then I check for um for
  2702. 1:47:39whether or not the the generation
  2703. 1:47:41actually answers the question then I
  2704. 1:47:42return my answer so again we're tying
  2705. 1:47:44together two ideas one is query analysis
  2706. 1:47:47like basically taking a question routing
  2707. 1:47:49it to the right place modifying it as
  2708. 1:47:51needed and then kind of online unit
  2709. 1:47:54testing and iterative flow
  2710. 1:47:56feedback so to do this I've actually
  2711. 1:48:00heard a lot of people talk online about
  2712. 1:48:02command r a new model release from gooh
  2713. 1:48:04here it has some pretty nice properties
  2714. 1:48:07that I was kind of reading about
  2715. 1:48:08recently so one it has nice support for
  2716. 1:48:11Tool use and it does support query
  2717. 1:48:14writing in the context of tool use uh so
  2718. 1:48:18this all rolls up in really nice
  2719. 1:48:20capabilities for routing it's kind of
  2720. 1:48:22one now two it's small it's 35 billion
  2721. 1:48:26parameter uh it's actually open weight
  2722. 1:48:28so you can actually run this locally and
  2723. 1:48:30I've tried that we can we can talk about
  2724. 1:48:31that later uh so and it's also fast
  2725. 1:48:34served via the API so it's kind of a
  2726. 1:48:36small model and it's well tuned for rag
  2727. 1:48:39so I heard a lot of people talking about
  2728. 1:48:40using coher for Rag and it has a large
  2729. 1:48:43context 120,000 tokens so this like a
  2730. 1:48:45nice combination of properties it
  2731. 1:48:47supports to and routing it's small and
  2732. 1:48:49fast so it's like quick for grading and
  2733. 1:48:51it's well tuned for rag so it's actually
  2734. 1:48:54a really nice fit for this particular
  2735. 1:48:55workflow where I want to do query
  2736. 1:48:57analysis and routing and I want to do
  2737. 1:48:59kind of online checking uh and rag so
  2738. 1:49:02kind of there you go now let's just get
  2739. 1:49:05to the coding bit so I have a notebook
  2740. 1:49:07kind of like usual I've done a few pip
  2741. 1:49:09installs you can see it's nothing exotic
  2742. 1:49:11I'm bringing Lang chain coh here I set
  2743. 1:49:13my coher API key now I'm just going to
  2744. 1:49:16set this Lang chain project within
  2745. 1:49:18lsmith so all my traces for this go to
  2746. 1:49:20that project and I have enabled tracing
  2747. 1:49:23so I'm using Langs Smith here so we're
  2748. 1:49:25going to walk through this flow and
  2749. 1:49:27let's do the first thing let's just
  2750. 1:49:28build a vector store so I'm going to
  2751. 1:49:30build a vector store using coherent
  2752. 1:49:32beddings with chroma open source Vector
  2753. 1:49:34DB runs locally from three different web
  2754. 1:49:37pages on blog post that I like so it
  2755. 1:49:39pertains to agents prompt engineering
  2756. 1:49:41and adversarial attacks so now I have a
  2757. 1:49:43retriever I can run retriever invoke and
  2758. 1:49:46I can ask a question about you know
  2759. 1:49:48agent
  2760. 1:49:50memory agent
  2761. 1:49:53memory and there we go so we get
  2762. 1:49:56documents back so there we go we have a
  2763. 1:49:58retriever now now here's where I'm going
  2764. 1:50:01to bring in coh here I also want a
  2765. 1:50:03router so you look at our flow the first
  2766. 1:50:06step is this routing stage right so what
  2767. 1:50:08I'm going to do is I'm guess we going to
  2768. 1:50:10find two tools a web search tool and a
  2769. 1:50:14vector store tool okay in my Preamble is
  2770. 1:50:17just going to say you're an expert
  2771. 1:50:18routing user questions to Vector store
  2772. 1:50:20web search now here's the key I tell it
  2773. 1:50:23what the vector store has so again my
  2774. 1:50:25index my Vector store has agents prompt
  2775. 1:50:28engineering adial tax I just repeat that
  2776. 1:50:30here agents prompt adversarial tax so it
  2777. 1:50:32knows what's in the vector store um so
  2778. 1:50:35use it for questions on these topics
  2779. 1:50:36otherwise use web search so that's it I
  2780. 1:50:40use command R here now I'm going to bind
  2781. 1:50:42these tools to the model and attach the
  2782. 1:50:44Preamble and I have a structured LM
  2783. 1:50:47router so let's give it a let's give
  2784. 1:50:49this a few tests just to like kind of
  2785. 1:50:51sandbox this a little bit
  2786. 1:50:53so I can inval here's my chain I have a
  2787. 1:50:55router prompt I pass that to the
  2788. 1:50:56structured LM router which I defined
  2789. 1:50:58right here and um let's ask a few
  2790. 1:51:01different questions like who will the
  2791. 1:51:02Bears draft in the NFL draft with types
  2792. 1:51:05of agent memory and Hi how are you so
  2793. 1:51:08I'm going to kick that off and you can
  2794. 1:51:10see you know it does web search it does
  2795. 1:51:13it goes to Vector store and then
  2796. 1:51:14actually returns this false so that's
  2797. 1:51:16kind of interesting um this is actually
  2798. 1:51:19just
  2799. 1:51:20saying if it does not use either tool so
  2800. 1:51:23for that particular query web search or
  2801. 1:51:26the vector store was inappropriate it'll
  2802. 1:51:28just say hey I didn't call one of those
  2803. 1:51:30tools so that's interesting we'll use
  2804. 1:51:31that later so that's my router tool now
  2805. 1:51:35the second thing is my grader and here's
  2806. 1:51:38where I want to show you something
  2807. 1:51:40really nice that is generally useful uh
  2808. 1:51:43for many different problems you might
  2809. 1:51:45encounter so here's what I'm doing I'm
  2810. 1:51:48defining a data model uh for My Grade so
  2811. 1:51:51basically grade documents it's going to
  2812. 1:51:54have this is a pantic object it is just
  2813. 1:51:57basically a binary score here um field
  2814. 1:52:00specified here uh documents are relevant
  2815. 1:52:03to the question yes no I have a preamble
  2816. 1:52:05your grer assessing relevance of
  2817. 1:52:06retrieve documents to a user question um
  2818. 1:52:09blah blah blah so you know and then
  2819. 1:52:11basically give it a b score yes no I'm
  2820. 1:52:13using command R but here's the catch I'm
  2821. 1:52:16using this wi structured outputs thing
  2822. 1:52:18and I'm passing my grade documents uh
  2823. 1:52:21data model to that that so this is the
  2824. 1:52:23key thing we can test this right now as
  2825. 1:52:26well it's going to return an object
  2826. 1:52:29based on the schema I give it which is
  2827. 1:52:31extremely useful for all sorts of use
  2828. 1:52:33cases and let's actually Zoom back up so
  2829. 1:52:36we're actually right here so this
  2830. 1:52:39greater stage right I want to constrain
  2831. 1:52:41the output to yes no I don't want any
  2832. 1:52:43preambles I want anything because the
  2833. 1:52:45logic I'm going to build in this graph
  2834. 1:52:48is going to require a yes no binary
  2835. 1:52:50response from this particular Edge in
  2836. 1:52:52our graph
  2837. 1:52:53so that's why this greater tool is
  2838. 1:52:55really
  2839. 1:52:56useful and I'm asking like a mock
  2840. 1:52:59question types of agent memory I do a
  2841. 1:53:01retriever I do a retrieval from our
  2842. 1:53:03Vector store I get the tuck and I test
  2843. 1:53:05it um I invoke our greater retrieval
  2844. 1:53:08grater chain with the question the doc
  2845. 1:53:11text and it's relevant as we would
  2846. 1:53:13expect so that's good but again let's
  2847. 1:53:16just kind of look at that a little bit
  2848. 1:53:17more closely what's actually happening
  2849. 1:53:19under the hood here here's the pantic
  2850. 1:53:21object we passed
  2851. 1:53:23here's the document in question I'm
  2852. 1:53:24providing basically it's converting this
  2853. 1:53:26object into coher function schema it's
  2854. 1:53:29binding that to the
  2855. 1:53:31llm we pass in the document question it
  2856. 1:53:34returns an object basic a Json string
  2857. 1:53:37per our pantic schema that's it and then
  2858. 1:53:41it's just going to like parse that into
  2859. 1:53:42a pantic object which we get at the end
  2860. 1:53:44of the day so that's what's happening
  2861. 1:53:45under the hood with this with structured
  2862. 1:53:47output thing but it's extremely useful
  2863. 1:53:49and you'll see we're going to use that a
  2864. 1:53:50few different places um um because we
  2865. 1:53:53want to ensure that in our in our flow
  2866. 1:53:57here we have three different grading
  2867. 1:53:58steps and each time we want to constrain
  2868. 1:54:00the output to yes no we're going to use
  2869. 1:54:02that structured output more than
  2870. 1:54:04once um this is just my generation so
  2871. 1:54:07this is good Old Rag let's just make
  2872. 1:54:09sure that works um I'm using rag chain
  2873. 1:54:12typical rag prompt again I'm using
  2874. 1:54:15cohere for rag pretty easy and yeah so
  2875. 1:54:19the rag piece works that's totally fine
  2876. 1:54:21nothing to it crazy there um I'm going
  2877. 1:54:24to find this llm fallback so this is
  2878. 1:54:27basically if you saw a router chain if
  2879. 1:54:31it doesn't use a tool I want to fall
  2880. 1:54:33back and just fall back to the llm so
  2881. 1:54:35I'm going to kind of build that as a
  2882. 1:54:37little chain here so okay this is just a
  2883. 1:54:39fallback I have my Preamble just you're
  2884. 1:54:42you're an assistant answer the question
  2885. 1:54:43based upon your internal knowledge so
  2886. 1:54:46again that fallback behavior is what we
  2887. 1:54:48have here so what we've done already is
  2888. 1:54:51we defined our router piece we've
  2889. 1:54:53defined our our basic retrieval our
  2890. 1:54:56Vector store we already have here um
  2891. 1:54:58we've defined our first logic or like
  2892. 1:55:01grade check and we defined our fallback
  2893. 1:55:04and we're just kind of roll through the
  2894. 1:55:05parts of our graph and Define each piece
  2895. 1:55:08um so I'm going to have two other
  2896. 1:55:10graders and they're going to use the
  2897. 1:55:11same thing we just talked about slightly
  2898. 1:55:14different data model I mean same output
  2899. 1:55:16but actually just slightly different uh
  2900. 1:55:19prompt um and you know descript destion
  2901. 1:55:22this in this case is the aners grounded
  2902. 1:55:24the facts yes no this is my
  2903. 1:55:25hallucination
  2904. 1:55:26grater uh and then I have an answer
  2905. 1:55:28grader as well and I've also run a test
  2906. 1:55:31on each one and you can see I'm getting
  2907. 1:55:32binary this this these objects out have
  2908. 1:55:35a binary score so this a pantic object
  2909. 1:55:38with a binary score uh and that's
  2910. 1:55:39exactly what we want cool
  2911. 1:55:43and I have a Search tool so that's
  2912. 1:55:46really nice we've actually gone through
  2913. 1:55:48and we've kind of laid out I have like a
  2914. 1:55:50router I've tested it we have a vector
  2915. 1:55:51story tested we've tested each of our
  2916. 1:55:54graders here we've also tested
  2917. 1:55:55generation of just doing rag so we have
  2918. 1:55:58a bunch of pieces built here we have a
  2919. 1:56:00fallback piece we have web search now
  2920. 1:56:02the question is how do I Stitch these
  2921. 1:56:04together into this kind of flow and for
  2922. 1:56:06that I I like to use Lang graph we'll
  2923. 1:56:09talk a little about Lang graph versus
  2924. 1:56:10agents a bit later but I want to show
  2925. 1:56:12you why this is really easy to do using
  2926. 1:56:14Lang graph so what's kind of nice is
  2927. 1:56:17I've kind of laid out all my logic here
  2928. 1:56:19we've tested individually and now all
  2929. 1:56:21I'm going to do
  2930. 1:56:22is I'm going to first lay out uh the
  2931. 1:56:25parts of my graph so what you're going
  2932. 1:56:27to notice here is first there's a graph
  2933. 1:56:30state so this state represents kind of
  2934. 1:56:32the key parts of the graph or the key
  2935. 1:56:35kind of objects within the graph that
  2936. 1:56:36we're going to be modifying so this is
  2937. 1:56:38basically a graph centered around rag
  2938. 1:56:40we're going to have question generation
  2939. 1:56:42and documents that's really kind of the
  2940. 1:56:44main things we're going to be working
  2941. 1:56:45with in our
  2942. 1:56:46graph so then you're going to see
  2943. 1:56:49something that's pretty intuitive I
  2944. 1:56:51think what you're going to see is we're
  2945. 1:56:53going to basically walk through this
  2946. 1:56:55flow and for each of these little
  2947. 1:56:57circles we're just going to find a
  2948. 1:56:58function and these uh little squares or
  2949. 1:57:02these these you can think about every
  2950. 1:57:04Circle as a node and every kind of
  2951. 1:57:06diamond here as as an edge or
  2952. 1:57:08conditional Edge so that's actually what
  2953. 1:57:10we're going to do right now we're going
  2954. 1:57:11to lay out all of our nodes and edges
  2955. 1:57:14and each one of them are just going to
  2956. 1:57:15be a function and you're going to see
  2957. 1:57:16how we do that right now so I'm going to
  2958. 1:57:20go down here I def find my graph state
  2959. 1:57:22so this is what's going to be kind of
  2960. 1:57:23modified and propagated throughout my
  2961. 1:57:24graph now all I'm going to do is I'm
  2962. 1:57:27just going to find a function uh for
  2963. 1:57:29each of those nodes so let me kind of go
  2964. 1:57:32side by side and show you the diagram
  2965. 1:57:33and then like kind of show the nodes
  2966. 1:57:35next to it so here's the
  2967. 1:57:38diagram so we have uh a retrieve node so
  2968. 1:57:42that kind of represents our Vector store
  2969. 1:57:43we have a fallback node that's this
  2970. 1:57:46piece we have a generate node so that's
  2971. 1:57:49basically going to do our rag you can
  2972. 1:57:51see there we have a grade documents node
  2973. 1:57:54kind of right
  2974. 1:57:55here um and we have a web search node so
  2975. 1:57:58that's right here cool now here's where
  2976. 1:58:02we're actually to find the edges so you
  2977. 1:58:04can see our edges are the pieces of the
  2978. 1:58:05graph that are kind of making different
  2979. 1:58:06decisions so this route question Edge
  2980. 1:58:09basic conditional Edge is basically
  2981. 1:58:12going to take an input question and
  2982. 1:58:14decide where it needs to go and that's
  2983. 1:58:15all we're doing down here it kind of
  2984. 1:58:18follows what we did up at the top where
  2985. 1:58:20we tested this individually so recall we
  2986. 1:58:23basically just invoke that question
  2987. 1:58:24router returns our source now remember
  2988. 1:58:27if tool calls were not in the source we
  2989. 1:58:30do our fall back so we show actually
  2990. 1:58:32showed that all the way up here remember
  2991. 1:58:34this if tool calls is not in the
  2992. 1:58:36response this thing will just be false
  2993. 1:58:38so that means we didn't either we didn't
  2994. 1:58:39call web search and we didn't call uh
  2995. 1:58:43our retriever tool so then we're just
  2996. 1:58:45going to fall
  2997. 1:58:46back
  2998. 1:58:48um yep right here and this is just like
  2999. 1:58:53uh you know a catch just in case a tool
  3000. 1:58:54could make a decision but most
  3001. 1:58:57interestingly here's where we choose a
  3002. 1:58:59data source basically so um this is the
  3003. 1:59:02output of our tool call we're just going
  3004. 1:59:05to fish out the name of the tool so
  3005. 1:59:08that's data source and then here we go
  3006. 1:59:10if the data source is web search I'm
  3007. 1:59:12returning web search as basically the
  3008. 1:59:14next node to go to um otherwise if it's
  3009. 1:59:18Vector store we return Vector store as
  3010. 1:59:19the next node to go to so what's this
  3011. 1:59:22search thing well remember we right up
  3012. 1:59:24here Define this node web search that's
  3013. 1:59:27it we're just going to go to that node
  3014. 1:59:30um what's this Vector store um you'll
  3015. 1:59:32see below how we can kind of tie these
  3016. 1:59:35strings that we returned from the
  3017. 1:59:36conditional Edge to the node we want to
  3018. 1:59:38go to that's really it um same kind of
  3019. 1:59:42thing here decide to generate that's
  3020. 1:59:44going to roll in these two conditional
  3021. 1:59:46edges into one um and basically it's
  3022. 1:59:49going to do if there's no documents so
  3023. 1:59:51basic basically if we filtered out all
  3024. 1:59:54of our documents from this first test
  3025. 1:59:56here um then what we're going to do is
  3026. 2:00:01we've decided all documents are not
  3027. 2:00:02relevant to the question and we're going
  3028. 2:00:04to kick back to web search exactly as we
  3029. 2:00:06show here so that's this piece um
  3030. 2:00:09otherwise we're going to go to generate
  3031. 2:00:11so that's this piece so again in these
  3032. 2:00:14conditional edges you're basically
  3033. 2:00:15implementing the logic that you see in
  3034. 2:00:17our diagram right here that's all that's
  3035. 2:00:19going on um and again this is just
  3036. 2:00:23implementing the final two checks uh for
  3037. 2:00:25hallucinations and and answer
  3038. 2:00:27relevance um
  3039. 2:00:30and um yep so here's our hallucination
  3040. 2:00:34grader we then extract the grade if the
  3041. 2:00:38if basically there are
  3042. 2:00:39hallucinations um oh sorry in this case
  3043. 2:00:42the grade actually yes means that the
  3044. 2:00:45answer is grounded so we say answer is
  3045. 2:00:48actually grounded and then we go to the
  3046. 2:00:50next step we go to the next test that's
  3047. 2:00:52all this is doing it's just basically
  3048. 2:00:53wrapping this logic that we're
  3049. 2:00:55implementing here in our graph so that's
  3050. 2:00:57all that's going on and let's go ahead
  3051. 2:00:59and Define all those things so nice we
  3052. 2:01:02have all that um now we can actually go
  3053. 2:01:05down a little bit and we can pull
  3054. 2:01:09um this is actually where we stitch
  3055. 2:01:12together everything so all it's
  3056. 2:01:13happening here is you see we defined all
  3057. 2:01:16these functions up here we just add them
  3058. 2:01:18as nodes in our graph here and then we
  3059. 2:01:21build our graph here basically by by
  3060. 2:01:24basically laying out the flow or the
  3061. 2:01:26connectivity between our nodes and edges
  3062. 2:01:29so you know you can look at this
  3063. 2:01:30notebook to kind of study in a bit of
  3064. 2:01:31detail what's going on but frankly what
  3065. 2:01:33I like to do here typically just draw
  3066. 2:01:36out a graph kind of like we did up
  3067. 2:01:38here and then Implement uh the Lo
  3068. 2:01:42logical flow here in your graph as nodes
  3069. 2:01:45and edges just like we're doing here
  3070. 2:01:47that's all that's happening uh so again
  3071. 2:01:49we have like our entry point is the
  3072. 2:01:51router
  3073. 2:01:52um this is like the output is this is
  3074. 2:01:55basically directing like here's what the
  3075. 2:01:57router is outputting and here's the next
  3076. 2:02:00node to go to so that's it um and then
  3077. 2:02:03for each node we're kind of applying
  3078. 2:02:05like we're saying like what's what's the
  3079. 2:02:06flow so web search goes to generate
  3080. 2:02:09after um and retrieve goes to grade
  3081. 2:02:12documents grade documents um kind of is
  3082. 2:02:16is like is a conditional Edge um
  3083. 2:02:18depending on the results we either do
  3084. 2:02:19web search or generate and then our
  3085. 2:02:21second one we go from generate to uh
  3086. 2:02:25basically this grade uh generation
  3087. 2:02:27versus documents in question based on
  3088. 2:02:29the output of that we either have
  3089. 2:02:31hallucinations we regenerate uh we found
  3090. 2:02:34that the answer is not useful we kick
  3091. 2:02:36back to web search or we end um finally
  3092. 2:02:39we have that llm fallback and that's
  3093. 2:02:41also if we go to the fallback we end so
  3094. 2:02:44what you're seeing here is actually the
  3095. 2:02:46the logic flow we're laying out in this
  3096. 2:02:49graph matches the diagram
  3097. 2:02:52that we laid out up top I'm just going
  3098. 2:02:54to copy these over and I'll actually go
  3099. 2:02:56then back to the diagram and and kind of
  3100. 2:02:57underscore that a little bit more so
  3101. 2:03:01here is the flow we've laid out again
  3102. 2:03:03here is our diagram and you can kind of
  3103. 2:03:05look at them side by side and see how
  3104. 2:03:07they basically match up so here's kind
  3105. 2:03:10of our flow diagram going from basically
  3106. 2:03:12query analysis that's this thing this
  3107. 2:03:15route question and you can see web
  3108. 2:03:18search Vector store LM fallback LM
  3109. 2:03:20fallback web search vector store so
  3110. 2:03:22those are like the three options that
  3111. 2:03:23can come out of this conditional Edge
  3112. 2:03:26and then here's where we connect so if
  3113. 2:03:27we go to web search then basically we
  3114. 2:03:30next go to
  3115. 2:03:31generate so that's kind of this whole
  3116. 2:03:34flow um now if we go to
  3117. 2:03:38retrieve um then we're going to grade so
  3118. 2:03:41that's
  3119. 2:03:42it um and you know it follows kind of as
  3120. 2:03:46you can see here that's really it uh so
  3121. 2:03:49it's just nice to draw the these
  3122. 2:03:51diagrams out first and then it's pretty
  3123. 2:03:54quick to implement each node and each
  3124. 2:03:56Edge just as a function and then stitch
  3125. 2:03:59them together in a graph just like I
  3126. 2:04:00show here and of course we'll make sure
  3127. 2:04:01this code's publ so you can use it as a
  3128. 2:04:03reference um so there we go now let's
  3129. 2:04:07try a few a few different test questions
  3130. 2:04:10so like what player the Bears to draft
  3131. 2:04:12and NFL draft right let's have a look at
  3132. 2:04:14that and they should print everything
  3133. 2:04:16it's doing as we go so okay this is
  3134. 2:04:19important route question it just decides
  3135. 2:04:21to route to web search that's good it
  3136. 2:04:23doesn't go to our Vector store this is a
  3137. 2:04:24current event not related to our Vector
  3138. 2:04:26store at all it goes to web search um
  3139. 2:04:29and then it goes to generate so that's
  3140. 2:04:30what we'd expect so basically web search
  3141. 2:04:32goes through to generate
  3142. 2:04:35um and we check hallucinations
  3143. 2:04:38Generations ground the documents we
  3144. 2:04:40check generation versus question the
  3145. 2:04:42generation addresses the question the
  3146. 2:04:43Chicago Bears expected to draft Caleb
  3147. 2:04:45Williams that's right that's that's the
  3148. 2:04:48consensus so cool that works now let's
  3149. 2:04:50ask a question related to our Vector
  3150. 2:04:52store what are the types of agent memory
  3151. 2:04:54we'll kick this off so we're routing
  3152. 2:04:56okay we're routing to rag now look how
  3153. 2:04:58fast this is that's really fast so we
  3154. 2:05:01basically whip through that document
  3155. 2:05:03grading determine they're all relevant
  3156. 2:05:06uh we go to decision to
  3157. 2:05:07generate um we check hallucinations we
  3158. 2:05:10check answer versus question and there
  3159. 2:05:13are several types of memory stored in
  3160. 2:05:15the human brain memory can also be
  3161. 2:05:16stored in G of Agents you have LM agents
  3162. 2:05:19memory stream retrieval model and and
  3163. 2:05:20reflection mechanism so it's
  3164. 2:05:22representing what's captured on the blog
  3165. 2:05:23post pretty reasonably now let me show
  3166. 2:05:25you something else is kind of nice I can
  3167. 2:05:27go to Langs Smith and I can go to my
  3168. 2:05:29projects we create this new project
  3169. 2:05:31coher adaptive rag at the start and
  3170. 2:05:33everything is actually logged there
  3171. 2:05:35everything we just did so I can open
  3172. 2:05:37this up and I can actually just kind of
  3173. 2:05:40look through all the stages of my Lang
  3174. 2:05:42graph to here's my retrieval stage um
  3175. 2:05:45here's my grade document stage and we
  3176. 2:05:47can kind of audit the grading itself we
  3177. 2:05:49kind of looked at this one by one
  3178. 2:05:51previously but it's actually pretty nice
  3179. 2:05:53we can actually audit every single
  3180. 2:05:54individual document grade to see what's
  3181. 2:05:56happening um we can basically go through
  3182. 2:06:00um to this is going to be one of the
  3183. 2:06:02other graders here
  3184. 2:06:05um yep so this is actually going to be
  3185. 2:06:09the hallucination grading right here uh
  3186. 2:06:12and then this is going to be the answer
  3187. 2:06:13grading right here so that's really it
  3188. 2:06:15you can kind of walk through the entire
  3189. 2:06:16graph you can you can kind of study
  3190. 2:06:18what's going on um which is actually
  3191. 2:06:20very useful so it looks like this worked
  3192. 2:06:23pretty well um and finally let's just
  3193. 2:06:25ask a question that should go to that
  3194. 2:06:27fallback uh path down at the bottom like
  3195. 2:06:30not related at all to our Vector store
  3196. 2:06:32current events and yeah hello I'm doing
  3197. 2:06:34well so it's pretty neat we've seen in
  3198. 2:06:36maybe 15 minutes we've from scratch
  3199. 2:06:38built basically a semi- sophisticated
  3200. 2:06:40rag system that has agentic properties
  3201. 2:06:42we've done in Lang graph we've done with
  3202. 2:06:44coher uh command R you can see it's
  3203. 2:06:46pretty darn fast in fact we can go to
  3204. 2:06:48Langs Smith and look at so this whole
  3205. 2:06:50thing took 7 seconds uh that is not bad
  3206. 2:06:54let's look at the most recent one so
  3207. 2:06:56this takes one second so the fallback
  3208. 2:06:58mechanism to the LM is like 1 second um
  3209. 2:07:01the let's just look here so 6 seconds
  3210. 2:07:04for the initial uh land graph so this is
  3211. 2:07:08not bad at all it's quite fast it done
  3212. 2:07:10it does quite a few different checks we
  3213. 2:07:12do routing uh and then we have kind of a
  3214. 2:07:15bunch of nice fallback behavior and
  3215. 2:07:17inline checking uh for both relevance
  3216. 2:07:20hallucinations and and answer uh kind of
  3217. 2:07:23groundedness or answer usefulness so you
  3218. 2:07:27know this is pretty nice I definitely
  3219. 2:07:28encourage you to play with a notebook
  3220. 2:07:30command R is a really nice option for
  3221. 2:07:31this due to the fact that is tool use
  3222. 2:07:33routing uh small and quite fast and it's
  3223. 2:07:36really good for Rags it's a very nice
  3224. 2:07:38kind of uh a very nice option for
  3225. 2:07:41workflows like this and I think you're
  3226. 2:07:43going to see more and more of this kind
  3227. 2:07:44of like uh adaptive or self-reflective
  3228. 2:07:47rag um just because this is something
  3229. 2:07:50that a lot systems can benefit from like
  3230. 2:07:53a a lot of production rack systems kind
  3231. 2:07:55of don't necessarily have
  3232. 2:07:57fallbacks uh depending on for example
  3233. 2:08:00like um you know if the documents
  3234. 2:08:03retrieved are not relevant uh if the
  3235. 2:08:05answer contains hallucinations and so
  3236. 2:08:07forth so this opportunity to apply
  3237. 2:08:09inline checking along with rag is like a
  3238. 2:08:11really nice theme I think we're going to
  3239. 2:08:13see more and more of especially as model
  3240. 2:08:15inference gets faster and faster and
  3241. 2:08:17these checks get cheaper and cheaper to
  3242. 2:08:18do kind of in the inference Loop
  3243. 2:08:22now as a final thing I do want to bring
  3244. 2:08:23up the a point about you know we've
  3245. 2:08:25shown this Lang graph stuff what about
  3246. 2:08:27agents you know how do you think about
  3247. 2:08:28agents versus Lang graph right and and I
  3248. 2:08:30think the way I like to frame this is
  3249. 2:08:33that um Lang graph is really good
  3250. 2:08:37for um flows that you have kind of very
  3251. 2:08:41clearly defined that don't have like
  3252. 2:08:43kind of open-endedness but like in this
  3253. 2:08:45case we know the steps we want to take
  3254. 2:08:47every time we want to do um basically
  3255. 2:08:49query analysis routing and then we want
  3256. 2:08:51to do a three grading steps and that's
  3257. 2:08:53it um Lang graph is really good for
  3258. 2:08:56building very reliable flows uh it's
  3259. 2:08:58kind of like putting an agent on guard
  3260. 2:09:00rails and it's really nice uh it's less
  3261. 2:09:04flexible but highly reliable and so you
  3262. 2:09:07can actually use smaller faster models
  3263. 2:09:08with langra so that's the thing I like
  3264. 2:09:10about we saw here command R 35 billion
  3265. 2:09:12parameter model works really well with
  3266. 2:09:14langra quite quick we' were able to
  3267. 2:09:16implement a pretty sophisticated rag
  3268. 2:09:18flow really quickly 15 minutes um in
  3269. 2:09:21time is on the order of like less than
  3270. 2:09:23you know around 5 to 6 seconds so so
  3271. 2:09:25pretty good right now what about agents
  3272. 2:09:28right so I think Agents come into play
  3273. 2:09:30when you want more flexible workflows
  3274. 2:09:32you don't want to necessarily follow a
  3275. 2:09:34defined pattern a priori you want an
  3276. 2:09:36agent to be able to kind of reason and
  3277. 2:09:38make of open-end decisions which is
  3278. 2:09:40interesting for certain like long
  3279. 2:09:41Horizon planning problems you know
  3280. 2:09:43agents are really
  3281. 2:09:44interesting the catch is that
  3282. 2:09:46reliability is a bit worse with agents
  3283. 2:09:48and so you know that's a big question a
  3284. 2:09:50lot of people bring up and that's kind
  3285. 2:09:52of where larger LMS kind of come into
  3286. 2:09:53play with a you know there's been a lot
  3287. 2:09:55of questions about using small LMS even
  3288. 2:09:57open source models with agents and
  3289. 2:09:59reliabilities kind of continuously being
  3290. 2:10:01an issue whereas I've been able to run
  3291. 2:10:03these types of land graphs with um with
  3292. 2:10:06uh like mraw or you know command R
  3293. 2:10:09actually is open weights you can run it
  3294. 2:10:10locally um I've been able to run them
  3295. 2:10:12very reproducibly with open source
  3296. 2:10:14models on my laptop um so you know I
  3297. 2:10:17think there's a tradeoff and Comm
  3298. 2:10:20actually there's a new coher model
  3299. 2:10:21coming out uh believe command R plus
  3300. 2:10:25which uh is a larger model so it's
  3301. 2:10:27probably more suitable for kind of more
  3302. 2:10:29open-ended agentic use cases and there's
  3303. 2:10:32actually a new integration with Lang
  3304. 2:10:33chain that support uh coher agents um
  3305. 2:10:36which is quite nice so I think it's it's
  3306. 2:10:38worth experimenting for certain problems
  3307. 2:10:40in workflows you may need more
  3308. 2:10:42open-ended reasoning in which case use
  3309. 2:10:43an agent with a larger model otherwise
  3310. 2:10:46you can use like Lang graph for more uh
  3311. 2:10:49a more reliable potential
  3312. 2:10:51but con strain flow and it can also use
  3313. 2:10:53smaller models faster LMS so those are
  3314. 2:10:56some of the trade-offs to keep in mind
  3315. 2:10:57but anyway encourage you play with a
  3316. 2:10:59notebook explore for yourself I think
  3317. 2:11:01command R is a really nice model um I've
  3318. 2:11:03also been experimenting with running it
  3319. 2:11:04locally with AMA uh currently the
  3320. 2:11:07quantise model is like uh two bit
  3321. 2:11:09quantise is like 13 billion uh or so uh
  3322. 2:11:14yeah 13 gigs it's it's a little bit too
  3323. 2:11:17large to run quickly locally for me
  3324. 2:11:22um inference for things like rag we're
  3325. 2:11:25on the order of 30 seconds so again it's
  3326. 2:11:27not great for a live demo but it does
  3327. 2:11:29work it is available on a llama so I
  3328. 2:11:30encourage you to play with that I have a
  3329. 2:11:32Mac M2 32 gig um so you know if I if
  3330. 2:11:35you're a larger machine then it
  3331. 2:11:36absolutely could be worth working with
  3332. 2:11:38locally so encourage you to play with
  3333. 2:11:40that anyway hopefully this was useful
  3334. 2:11:41and interesting I think this is a cool
  3335. 2:11:43paper cool flow um coher command R is a
  3336. 2:11:46nice option for these types of like
  3337. 2:11:48routing uh it's quick good with Lang
  3338. 2:11:52graph good for rag good for Tool use so
  3339. 2:11:55you know have a have a look and uh you
  3340. 2:11:57know reply anything uh any feedback in
  3341. 2:12:00the comments
  3342. 2:12:06thanks hi this is Lance from Lang chain
  3343. 2:12:09this is a talk I gave at two recent
  3344. 2:12:10meetups in San Francisco called is rag
  3345. 2:12:13really dead um and I figured since you
  3346. 2:12:16know a lot of people actually weren't
  3347. 2:12:17able to make those meetups uh I just
  3348. 2:12:19record this and put this on YouTube and
  3349. 2:12:21see if this is of interest to folks um
  3350. 2:12:24so we all kind of recognize that Contex
  3351. 2:12:26windows are getting larger for llms so
  3352. 2:12:29on the x-axis you can see the tokens
  3353. 2:12:31used in pre-training that's of course
  3354. 2:12:33you know getting larger as well um
  3355. 2:12:35proprietary models are somewhere over
  3356. 2:12:37the two trillion token regime we don't
  3357. 2:12:39quite know where they sit uh and we've
  3358. 2:12:41all the way down to smaller models like
  3359. 2:12:4352 trained on far fewer tokens um but
  3360. 2:12:46what's really notable is on the y axis
  3361. 2:12:49you can see about a year ago da the art
  3362. 2:12:52models were on the order of 4,000 to
  3363. 2:12:548,000 tokens and that's you know dozens
  3364. 2:12:56of pages um we saw Claude 2 come out
  3365. 2:12:59with the 200,000 token model earlier I
  3366. 2:13:02think it was last year um gbd4 128,000
  3367. 2:13:06tokens now that's hundreds of pages and
  3368. 2:13:09now we're seeing Claud 3 and Gemini come
  3369. 2:13:11out with million token models so this is
  3370. 2:13:13hundreds to thousands of pages so
  3371. 2:13:16because of this phenomenon people have
  3372. 2:13:17been kind of wondering is rag dead if
  3373. 2:13:19you can stuff you know many thousands of
  3374. 2:13:22pages into the context window llm why do
  3375. 2:13:24you need a reteval system um it's a good
  3376. 2:13:27question spoke sparked a lot of
  3377. 2:13:29interesting debate on Twitter um and
  3378. 2:13:32it's maybe first just kind of grounding
  3379. 2:13:33on what is rag so rag is really the
  3380. 2:13:35process of reasoning and retrieval over
  3381. 2:13:37chunks of of information that have been
  3382. 2:13:40retrieved um it's starting with you know
  3383. 2:13:42documents that are indexed um they're
  3384. 2:13:45retrievable through some mechanism
  3385. 2:13:47typically some kind of semantic
  3386. 2:13:48similarity search or keyword search
  3387. 2:13:50other mechanisms
  3388. 2:13:51retriev docs should then pass to an llm
  3389. 2:13:54and the llm reasons about them to ground
  3390. 2:13:56response to the question in the retrieve
  3391. 2:13:58document so that's kind of the overall
  3392. 2:14:00flow but the important point to make is
  3393. 2:14:02that typically it's multiple documents
  3394. 2:14:04and involve some form of
  3395. 2:14:06reasoning so one of the questions I
  3396. 2:14:08asked recently is you know if long
  3397. 2:14:10condex llms can replace rag it should be
  3398. 2:14:12able to perform you know multia
  3399. 2:14:15retrieval and reasoning from its own
  3400. 2:14:16context really effectively so I teamed
  3401. 2:14:19up with Greg Cameron uh to kind of
  3402. 2:14:21pressure test this and he had done some
  3403. 2:14:23really nice needle the Haack analyses
  3404. 2:14:25already focused on kind of single facts
  3405. 2:14:28called needles placed in a Hy stack of
  3406. 2:14:31Paul Graham essays um so I kind of
  3407. 2:14:33extended that to kind of mirror the rag
  3408. 2:14:36use case or kind of the rag context uh
  3409. 2:14:39where I took multiple facts so I call it
  3410. 2:14:41multi needle um I buil on a funny needle
  3411. 2:14:45in the HTO challenge published by
  3412. 2:14:46anthropic where they add they basically
  3413. 2:14:48placed Pizza ingredients in the context
  3414. 2:14:51uh and asked the LM to retrieve this
  3415. 2:14:53combination of pizza ingredients I did I
  3416. 2:14:56kind of Rift on that and I basically
  3417. 2:14:58split the pizza ingredients up into
  3418. 2:14:59three different needles and place those
  3419. 2:15:01three ingredients in different places in
  3420. 2:15:03the context and then ask the um to
  3421. 2:15:06recover those three ingredients um from
  3422. 2:15:09the context so again the setup is the
  3423. 2:15:12question is what the secret ingredients
  3424. 2:15:13need to build a perfect Pizza the
  3425. 2:15:15needles are the ingredients figs Pudo
  3426. 2:15:17goat cheese um I place them in the
  3427. 2:15:21context at some specified intervals the
  3428. 2:15:23way this test works is you can basically
  3429. 2:15:25set the percent of context you want to
  3430. 2:15:28place the first needle and the remaining
  3431. 2:15:30two are placed at roughly equal
  3432. 2:15:31intervals in the remaining context after
  3433. 2:15:33the first so that's kind of the way the
  3434. 2:15:35test is set up now it's all open source
  3435. 2:15:36by the way the link is below so needs
  3436. 2:15:39are placed um you ask a question you
  3437. 2:15:42promp L them with with kind of um with
  3438. 2:15:45this context and the question and then
  3439. 2:15:47produces the answer and now the the
  3440. 2:15:48framework will grade the response
  3441. 2:15:51both one are you know all are all the
  3442. 2:15:55the specified ingredients present in the
  3443. 2:15:56answer and two if not which ones are
  3444. 2:16:00missing so I ran a bunch of analysis on
  3445. 2:16:03this with GPD 4 and came kind of came up
  3446. 2:16:05with some with some fun results um so
  3447. 2:16:07you can see on the left here what this
  3448. 2:16:09is looking at is different numbers of
  3449. 2:16:11needles placed in 120,000 token context
  3450. 2:16:14window for
  3451. 2:16:15gbd4 and I'm asking um gbd4 to retrieve
  3452. 2:16:20either one three or 10 needles now I'm
  3453. 2:16:24also asking it to do reasoning on those
  3454. 2:16:26needles that's what you can see in those
  3455. 2:16:28red bars so green is just retrieve the
  3456. 2:16:30ingredients red is reasoning and the
  3457. 2:16:32reasoning challenge here is just return
  3458. 2:16:34the first letter of each ingredient so
  3459. 2:16:37we find is basically two things the
  3460. 2:16:39performance or the percentage of needles
  3461. 2:16:42retrieved drops with respect to the
  3462. 2:16:44number of needles that's kind of
  3463. 2:16:45intuitive you place more facts
  3464. 2:16:48performance gets worse but also it gets
  3465. 2:16:50worse if you ask it to reason so if you
  3466. 2:16:53say um just return the needles it does a
  3467. 2:16:56little bit better than if you say return
  3468. 2:16:58the needles and tell me the first letter
  3469. 2:17:00so you overlay reasoning so this is the
  3470. 2:17:02first observation more facts is harder
  3471. 2:17:06uh and reasoning is harder uh than just
  3472. 2:17:09retrieval now the second question we ask
  3473. 2:17:11is where are these needles actually
  3474. 2:17:12present in the context that we're
  3475. 2:17:14missing right so we know for example um
  3476. 2:17:18retrieval of um 10 needles is around 60%
  3477. 2:17:23so where are the missing needles in the
  3478. 2:17:26context so on the right you can see
  3479. 2:17:28results telling us actually which
  3480. 2:17:30specific needles uh are are the model
  3481. 2:17:33fails to retrieve so what we can see is
  3482. 2:17:36as you go from a th000 tokens up to
  3483. 2:17:39120,000 tokens on the X here and you
  3484. 2:17:42look at needle one place at the start of
  3485. 2:17:44the document to needle 10 placed at the
  3486. 2:17:46end at a th000 token context link you
  3487. 2:17:49can retrieve them all so again kind of
  3488. 2:17:52match what we see over here small well
  3489. 2:17:54actually sorry over here everything I'm
  3490. 2:17:56looking at is 120,000 tokens so that's
  3491. 2:17:59really not the point uh the point is
  3492. 2:18:01actually smaller context uh better
  3493. 2:18:04retrieval so that's kind of point one um
  3494. 2:18:08as I increase the context window I
  3495. 2:18:10actually see that uh there is increased
  3496. 2:18:14failure to retrieve needles which you
  3497. 2:18:15see can see in red here towards the
  3498. 2:18:18start of the
  3499. 2:18:19document um and so this is an
  3500. 2:18:21interesting result um and it actually
  3501. 2:18:23matches what Greg saw with single needle
  3502. 2:18:25case as well so the way to think about
  3503. 2:18:27it is it appears that um you know if you
  3504. 2:18:31for example read a book and I asked you
  3505. 2:18:33a question about the first chapter you
  3506. 2:18:34might have forgotten it same kind of
  3507. 2:18:36phenomenon appears to happen here with
  3508. 2:18:38retrieval where needles towards the
  3509. 2:18:40start of the context are are kind of
  3510. 2:18:42Forgotten or are not well retrieved
  3511. 2:18:45relative to those of the end so this is
  3512. 2:18:47an effect we see with gbd4 it's been
  3513. 2:18:49reproduced quite a bit so ran nine
  3514. 2:18:51different trials here Greg's also seen
  3515. 2:18:53this repeatedly with single needle so it
  3516. 2:18:55seems like a pretty consistent
  3517. 2:18:57result and there's an interesting point
  3518. 2:18:59I put this on Twitter and a number of
  3519. 2:19:00folks um you know replied and someone
  3520. 2:19:03sent me this paper which is pretty
  3521. 2:19:04interesting and it mentions recency bias
  3522. 2:19:07is one possible reason so the most
  3523. 2:19:09informative tokens for predicting the
  3524. 2:19:10next token uh you know are are are
  3525. 2:19:14present close to or recent to kind of
  3526. 2:19:17where you're doing your generation and
  3527. 2:19:18so there's a bias to attend to recent
  3528. 2:19:20tokens which is obviously not great for
  3529. 2:19:23the retrieval problem as we saw here so
  3530. 2:19:26again the results show us that um
  3531. 2:19:30reasoning is a bit harder than retrieval
  3532. 2:19:32more needles is more difficult and
  3533. 2:19:34needles towards the start of your
  3534. 2:19:36context are harder to retrieve than
  3535. 2:19:38towards the end those are three main
  3536. 2:19:40observations from this and it maybe
  3537. 2:19:42indeed due to this recency bias so
  3538. 2:19:45overall what this kind of tells you is
  3539. 2:19:47be wary of just context stuffing in
  3540. 2:19:49large long context
  3541. 2:19:51there are no retrieval
  3542. 2:19:52guarantees and also there's some recent
  3543. 2:19:55results that came out actually just
  3544. 2:19:56today suggesting that single needle may
  3545. 2:19:58be misleadingly easy um you know there's
  3546. 2:20:02no reasoning it's retrieving a single
  3547. 2:20:04needle um and also these guys I'm I
  3548. 2:20:08showed this tweet here show that um the
  3549. 2:20:12in a lot of these needle and Haack
  3550. 2:20:13challenges including mine the facts that
  3551. 2:20:16we look for are very different than um
  3552. 2:20:20the background kind of Hy stack of Paul
  3553. 2:20:21Graham essays and so that may be kind of
  3554. 2:20:23an interesting artifact they note that
  3555. 2:20:25indeed if the needle is more subtle
  3556. 2:20:28retrievals is worse so I think basically
  3557. 2:20:31when you see these really strong
  3558. 2:20:33performing needle and hyack analyses put
  3559. 2:20:35up by model providers you should be
  3560. 2:20:37skeptical um you shouldn't necessarily
  3561. 2:20:39assume that you're going to get high
  3562. 2:20:40quality retrieval from these long
  3563. 2:20:41contact LMS uh for numerous reasons you
  3564. 2:20:45need to think about retrieval of
  3565. 2:20:46multiple facts um you need to think
  3566. 2:20:48about reasoning on top of retrieval you
  3567. 2:20:50need need to think about the subtlety of
  3568. 2:20:52the retrieval relative to the background
  3569. 2:20:54context because for many of these needle
  3570. 2:20:56and the Haack challenges it's a single
  3571. 2:20:57needle no reasoning and the needle
  3572. 2:21:00itself is very different from the
  3573. 2:21:01background so anyway those may all make
  3574. 2:21:03the challenge a bit easier than a real
  3575. 2:21:05world scenario of fact retrieval so I
  3576. 2:21:07just want to like kind of lay out that
  3577. 2:21:09those cautionary notes but you know I
  3578. 2:21:13think it is fair to say this will
  3579. 2:21:14certainly get better and I think it's
  3580. 2:21:17also fair to say that rag will change
  3581. 2:21:19and this is just like a nearly not a
  3582. 2:21:21great joke but Frank zap a musician made
  3583. 2:21:24the point Jazz isn't dead it just smells
  3584. 2:21:26funny you know I think same for rag rag
  3585. 2:21:28is not dead but it will change I think
  3586. 2:21:30that's like kind of the key Point here
  3587. 2:21:32um so just as a followup on that rag
  3588. 2:21:35today's focus on precise retrieval of
  3589. 2:21:37relevant doc chunks so it's very focused
  3590. 2:21:39on typically taking documents chunking
  3591. 2:21:42them in some particular way often using
  3592. 2:21:44very OS syncratic chunking methods
  3593. 2:21:46things like chunk size are kind of
  3594. 2:21:48picked almost arbitrarily embeding them
  3595. 2:21:50storing them in an index taking a
  3596. 2:21:52question embedding it doing K&N uh
  3597. 2:21:55similarity search to retrieve relevant
  3598. 2:21:57chunks you're often setting a k
  3599. 2:21:59parameter which is the number of chunks
  3600. 2:22:00you retrieve you often will do some kind
  3601. 2:22:02of filtering or Pro processing on the
  3602. 2:22:04retrieve chunks and then ground your
  3603. 2:22:06answer in those retrieved chunks so it's
  3604. 2:22:08very focused on precise retrieval of
  3605. 2:22:10just the right chunks now in a world
  3606. 2:22:14where you have very long context models
  3607. 2:22:16I think there's a fair question to ask
  3608. 2:22:18is is this really kind of the most most
  3609. 2:22:20reasonable approach so kind of on the
  3610. 2:22:22left here you can kind of see this
  3611. 2:22:25notion closer to today of I need the
  3612. 2:22:27exact relevant chunk you can risk over
  3613. 2:22:29engineering you can have you know higher
  3614. 2:22:31complexity sensitivity to these odd
  3615. 2:22:33parameters like chunk size k um and you
  3616. 2:22:36can indeed suffer lower recall because
  3617. 2:22:38you're really only picking very precise
  3618. 2:22:40chunks you're beholden to very
  3619. 2:22:41particular embedding models so you know
  3620. 2:22:44I think going forward as long context
  3621. 2:22:46models get better and better there are
  3622. 2:22:48definitely question you should certainly
  3623. 2:22:50question the current kind of very
  3624. 2:22:52precise chunking rag Paradigm but on the
  3625. 2:22:54flip side I think just throwing all your
  3626. 2:22:56docs into context probably will also not
  3627. 2:22:59be the preferred approach you'll suffer
  3628. 2:23:01higher latency higher token usage I
  3629. 2:23:03should note that today 100,000 token GPD
  3630. 2:23:064 is like $1 per generation I spent a
  3631. 2:23:09lot of money on Lang Chain's account uh
  3632. 2:23:11on that multile analysis I don't want to
  3633. 2:23:13tell Harrison how much I spent uh so
  3634. 2:23:16it's it's you know it's not good right
  3635. 2:23:18um You Can't audit retrieve
  3636. 2:23:21um and security and and authentication
  3637. 2:23:23are issues if for example you need
  3638. 2:23:25different users different different
  3639. 2:23:26access to certain kind of retriev
  3640. 2:23:28documents or chunks in the Contex
  3641. 2:23:30stuffing case you you kind of can't
  3642. 2:23:32manage security as easily so there's
  3643. 2:23:33probably some predo optimal regime kind
  3644. 2:23:35of here in the middle and um you know I
  3645. 2:23:39I put this out on Twitter I think
  3646. 2:23:40there's some reasonable points raised I
  3647. 2:23:42think you know this inclusion at the
  3648. 2:23:44document level is probably pretty sane
  3649. 2:23:46documents are self-contained chunks of
  3650. 2:23:48context um so you know what about
  3651. 2:23:51document Centric rag so no chunking uh
  3652. 2:23:54but just like operate on the context of
  3653. 2:23:56full documents so you know if you think
  3654. 2:23:59forward to the rag Paradigm that's
  3655. 2:24:01document Centric you still have the
  3656. 2:24:03problem of taking an input question
  3657. 2:24:05routing it to the right document um this
  3658. 2:24:07doesn't change so I think a lot of
  3659. 2:24:09methods that we think about for kind of
  3660. 2:24:11query analysis um taking an input
  3661. 2:24:14question rewriting it in a certain way
  3662. 2:24:16to optimize retrieval things like
  3663. 2:24:18routing taking a question routing to the
  3664. 2:24:20right database be it a relational
  3665. 2:24:22database graph database Vector store um
  3666. 2:24:25and quer construction methods so for
  3667. 2:24:27example text to SQL text to Cipher for
  3668. 2:24:30graphs um or text to even like metadata
  3669. 2:24:32filters for for Vector stores those are
  3670. 2:24:35all still relevant in the world that you
  3671. 2:24:37have long Contex llms um you're probably
  3672. 2:24:40not going to dump your entire SQL DB and
  3673. 2:24:42feed that to the llm you're still going
  3674. 2:24:43to have SQL queries you're still going
  3675. 2:24:45to have graph queries um you may be more
  3676. 2:24:48permissive with what you extract but it
  3677. 2:24:50still is very reasonable to store the
  3678. 2:24:52majority of your structured data in
  3679. 2:24:53these in these forms likewise with
  3680. 2:24:55unstructured data like documents like we
  3681. 2:24:58said before it still probably makes
  3682. 2:25:00sense to ENC to you know store documents
  3683. 2:25:02independently but just simply aim to
  3684. 2:25:04retrieve full documents rather than
  3685. 2:25:05worrying about these idiosyncratic
  3686. 2:25:07parameters like like chunk size um and
  3687. 2:25:10along those lines there's a lot of
  3688. 2:25:12methods out there we've we've done a few
  3689. 2:25:14of these that are kind of well optimized
  3690. 2:25:16for document retrieval so one I want a
  3691. 2:25:18flag is what we call multi repesent
  3692. 2:25:20presentation indexing and there's
  3693. 2:25:21actually a really nice paper on this
  3694. 2:25:23called dense X retriever or proposition
  3695. 2:25:25indexing but the main point is simply
  3696. 2:25:27this would you do is you take your OD
  3697. 2:25:29document you produce a representation
  3698. 2:25:31like a summary of that document you
  3699. 2:25:33index that summary right and then um at
  3700. 2:25:37retrieval time you ask your question you
  3701. 2:25:39embed your question and you simply use a
  3702. 2:25:41highle summary to just retrieve the
  3703. 2:25:43right document you pass the full
  3704. 2:25:45document to the LM for a kind of final
  3705. 2:25:48generation so it's kind of a trick where
  3706. 2:25:51you don't have to worry about embedding
  3707. 2:25:52full documents in this particular case
  3708. 2:25:54you can use kind of very nice
  3709. 2:25:56descriptive summarization prompts to
  3710. 2:25:58build descriptive summaries and the
  3711. 2:26:00problem you're solving here is just get
  3712. 2:26:02me the right document it's an easier
  3713. 2:26:04problem than get me the right chunk so
  3714. 2:26:06this is kind of a nice approach it
  3715. 2:26:08there's also different variants of it
  3716. 2:26:10which I share below one is called parent
  3717. 2:26:11document retriever where you could use
  3718. 2:26:13in principle if you wanted smaller
  3719. 2:26:15chunks but then just return full
  3720. 2:26:17documents but anyway the point is
  3721. 2:26:19preserving full documents for Generation
  3722. 2:26:21but using representations like summaries
  3723. 2:26:23or chunks for retrieval so that's kind
  3724. 2:26:25of like approach one that I think is
  3725. 2:26:27really interesting approach two is this
  3726. 2:26:30idea of raptor is a cool paper came out
  3727. 2:26:32of Stamper somewhat recently and this
  3728. 2:26:34solves the problem of what if for
  3729. 2:26:36certain questions I need to integrate
  3730. 2:26:38information across many documents so
  3731. 2:26:41what this approach does is it takes
  3732. 2:26:42documents and it it embeds them and
  3733. 2:26:45clusters them and then it summarizes
  3734. 2:26:47each cluster um and it does this
  3735. 2:26:49recursively in up with only one very
  3736. 2:26:51high level summary for the entire Corpus
  3737. 2:26:53of documents and what they do is they
  3738. 2:26:55take this kind of this abstraction
  3739. 2:26:57hierarchy so to speak of different
  3740. 2:26:59document summarizations and they just
  3741. 2:27:01index all of it and they use this in
  3742. 2:27:03retrieval and so basically if you have a
  3743. 2:27:05question that draws an information
  3744. 2:27:07across numerous documents you probably
  3745. 2:27:09have a summary present and and indexed
  3746. 2:27:12that kind of has that answer captured so
  3747. 2:27:15it's a nice trick to consolidate
  3748. 2:27:16information across documents um they
  3749. 2:27:19they paper actually reports you know
  3750. 2:27:22these documents in their case or the
  3751. 2:27:23leavs are actually document chunks or
  3752. 2:27:25slices but I actually showed I have a
  3753. 2:27:28video on it and a notebook that this
  3754. 2:27:29works across full documents as well um
  3755. 2:27:33and this and I segue into to do this you
  3756. 2:27:36do need to think about long context
  3757. 2:27:37embedding models because you're
  3758. 2:27:38embedding full documents and that's a
  3759. 2:27:40really interesting thing to track um the
  3760. 2:27:43you know hazy research uh put out a
  3761. 2:27:45really nice um uh blog post on this
  3762. 2:27:48using uh what the Monch mixer so it's
  3763. 2:27:50kind of a new architecture that tends to
  3764. 2:27:53longer context they have a 32,000 token
  3765. 2:27:56embedding model that's pres that's
  3766. 2:27:58available on together AI absolutely
  3767. 2:27:59worth experimenting with I think this is
  3768. 2:28:01really interesting Trend so long long
  3769. 2:28:03Contex and beddings kind of play really
  3770. 2:28:05well with this kind of idea you take
  3771. 2:28:07full documents embed them using for
  3772. 2:28:09example long Contex embedding models and
  3773. 2:28:11you can kind of build these document
  3774. 2:28:12summarization trees um really
  3775. 2:28:14effectively so I think this another nice
  3776. 2:28:16trick for working with full documents in
  3777. 2:28:19the long context kind of llm regime um
  3778. 2:28:23one other thing I'll note I think
  3779. 2:28:25there's also going to Mo be move away
  3780. 2:28:27from kind of single shot rag well
  3781. 2:28:29today's rag we typically you know we
  3782. 2:28:31chunk documents uh uh embed them store
  3783. 2:28:34them in an index you know do retrieval
  3784. 2:28:36and then do generation but there's no
  3785. 2:28:38reason why you shouldn't kind of do
  3786. 2:28:40reasoning on top of the generation or
  3787. 2:28:42reasoning on top of the retrieval and
  3788. 2:28:44feedback if there are errors so there's
  3789. 2:28:46a really nice paper called selfrag um
  3790. 2:28:49that kind of reports this we implemented
  3791. 2:28:50this using Lang graph works really well
  3792. 2:28:53and the simp the idea is simply to you
  3793. 2:28:55know grade the relevance of your
  3794. 2:28:57documents relative to your question
  3795. 2:28:59first if they're not relevant you
  3796. 2:29:01rewrite the question you can do you can
  3797. 2:29:02do many things in this case we do
  3798. 2:29:04question rewriting and try again um we
  3799. 2:29:06also grade for hallucinations we grade
  3800. 2:29:09for answer relevance but anyway it kind
  3801. 2:29:11of moves rag from like a single shot
  3802. 2:29:12Paradigm to a kind of a cyclic flow uh
  3803. 2:29:16in which you actually do various
  3804. 2:29:17gradings Downstream and this is all
  3805. 2:29:19relev in the long context llm regime as
  3806. 2:29:22well in fact you know it you you
  3807. 2:29:24absolutely should take advantage of of
  3808. 2:29:27for example increasingly fast and
  3809. 2:29:30Performing LMS to do this grading um
  3810. 2:29:33Frameworks like langra allow you to
  3811. 2:29:35build these kind of these flows which
  3812. 2:29:37build which allows you to kind of have a
  3813. 2:29:38more performant uh kind of kind of
  3814. 2:29:42self-reflective rag pipeline now I did
  3815. 2:29:44get a lot of questions about latency
  3816. 2:29:46here and I completely agree there's a
  3817. 2:29:47trade-off between kind of performance
  3818. 2:29:49accuracy and latency that's present here
  3819. 2:29:51I think the real answer is you can opt
  3820. 2:29:54to use very fast uh for example models
  3821. 2:29:57like grock where seeing um you know gp35
  3822. 2:30:00turbos very fast these are fairly easy
  3823. 2:30:03grading challenges so you can use very
  3824. 2:30:05very fast LMS to do the grading and for
  3825. 2:30:07example um you you can also restrict
  3826. 2:30:11this to only do one turn of of kind of
  3827. 2:30:13cyclic iteration so you can kind of
  3828. 2:30:14restrict the latency in that way as well
  3829. 2:30:16so anyway I think it's a really cool
  3830. 2:30:18approach still relevant in the world as
  3831. 2:30:20we move towards longer context so it's
  3832. 2:30:22kind of like building reasoning on top
  3833. 2:30:23of rag um in the uh generation and
  3834. 2:30:28retrieval stages and a related point one
  3835. 2:30:30of the challenges with rag is that your
  3836. 2:30:33index for example you you may have a
  3837. 2:30:36question that is that asks something
  3838. 2:30:38that's outside the scope of your index
  3839. 2:30:39and this is kind of always a problem so
  3840. 2:30:41a really cool paper called c c rag or
  3841. 2:30:44corrective rag came out you know a
  3842. 2:30:45couple months ago that basically does
  3843. 2:30:47grading just like we talked about before
  3844. 2:30:50and then if the documents are not
  3845. 2:30:51relevant you kick off and do a web
  3846. 2:30:53search and basically return the search
  3847. 2:30:55results to the LM for final generation
  3848. 2:30:57so it's a nice fallback in cases where
  3849. 2:31:00um your you the questions out of the
  3850. 2:31:02domain of your retriever so you know
  3851. 2:31:04again nice trick overlaying reasoning on
  3852. 2:31:07top of rag I think this trend you know
  3853. 2:31:09continues um because you know it it just
  3854. 2:31:12it makes rag systems you know more
  3855. 2:31:14performant uh and less brittle to
  3856. 2:31:18questions that are out of domain so you
  3857. 2:31:19know you know that's another kind of
  3858. 2:31:21nice idea this particular approach also
  3859. 2:31:23we showed works really well with with uh
  3860. 2:31:25with open source models so I ran this
  3861. 2:31:27with mraw 7B it can run locally on my
  3862. 2:31:29laptop using a llama so again really
  3863. 2:31:32nice approach I encourage you to look
  3864. 2:31:33into this um and this is all kind of
  3865. 2:31:35independent of the llm kind of context
  3866. 2:31:37length these are reasoning you can add
  3867. 2:31:40on top of the retrieval stage that that
  3868. 2:31:42can kind of improve overall performance
  3869. 2:31:45and so the overall picture kind of looks
  3870. 2:31:47like this where you know I think that
  3871. 2:31:49the the the the problem of routing your
  3872. 2:31:52question to the right database Andor to
  3873. 2:31:54the right document kind of remains in
  3874. 2:31:56place query analysis is still quite
  3875. 2:31:58relevant routing is still relevant query
  3876. 2:32:00construction is still relevant um in the
  3877. 2:32:02long Contex regime I think there is less
  3878. 2:32:04of an emphasis on document chunking
  3879. 2:32:07working with full documents is probably
  3880. 2:32:08kind of more parto optimal so to speak
  3881. 2:32:11um there's some some clever tricks for
  3882. 2:32:13IND indexing of documents like the
  3883. 2:32:15multi-representation indexing we talked
  3884. 2:32:16about the hierarchical indexing using
  3885. 2:32:19Raptor that we talked about as well are
  3886. 2:32:20two interesting ideas for document
  3887. 2:32:22Centric indexing um and then kind of
  3888. 2:32:25reasoning in generation post retrieval
  3889. 2:32:28on retrieval itself tog grade on the
  3890. 2:32:30generations themselves checking for
  3891. 2:32:32hallucinations those are all kind of
  3892. 2:32:34interesting and relevant parts of a rag
  3893. 2:32:36system that I think we'll probably will
  3894. 2:32:38see more and more of as we move more
  3895. 2:32:40away from like a more naive prompt
  3896. 2:32:42response Paradigm more to like a flow
  3897. 2:32:44Paradigm we're seeing that actually
  3898. 2:32:45already in codenation it's probably
  3899. 2:32:47going to carry over to rag as well where
  3900. 2:32:49we kind of build rag systems that have
  3901. 2:32:51kind of a cyclic flow to them operate on
  3902. 2:32:53documents use longc Comics llms um and
  3903. 2:32:55still use kind of routing and query
  3904. 2:32:57analysis so reasoning pre- retrieval
  3905. 2:32:59reasoning post- retrieval so anyway that
  3906. 2:33:01was kind of my talk um and yeah feel
  3907. 2:33:04free to leave any comments on the video
  3908. 2:33:05and I'll try to answer any questions but
  3909. 2:33:07um yeah that's that's probably about it
  3910. 2:33:09thank you

About this transcript

This page contains the full transcript of Learn RAG From Scratch – Python AI Tutorial from a LangChain Engineer by freeCodeCamp.org, generated from the public captions YouTube serves with the video. The transcript has 27,073 words across 3,910 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.