Skip to content

Instantly share code, notes, and snippets.

@alexpovel
Last active March 10, 2024 07:23
Show Gist options
  • Save alexpovel/8dfa9ccfaabbd41e2aa3f6413d9de2cd to your computer and use it in GitHub Desktop.
Save alexpovel/8dfa9ccfaabbd41e2aa3f6413d9de2cd to your computer and use it in GitHub Desktop.
qdrant proof of concept
FROM python:3.11
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY qdrant.py .
CMD ["python", "qdrant.py"]
from dataclasses import dataclass
from pprint import pprint
from qdrant_client import AsyncQdrantClient
from qdrant_client.http.models import (
FieldCondition,
Filter,
MatchValue,
)
@dataclass
class Entry:
id: int
document: str
payload: dict[str, str]
async def main():
client = AsyncQdrantClient(":memory:")
collection = "some-collection"
data = [
(
Entry(
id=0,
document="Hamburg, a major port city in northern Germany, is known for its maritime flair. The city is intertwined with canals and boasts the historic Speicherstadt, a UNESCO World Heritage Site, featuring red-brick warehouses. Hamburg's reputation for music is significant, with the Elbphilharmonie concert hall as a contemporary landmark. Its vibrant cultural scene, rich history, and bustling harbor make it a unique destination.",
payload={
"city": "Hamburg",
"country": "Germany",
"original": "Hamburg, a major port city in northern Germany, is known for its maritime flair. The city is intertwined with canals and boasts the historic Speicherstadt, a UNESCO World Heritage Site, featuring red-brick warehouses. Hamburg's reputation for music is significant, with the Elbphilharmonie concert hall as a contemporary landmark. Its vibrant cultural scene, rich history, and bustling harbor make it a unique destination.",
},
)
),
(
Entry(
id=1,
document="Berlin, the capital of Germany, is a city steeped in history and contemporary culture. Known for its significant role in 20th-century history, it's home to iconic landmarks like the Brandenburg Gate and the Berlin Wall remnants. Berlin's dynamic art scene, diverse nightlife, and modern architecture, alongside historical sites, offer a unique blend of old and new.",
payload={
"city": "Berlin",
"country": "Germany",
"original": "Berlin, the capital of Germany, is a city steeped in history and contemporary culture. Known for its significant role in 20th-century history, it's home to iconic landmarks like the Brandenburg Gate and the Berlin Wall remnants. Berlin's dynamic art scene, diverse nightlife, and modern architecture, alongside historical sites, offer a unique blend of old and new.",
},
)
),
(
Entry(
id=2,
document="Paris, France's capital, is renowned for its art, fashion, and gastronomy. The city's 19th-century cityscape is crisscrossed by wide boulevards and the River Seine. Landmarks like the Eiffel Tower, Notre-Dame Cathedral, and the Louvre Museum highlight its rich history and cultural importance. Paris is often seen as a symbol of romance and beauty, attracting visitors from around the world.",
payload={
"city": "Paris",
"country": "France",
"original": "Paris, France's capital, is renowned for its art, fashion, and gastronomy. The city's 19th-century cityscape is crisscrossed by wide boulevards and the River Seine. Landmarks like the Eiffel Tower, Notre-Dame Cathedral, and the Louvre Museum highlight its rich history and cultural importance. Paris is often seen as a symbol of romance and beauty, attracting visitors from around the world.",
},
)
),
(
Entry(
id=3,
document="Munich, the capital of Bavaria in southern Germany, is famous for its Oktoberfest and beer halls, including the 16th-century Hofbräuhaus. The city blends traditional Bavarian culture with modernity, exemplified by its beautiful architecture like the neo-Gothic Neues Rathaus. Munich is also a gateway to the Alps and is known for its museums and historic buildings.",
payload={
"city": "Munich",
"country": "Germany",
"original": "Munich, the capital of Bavaria in southern Germany, is famous for its Oktoberfest and beer halls, including the 16th-century Hofbräuhaus. The city blends traditional Bavarian culture with modernity, exemplified by its beautiful architecture like the neo-Gothic Neues Rathaus. Munich is also a gateway to the Alps and is known for its museums and historic buildings.",
},
)
),
]
# https://qdrant.tech/documentation/tutorials/neural-search-fastembed/#upload-data-to-qdrant
client.set_model("sentence-transformers/all-MiniLM-L6-v2")
await client.recreate_collection(
collection_name=collection,
vectors_config=client.get_fastembed_vector_params(),
)
await client.add(
collection_name=collection,
ids=[entry.id for entry in data],
documents=[entry.document for entry in data],
metadata=[entry.payload for entry in data],
)
results = await client.query(
collection_name=collection,
query_text="Capital city",
query_filter=Filter(
must=[FieldCondition(key="country", match=MatchValue(value="Germany"))]
),
limit=5,
)
for i, result in enumerate(results, start=1):
print(f"Result number {i}:")
pprint(result)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
fastembed==0.1.1
qdrant-client[fastembed]==1.7.0
100%|██████████| 83.2M/83.2M [00:08<00:00, 9.42MiB/s]
Result number 1:
QueryResponse(id=1, embedding=None, metadata={'document': "Berlin, the capital of Germany, is a city steeped in history and contemporary culture. Known for its significant role in 20th-century history, it's home to iconic landmarks like the Brandenburg Gate and the Berlin Wall remnants. Berlin's dynamic art scene, diverse nightlife, and modern architecture, alongside historical sites, offer a unique blend of old and new.", 'city': 'Berlin', 'country': 'Germany', 'original': "Berlin, the capital of Germany, is a city steeped in history and contemporary culture. Known for its significant role in 20th-century history, it's home to iconic landmarks like the Brandenburg Gate and the Berlin Wall remnants. Berlin's dynamic art scene, diverse nightlife, and modern architecture, alongside historical sites, offer a unique blend of old and new."}, document="Berlin, the capital of Germany, is a city steeped in history and contemporary culture. Known for its significant role in 20th-century history, it's home to iconic landmarks like the Brandenburg Gate and the Berlin Wall remnants. Berlin's dynamic art scene, diverse nightlife, and modern architecture, alongside historical sites, offer a unique blend of old and new.", score=0.7205214818401329)
Result number 2:
QueryResponse(id=0, embedding=None, metadata={'document': "Hamburg, a major port city in northern Germany, is known for its maritime flair. The city is intertwined with canals and boasts the historic Speicherstadt, a UNESCO World Heritage Site, featuring red-brick warehouses. Hamburg's reputation for music is significant, with the Elbphilharmonie concert hall as a contemporary landmark. Its vibrant cultural scene, rich history, and bustling harbor make it a unique destination.", 'city': 'Hamburg', 'country': 'Germany', 'original': "Hamburg, a major port city in northern Germany, is known for its maritime flair. The city is intertwined with canals and boasts the historic Speicherstadt, a UNESCO World Heritage Site, featuring red-brick warehouses. Hamburg's reputation for music is significant, with the Elbphilharmonie concert hall as a contemporary landmark. Its vibrant cultural scene, rich history, and bustling harbor make it a unique destination."}, document="Hamburg, a major port city in northern Germany, is known for its maritime flair. The city is intertwined with canals and boasts the historic Speicherstadt, a UNESCO World Heritage Site, featuring red-brick warehouses. Hamburg's reputation for music is significant, with the Elbphilharmonie concert hall as a contemporary landmark. Its vibrant cultural scene, rich history, and bustling harbor make it a unique destination.", score=0.7027894503755593)
Result number 3:
QueryResponse(id=3, embedding=None, metadata={'document': 'Munich, the capital of Bavaria in southern Germany, is famous for its Oktoberfest and beer halls, including the 16th-century Hofbräuhaus. The city blends traditional Bavarian culture with modernity, exemplified by its beautiful architecture like the neo-Gothic Neues Rathaus. Munich is also a gateway to the Alps and is known for its museums and historic buildings.', 'city': 'Munich', 'country': 'Germany', 'original': 'Munich, the capital of Bavaria in southern Germany, is famous for its Oktoberfest and beer halls, including the 16th-century Hofbräuhaus. The city blends traditional Bavarian culture with modernity, exemplified by its beautiful architecture like the neo-Gothic Neues Rathaus. Munich is also a gateway to the Alps and is known for its museums and historic buildings.'}, document='Munich, the capital of Bavaria in southern Germany, is famous for its Oktoberfest and beer halls, including the 16th-century Hofbräuhaus. The city blends traditional Bavarian culture with modernity, exemplified by its beautiful architecture like the neo-Gothic Neues Rathaus. Munich is also a gateway to the Alps and is known for its museums and historic buildings.', score=0.6295805134444555)
@alexpovel
Copy link
Author

Run with:

docker build -t qdrant-test . && docker run qdrant-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment