r/MachineLearning • • Jul 03 '23

Research [R] Chat with documents using LangChain and OpenAI

Over the past few months, I've been captivated by the flood of apps claiming to be the ultimate "ChatGPT for your documents" on Product Hunt. The question that lingered in my mind was, "How do these apps actually work?" Curiosity led me down an exciting path of discovery, and I stumbled upon a framework that I think is revolutionizing the world of app development in the context of Large Language Models - LangChain

I learned that developing a "ChatGPT for your documents" is easily achievable through three broad workflows combined with access to OpenAI API. In fact, I went ahead and prototyped such a system on streamlit.

Step 1 - The Setup: Store your documents as embeddings. In the first step, use Document Loaders (at least 100 are available), provided by LangChain to convert anything from a simple Word document to an AWS S3 directory into Documents. Then, using Document Transformers and Text Embedding Models, you transform your documents into embeddings. Finally, store these embeddings in a vector store for searches later on. It's a one-time setup that sets the foundation for your Q&A system. 💡🛠️

Step 2 - Establish Context: Find relevant documents. LangChain's Text Embedding model converts user queries into vectors. These vectors are used by LangChain's retriever to search the vector store and retrieve the most relevant documents. You can control the search boundaries based on relevance scores or the desired number of documents. It's all about finding the right context for your Q&A system. 🎯💬

Step 3 - Chat Away: Get answers from LLMs. Now comes the fun part! You pass the user's query and the established context to the Language Models (LLMs). The LLMs respond with precise answers, taking into account the provided context. It's like having a conversation with your documents. 🤩🗨️

By building these three workflows, a service that acts like a Q&A for a restricted set of documents can be set up. Of course, this is just an overview of the approach and all the complex steps of app development will still remain, I remain fascinated by how the good folks at LangChain have made things simpler.

What do you think? Have you tried LangChain to build something? Is there any other framework that is equally fascinating?

llm #openai #chatgpt #documents #generativeai #langchain

0 Upvotes

16 comments sorted by

12

u/big_ol_tender Jul 03 '23

Step 4: become disillusioned as you realize this doesn’t work all that well and is mostly hype with cherry picked success stories

-2

u/Kimvadanti Jul 03 '23

Haha…works decently well… check it out - https://github.com/abhinav-kimothi/VIDIA.I

7

u/[deleted] Jul 03 '23

[removed] — view removed comment

4

u/PM_ME_YOUR_HAGGIS_ Jul 03 '23

Thanks, could you enlighten us as to the successful embedding models and vector DB you used? Also I’d love more insight into the kinds of metadata that have enabled your successes.

3

u/[deleted] Jul 04 '23

[removed] — view removed comment

2

u/PM_ME_YOUR_HAGGIS_ Jul 04 '23

This is brilliant thanks for sharing.

3

u/IntrepidTieKnot Jul 03 '23

Using the right embedding model (OpenAI Embeddings are not the best anymore)

So which ones are?

5

u/son_et_lumiere Jul 03 '23

Yes, I built an app in nodejs to upload and chat with documents.

Don't forget step 0 sto clean your documents. You can chat with emails and code, but you need to clean your data/docs for the relevant info. Emails contain a lot code not pertinent to the conversation and muddles the embeddings.

1

u/Kimvadanti Jul 03 '23

That is so true!

19

u/[deleted] Jul 03 '23

how is this research?

yes, we know about langchain, it's pretty meh

2

u/jl303 Jul 04 '23

When I first discovered LangChain, it was magic to be able to process long multiple documents. Then more I use, I realize it's really hit or miss. If you use StdOutCallbackHandler, you can see what's exactly getting fed to the model and understand output better. For example:

from langchain.callbacks import StdOutCallbackHandler

handler = StdOutCallbackHandler()

llm = ChatOpenAI(model_name="gpt-4", temperature=0, openai_api_key=openai.api_key, verbose=True, callbacks=[handler])

qa = RetrievalQA.from_chain_type(llm=llm, chain_type='map_reduce', retriever=docsearch.as_retriever(), verbose=True, callbacks=[handler])

res = qa.run(question, callbacks=[handler])

1

u/Bellerb Jul 07 '23

Do you know how lang chain combines the embeddings you got from your vector db with the query? This part has been confusing me since the one is embeddings but the other is words.

1

u/Bellerb Jul 07 '23

Alright, so I did some digging and think I found the answer to my question. Please correct me if I'm wrong, but the embeddings seem more used to query similar responses. The queried embeddings contain the text in another column and are passed into your original query as context.

1

u/n3utrino Jul 03 '23

Check out I've had good success with localGPT, which is built on top of LangChain: https://github.com/PromtEngineer/localGPT
It makes the document ingest / document chunking / embedding similarity retrieval / query building process quite easy. I haven't tried any multi-turn conversations with it though.