Files
openai--openai-cookbook/examples/codex/data/docs/qdrant_embeddings_search_pre_repair.ipynb
wehub-resource-sync 327604cc89
Rebuild Cookbook Website / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:41:49 +08:00

127 lines
4.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Qdrant embedding search, sampled fixture\n",
"\n",
"This fixture is derived from the Cookbook Qdrant search example. It keeps the same teaching arc with a tiny local article set so validation can execute quickly.\n"
],
"id": "cell-000"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from math import sqrt\n",
"\n",
"EMBEDDING_MODEL = \"text-embedding-ada-002\" # legacy model kept intentionally for the repair loop\n",
"\n",
"articles = [\n",
" {\"id\": 1, \"title\": \"Modern art in Europe\", \"url\": \"https://example.com/art\", \"content\": \"Cubism and surrealism reshaped European museums.\"},\n",
" {\"id\": 2, \"title\": \"Scottish battle history\", \"url\": \"https://example.com/scotland\", \"content\": \"Bannockburn and Stirling Bridge shaped Scottish history.\"},\n",
" {\"id\": 3, \"title\": \"Space telescope discoveries\", \"url\": \"https://example.com/space\", \"content\": \"Modern telescopes reveal planets, galaxies, and stars.\"},\n",
"]\n"
],
"id": "cell-001"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def embed(text: str) -> list[float]:\n",
" buckets = [0.0, 0.0, 0.0, 0.0]\n",
" for index, char in enumerate(text.lower()):\n",
" buckets[index % len(buckets)] += ord(char) / 1000\n",
" length = sqrt(sum(value * value for value in buckets)) or 1\n",
" return [round(value / length, 4) for value in buckets]\n",
"\n",
"for article in articles:\n",
" article[\"title_vector\"] = embed(article[\"title\"])\n",
" article[\"content_vector\"] = embed(article[\"content\"])\n"
],
"id": "cell-002"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class LocalQdrant:\n",
" def __init__(self, rows):\n",
" self.rows = rows\n",
"\n",
" def search(self, collection_name, query_vector, limit=3, query_filter=None):\n",
" vector_name, query = query_vector\n",
" scored = []\n",
" for row in self.rows:\n",
" vector = row[f\"{vector_name}_vector\"]\n",
" score = sum(a * b for a, b in zip(query, vector))\n",
" scored.append((score, row))\n",
" return sorted(scored, reverse=True)[:limit]\n",
"\n",
" def query_points(self, collection_name, query, using=\"title\", limit=3):\n",
" return self.search(collection_name, (using, query), limit=limit)\n",
"\n",
"qdrant = LocalQdrant(articles)\n"
],
"id": "cell-003"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def query_qdrant(query, collection_name, vector_name=\"title\", top_k=3):\n",
" embedded_query = embed(query)\n",
" return qdrant.search(\n",
" collection_name=collection_name,\n",
" query_vector=(vector_name, embedded_query),\n",
" limit=top_k,\n",
" query_filter=None,\n",
" )\n",
"\n",
"for score, article in query_qdrant(\"modern art in Europe\", \"Articles\", \"title\"):\n",
" print(f'{article[\"title\"]}: {score:.3f}')\n"
],
"id": "cell-004"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"pygments_lexer": "ipython3"
},
"codex_case_study": {
"source_repo": "https://github.com/openai/openai-cookbook",
"source_path": "examples/vector_databases/qdrant/Using_Qdrant_for_embeddings_search.ipynb",
"source_commit": "96b1d67^",
"purpose": "Runtime-sampled pre-repair fixture derived from a Cookbook documentation reliability run.",
"sampling_note": "Compact Qdrant-style maintenance sample with stale embedding-model and query API issues.",
"repair_story": {
"target_iteration": 1,
"repair_depth": "One-pass cleanup: modernize the local Qdrant query path and clarify the sampled fixture framing.",
"issue_layers": [
"stale embedding model",
"deprecated qdrant.search call",
"fixture framing/setup clarity"
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}