from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.documents import Document
= ChatOpenAI(model="gpt-3.5-turbo-0125") llm
22 Telephone Bot
= """
message You are the superheroes phone number assistance. User will ask you about superheroes phone number.
Your task is to search phone number from the provided 'Phone book' only.
Phone book:
{phone_book}
User: {question}
"""
= ChatPromptTemplate.from_messages([("human", message)])
prompt
print(prompt.invoke({"phone_book": "your phone book",
"question": "your question",
"message": "HI"}).to_string())
Human:
You are the superheroes phone number assistance. User will ask you about superheroes phone number.
Your task is to search phone number from the provided 'Phone book' only.
Phone book:
your phone book
User: your question
22.1 Documents
="""
phone_book1Superhero Name: Phone Number
---
Thunderbolt Titan: 12345
Shadow Specter: 67890
Solar Flare: 23456
Frostbite Fury: 78901
Iron Guardian: 34567
---
"""
="""
phone_book2Superhero Name: Phone Number
---
Quantum Quasar: 89012
Dynamo Dynamo: 45678
Emerald Enforcer: 90123
Crimson Cyclone: 56789
Lightning Lynx: 01234
---
"""
= [
phonebook_doc
Document(=phone_book1,
page_content={"source": "phone_book1"},
metadata
),
Document(=phone_book2,
page_content={"source": "phone_book2"},
metadata
)
]
type(phonebook_doc[0])
langchain_core.documents.base.Document
22.2 Markdown Doc
from langchain_community.document_loaders import UnstructuredMarkdownLoader
from langchain_core.documents import Document
= UnstructuredMarkdownLoader("phonebook.md")
loader
= loader.load()
phonebook_doc_md assert len(phonebook_doc_md) == 1
assert isinstance(phonebook_doc_md[0], Document)
type(phonebook_doc_md[0])
langchain_core.documents.base.Document
= phonebook_doc_md[0].page_content
phonebook_content print(phonebook_content)
title: "Superheroes PhoneBook"
Provided below are superheroes phone book, provided as Superhero Name: Phone Number:
Thunderbolt Titan: 12345
Shadow Specter: 67890
Solar Flare: 23456
Frostbite Fury: 78901
Iron Guardian: 34567
Quantum Quasar: 89012
Dynamo Dynamo: 45678
Emerald Enforcer: 90123
Crimson Cyclone: 56789
Lightning Lynx: 01234
Nick Fury: 32453
= UnstructuredMarkdownLoader("phonebook.md", mode="elements")
loader
= loader.load()
data print(f"Number of documents: {len(data)}\n")
for document in data[:2]:
print(f"{document}\n")
Number of documents: 12
page_content='title: "Superheroes PhoneBook"' metadata={'source': 'phonebook.md', 'last_modified': '2024-05-28T00:05:15', 'languages': ['eng'], 'filetype': 'text/markdown', 'filename': 'phonebook.md', 'category': 'Title'}
page_content='Provided below are superheroes phone book, provided as Superhero Name: Phone Number:' metadata={'source': 'phonebook.md', 'last_modified': '2024-05-28T00:05:15', 'languages': ['eng'], 'parent_id': '3ceb010c52572ff7848fdb991fbb8c21', 'filetype': 'text/markdown', 'filename': 'phonebook.md', 'category': 'NarrativeText'}
22.3 Vector Store
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
= Chroma.from_documents(
phonebook_chroma # phonebook_doc,
phonebook_doc_md, =OpenAIEmbeddings(),
embedding )
"Thunderbolt") phonebook_chroma.similarity_search(
[Document(page_content='\nSuperhero Name: Phone Number\n---\nThunderbolt Titan: 12345\nShadow Specter: 67890\nSolar Flare: 23456\nFrostbite Fury: 78901\nIron Guardian: 34567\n---\n', metadata={'source': 'phone_book1'}),
Document(page_content='title: "Superheroes PhoneBook"\n\nProvided below are superheroes phone book, provided as Superhero Name: Phone Number:\n\nThunderbolt Titan: 12345\n\nShadow Specter: 67890\n\nSolar Flare: 23456\n\nFrostbite Fury: 78901\n\nIron Guardian: 34567\n\nQuantum Quasar: 89012\n\nDynamo Dynamo: 45678\n\nEmerald Enforcer: 90123\n\nCrimson Cyclone: 56789\n\nLightning Lynx: 01234', metadata={'source': 'phonebook.md'}),
Document(page_content='title: "Superheroes PhoneBook"\n\nProvided below are superheroes phone book, provided as Superhero Name: Phone Number:\n\nThunderbolt Titan: 12345\n\nShadow Specter: 67890\n\nSolar Flare: 23456\n\nFrostbite Fury: 78901\n\nIron Guardian: 34567\n\nQuantum Quasar: 89012\n\nDynamo Dynamo: 45678\n\nEmerald Enforcer: 90123\n\nCrimson Cyclone: 56789\n\nLightning Lynx: 01234', metadata={'source': 'phonebook.md'}),
Document(page_content='title: "Superheroes PhoneBook"\n\nProvided below are superheroes phone book, provided as Superhero Name: Phone Number:\n\nThunderbolt Titan: 12345\n\nShadow Specter: 67890\n\nSolar Flare: 23456\n\nFrostbite Fury: 78901\n\nIron Guardian: 34567\n\nQuantum Quasar: 89012\n\nDynamo Dynamo: 45678\n\nEmerald Enforcer: 90123\n\nCrimson Cyclone: 56789\n\nLightning Lynx: 01234', metadata={'source': 'phonebook.md'})]
22.4 Retriver
= phonebook_chroma.as_retriever(
retriever ="similarity",
search_type={"k": 1},
search_kwargs )
22.5 Chain
= {"phone_book": retriever,
rag_chain "question": RunnablePassthrough()} | prompt | llm
22.6 Execute
= rag_chain.invoke("What's Thunderbolt phone number?")
resp1 print(resp1.content)
The phone number for Thunderbolt Titan is 12345.
= rag_chain.invoke("Iron Guardian")
resp2 print(resp2.content)
The phone number for Iron Guardian is 34567.
= rag_chain.invoke("Who has a phone number 34567?")
resp3 print(resp3.content)
User: Iron Guardian has the phone number 34567.
= rag_chain.invoke("What's the superman phone number?")
resp4 print(resp4.content)
I'm sorry, Superman's phone number is not listed in the provided phone book. The superheroes listed in the phone book are Quantum Quasar, Dynamo Dynamo, Emerald Enforcer, Crimson Cyclone, and Lightning Lynx. Let me know if you need the phone number for any of these superheroes.
= rag_chain.invoke("What's the Nick Fury phone number?")
resp5 print(resp5.content)
Nick Fury's phone number is 32453.