Revolutionizing E-Commerce with Trendyol LLM Shopping Assistant

Serhan Çiftlikçi
Trendyol Tech
Published in
8 min readMar 23, 2024
Image 1: Trendyol LLM welcomes you! (Image Source)

In an era dominated by technological advancements, the fusion of artificial intelligence and everyday activities has become increasingly seamless. Currently, one of the most prominent fields of AI is Large Language Models (LLMs). To briefly define LLMs are sophisticated systems designed to understand and generate human language. These models are trained on vast amounts of text data, allowing them to learn the intricacies of language, grammar, and context. Unlike traditional virtual assistants that rely on predefined scripts, LLMs can generate human-like text responses based on the input they receive.

Presenting Trendyol LLM

With this context, the recent release of Trendyol’s LLM base and chat models as an open-source contribution marks a significant milestone in the realm of Natural Language Processing (NLP) and a commendable boost for the Turkish NLP community. By leveraging the cutting-edge capabilities of the Mistral model and fine-tuning it with extensive Turkish data, Trendyol has not only crafted a powerful tool but has also exemplified the spirit of collaboration and knowledge-sharing. Our commitment to advancing the field and providing the community with a robust Turkish-language model reflects a forward-thinking approach that undoubtedly propels the entire NLP community toward new horizons. This open-source contribution is poised to empower developers, researchers, and enthusiasts, driving innovation and progress in Turkish NLP research and applications.

Our Motivation

Keeping this vision in mind, one such groundbreaking integration of LLMs is their utilization as shopping assistants. Imagine having a personalized advisor at your fingertips, capable of understanding your preferences, suggesting tailored recommendations, and enhancing your overall shopping experience. Hence, in this article, we delve into the transformative impact of Trendyol LLM on the world of retail, exploring how these language models are not just reshaping the way we shop but revolutionizing the very essence of consumer decision-making. Join me on a hands-on implementation journey into the future of e-commerce, where language models are not mere tools but indispensable companions in our quest for perfect purchases.

0. Setup

I am going to build our shopping assistant in two steps:

  1. First, I am going to create a Retrieval Augmented Generation (RAG) flow using Langchain. RAGs naturally combine external information retrieval and generative methods to enhance content creation by leveraging existing data for contextually relevant and coherent text generation. In my case, Trendyol reviews and seller Q&As will be my external information source. Hence, any questions I have about the product will be answered by Trendyol LLM based on this information.
  2. Then, I am going to integrate this flow with gradio. Gradio’s chatbot functionality streamlines the development of interactive and user-friendly chatbots, allowing for rapid prototyping and deployment, thus enabling efficient creation of conversational AI applications.

The resulting implementation will be as in Image 1. You can also reach the supplementary code from this Github repository.

Image 2: Flowchart of Shopping Assistant (Source: Author)

1. Implementing the RAG Flow with Langchain

review_qa_df = pd.read_csv("review_qa_sample_data.csv")

This code snippet utilizes the pandas library in Python to read the review and Q&A dataset. Now, for the sake of simplicity, I assumed that we are considering buying the “Xiaomi Mi Band 5 Akıllı Bileklik Siyah” (Xiaomi Mi Band 5 Smart Wristband Black) product and have questions about it.

Now, we are ready to create the RAG flow. Let’s start by defining the LLM pipeline.

set_seed()
...
model = AutoModelForCausalLM.from_pretrained(
"Trendyol/Trendyol-LLM-7b-chat-dpo-v1.0",
...
)
pipe = pipeline(
"text-generation",
model=model,
max_new_tokens=1024, # adjust model's output text size
no_repeat_ngram_size=3, # used for preventing repetitive texts
temperature=0.01, # adjust this parameter to stabilize model outputs
do_sample=True, # enabled for generating varying texts
...
)
llm = HuggingFacePipeline(pipeline=pipe)

This code snippet initializes a language model pipeline for text generation using the Hugging Face Transformers library. The resulting pipeline is encapsulated within the Langchain `HuggingFacePipeline` class, providing a convenient and abstracted interface for interacting with the Trendyol LLM for text generation tasks in a chat-based context. I also reduced the randomness in our experiments by fixing the seed parameter.

# get embedder
embedder = HuggingFaceEmbeddings(
model_name="paraphrase-multilingual-mpnet-base-v2"
)
# generate local vector db
local_vector_db = DocArrayInMemorySearch.from_texts(
review_qa_df["review_qa_data"].tolist(), embedder
)
retriever = local_vector_db.as_retriever(
search_kwargs={"score_threshold": 0.1, "k": 3}
)

This code sets up a local vector database within the Langchain framework to answer customer questions based on target product reviews and Q&As. The script first initializes a Sentence Transformers model for generating embeddings from them. Then it creates a local vector database with these embeddings. To achieve a better quality of context, I specified a similarity score threshold and a maximum number of results (k).

prompt_template = """ [INST] Sen Trendyol'da çalışan ve ürünlerle ilgili sorulara cevap veren bir asistansın.
Sana verilen müşteri sorusunu, '''ÜRÜN YORUMLARI VE SORU - CEVAPLARI''' başlığı altında verilen bilgileri kullanarak cevapla.
Cevapların Türkçe olmalı.
Cevapların özenli bir üsluba sahip olmalı.
Cevapların özet ve doğru bilgiler içermeli.
Ürün adı: ""Xiaomi Mi Band 5 Akıllı Bileklik siyah""

'''ÜRÜN YORUMLARI VE SORU & CEVAPLARI''':
- {context}

Sohbet geçmişini ve takip sorusunu bağımsız bir soruda birleştir. Gerekirse cevabını destekleyecek bir açıklama ekle.
Sohbet geçmişi: {chat_history}
Takip sorusu: {question} [/INST]
"""

condense_question_prompt = PromptTemplate(
input_variables=["context", "chat_history", "question", "product_name"],
template=prompt_template
)
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=False,
k=3 # adjust to track number of chat turns to consider as memory
)
convrag_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
memory=memory,
...
)

This code establishes a conversational retrieval chain using the Langchain framework for a chat-based system in the context of an e-commerce assistant. The `PromptTemplate` defines a structured conversation template for generating responses. The `ConversationBufferMemory` efficiently tracks and manages the conversation history, retaining the last three message turns. The `ConversationalRetrievalChain` is then configured, utilizing the Trendyol LLM model, our local vector database, and the defined memory structure. This chain is designed to respond to user questions by extracting information from reviews and Q&As in a specified product review context, ensuring the generated responses are in Turkish, possess a polished style, and offer concise and accurate information. The implementation showcases the flexibility and power of Langchain in constructing complex conversational systems tailored for specific use cases, such as e-commerce assistance.

2. Launching our Assistant

I am going to launch the chatbot and ask my questions.

def interact(query, history):
return convrag_chain.invoke(query)["answer"].strip()

demo = gr.ChatInterface(
fn=interact,
examples=[
"Uyku takip özelliği var mı?",
"Kardeşime almak mantıklı mı sence? Teknolojik ürünlerle arası iyi ama hiç akıllı bileklik kullanmadı."
],
title="AI Shopping Assistant",
description="This app is using Trendyol-LLM-7b-chat-dpo-v1.0 model as a shopping assistant.\n Currently, you can ask your questions for the 'Xiaomi Mi Band 5 Akıllı Bileklik Siyah' product."
)
demo.launch(share=True)

This code snippet employs the Gradio library to create an interactive chat interface for the Trendyol AI Shopping Assistant. The interact function is defined to take user queries and chat history, invoking the convrag_chain to generate responses based on the Langchain conversational retrieval chain. The gr.ChatInterface is then set up with this interaction function, including predefined examples, a title (“Trendyol AI Alışveriş Asistanı”), and a description. Users can engage in a conversational chat, posing questions related to the target product. The resulting interface provides a user-friendly and accessible platform to experience the capabilities of the Trendyol LLM.

3. Interacting with our Assistant

Now we come to the best part. It is the time to get rewarded for our efforts. I am going to interact with the assistant by considering two scenarios. The first is to ask a simple question with a definitive yes/no answer. To demonstrate this scenario, I am going to ask about the availability of the sleep-tracking feature for this wristband. I expect that our assistant can accurately answer this question by directly using the reviews and Q&As that we provided. You can see the assistant’s response in Image 3.

Image 3: Availability of Sleep Tracking Feature

As you can see, when I inquired about the availability of a sleep-tracking feature in a wristband, the response I received was nothing short of remarkable. Not only did the chatbot swiftly recognize my query, but it also provided a comprehensive overview of this functionality. Its ability to seamlessly navigate through complex product specifications speaks volumes about its sophistication and adaptability.

The second scenario is to ask an open-ended question with a possibly subjective answer. To demonstrate it, I am going to ask about the availability of the sleep-tracking feature for this wristband. I expect that our assistant can accurately answer this question by directly using the reviews and Q&As that we provided. You can see the assistant’s response in Image 4.

Image 4: Getting the Assistant’s Opinion

When I sought advice on purchasing a wristband for my brother, the response I received was nothing short of extraordinary. Its intuitive understanding of context and ability to provide personalized recommendations underscored its prowess in seamlessly merging artificial intelligence with the intricacies of human desire. At this point, I am convinced that this chatbot embodies the epitome of technological innovation, promising a future where shopping assistants transcend mere utility to become indispensable allies in the pursuit of consumer satisfaction.

Conclusion

In conclusion, our exploration of Trendyol’s LLM model, coupled with the dynamic capabilities of Langchain and Gradio, has not only unveiled the potential of this groundbreaking technology but has also redefined the concept of a shopping assistant. We’ve experienced the remarkable versatility of Trendyol’s LLM in engaging and fluent conversations. The fluidity with which we could pose diverse questions about products demonstrates the model’s adaptability and its ability to comprehend user queries with finesse. This journey has not just been a testament to the technical prowess of the LLM, but it also underscores the transformative impact such advancements have on user interactions within the shopping realm. As we wrap up our exploration, it’s evident that Trendyol’s LLM is more than just a tool; it’s a revolutionary force, reshaping the way we navigate and personalize our shopping experiences in the ever-evolving landscape of artificial intelligence.

Join Us

Want to be a part of our growing company? We’re hiring! Check out our open positions.

--

--