LangChain: Build AI apps with LLMs through composability
github.com
github.com
You can't pass the entire corpus into the prompt. So you might: - preprocess the corpus by iterating over documents, splitting them into chunks, and summarizing them - embed those chunks/summaries in some vector space - when you get a question, search your vector space for similar chunks - pass those chunks to the LLM in the prompt, along with your question
This ends up being a very common pattern, where you need to do some preprocessing of some information, some real-time collecting of pieces, and then an interaction with the LLM (in some cases, you might go back and forth with the LLM). For instance, code and semantic search follows a similar pattern (preprocess -> embed -> nearest-neighbors at query time -> LLM).
Langchain provides a great abstraction for composing these pieces. IMO, this sort of "prompt plumbing" is far more important than all the slick (but somewhat gimicky) "prompt engineering" examples we see.
I suspect this will get more important as the LLMs become more powerful and more integrated, requiring more data to be provided at prompt time.
Specific to prompt-chaining: I've spent a lot of time ideating about where "prompts live" (are they best as API endpoint, as cli programs, as machines with internal state, treated as a single 'assembly instruction' -- where do "prompts" live naturally) and eventually decided on them being the most synonymous with functions (and api endpoints via the RPC concept)
mental model I've developed (sharing in case it resonates with anyone else)
a "chain" is `a = 'text'; b = p1(a); c = p2(b)` where p1 and p2 are LLM prompts.
What comes next (in my opinion) is other programming constructs: loops, conditionals, variables (memory), etc. (I think LangChain represents some of these concepts as their "areas" -> chain (function chaining), agents (loops), memory (variables))
To offer this code-style interface on top of LLMs, I made something similar to LangChain, but scoped what i made to only focus on the bare functional interface and the concept of a "prompt function", and leave the power of the "execution flow" up to the language interpreter itself (in this case python) so the user can make anything with it.
https://github.com/approximatelabs/lambdaprompt
I've had so much fun recently just playing with prompt chaining in general, it feels like the "new toy" in the AI space (orders of magnitude more fun than dall-e or chat-gpt for me). (I built sketch (posted the other day on HN) based on lambdaprompt)
My favorites have been things to test the inherent behaviors of language models using iterated prompts. I spent some time looking for "fractal" like behavior inside the functions, hoping that if I got the right starting point, an iterated function would avoid fixed points --> this has eluded me so far, so if anyone finds non-fixed points in LLMs, please let me know!
I'm a believer that the "next revolution" in machine-written code and behavior from LLMs will come when someone can tame LLM prompting to self-write prompt chains themselves (whether that is on lambdaprompt, langchain, or something else!)
All in all, I'm super hyped about LangChain, love the space they are in and the rapid attention they are getting~
What applications were you envisioning when you built it?
Raw prompt-structure ideas i've worked with:
- Iterate on a prompt with another "discriminator" prompt, that determines the result is good / safe
- Write N-trials of an answer, then use another prompt to select the best answer
- When doing code writing (SQL or Pandas) write the output, then use a parser (eg. `ast` in python) to validate code is valid, if not, feed back into a prompt for fixes
- Logical negation checks (check if X, and if ~X, give opposite answers, then it's likely consistent, if it's both "affirmative" (as the models tend to bias towards), then it's definitely hallucinating)
Other 'product' ideas i've tried:
- A chat style interface (I made a chat-bot last year, similar to chatGPT)
- A "google-this-for-me" style chain, that checks google, summarizes multiple results, then synthesizes a final result
Ideas I've been sitting on, that I think would be fun to prototype:
- An iterative "large document" editor: storing global intent, instructions, outline, and the raw text, and each iteration of the prompt works on the these objects to build a large document.
- A "research this topic for me", similar to the above, but include the google searching, summarizing, and such
- A code-repository "AI agent" that takes `Issues` and `Pull requests` as input, and writes and edits code for you, and by adding feedback in github, it uses that to modify the branch and act as a developer. (Code via github interface, rather than an IDE)
https://github.com/daveshap/LiteratureReviewBot
It does something similar, but uses the ArXiv dataset to search PDFs instead of the internet.
The next step for me is the workflow composition part. Instead of a functional model of python functions Im going to try to compose workflows with AWS step functions where each step function calls a particular one of the templated api endpoints.
I'm excited to see your progress.
I'm guessing that most folks haven't created composable prompts, so a few experiences on why this stuff is necessary. I've been building a language learning app and it makes heavy use of GPT-3 [1], which has made clear a number of things for me:
1) Composability is fundamental to leveraging language models. You can't get language models to just generate things in one go. It's rather like a human. If you're writing a book, you start with an idea, then an outline, then a chapter outline, then writing paragraphs... Or in our case, generate a flashcard deck description, then vocab, then example sentences, etc.
2) Externalized prompt templates are also important. Engineers need to be able interface with experts who can create custom prompts. E.g. in our case, I need experts who speaks to build prompts specific to other languages for a language learning app [2].
3) Unit testing is critical. There is no linter for a prompt. I have made so many typos over the last year that broke things. OpenAI has released several new models over the last 2 years. Anthropic is coming out with a model. You need to have assurances your prompts work. I've actually had to start building a basic unit tester for our prompts because of this... [3] (please someone else do this so I don't have to)
4) external data is the next step forward for large language models. E.g. in my case, someone may want to learn about the history/culture of a country and we may want to reference existing articles on it since LLMs are known to hallucinate. We need to be able to interface with the web and databases easily. I'm not convinced that LangChain is the write layer of abstraction for this. I suspect/hope that the next version of GPT will have some significant advances in this regard.
2. https://github.com/squidgyai/squidgy-prompts - open source composable prompts for Squidgies
3. https://github.com/squidgyai/squidgy-prompts/tree/main/tests - unit test in YAML
I just feel like this entire concept verges on this: https://imgur.com/EiGL1Z0
GPT-3.5 and Wolfram Alpha via LangChain - https://news.ycombinator.com/item?id=34422122
By far the most interesting aspect of this, for me, is that we're now seeing tools for building software infrastructure with layers of APIs that operate on -- gasp! -- natural language, which is notoriously prone to imprecision and ambiguity. And yet it works remarkably well. It's hard not to look at all this, mouth agape, in awe.
Part of me wonders, though:
Wouldn't it be better if we could compose LLMs by passing sequences of embeddings (e.g., in a standardized high-dimensional space), which are much richer representations of LLM input, internal, and output states?
Imagine if you and others building apps had access to "GPT3 deep sequence embeddings v1.0" via an API.
Imagine getting generated text from a GPT LLM that comes with a deep embedding of each generated token's "contextual meaning":
[(text_token, deep_emb), (text_token, deep_emb), ...]
allowing higher-level models and apps to use all the information in those rich representations as inputs.by which measure are you making this claim? even a 95% reliability means you get 5% wrong. on top of that you have prompt injection attacks. this stuff is much less suitable the more you move away from demos to predictable business applications
What I did say is that I'm in awe at the fact that this stuff works as well as it does, given that natural language is so notoriously prone to imprecision and ambiguity. I mean, if you had told me six months ago that this would be working even "95%" of the time in demos, I would have said, no way.
Basically, I agree with you that at present this becomes "less suitable the more you move away from demos to predictable business applications" :-)
I am prototyping new features where I work, on top of GPT3. To get it beyond fancy demos and actually delivering customer utility, you need a LOT of work to build in robustness and correctness.
Prompt chains is where I’ve landed at the moment. This library seems very relevant and timely.
While bias is the more politically charged problem, variance is the problem of the current iteration that needs to be solved.
Using a Google search api + a calculator to answer a question is cool [0]. but... we could already do that?
[0] https://langchain.readthedocs.io/en/latest/modules/agents/ex...
For instance, if you want it to answer questions about your code-base, the model doesn't know your code base. You can't feed the entire code-base into a prompt. So, you'd use langchain to: - preprocess your code-base, by chunking it and embedding it in some vector space - when you get a question, see where it is in the vector space and find the "k nearest neighbors" - pass those nearest neighbors, along with your question, to the LLM (because those neighbors are the contextually relevant pieces, and they'd fit in the prompt)
AI > Yes, you can use Docker with LangChain. For more information, please see the Docker Installation Guide in the LangChain documentation.
Then it links to a 404 lol. I checked the docs and there is nothing about docker in them. I wonder why its incorrect here
It seems as if it should also have the entire knowledge of the LLM, but
> Who was Thomas Jefferson?
Outputs
> Hmm, I'm not sure. I'm an AI assistant for the open source library LangChain. You can find more information about LangChain at https://langchain.readthedocs.io.
Very useful article explaining this approach: https://dagster.io/blog/chatgpt-langchain
It doesn't explain why the model only refers to the documentation though. Basically what they are doing is giving GPT-3 a prompt that includes the (semantically relevant) pieces of the documentation.
But I don't see why a User can't ask about something that is tangentially relevant ("Who is Bill Gates?") and get an answer that really comes from GPT-3 pre-existing knowledge.
And very clever way for these guys to go from "hey we found this cool problem" to "well, did you notice that it ends up super complex and slow? Well well well, we could make it so much better with... wait for it... a data pipeline!"
("...and don't we just happen to sell data pipeline software! What a coincidence!")
lol. Great read, thank you for the recommendation.
Last time I was challenged by regex I easily found very fancy web page with so many nice features. Actual documentation, ability to select a specific regex engine (or implementation or whatever you call it) , real-time results on test data, highlighting that shows how the regex works, etc. and that was years ago I’m sure there’s even better web apps now.
I can’t imagine having a better experience asking AI chat than using a web app made for the purpose
Being able give some examples and just state in plain English what you want the capture groups to be is pretty much my ideal regex experience (in other words, I don’t want to think about the semantics of regex ever).
Btw I asked chat.langchain.dev and it said:
> LangChain uses pre-trained models from Hugging Face, such as BERT, GPT-2, and XLNet. For more information, please see the Getting Started Documentation[0].
That links to a 404, but I did find the correct link[1]. Oddly that doc only mentions an OpenAI API wrapper. I couldn’t find anything about the other models from huggingface.
Does LangChain have any tooling around fine tuning pre-trained LLMs like GPTNeoX[2]?
[0]https://langchain.readthedocs.io/en/latest/getting_started.h...
[1]https://langchain.readthedocs.io/en/latest/getting_started/g...
One of their examples is
from langchain import NLPCloud
nlpcloud = NLPCloud(model="gpt-neox-20b")
So it looks like you're good to go.I am working on a project in the old media space and we were planning to build several of these elements ourselves if something like this didn’t come along. Love the open source nature and would love to contribute.
Had a play around with this early-GPT3 days by creating a Python decorator that let you easily write "prompt functions" and then you could combine these to create higher-level prompt generating machines.
LangChain takes this to the logical end-point, awesome!
https://github.com/jerryjliu/gpt_index https://github.com/jerryjliu/gpt_index/blob/main/examples/pa...
GPT isn't multi-modal yet (so no images), but that's coming.
Academics became a shameful thing.
https://ai.googleblog.com/2022/02/guiding-frozen-language-mo...
There's a really strong dependency on their service and I sometimes get doubtful about dedicating time to build things on top of it.
They already do: https://openai.com/api/pricing/
This would be a great way to get a pair programmer for people who are just coding for hobby or transitioning to SWE. Or to alleviate imposter syndrome.
how can we use ChatGPT to design a 3d model?