Build a Chatbot in 10 Minutes
In this tutorial, we will build a simple Retrieval Augmented Generation (RAG) system using Embex and LanceDB (embedded, no setup required).
Prerequisite
Section titled “Prerequisite”Install the libraries:
pip install embex lancedb sentence-transformers openaiStep 1: Initialize
Section titled “Step 1: Initialize”Create a new file bot.py:
import asynciofrom embex import EmbexClient, Pointfrom sentence_transformers import SentenceTransformer
# 1. Setupembedder = SentenceTransformer('all-MiniLM-L6-v2')client = await EmbexClient.new_async("lancedb", "./chatbot_data")collection = client.collection("knowledge_base")Step 2: Ingest Data
Section titled “Step 2: Ingest Data”Let’s add some knowledge.
async def ingest(): texts = [ "Embex is a unified vector database ORM.", "It supports Qdrant, Chroma, and more.", "Rust core makes it blazingly fast." ]
# Create DB await collection.create(384) # dimensionality of all-MiniLM-L6-v2
# Embed and Insert points = [] for i, text in enumerate(texts): vector = embedder.encode(text).tolist() points.append(Point(id=str(i), vector=vector, metadata={"text": text}))
await collection.insert(points) print("Ingestion complete!")Step 3: Search
Section titled “Step 3: Search”Now let’s search.
async def search(query): vector = embedder.encode(query).tolist() results = await collection.search(vector, top_k=1)
if results: best = results[0] print(f"Query: {query}") print(f"Answer: {best.metadata['text']}") else: print("I don't know.")
async def main(): await ingest() await search("What makes Embex fast?")
if __name__ == "__main__": asyncio.run(main())Run it:
python bot.py# Output:# Query: What makes Embex fast?# Answer: Rust core makes it blazingly fast.