Valentineโs gift idea using RAG: Roided out LLMs
Its Valentineโs day again. (The bane of a loverโs existence)
For on this fated day, the perfect gift must be chosen for your lover else you might incur a fury like Hell hath none. ๐ฟ๐ฟ๐ฟ
You browse some sites for quick gifting ideas but nothing seems to be good enough!
You zero in on a couple of options. Will she like it?
Will she or wonโt she? ๐๐๐
Why kill yourself? Make the easy choice. Teach an LLM to decide the idea for you.
I browsed the internet for some sites that have good gifting ideas. Then I shamelessly pulled the HTML content, scrubbed it for my intents and purposes and shoved them into PDF files.
Hint: BeautifulSoup
Now that I have the PDF files, what next?
Well hereโs a recipe to train your favorite model to generate gifting ideas
๐ฆ๐๐ฒ๐ฝ ๐ญ : ๐๐ฑ๐ฑ ๐ฎ ๐ฏ๐๐ป๐ฐ๐ต ๐ผ๐ณ ๐๐ฎ๐ป๐ด๐ฐ๐ต๐ฎ๐ถ๐ป ๐ฑ๐ฒ๐ฝ๐ฒ๐ป๐ฑ๐ฒ๐ป๐ฐ๐ถ๐ฒ๐
import os
from langchain.document_loaders import PyPDFDirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain.vectorstores import Chroma
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain.chains.query_constructor.base import AttributeInfo
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
๐ฆ๐๐ฒ๐ฝ ๐ฎ : ๐๐ฒ๐ ๐ฎ๐ป ๐ข๐ฝ๐ฒ๐ป ๐๐ ๐ธ๐ฒ๐ ๐ฎ๐ป๐ฑ ๐น๐ผ๐ฎ๐ฑ ๐ถ๐ ๐ถ๐ป๐๐ผ ๐๐ต๐ฒ ๐ฒ๐ป๐
with open("./openai-key.txt") as oakf:
os.environ["OPENAI_API_KEY"] = oakf.read()
๐ฆ๐๐ฒ๐ฝ ๐ฏ : ๐๐ผ๐ฎ๐ฑ ๐๐ต๐ฒ ๐ฝ๐ฑ๐ณ๐
loader = PyPDFDirectoryLoader("data")
data = loader.load()
๐ฆ๐๐ฒ๐ฝ ๐ฐ : ๐ฆ๐ฝ๐น๐ถ๐ ๐๐ต๐ฒ ๐ฐ๐ผ๐ป๐๐ฒ๐ป๐ ๐๐๐ถ๐ป๐ด ๐ฅ๐ฒ๐ฐ๐๐ฟ๐๐ถ๐๐ฒ๐ง๐ฒ๐ ๐๐ฆ๐ฝ๐น๐ถ๐๐๐ฒ๐ฟ
r_splitter = RecursiveCharacterTextSplitter(
chunk_size=450,
chunk_overlap=0,
separators=["\n\n", "\n", " "]
)
splits = r_splitter.split_documents(data)
print(splits[0])
๐ฆ๐๐ฒ๐ฝ ๐ฑ : ๐๐ฒ๐ป๐ฒ๐ฟ๐ฎ๐๐ฒ ๐ฒ๐บ๐ฏ๐ฒ๐ฑ๐ฑ๐ถ๐ป๐ด๐ ๐๐ผ ๐ฑ๐๐บ๐ฝ ๐ถ๐ป๐๐ผ ๐ฎ ๐ฉ๐ฒ๐ฐ๐๐ผ๐ฟ ๐ฑ๐ฎ๐๐ฎ๐ฏ๐ฎ๐๐ฒ (๐๐ต๐ฟ๐ผ๐บ๐ฎ ๐๐)
embedding = OpenAIEmbeddings()
# save chroma db embeddings in this directory
persist_directory = 'docs/chroma/'
# Create the vector store
vectordb = Chroma.from_documents(
documents=splits,
embedding=embedding,
persist_directory=persist_directory
)
๐ฆ๐๐ฒ๐ฝ ๐ฒ : ๐๐ถ๐ป๐ฑ ๐๐ถ๐บ๐ถ๐น๐ฎ๐ฟ ๐ฟ๐ฒ๐น๐ฎ๐๐ฒ๐ฑ ๐ฐ๐ผ๐ป๐๐ฒ๐ป๐ ๐ณ๐ฟ๐ผ๐บ ๐๐ต๐ฒ ๐ฑ๐ฏ
question = "What would be a memorable valentine gift for a woman aged 39 years?"
docs = vectordb.similarity_search(question, k=2)
vectordb.persist()
content = docs[0].page_content
print(content)
I get :โ
Valentineโs Day flowers
and a romantic homemade dinner.
So it seems from my knowledge base that flowers and a romantic homemade dinner might be a good idea.
๐ฆ๐๐ฒ๐ฝ ๐ณ : ๐๐๐ถ๐น๐ฑ ๐ฎ ๐ฟ๐ฒ๐๐ฟ๐ถ๐ฒ๐๐ฒ๐ฟ ๐๐ผ ๐พ๐๐ฒ๐ฟ๐ ๐๐ต๐ฒ ๐๐๐ ๐ฎ๐ป๐ฑ ๐๐๐ฝ๐ฝ๐ผ๐ฟ๐ ๐๐ต๐ฒ ๐บ๐ผ๐๐ ๐ฟ๐ฒ๐น๐ฒ๐๐ฎ๐ป๐ ๐ฟ๐ฒ๐๐๐น๐ ๐ณ๐ฟ๐ผ๐บ ๐๐ต๐ฒ ๐ฉ๐ฒ๐ฐ๐๐ผ๐ฟ ๐๐
# build some metadata to support the query to the LLM
# the preferable source file of the gift
metadata_field_info = [
AttributeInfo(
name="source",
description="The source of the gift. One of ['file3.pdf']",
type="string",
),
]
document_content_description = "memorable gift for a woman"
llm = OpenAI(temperature=0)
retriever = SelfQueryRetriever.from_llm(
llm,
vectordb,
document_content_description,
metadata_field_info,
verbose=True
)
#get the most relevant result for the query based on our own corpus
question = "What would be a memorable valentine gift for a woman aged 39 years?"
docs = retriever.get_relevant_documents(question)
print(docs)
I get :โ
page_content='Valentineโs Day flowers\n \nand a romantic homemade dinner.'
So it seems (according to the knowledge base) that a combination of flowers and a home made dinner would be an excellent choice.
๐ฆ๐๐ฒ๐ฝ ๐ด : ๐๐ฒ๐๐ ๐ฏ๐๐ถ๐น๐ฑ ๐๐ต๐ฒ ๐ฝ๐ฟ๐ผ๐บ๐ฝ๐
template = """Given a person with the following profile:
{user_profile}
Use the following pieces of context to answer the question at the end.
{context}
Question: {question}
Helpful Answer:"""
user_profile = {
"age": 39,
"gender": "female",
"interests": ["reading", "gardening", "music", "clothes", "outdoors"],
"profession": ["teacher"]
}
# add context to the query template
prompt = PromptTemplate(template=template, input_variables=["user_profile", "context", "question"])
๐ฆ๐๐ฒ๐ฝ ๐ต : ๐๐ฒ๐๐ ๐ณ๐ถ๐ป๐ฎ๐น๐น๐ ๐ฎ๐๐ธ ๐๐ต๐ฎ๐๐๐ฃ๐ง
llm = ChatOpenAI(model_name="gpt-3.5-turbo-0125", temperature=0.0002)
llm_chain = LLMChain(prompt=prompt, llm=llm)
question = "What would be a memorable valentine's day gift for my wife aged 39?"
generated = llm_chain.run(user_profile=user_profile, context = docs, question = question)
# generate results
print("Gift ideas: " + generated)
And I get :โ
Gift ideas: Based on the profile of your wife, a memorable Valentine's Day gift could be a combination of things she enjoys such as a book from her favorite author, a new plant or gardening tool for her garden, a vinyl record of her favorite music artist, a stylish piece of clothing, or a gift card for outdoor activities or experiences. You could also consider planning a romantic homemade dinner or surprising her with Valentine's Day flowers. Ultimately, the most memorable gift will be something thoughtful and personalized to her interests and preferences.
I actually followed the advice.
What do you think?
Which gift did I get her?
And Did she like the gift?
Follow me Ritesh Shergill
for more articles on
๐จโ๐ป Tech
๐ฉโ๐ Career advice
๐ฒ User Experience
๐ Leadership
I also do
โ Career Guidance counselling โ https://topmate.io/ritesh_shergill/149890
โ Mentor Startups as a Fractional CTO โ https://topmate.io/ritesh_shergill/193786