Connect AI to Billions of Legal Documents — Simon Eskildsen, turbopuffer & Jacob Lauritzen, Legora

AI Engineer · 20 min · 236 sentences · from YouTube's caption track

Each timecode opens YouTube at the start of that sentence. Line anchors (#s42) are the cue ids in the WebVTT, and every line carries its start and end seconds. All transcripts has every talk, and the whole corpus as one file.

  1. 00:01[music]
  2. 00:13Okay.
  3. 00:14Hi everyone.
  4. 00:15Welcome um to this 20 minute talk about connecting AI to loads of legal documents.
  5. 00:22My name is Jacob.
  6. 00:23I'm an engineer at Legora.
  7. 00:26Yeah.
  8. 00:26And I got to step into frame here.
  9. 00:28Uh I'm Simon.
  10. 00:28I'm the CEO and uh CEO and co-founder of Turopuffer, a search engine that we work with Legora and others on.
  11. 00:35Neat.
  12. 00:36Um super quickly um introduction to Legora.
  13. 00:41We're a collaborative AI platform for legal work.
  14. 00:45And so that means we have law firms that are clients and we have uh in-house legal teams that are clients.
  15. 00:50And they use Legora to do reviews of contracts.
  16. 00:54They use it to go through an absurd amount of contracts and make sure that they all look good.
  17. 00:58They use them to create new contracts.
  18. 01:00They do legal research which means um looking over all potential law and they collaborate inside Lor.
  19. 01:07So you can think of Lorra sort of as a linearfigmas notion GitHub for legal work.
  20. 01:14It's a lot.
  21. 01:16We um are one of the fastest growing companies right now.
  22. 01:20We grow extremely fast.
  23. 01:21Yeah, tons of numbers on the screen.
  24. 01:24I'll just skip through that.
  25. 01:26What we really want to talk about is search today.
  26. 01:29So at Lor there's two types of search that we do.
  27. 01:33There is project search and legal research.
  28. 01:35Project search is um basically projects in Lor is like the the unit of work that you have.
  29. 01:41So if um let's say you are SpaceX and you want to acquire Cursor, then that would be one project with your law firm.
  30. 01:50H and so they would go into Lor and they would upload all these documents and the law firm that helped you would go through all of the employment agreements, all of the contracts with suppliers.
  31. 01:59Um I know cursor is using Turbo Puffer so maybe there's a contract there they'd look at.
  32. 02:04Um but basically you do the search confined to a project and projects can be tens of documents to millions of documents.
  33. 02:11The other use case is legal research.
  34. 02:13And legal research is sort of a deep research style workload where we'll search across tons of laws, previous cases, regulations, etc., etc. And people use this to answer questions such as like how do I handle this specific thing?
  35. 02:27And they'll also use it for litigation.
  36. 02:29Maybe they want to sue someone or maybe they are getting sued and they'll use legal research to support and help their case.
  37. 02:36So if we start at number one, project search, we've been through a little bit of a ride here on how we do search.
  38. 02:43Um, starting at, you know, hundreds of thousands of documents all the way into two billions of documents.
  39. 02:49And we've tried a lot of different things.
  40. 02:52So first we start started with the very very simple one, which is just a single elastic search cluster for all of our search workloads.
  41. 02:59Um, that worked relatively well.
  42. 03:02It was sort of a simple setup.
  43. 03:03All of the tenants, you know, our clients, our users would be on one big blob storage where we store the raw documents and on one big elastic search where we would do all of the searching, the indexing and the searching.
  44. 03:15Super simple, worked relatively well initially.
  45. 03:19Then we wanted to enter the land of the free and uh we got some new requirements.
  46. 03:24They you know Americans only want processing to happen within the US and Europeans only want it to happen within the EU and Australians only want it to happen within Australia.
  47. 03:32And so we had to basically um well here's a scaling graph.
  48. 03:37We had to move to multiple elastic searches.
  49. 03:39And what we actually did was we took the entire setup and we just basically iterated over the set that is EU, US and Asia Pacific.
  50. 03:47And so we just had this like multiplied by three or four.
  51. 03:50Kind of annoying, lots of overhead, but it uh got us to where we needed to be.
  52. 03:57Then the next iteration of the story is enterprise.
  53. 04:01So really big banks, the biggest law firms in the world, they have really annoying requirements.
  54. 04:07And number one they have is they'll ask for full physical isolation of all of their data.
  55. 04:12There's probably a little bit of a war on what physical, you know, isolation actually means, but essentially means they want their own database.
  56. 04:19They also want customer managed encryption keys.
  57. 04:22And what that means is they basically have a key volt thing where they have an encryption key and they give us access to read the key and we then use that key to
  58. 04:30encrypt and decrypt all of their data at rest.
  59. 04:33And what that gives them is they can just revoke our access to their key and then we can't decrypt their data anymore and so it's safe.
  60. 04:39And so in a way that gives enterprises a lot of control over all of their data because they control you know the key to to reading it.
  61. 04:48So we moved from elastic search to Postgress and I imagine a bunch of you guys are like why would you ever put your vectors into Postgress.
  62. 04:54Um it actually worked surprisingly well and the reason that we did this was we were already using Postgress for OOLTP workloads and uh so we sort of already had to do this split
  63. 05:06of multiple Postgresses and multiple blobs and so it was really easy for us to try to shift all of our search into Postgress as well because then we only have one system.
  64. 05:15So the setup here was PG vector uh specifically disk NNN TS vector for the search.
  65. 05:20So not BM25 which was you know we lost a little bit of retrieval performance there.
  66. 05:25And what we do is we would partition the table where we would store all the document chunks.
  67. 05:29We'd partition it super aggressively like 4,000 partitions and then each project we'd basically have the project key and we'd binack them into the partitions.
  68. 05:38that actually worked relatively well, but it was expensive and search performance wasn't super super good.
  69. 05:45And what happened was when we scaled a lot, uh everything just broke and exploded.
  70. 05:51And so what happened was the basically you can imagine like you have a bunch of projects and some of them you spin up a project, you work on it and then you close it and you basically never go back to it again.
  71. 06:01And we have a bunch of those where like they never get queried and we have a bunch that get queried all the time because they're super active projects.
  72. 06:06And when we pack them into partitions, the cold ones and the hot ones would land on the same ones and the partitions would get really really big.
  73. 06:13And so when we query them, they would Postgress would pull the partition, put it into memory, we do the stuff and then we create another partition and another partition and it would essentially thrash the cache all the time.
  74. 06:23And what that meant was our latencies would spike.
  75. 06:26So we went from like search and ingestion P99 of 100 milliseconds into 20 seconds, which you can imagine is a really bad user experience.
  76. 06:37So then we went to turbopuffer in about you know when we're about I think 400 million documents something like that and what we did with turbopuffer was we did one name space per project
  77. 06:50and the advantages of moving to turbuffer is we got BM25 real BM25 much better uh relevancy much better latencies and it was extremely simple to operate because we could just have a single
  78. 07:03turbo puffer cluster you know we didn't have to have a bunch of different ones like with Postgress and it would since it's blob based it could just query the blobs that we had anyway
  79. 07:12and so much lower cost and it was extremely simple to operate and we didn't have this problem with the partitions because if a project's not used it's just in blob and so it's really easy and Simon can talk a bit more about why that works so well.
  80. 07:28Yeah.
  81. 07:29So, Legora has some of and and legal in general has, by the way, if if uh Jake and I have similar accents and maybe even look a bit similar, it's because we're both Danish.
  82. 07:39Um, Turboper has a very has a particular architecture that supports these kinds of very regulated environments really really well.
  83. 07:46But in order to understand that, we have to understand what kind of search engine is Turboper.
  84. 07:50Why is it different than the ones that they used in the past?
  85. 07:54Since the very beginning of Turbopuffer, um the design has more or less been the same.
  86. 07:58Uh there may be changes in the future, but the design has stood the test of time.
  87. 08:03When you do a write to Turbopuffer, we write directly to obic storage.
  88. 08:07There is no like disk replication.
  89. 08:10There's no paxas.
  90. 08:11There's there's none of that.
  91. 08:12Direct to S3.
  92. 08:14That's the fundamental trade-off in Turbopuffer, right?
  93. 08:17Hundreds of milliseconds.
  94. 08:18If you're like Shopify and doing inventory uh reservations for a Kylie Jenner flash sale not going to work very very good for search because generally when you're doing search doing a slow write is fine
  95. 08:29um as long as the read performance is is adaptable and good.
  96. 08:32So that's what happens on write it just goes into write ahead log you can imagine you write 1.json 2.json 3.json JSON.
  97. 08:38Obviously, it's a it's a database, so it's not JSON, but for illustrative purposes, that's what happens.
  98. 08:43And in the background, we build the vector indexes, the text indexes, the colmer indexes, and so on to satisfy the queries that that Jake and other customers have.
  99. 08:53Um, so then at query time, we can go in and then um the query reaches some namespace.
  100. 08:59The namespace is kind of our concept of a of a table.
  101. 09:01Um, you can think of it as a directory on S3 that's isolated from everything else.
  102. 09:06We go to the node that is most likely to have it.
  103. 09:08It could go to any node, right?
  104. 09:10It could go to every single node and they're all read replicas, but we go with some affinity to the node that has the highest probability of having it in cache.
  105. 09:16We check the memory cache for any objects, NVME SSD cache, and then finally to object storage.
  106. 09:21Everything in Turbopuffer is optimized around doing as much work in as few round trips as possible.
  107. 09:26Right?
  108. 09:26S3 has a P99 um on a like 1 megaby blob size of around uh 200 milliseconds.
  109. 09:32So, you want to do as few round trips as possible, right?
  110. 09:34Ideally, you do around three.
  111. 09:36And everything in Turbopuffer, the database is Oh, I'm going to need your fingerprint.
  112. 09:42Got it.
  113. 09:42Um, everything in in Turbopuffer is designed around minimizing the number of round trips.
  114. 09:47This is also amazing for modern D discs.
  115. 09:49If you do a lot of concurrency and few round trips, you utilize them optimally.
  116. 09:53And everything in Turbopuffer is designed around this.
  117. 09:56So, why is this so good for a company like Lora?
  118. 09:59Well, object storage native if you design it around the atomic unit of separation being the namespace or the table, every single table could be encrypted with a different key.
  119. 10:09Every single namespace could be in a different bucket.
  120. 10:11We have customers that have um thousands of buckets that they have namespaces in so that their customers get the warm IT fuzzies of having the bucket in their own cloud account.
  121. 10:24They can also be encrypted with their own keys.
  122. 10:26You can share buckets.
  123. 10:27You can you can do whatever configuration that you need at the namespace level.
  124. 10:31You can re-encrypt with different keys.
  125. 10:32You can move them around.
  126. 10:33Um and you can re-encrypt with other keys.
  127. 10:36Um for Lora in particular, this was really important for this full physical separation, right?
  128. 10:41An encryption separation.
  129. 10:42All of the namespaces needed to be physically at rest with different keys and as separated as possible.
  130. 10:48S3, GCS, Azure BOP stories, they pass that um and the other parts of the hierarchy also except the NVME SSD cache because in the SSD cache we consider that to be volatile like memory
  131. 11:00um but your customers did not.
  132. 11:03So in the uh so what we did was that we thought we were going to implement encryption into the disc cache but instead we just disabled the disc cache and saw how it fared and the performance of turbopuffer even without the disc cache with just the memory cache was so good that we just kept it that way
  133. 11:18for some of the lora workloads where we couldn't have the disc cache for multi-tenency turbuffer will support that in the future but it just goes to show the um natural point where turbopuffer
  134. 11:29allows this encryption and storage and separation to become fully multi-tenency uh native.
  135. 11:36I'll hand it back to you on what happened then.
  136. 11:39And then, drum roll please.
  137. 11:41Latencies look like this.
  138. 11:44Um, is my mic working?
  139. 11:47No. Could I or I'll start screaming really loudly.
  140. 11:50Um, it speaks for itself if you can't hear me.
  141. 11:53Okay.
  142. 11:54Um, latency is improved in order magnitude basically.
  143. 11:57And these are median latencies.
  144. 11:58So, P99 were even better.
  145. 12:01Um, so obviously this is a huge thing when you're doing I mean one thing is if you're doing a single sort of rag style thing but if you have an agent that does 20 queries 100 queries these really really add up.
  146. 12:12So that was on the project side and then a more recent thing is legal research.
  147. 12:18So legal research um is a kind of a difficult problem and the reason it's difficult is that um the corpus is extremely big.
  148. 12:26So we're racing towards 10 billion vectors and we're growing extremely fast.
  149. 12:32We also have quite high read.
  150. 12:34Um so QPS can spike a lot because we do a lot of fan out.
  151. 12:38Like if you do a a sort of legal research query, we will fan it out to a bunch of different queries and we'll keep going.
  152. 12:44And the reason we do that is we need this um heavy filtering because essentially it's a kind of like a graph for a few different reasons.
  153. 12:51Firstly, it's um hierarchical.
  154. 12:53you know, you have cities and you have counties and you have states and you have federal law and it's the same all around the world.
  155. 12:59Um, and so you need to respect that authoritative sort of hierarchy.
  156. 13:03There's also some temporal validity.
  157. 13:05So, one judge might overrule a decision that's been made somewhere else and you need to also respect that and figure that out.
  158. 13:11And then sometimes there's even like a new regulation that has exemptions or special cases of an old regulation.
  159. 13:18And so if you're finding this one, you need to find all the other ones as well.
  160. 13:20So you can imagine that it sort of explodes the search and so we started on elastic search for this but also moving to turbuffer um elastic search just got extremely expensive because we have to have everything there
  161. 13:35but um with turbopuffer we can basically uh take different jurisdictions and we can make them name spaces in turbo buffer and that means some of them here's an example where like you have the EU that gets quered all the time that's super hot and some of them let's say Danish law cuz we're Danish, no one cares really.
  162. 13:52It's such a small country, so like it doesn't really get queried and so that can just stay on blob and that's fine.
  163. 13:56Um, and because it's sort of a deep research style workload, if there's 500 milliseconds latency to fetch that cold blob, that's okay.
  164. 14:05That's fine.
  165. 14:05It's not really a big problem.
  166. 14:07So, the way that Turbo is designed lends itself super well to this super long scale of like cold weird name spaces and a few that are really, really hot.
  167. 14:15Um, yeah.
  168. 14:17And Simon wants to talk more about that.
  169. 14:20Yeah.
  170. 14:20So, um um I was I was talking about why the company is called Turbopuffer at another uh talk here earlier today, but uh one of the other explanations of the name of Turbopuffer
  171. 14:32is that it's about puffing into the different memory hierarchies and really mastering when data should be in particular memory hierarchies.
  172. 14:39So, you can think about it here, right, of something like the EU law might be more or less part of almost every one of the legal research uh queries, right?
  173. 14:48So that probably sits closer to NVME SSDs in memory, right?
  174. 14:52The economics kind of change as you move up and down this hierarchy.
  175. 14:55Um in in memory, you want things that are queried a lot, right?
  176. 15:00Then the economics of memory are great.
  177. 15:02NVME SSDs can you can do a lot of things directly on them, but the economics change as you move up and down this boundary, the latency changes and the way that the database is architected to take advantage of it in terms of round trips versus random versus sequential
  178. 15:14all changes as you navigate this hierarchy.
  179. 15:16Turbopuffer is a database that is really designed around the memory hierarchy and all of the smarts in Turbopuffer is that all of these namespaces are puffed in and out um of the cache.
  180. 15:26You can think of this as we want to spend as much time have as much data pushed as far down in this hierarchy as possible to get the the best um performance cost ratios.
  181. 15:35So how does that apply to search?
  182. 15:38Well, for something like vector search for example, there's two fundamental ways to do vector search.
  183. 15:42One is to navigate it basically design a graph.
  184. 15:46The problem with a graph on something like object storage or disk, again, we want to have things as far down that memory hierarchy as possible.
  185. 15:53The problem with a graph, this is not a graph, this is a tree.
  186. 15:56Um, but in a graph, you have to navigate from the center of the graph.
  187. 15:59So, and then every time you navigate through these nodes, you're doing 200 millisecond P99 to S3, right?
  188. 16:06And so, you're trying to shrink the diameter of the graph.
  189. 16:08You're trying to do all these tricks to make the graph, but fundamentally you're at odds with the fact that graph is about a random sequential trade-off that you have in memory and in registers,
  190. 16:18but not further down the memory hierarchy.
  191. 16:20The way Turbopuffer does it is organize it into clusters, right?
  192. 16:23Vectors you can think of in two dimensions just as point is a massive coordinate system and we can organize them into clusters.
  193. 16:29Turbopuffer then creates clusters of clusters and clusters of clusters of clusters to essentially organize all of the vector data in a tree.
  194. 16:38You can basically think of turbopuffer as a very very complicated B tree, right?
  195. 16:43Because it's a tree on this geometry of this entire space and the clustering of it in an approximate way.
  196. 16:48Now the root centroidids further up the tree you can imagine are part of every single time you search right there.
  197. 16:55We're always trying to figure out which clusters that we're in and we're always looking at the upper levels of the tree.
  198. 16:59So they're going to be further of the memory hierarchy, right?
  199. 17:02Closer to the registers almost all in DRAM.
  200. 17:05Now the leaves that have all of the actual legal cases or whatever long document it could be could be images all of that is probably going to be on SSDs with that single 1 millisecond roundtrip at the end.
  201. 17:15It doesn't make sense to have all that puffed into DRAM.
  202. 17:18This is fundamentally the cheapest way that you can run a database period.
  203. 17:24So for something like Lora or even web search which is in hundreds of billion or tens of billions depending on how much of the web you've scraped this is fundamentally the cheapest way to do it.
  204. 17:33And we have customers that are indexing massive parts of the entire web into turbopuffer which is really also a part of what legal research is.
  205. 17:41Full text is also also really respectful of the memory hierarchies.
  206. 17:46The way the text search works is essentially you can think of it as a hashmap.
  207. 17:49You have a big document and then you take every single one of the tokens and you put them into the key in the hashmap.
  208. 17:55The value in the hashmap is some set with all of the document IDs that has that term.
  209. 18:00So then if you search for New York population, you're finding those three places in the hashmap and then you're taking the three sets and doing an intersect on the intersect on the sets.
  210. 18:08While you're intersecting, you're also trying to do some kind of scoring, right?
  211. 18:12A document that has York in it is probably more valuable than a document that has new in it because York is a more rare word.
  212. 18:20When people say BM25, this is the scoring that they're referring to.
  213. 18:25The art of fulltech search is one, we want to minimize the number of round trips.
  214. 18:29So first you download the parts of the dictionary that are relevant.
  215. 18:31Round trip one maybe a round trip one before that to index into where the parts of the term terms are.
  216. 18:37And then the second round trip is to get these massive lists.
  217. 18:41Try to make the list as small as possible by compressing them.
  218. 18:44But also while you're doing the text search you're trying to minimize the amount again of memory bandwidth that you want to intersect these list.
  219. 18:51You can probably imagine that at some point there's a point where you've seen so many documents with population in York that have much higher scores that documents that just have new in it are irrelevant anymore.
  220. 19:01This is like a mega crash course in how text search works.
  221. 19:04And counterintuitively to most people, text search at web scale is more difficult and more computationally expensive than doing vector search.
  222. 19:13I'll hand it over to you.
  223. 19:16Cool.
  224. 19:17So key learnings from um what you heard today, retrieval is extremely important to legora.
  225. 19:24Um it's key to legal reasoning.
  226. 19:27Turbopuffer really excels when uh for us because it makes it extremely easy to operate.
  227. 19:34We have 70 plus tenants.
  228. 19:35We have 100, we have 200 tenants.
  229. 19:37You know, if we had to have separate elastic search databases for each of these, it would be just hell.
  230. 19:41Um but we can do this natively with turboper with data residency and cmech, etc., etc. And then it's extremely cost efficient generally when you have these types of workflows
  231. 19:51um or workloads that we do where there's a long tail of cold indices basically that you don't need to query so much and you don't you're sort of you're okay paying the small latency cost for it.
  232. 20:02So now with um turbo buffer in 4 seconds to go now we can focus on making lora we can focus on the product making it really really great and not on scalability and infra and also David our CFO is really happy about the cost.
  233. 20:14So, it's great.
  234. 20:15Thanks everyone.
  235. 20:17[applause]
  236. 20:34[music]