Live data from Hacker News

The Problem with LangChain

minimaxir.com

81–90 of 97 posts

Re: The Problem with LangChain

#81

People have already said that LangChain is useful to get ideas about what can be done with LLMs past the single prompt-response dynamic. Something else I thought to add is that LangChain is also useful for prototyping, so you'll be able to have a proof of concept of your idea before the weekend ends. I do agree that once you have that nailed down then you should rewrite everything from the ground up. I do recommend p…

I've looked at LangChain multiple times and there is some cool stuff in there to enable a quick prototype. That said, needing ALL the cool stuff in one particular use is unlikely and trying to figure out what to do when you have a specific requirement might not be worth the learning curve.

To illustrate the complexity of this, here's a list of things that you might have to do if implementing a document bot:

  1. Handle uploading or storing documents somewhere and keeping track of the location.
  2. Handling different document types. Sticking to PDFs for this list.
  3. Manually or using the PDF to augment the documents with tags, keyterms, titles, etc.
  4. At this point you need somewhere to store the metadata. Maybe a DB or using the vector store.
  5. Just dealing with PDFs requires some type of PDF library. Other documents may or may not require an additional library.
  6. Extracting text from the PDF with something like pdf2image. Not all PDFs have extracted (selectable) text in them. Also, PDFs have images, which sometimes have text.
  7. Doing some sort of OCR is very likely. Think about OCR'ing the whole thing to deal with no extracted text/images with text.
  8. Assuming that, converting pages to images. Also, consider images have data in them, so extracting an image from the image and running some type of detection on it...
  9. Using some OCR model to extract text, or figure out how to extract them from the PDF data.
  10. Cleaning up that text, then parsing it cleanly. nltk comes into play here.
  11. Fragmentation/windowing of text. How long to create the fragments? Or should it be variable?
  12. Using the text to get more text via a prompt to a model. Here we can get additional keyterms, or perhaps a summary or question about the text fragment. (we'll use something we write for prompting the LLM here in a second)
  13. Storing the fragment. Most people use a vector database for this now, so we can use Weaviate or Pinecone, or ???. Also, consider a moderate amount of fragments and their vectors can be stored in a pickle format with manual dot products for ranking.
  14. Figuring out where you are going to get a user prompt. Assuming the easiest thing, collect input from the user in a command prompt.
  15. What to do with the user's prompt once you get it. Do you ask an LLM for more info on the prompt? Or do you just jump to...
  16. Embed the user's prompt to get a vector back. (Weaviate does this transparently, but you can easily do it yourself using the ada-002 endpoint from OpenAI)
  17. Taking that vector (from the embedding/inference to the embed model) do a comparison to other vectors/text you've stored.
  18. Think about what text is important for a new prompt to the LLM. Should it contain directives? How much reference text from the documents does it need? Is a cosign distance or some approximate nearest neighbor match going to be enough?
  19. Think about augmenting the vector search with keyterms that were extracted earlier (by both the PDF itself + any LLM inference step you impelment)
  20. Take the text you pull back from wherever you stored the vector/text and then build a long string to stuff into a prompt.
  21. Consider some type of template structure for the prompts, so you can tweak them without losing your mind. String templates for files in Python are great for $this.
  22. Calling the various LLM endpoints. There are multiple models, in a variety of API endpoints, with tokens usually for auth.
  23. Consider you may just want text back from the LLM, or maybe you want it to complete or write a dict or array (in which case you may want to make this configurable). You may want to eval things that the LLM writes too.
  24. Consider the LLM (ChatGPT for example) may do a completion that contains a block delimited by ```python or similar.
  25. Consider those two things may require different completion endpoints, and some endpoints may be deprecated by the provider later.
  26. Think if you need function completion calling. GPT-X supports this, so you need a function and a way to pass that function's parameters to the LLM. 
  27. Build the prompt and submit it. Don't forget to protect your tokens, using env or config.py files.
  28. Take the response and do something with it that makes sense. Maybe give it to the user, or use it to build another prompt.
  29. Loop back to interact with the user. If the interaction is complicated, like with Discord integration, you may have to do this asynchronously.
  27. Think about storing the interaction for use in building future prompts. Hack this into #15.
  28. Always consider optimizing your prompt length.
  29. Consider how many tokens you are chewing through doing all this.
  30. Consider questions by the user about "what is on page 2?" need context. Another good one is "how many pages is this document", or "what is the title of the document?". A hard one would be "how many images are in this PDF?", meaning how many illustrations...
  31. If the document discusses code, and the model outputs code, or SQL, do you run it and if you do, how?
Example of most of this in action: https://github.com/FeatureBaseDB/DoctorGPT

Re: The Problem with LangChain

#82
post #76
post #3

I added a Python library API to my LLM CLI tool recently which offers a very lightweight way to call models: https://llm.datasette.io/en/stable/python-api.html import llm model = llm.get_model("gpt-3.5-turbo") model.key = 'YOUR_API_KEY_HERE' response = model.prompt( "Five surprising names for a pet pelican" ) print(response.text()) Or you can stream the responses like this: response = model.prompt( "Five diabolical n…

Nice. Now all we need is a vector database atop SQLite.

I had a go at one of those a few months ago: https://datasette.io/plugins/datasette-faiss

Alex Garcia built a better one here as a SQLite Rust extension: https://github.com/asg017/sqlite-vss

Re: The Problem with LangChain

#83
post #3

I added a Python library API to my LLM CLI tool recently which offers a very lightweight way to call models: https://llm.datasette.io/en/stable/python-api.html import llm model = llm.get_model("gpt-3.5-turbo") model.key = 'YOUR_API_KEY_HERE' response = model.prompt( "Five surprising names for a pet pelican" ) print(response.text()) Or you can stream the responses like this: response = model.prompt( "Five diabolical n…

This looks wonderful, a similar breath of fresh air to using the requests library for the first time. Really impressed by the amount of documentation too. Is support for embedding and querying a corpus of custom text planned at all? 99% of what I wanted to use langchain for was building a chatbot that can answer questions about my own documents.

Undecided yet, but I think there's a good chance embedding stuff will eventually show up as an LLM plugin.

Re: The Problem with LangChain

#84
I don't find it difficult to read the langchain code, although I think the documentation needs to be improved. It is based on a simple concept that is easy to understand, and once you get used to it, you will find it rather easy to read.

However, I think that langchain's minor version should be increased to 1 or more. The current latest version of langchain is 0.0.234. I think it's a problem that the use in production is very peaky because all the changes that should be used properly by minor, patch version, etc. are all lumped together.

Re: The Problem with LangChain

#85

Earlier quoted context omitted.

This is basically the middleware pattern except that each function is responsible for calling the next function in the chain. As a consequence, and contrary to the classic middleware pattern, the chain goes both ways, up to thhe api call, and down returning the result. The first expr is indeed special. It is in this context a level 0 function: it takes a context, does stuff, and returns the context. The other functio…

Thanks. Have you found good libraries for working with openai api's in clj?

According to https://github.com/search?q=language:clojure%20gpt&type=repo...

The most advanced lib for dealing with LLMs is

https://github.com/zmedelis/bosquet

There is also https://github.com/cjbarre/multi-gpt/tree/main but it hasn't been update in 3 months and seems rather basic.

Alternatively, you can shoot me an email at

(->> '(102 117 110 116 97 105 110 64 109 101 46 99 111 109) (map char) (apply str))

and I'll prepare a repo for what I've been working on. It's usable but I wanted to clear some things up before a public release.

Re: The Problem with LangChain

#86
On my latest project I’ve been using it in a few places and rolling my own stuff in others. It’s definitely handy for ingesting documents, chunking, and handling IO with vector dbs.

With OpenAI functions though, I find it easier to just make a local sequence of function executions than work w langchain abstractions.

Re: The Problem with LangChain

#87

The core data structure, the Chain, is basically just a function. Combining chains is function composition, like literally it's just f(g(x)), but incompatible with _your_ f's and g's without an adapter. Read this page and mentally swap "chain" for "function": https://python.langchain.com/docs/modules/chains/foundationa... They build all these adapters and integrations and make it seem like they're helping you piece t…

> ...but in how many cases were they necessary as a middleman? Some prefer React, somet NextJS. LangChain has its place.

React works. NextJS works. OpenAI's ChatCompletion API with `functions` works. Go on langchain's discord and ask their documentation bot anything.

Re: The Problem with LangChain

#88

I'm in the exact same spot as the author just a few days in instead of months. Frankly I could see langchain is garbage software just by looking at the code. It still helps me get shit done fast to figure out how things are supposed to work. Sort of a cookbook of AI recepies. Once I have an approach narrowed down I'll rewrite everything on top of stuff langchain is supposedly wrapping. For now it's faster than tracki…

LLM calls are just function calls, so most functional composition is already afforded by any general-purpose language out there. If you need fancy stuff, use something like Python‘s functools.

Working on https://github.com/eth-sri/lmql (shameless plug, sorry), we have always found that compositional abstractions on top of LMQL are mostly there already, once you internalize prompts being functions.

Re: The Problem with LangChain

#89

Earlier quoted context omitted.

GPT-4 has good json support now. It’s nice to be able to give it a a schematic get structured json back.

But why tho? At the start of the chain there's a human and its natural language questions. At the end of the chain there's a human waiting natural language answers. In between you may want some form of data storage, and natural language can be that as well, as it makes retrieval trivial for LLM.

Why wouldn’t you want structured data out?

It also feels like you can keep it more on track by telling it to put the data into specific fields with very specific descriptions.

Re: The Problem with LangChain

#90
Exact same feelings as the author. Tried using langchain for my q and a task. I hoped it would let me a oid manually dealing with embeddings. Except... Perf was horrible and it spent my entire opening API quota in an hour or so

Decided to reimplements using embeddings and my own glue code. Took like a week (much less than the langchain work), and it's cheaper and better

Post reply on HN