mirror of
https://github.com/qdrant/landing_page.git
synced 2026-10-01 17:08:30 +02:00
* Support generating multiple snippets from one source file * Convert Python code snippets from one source file * Add code snippets for C#, Go, Java, Rust and TS * Make intro less Python-oriented * Add client installation instructions for all languages * Cleanup python code --------- Co-authored-by: xzfc <xzfcpw@gmail.com>
168 lines
4.8 KiB
Python
168 lines
4.8 KiB
Python
# @hide-start
|
|
# mypy: disable-error-code="arg-type"
|
|
QDRANT_URL=""
|
|
QDRANT_API_KEY=""
|
|
# @hide-end
|
|
# @block-start client-connection
|
|
from qdrant_client import QdrantClient, models
|
|
|
|
client = QdrantClient(
|
|
url=QDRANT_URL,
|
|
api_key=QDRANT_API_KEY,
|
|
cloud_inference=True
|
|
)
|
|
# @block-end client-connection
|
|
|
|
# @block-start create-collection
|
|
COLLECTION_NAME="my_books"
|
|
|
|
client.create_collection(
|
|
collection_name=COLLECTION_NAME,
|
|
vectors_config=models.VectorParams(
|
|
size=384, # Vector size is defined by used model
|
|
distance=models.Distance.COSINE,
|
|
),
|
|
)
|
|
# @block-end create-collection
|
|
|
|
# @block-start upload-data
|
|
documents = [
|
|
{
|
|
"name": "The Time Machine",
|
|
"description": "A man travels through time and witnesses the evolution of humanity.",
|
|
"author": "H.G. Wells",
|
|
"year": 1895,
|
|
},
|
|
{
|
|
"name": "Ender's Game",
|
|
"description": "A young boy is trained to become a military leader in a war against an alien race.",
|
|
"author": "Orson Scott Card",
|
|
"year": 1985,
|
|
},
|
|
{
|
|
"name": "Brave New World",
|
|
"description": "A dystopian society where people are genetically engineered and conditioned to conform to a strict social hierarchy.",
|
|
"author": "Aldous Huxley",
|
|
"year": 1932,
|
|
},
|
|
{
|
|
"name": "The Hitchhiker's Guide to the Galaxy",
|
|
"description": "A comedic science fiction series following the misadventures of an unwitting human and his alien friend.",
|
|
"author": "Douglas Adams",
|
|
"year": 1979,
|
|
},
|
|
{
|
|
"name": "Dune",
|
|
"description": "A desert planet is the site of political intrigue and power struggles.",
|
|
"author": "Frank Herbert",
|
|
"year": 1965,
|
|
},
|
|
{
|
|
"name": "Foundation",
|
|
"description": "A mathematician develops a science to predict the future of humanity and works to save civilization from collapse.",
|
|
"author": "Isaac Asimov",
|
|
"year": 1951,
|
|
},
|
|
{
|
|
"name": "Snow Crash",
|
|
"description": "A futuristic world where the internet has evolved into a virtual reality metaverse.",
|
|
"author": "Neal Stephenson",
|
|
"year": 1992,
|
|
},
|
|
{
|
|
"name": "Neuromancer",
|
|
"description": "A hacker is hired to pull off a near-impossible hack and gets pulled into a web of intrigue.",
|
|
"author": "William Gibson",
|
|
"year": 1984,
|
|
},
|
|
{
|
|
"name": "The War of the Worlds",
|
|
"description": "A Martian invasion of Earth throws humanity into chaos.",
|
|
"author": "H.G. Wells",
|
|
"year": 1898,
|
|
},
|
|
{
|
|
"name": "The Hunger Games",
|
|
"description": "A dystopian society where teenagers are forced to fight to the death in a televised spectacle.",
|
|
"author": "Suzanne Collins",
|
|
"year": 2008,
|
|
},
|
|
{
|
|
"name": "The Andromeda Strain",
|
|
"description": "A deadly virus from outer space threatens to wipe out humanity.",
|
|
"author": "Michael Crichton",
|
|
"year": 1969,
|
|
},
|
|
{
|
|
"name": "The Left Hand of Darkness",
|
|
"description": "A human ambassador is sent to a planet where the inhabitants are genderless and can change gender at will.",
|
|
"author": "Ursula K. Le Guin",
|
|
"year": 1969,
|
|
},
|
|
{
|
|
"name": "The Three-Body Problem",
|
|
"description": "Humans encounter an alien civilization that lives in a dying system.",
|
|
"author": "Liu Cixin",
|
|
"year": 2008,
|
|
},
|
|
]
|
|
# @block-end upload-data
|
|
|
|
# @block-start upload-points
|
|
EMBEDDING_MODEL="sentence-transformers/all-minilm-l6-v2"
|
|
|
|
client.upload_points(
|
|
collection_name=COLLECTION_NAME,
|
|
points=[
|
|
models.PointStruct(
|
|
id=idx,
|
|
vector=models.Document(
|
|
text=doc["description"],
|
|
model=EMBEDDING_MODEL
|
|
),
|
|
payload=doc
|
|
)
|
|
for idx, doc in enumerate(documents)
|
|
],
|
|
)
|
|
# @block-end upload-points
|
|
|
|
# @block-start query-engine
|
|
hits = client.query_points(
|
|
collection_name=COLLECTION_NAME,
|
|
query=models.Document(
|
|
text="alien invasion",
|
|
model=EMBEDDING_MODEL
|
|
),
|
|
limit=3,
|
|
).points
|
|
|
|
for hit in hits:
|
|
print(hit.payload, "score:", hit.score)
|
|
# @block-end query-engine
|
|
|
|
# @block-start create-payload-index
|
|
client.create_payload_index(
|
|
collection_name=COLLECTION_NAME,
|
|
field_name="year",
|
|
field_schema=models.PayloadSchemaType.INTEGER,
|
|
)
|
|
# @block-end create-payload-index
|
|
|
|
# @block-start query-with-filter
|
|
hits = client.query_points(
|
|
collection_name=COLLECTION_NAME,
|
|
query=models.Document(
|
|
text="alien invasion",
|
|
model=EMBEDDING_MODEL
|
|
),
|
|
query_filter=models.Filter(
|
|
must=[models.FieldCondition(key="year", range=models.Range(gte=2000))]
|
|
),
|
|
limit=1,
|
|
).points
|
|
|
|
for hit in hits:
|
|
print(hit.payload, "score:", hit.score)
|
|
# @block-end query-with-filter
|