Harrison/qa eg (#3052)

Co-authored-by: Sukhpal Saini <bdcorps@users.noreply.github.com>
fix_agent_callbacks
Harrison Chase 1 year ago committed by GitHub
parent db7106cb79
commit eee2f23a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,8 +2,7 @@
> [Conceptual Guide](https://docs.langchain.com/docs/use-cases/qa-docs)
Question answering in this context refers to question answering over your document data.
Question answering in this context refers to question answering over your document data.
For question answering over other types of data, please see other sources documentation like [SQL database Question Answering](./tabular.md) or [Interacting with APIs](./apis.md).
For question answering over many documents, you almost always want to create an index over the data.
@ -12,29 +11,37 @@ This can be used to smartly access the most relevant documents for a given quest
See [this notebook](../modules/indexes/getting_started.ipynb) for a more detailed introduction to this, but for a super quick start the steps involved are:
**Load Your Documents**
```python
from langchain.document_loaders import TextLoader
loader = TextLoader('../state_of_the_union.txt')
```
See [here](../modules/indexes/document_loaders.rst) for more information on how to get started with document loading.
**Create Your Index**
```python
from langchain.indexes import VectorstoreIndexCreator
index = VectorstoreIndexCreator().from_loaders([loader])
```
The best and most popular index by far at the moment is the VectorStore index.
**Query Your Index**
```python
query = "What did the president say about Ketanji Brown Jackson"
index.query(query)
```
Alternatively, use `query_with_sources` to also get back the sources involved
```python
query = "What did the president say about Ketanji Brown Jackson"
index.query_with_sources(query)
```
Again, these high level interfaces obfuscate a lot of what is going on under the hood, so please see [this notebook](../modules/indexes/getting_started.ipynb) for a lower level walkthrough.
## Document Question Answering
@ -51,6 +58,7 @@ chain.run(input_documents=docs, question=query)
```
The following resources exist:
- [Question Answering Notebook](../modules/chains/index_examples/question_answering.ipynb): A notebook walking through how to accomplish this task.
- [VectorDB Question Answering Notebook](../modules/chains/index_examples/vector_db_qa.ipynb): A notebook walking through how to do question answering over a vector database. This can often be useful for when you have a LOT of documents, and you don't want to pass them all to the LLM, but rather first want to do some semantic search over embeddings.
@ -67,11 +75,19 @@ chain({"input_documents": docs, "question": query}, return_only_outputs=True)
```
The following resources exist:
- [QA With Sources Notebook](../modules/chains/index_examples/qa_with_sources.ipynb): A notebook walking through how to accomplish this task.
- [VectorDB QA With Sources Notebook](../modules/chains/index_examples/vector_db_qa_with_sources.ipynb): A notebook walking through how to do question answering with sources over a vector database. This can often be useful for when you have a LOT of documents, and you don't want to pass them all to the LLM, but rather first want to do some semantic search over embeddings.
## Additional Related Resources
Additional related resources include:
- [Utilities for working with Documents](/modules/utils/how_to_guides.rst): Guides on how to use several of the utilities which will prove helpful for this task, including Text Splitters (for splitting up long documents) and Embeddings & Vectorstores (useful for the above Vector DB example).
- [CombineDocuments Chains](/modules/indexes/combine_docs.md): A conceptual overview of specific types of chains by which you can accomplish this task.
## End-to-end examples
For examples to this done in an end-to-end manner, please see the following resources:
- [Semantic search over a group chat with Sources Notebook](question_answering/semantic-search-over-chat.ipynb): A notebook that semantically searches over a group chat conversation.

@ -0,0 +1,246 @@
Sure, here's a sample chat with Joey, Rachel, and Monica!
Rachel: Hey guys, how was your day?
Joey: It was pretty good, Rach. I went to the Museum of Natural History today.
Monica: Oh, I love that place! Did you see the dinosaur exhibit?
Joey: Yeah, it was amazing! They had this huge T-Rex skeleton and I was like, "Whoa, that guy's got some serious bite!"
Rachel: (laughs) Classic Joey.
Monica: So, Rachel, what did you do today?
Rachel: Well, I had to work at Central Perk all day. But it wasn't too bad because Gunther let me have an extra espresso shot in my latte.
Joey: (smirks) That's my girl, always pushing the limits.
Monica: Speaking of limits, I went to Barry's Bootcamp this morning and it was insane. I feel like I worked out every muscle in my body.
Rachel: Ugh, I hate working out. Can we please talk about something more fun?
Joey: I know what's fun! I saw my old buddy Chandler today.
Monica: Chandler?! I haven't seen him in ages! How's he doing?
Joey: He's good. He's still living in Tulsa and working at that data processing company.
Rachel: (sarcastically) Wow, sounds thrilling.
Joey: Hey, don't knock it 'til you try it. Chandler's got a great life there. He's got a house, a car, and a pet duck named Yasmine.
Monica: (laughs) A pet duck? Only Chandler would do something like that.
Rachel: You know what's even crazier? I heard Ross went to a furry convention last weekend.
Joey: (spits out his drink) What?! That's insane! Why would he do that?
Monica: I don't know, but I heard he dressed up like a giant squirrel.
Rachel: (laughs) I can't even imagine what that would look like.
Joey: (shakes his head) Ross, man. He's always been a little...quirky.
Monica: (smiling) That's why we love him.
Rachel: (smiling back) Yeah, he's our weird little friend.
Joey: (raising his glass) To weird friends and fun days!
Monica and Rachel: (raising their glasses) Cheers!
Joey: Hey, did you guys hear about that new restaurant that just opened up in the city?
Monica: No, what restaurant?
Joey: It's called "The Hungry Lobster". They serve the biggest lobster you've ever seen!
Rachel: Oh, that sounds amazing! We have to go there.
Monica: Definitely. But first, let's plan a night out with everyone. Maybe we can invite Phoebe and Chandler too?
Joey: (excitedly) Yes! The whole gang together again.
Rachel: (smiling) It'll be just like old times.
Monica: (nodding) We can go to a comedy club or a karaoke bar. What do you guys think?
Joey: Karaoke, definitely. I'll show you guys my killer rendition of "Livin' on a Prayer".
Rachel: (laughs) Oh boy, I can't wait for that.
Monica: (smiling) Okay, so we'll plan for next weekend. Let's make sure everyone can come.
Joey: (raising his glass) To old friends and new memories!
Rachel and Monica: (raising their glasses) Cheers!
As they continued chatting and laughing, they couldn't help but feel grateful for their close friendship and the memories they've shared together. It didn't matter where they were or what they were doing, as long as they were together, they knew they would always have a good time.
As the night went on, the trio found themselves reminiscing about some of their funniest moments together.
Joey: Hey, remember when we all went to that escape room and Ross got stuck in the trapdoor?
Monica: (laughing) Yes! And he was yelling for help like a little kid.
Rachel: (chuckles) Or what about the time when Phoebe got her head stuck in the turkey?
Joey: (cracking up) Oh man, that was hilarious. She looked like she had a turkey on her head!
Monica: (smiling) And let's not forget about the time when Joey got his head stuck in the doorframe.
Joey: (rolling his eyes) Yeah, thanks for bringing that up, Mon.
Rachel: (grinning) Hey, it was a classic moment. We all got a good laugh out of it.
As the night came to an end, the trio hugged each other goodbye, promising to make more fun memories in the future.
Monica: (smiling) I'm so glad we have each other.
Rachel: (nodding) Me too. You guys are like family to me.
Joey: (grinning) Yeah, and the best part is, we're not related by blood so we don't have to invite our weird cousins to our parties.
The girls laughed and waved goodbye to Joey as he walked away, still smiling.
As they walked home, Rachel turned to Monica and said, "You know, we really are lucky to have each other."
Monica nodded in agreement. "Yeah, we are. And I wouldn't trade our crazy, hilarious, and unforgettable moments for anything in the world."
Rachel smiled. "Me neither."
And with that, they continued walking, looking forward to many more fun-filled days and nights with their beloved friends.
The next day, Rachel decided to surprise the gang by booking a private cooking class with a famous chef from Paris.
Rachel: Hey guys, I have a surprise for you!
Joey: (excitedly) What is it? Did you win the lottery?
Monica: (rolling her eyes) Oh, stop it Joey.
Rachel: (smiling) No, even better. I booked us a private cooking class with Chef Jean-Pierre!
Monica: (gasping) What?! Rachel, that's amazing!
Joey: (impressed) Chef Jean-Pierre? He's like, the king of cuisine!
Rachel: (nodding) I know, right? We're going to learn how to make his famous coq au vin.
Monica: (grinning) I can't wait! When is it?
Rachel: (checking her phone) It's this Saturday at 2 pm.
Joey: (smiling) This is going to be awesome.
As Saturday approached, the gang found themselves eagerly anticipating the cooking class. They arrived at the kitchen and were greeted by Chef Jean-Pierre himself.
Chef Jean-Pierre: Bonjour mes amis! Welcome to my kitchen.
Monica: (smiling) Bonjour Chef! We're so excited to learn from you.
Rachel: (nodding) Yes, we're huge fans of your cuisine.
Chef Jean-Pierre: (smiling) Merci beaucoup. Now, let's get started.
For the next few hours, the gang chopped, sautéed, and simmered their way to making the perfect coq au vin. Chef Jean-Pierre showed them his secret ingredients and techniques, and they all enjoyed the delicious meal together.
Joey: (smiling) This is amazing. I feel like I'm in a fancy French restaurant.
Monica: (nodding) And to think, we made this ourselves!
Rachel: (grinning) We're like professional chefs now.
As they finished their meal and said their goodbyes to Chef Jean-Pierre, the gang couldn't help but feel grateful for their friendship and the amazing experiences they shared together.
Rachel: (smiling) This was definitely one of the best days ever.
Monica: (nodding) I agree. And it's all thanks to you, Rach.
Joey: (grinning) Yeah, you're like our own personal Julia Child.
Rachel: (laughing) Hey, I'll take that as a compliment.
And with that, they walked out of the kitchen, already planning their next adventure together.
As they walked down the street, they stumbled upon a street fair happening just a few blocks away.
Monica: (excitedly) Oh my gosh, look at all the food stalls!
Joey: (smelling the air) I can smell the funnel cakes from here.
Rachel: (grinning) Let's check it out!
They weaved their way through the crowd, trying all sorts of delicious street food and drinks. They even came across a dunk tank with a familiar face sitting inside.
Rachel: (gasping) Oh my gosh, it's Gunther!
Joey: (laughing) This I gotta see.
Monica: (smiling) Let's do it!
They each took a turn trying to dunk Gunther, and eventually Rachel got the bullseye and sent him tumbling into the water.
Rachel: (laughing) Sorry Gunther, but that was too fun.
Gunther: (climbing out of the tank) No worries, Rachel. You got me good.
As the night wore on, the gang found themselves exhausted but happy from their fun-filled day.
Joey: (smiling) I don't know about you guys, but I think I'm ready for bed.
Monica: (nodding) Yeah, me too.
Rachel: (grinning) But before we go, we have to take a picture together!
They huddled together and snapped a selfie, capturing the moment forever.
Rachel: (smiling) This was such an amazing day.
Monica: (nodding) Agreed. And we have to do it again soon.
Joey: (grinning) Yeah, and maybe next time we can try something even crazier.
As they said their goodbyes and went their separate ways, the gang couldn't help but feel grateful for their friendship and the many adventures they shared together. They knew that no matter where life took them, they would always have each other to rely on and to make the most of every moment.
The next day, Monica decided to invite the gang over to her apartment for a game night. She prepared some snacks and drinks and set up a board game on the coffee table.
Monica: (smiling) Okay guys, who's ready to play?
Rachel: (grinning) I am! What game are we playing?
Monica: (holding up the box) We're playing Monopoly!
Joey: (groaning) Oh no, not Monopoly. That game goes on forever.
Monica: (laughing) Don't worry Joey, we'll make sure it doesn't go on too long.
As they played the game, the gang found themselves getting more and more competitive. Rachel ended up buying all the properties on one side of the board, while Joey and Monica fought over who would control the other side.
Monica: (smirking) You landed on my property, Joey. That's gonna cost you $200.
Joey: (grumbling) Come on Mon, be a pal. I'm already broke!
Rachel: (grinning) Looks like someone's not cut out for the real estate business.
As the game went on, the gang found themselves laughing and joking with each other, forgetting about the stresses of their daily lives.
Monica: (smiling) This is so much fun. We should do this more often.
Rachel: (nodding) Yeah, it's like we're kids again.
Joey: (grinning) And I love beating you guys at this game.
Monica: (laughing) Oh yeah, keep dreaming Joey.
As the night wore on, the gang decided to take a break from the game and watch a movie together. They snuggled up on the couch and watched a comedy, laughing and enjoying each other's company.
Rachel: (yawning) Okay guys, I think it's time for me to head to bed.
Monica: (nodding) Yeah, it's getting late. Thanks for coming over, everyone.
Joey: (smiling) Yeah, this was great. Let's do it again soon.
As they hugged and said their goodbyes, the gang couldn't help but feel grateful for their friendship and the simple pleasures of spending time together. They knew that even though life could be tough sometimes, they had each other to rely on and to make even the most mundane days into something special.

@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Semantic Search over a group chat messages\n",
"In this tutorial, we are going to use Langchain + Deep Lake with GPT4 to semantically search a group chat.\n",
"\n",
"View a working demo here: https://twitter.com/thisissukh_/status/1647223328363679745"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Install required packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!python3 -m pip install --upgrade langchain deeplake openai tiktoken"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Add API keys"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import getpass\n",
"from langchain.document_loaders import PyPDFLoader, TextLoader\n",
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
"from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter\n",
"from langchain.vectorstores import DeepLake\n",
"from langchain.chains import ConversationalRetrievalChain, RetrievalQA\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.llms import OpenAI\n",
"\n",
"os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')\n",
"os.environ['ACTIVELOOP_TOKEN'] = getpass.getpass('Activeloop Token:')\n",
"os.environ['ACTIVELOOP_ORG'] = getpass.getpass('Activeloop Org:')\n",
"\n",
"org = os.environ['ACTIVELOOP_ORG']\n",
"embeddings = OpenAIEmbeddings()\n",
"\n",
"dataset_path = 'hub://' + org + '/data'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"## 2. Create sample data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can generate a sample group chat conversation using ChatGPT with this prompt:\n",
"\n",
"```\n",
"Generate a group chat conversation with three friends talking about their day, referencing real places and fictional names. Make it funny and as detailed as possible.\n",
"```\n",
"\n",
"I've already generated such a chat in `../../messages.txt`. We can keep it simple and use this for our example.\n",
"\n",
"## 3. Ingest chat embeddings\n",
"\n",
"We load the messages in the text file, chunk and upload to ActiveLoop Vector store."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"messages.txt\") as f:\n",
" state_of_the_union = f.read()\n",
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"pages = text_splitter.split_text(state_of_the_union)\n",
"\n",
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)\n",
"texts = text_splitter.split_documents(pages)\n",
"\n",
"print (texts)\n",
"\n",
"dataset_path = 'hub://'+org+'/data'\n",
"embeddings = OpenAIEmbeddings()\n",
"db = DeepLake.from_documents(texts, embeddings, dataset_path=dataset_path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Ask questions\n",
"\n",
"Now we can ask a question and get an answer back with a semantic search:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"db = DeepLake(dataset_path=dataset_path, read_only=True, embedding_function=embeddings)\n",
"\n",
"retriever = db.as_retriever()\n",
"retriever.search_kwargs['distance_metric'] = 'cos'\n",
"retriever.search_kwargs['k'] = 4\n",
"\n",
"qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type=\"stuff\", retriever=retriever, return_source_documents=False)\n",
"\n",
"# What was the restaurant the group was talking about called?\n",
"query = input(\"Enter query:\")\n",
"\n",
"# The Hungry Lobster\n",
"ans = qa({\"query\": query})\n",
"\n",
"print(ans)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading…
Cancel
Save