DEV Community

msbala007
msbala007

Posted on

Building a Simple Powerful Chatbot Using ChromaDB and Sentence-Transformers

Created an AI chatbot using ChromaDB and Sentence-Transformers. These tools made the whole process smooth and efficient, and I wanted to share my experience in hopes that it might help someone else starting out with similar projects.

Steps I Took to Build the Chatbot:
I used sentence-transformers to convert text data into embeddings and ChromaDB for vector storage.

so install these dependencies..

pip install -U sentence-transformers

pip install chromadb
Enter fullscreen mode Exit fullscreen mode

Next

import chromadb
chroma_client = chromadb.Client()
collection = chroma_client.create_collection(name="data")
Enter fullscreen mode Exit fullscreen mode

Don't forget to load your dataset

Using Sentence-Transformers, I generated embeddings for my dataset:

for index,text in data.iterrows():
   res=text['Answer']
   embedding = model.encode(res)
   collection.add(
       documents=[str(res)],
       embeddings=[embedding.tolist()],
       ids=[str(index)],
       metadatas=[{'text':res}]
   )
Enter fullscreen mode Exit fullscreen mode

*Query and Response: *

The chatbot listens for user input, converts the query into an embedding, and uses ChromaDB to find the closest match in the stored embeddings. Here’s the basic query

def get_response(text):
    res=model.encode(text)
    result=collection.query(
        query_embeddings=[res.tolist()],
        n_results=1
    )
    return result
Enter fullscreen mode Exit fullscreen mode

That's it ,If you pass any text relevant to your dataset it'll response for you which is has close distance


πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡

I'm excited to introduce Terminal Troubleshooters β€” a dynamic Slack community for tech enthusiasts to learn, share, and grow together! Our mission is to build a collaborative space where you can:

✨ Share daily tech insights and experiences
πŸ’‘ Discuss innovative ideas and industry trends
πŸ—£οΈ Enhance communication skills through engaging conversations
🀝 Connect with like-minded professionals and expand your network

Community Link:

Top comments (0)