Repository navigation
Expand file tree
/
Copy pathsearch_with_openai.py
More file actions
174 lines (144 loc) · 7.21 KB
/
Copy pathsearch_with_openai.py
File metadata and controls
174 lines (144 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""Search with OpenAI Embedder — real vector search using OpenAI's API.
This example mirrors `search_with_openai.rs` but produces embeddings on the
Python side using the `openai` SDK, then passes the resulting vectors to
Laurus as `VectorQuery`.
Prerequisites:
pip install openai
export OPENAI_API_KEY=your-api-key-here
maturin develop
python examples/search_with_openai.py
"""
from __future__ import annotations
import os
import sys
import laurus
# ---------------------------------------------------------------------------
# OpenAI embedding helper
# ---------------------------------------------------------------------------
try:
from openai import OpenAI # type: ignore
except ImportError:
print("ERROR: openai package not installed.")
print(" Install with: pip install openai")
sys.exit(1)
_MODEL = "text-embedding-3-small"
_DIM = 1536
def embed(client: "OpenAI", text: str) -> list[float]:
"""Call OpenAI Embeddings API and return the vector."""
response = client.embeddings.create(input=text, model=_MODEL)
return response.data[0].embedding
# ---------------------------------------------------------------------------
# Dataset (same as the Rust example)
# ---------------------------------------------------------------------------
CHUNKS = [
("django_guide", "Django Web Development", "Django follows the model-template-view architecture pattern for clean separation of concerns.", 1, "framework"),
("django_guide", "Django Web Development", "Django ORM maps Python classes to database tables with migrations for schema management.", 2, "framework"),
("django_guide", "Django Web Development", "Django middleware processes requests and responses through a chain of pluggable components.", 3, "framework"),
("flask_guide", "Flask Microservices", "Flask provides lightweight routing and Jinja2 templating for building APIs and web apps.", 1, "framework"),
("flask_guide", "Flask Microservices", "Flask extensions like Flask-SQLAlchemy and Flask-Migrate add database support to Flask projects.", 2, "framework"),
("numpy_docs", "NumPy Fundamentals", "NumPy arrays provide vectorized operations that are much faster than Python loops for numerical computing.", 1, "scientific"),
("numpy_docs", "NumPy Fundamentals", "Broadcasting in NumPy allows arithmetic operations on arrays of different shapes without copying data.", 2, "scientific"),
("pytest_book", "Testing with pytest", "pytest fixtures provide reusable setup and teardown logic for test functions with dependency injection.", 1, "testing"),
("pytest_book", "Testing with pytest", "pytest parametrize decorator runs the same test with different input datasets automatically.", 2, "testing"),
]
def main() -> None:
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
print("ERROR: OPENAI_API_KEY environment variable not set.")
print(" export OPENAI_API_KEY=your-api-key-here")
sys.exit(1)
print("=== Laurus Search with OpenAI Embedder ===\n")
client = OpenAI(api_key=api_key)
print(f"OpenAI embedder ready (model={_MODEL}, dim={_DIM})\n")
# ── Schema ─────────────────────────────────────────────────────────────
schema = laurus.Schema()
schema.add_text_field("title")
schema.add_text_field("text")
schema.add_text_field("category")
schema.add_integer_field("page")
schema.add_flat_field("text_vec", dimension=_DIM, distance="cosine")
schema.set_default_fields(["text"])
index = laurus.Index(schema=schema)
# ── Index ──────────────────────────────────────────────────────────────
print("--- Indexing chunked documents ---\n")
for doc_id, title, text, page, category in CHUNKS:
vec = embed(client, text)
index.add_document(
doc_id,
{
"title": title,
"text": text,
"category": category,
"page": page,
"text_vec": vec,
},
)
print(f" Indexed {doc_id} page {page}: {text[:50]!r}...")
index.commit()
print(f"\nIndexed {len(CHUNKS)} chunks.\n")
# =====================================================================
# [A] Vector Search
# =====================================================================
print("=" * 60)
print("[A] Vector Search: 'database ORM queries'")
print("=" * 60)
_print_results(
index.search(
laurus.VectorQuery("text_vec", embed(client, "database ORM queries")), limit=3
).results
)
# =====================================================================
# [B] Filtered Vector Search — category filter
# =====================================================================
print("\n" + "=" * 60)
print("[B] Filtered Vector Search: 'database ORM queries' + category='testing'")
print("=" * 60)
request = laurus.SearchRequest(
vector_query=laurus.VectorQuery("text_vec", embed(client, "database ORM queries")),
filter_query=laurus.TermQuery("category", "testing"),
limit=3,
)
_print_results(index.search(request).results)
# =====================================================================
# [C] Filtered Vector Search — numeric range filter
# =====================================================================
print("\n" + "=" * 60)
print("[C] Filtered Vector Search: 'web server HTTP' + page=1")
print("=" * 60)
request = laurus.SearchRequest(
vector_query=laurus.VectorQuery("text_vec", embed(client, "web server HTTP")),
filter_query=laurus.NumericRangeQuery("page", min=1, max=1),
limit=3,
)
_print_results(index.search(request).results)
# =====================================================================
# [D] Lexical Search
# =====================================================================
print("\n" + "=" * 60)
print("[D] Lexical Search: 'fixtures'")
print("=" * 60)
_print_results(index.search(laurus.TermQuery("text", "fixtures"), limit=3).results)
# =====================================================================
# [E] Hybrid Search (RRF)
# =====================================================================
print("\n" + "=" * 60)
print("[E] Hybrid Search (RRF): vector='template rendering' + lexical='jinja2'")
print("=" * 60)
request = laurus.SearchRequest(
lexical_query=laurus.TermQuery("text", "jinja2"),
vector_query=laurus.VectorQuery("text_vec", embed(client, "template rendering")),
fusion=laurus.RRF(k=60.0),
limit=3,
)
_print_results(index.search(request).results)
print("\nSearch with OpenAI example completed!")
def _print_results(results: list) -> None:
if not results:
print(" (no results)")
return
for r in results:
doc = r.document or {}
text = doc.get("text", "")
print(f" id={r.id!r:8s} score={r.score:.4f} text={text!r:.60s}")
if __name__ == "__main__":
main()