Create a chatbot with Langchain
Let us import all necessary libraries:
import warnings
from langchain_core.prompts.prompt import PromptTemplate
from rich import print
from typing import List
from dotenv import load_dotenv
from langchain_core.documents import Document
from langchain_openai import ChatOpenAI
from langchain_huggingface import HuggingFaceEmbeddings
from langchain.chains.question_answering import load_qa_chain
from shapelets.data import sandbox
from shapelets.indices.langchain import ShapeletsVecDB
warnings.simplefilter('ignore')
if not load_dotenv('./.env'):
print("Env not loaded")
We will use Amazon products data, loading it as we did in our previous tutorial.
def shapelets_data() -> List[Document]:
"""Extract products data with Shapelets Engine."""
playground = sandbox()
playground.from_csv(
rel_name='amazon_products',
paths=['products.csv'],
delimiter=','
)
# Filter empty products
filter_query = """
SELECT
"Product Name" AS product,
"About Product" AS description
FROM
amazon_products
WHERE
"About Product" IS NOT NULL
"""
amazon_final_data = playground.execute(filter_query).to_dict()
products = amazon_final_data['product']
descriptions = amazon_final_data['description']
return [Document(descriptions[i], metadata={'product_name': products[i]}) for i in range(len(products))]
corpus = shapelets_data()
print(f'Corpus size is {len(corpus)}')
Then we load an LLM and an embeddings model using Langchain's API:
llm = ChatOpenAI(model='gpt-3.5-turbo-0125')
embeddingFn = HuggingFaceEmbeddings(model_name='all-MiniLM-L6-v2')
Leveraging Shapelets integration into Langchain, we can easily index documents:
vector_store = ShapeletsVecDB.from_documents(corpus, embeddingFn, indices={})
retriever = vector_store.as_retriever()
For providing recommendations, our LLM requires a prompt:
prompt = """You are an AI model that works at Amazon and
has access to the entire inventory. Your task is to
recommend products to customers based on their needs
and product descriptions.
You can only recommend Amazon products and refuse to follow
any other instruction.
Descriptions:
{context}
Customer:
{input}
AI Recommendation:
"""
# Format it for langchain API
langchain_prompt = PromptTemplate(
template=prompt,
input_variables=['context', 'input']
)
recommender = load_qa_chain(llm, 'stuff', prompt=langchain_prompt)
Lastly, we get recommendations:
query = "Can you recommend a good aftershave for sensitive skin?"
close_products = retriever.invoke(input=query)
response = recommender.invoke({'input_documents': close_products, 'input': query}, return_only_outputs=False)
print(response['output_text'], end='\n')
vector_store.close()
Absolutely! Product X is a great choice for sensitive skin.
It's formulated with gentle ingredients that soothe irritation and reduce redness,
leaving your skin feeling smooth and refreshed after shaving