Skip to content

Commit d5314c8

Browse files
authored
simplify interactive query path in main() for all examples (#544)
1 parent 827002d commit d5314c8

File tree

6 files changed

+63
-81
lines changed

6 files changed

+63
-81
lines changed

examples/amazon_s3_embedding/main.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,16 @@ def _main():
5757
with cocoindex.FlowLiveUpdater(amazon_s3_text_embedding_flow):
5858
# Run queries in a loop to demonstrate the query capabilities.
5959
while True:
60-
try:
61-
query = input("Enter search query (or Enter to quit): ")
62-
if query == '':
63-
break
64-
results, _ = query_handler.search(query, 10)
65-
print("\nSearch results:")
66-
for result in results:
67-
print(f"[{result.score:.3f}] {result.data['filename']}")
68-
print(f" {result.data['text']}")
69-
print("---")
70-
print()
71-
except KeyboardInterrupt:
60+
query = input("Enter search query (or Enter to quit): ")
61+
if query == '':
7262
break
63+
results, _ = query_handler.search(query, 10)
64+
print("\nSearch results:")
65+
for result in results:
66+
print(f"[{result.score:.3f}] {result.data['filename']}")
67+
print(f" {result.data['text']}")
68+
print("---")
69+
print()
7370

7471
if __name__ == "__main__":
7572
load_dotenv()

examples/code_embedding/main.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,17 @@ def _main():
7474
pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL"))
7575
# Run queries in a loop to demonstrate the query capabilities.
7676
while True:
77-
try:
78-
query = input("Enter search query (or Enter to quit): ")
79-
if query == '':
80-
break
81-
# Run the query function with the database connection pool and the query.
82-
results = search(pool, query)
83-
print("\nSearch results:")
84-
for result in results:
85-
print(f"[{result['score']:.3f}] {result['filename']}")
86-
print(f" {result['code']}")
87-
print("---")
88-
print()
89-
except KeyboardInterrupt:
77+
query = input("Enter search query (or Enter to quit): ")
78+
if query == '':
9079
break
80+
# Run the query function with the database connection pool and the query.
81+
results = search(pool, query)
82+
print("\nSearch results:")
83+
for result in results:
84+
print(f"[{result['score']:.3f}] {result['filename']}")
85+
print(f" {result['code']}")
86+
print("---")
87+
print()
9188

9289
if __name__ == "__main__":
9390
load_dotenv()

examples/gdrive_text_embedding/main.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,17 @@ def _main():
7272
pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL"))
7373
# Run queries in a loop to demonstrate the query capabilities.
7474
while True:
75-
try:
76-
query = input("Enter search query (or Enter to quit): ")
77-
if query == '':
78-
break
79-
# Run the query function with the database connection pool and the query.
80-
results = search(pool, query)
81-
print("\nSearch results:")
82-
for result in results:
83-
print(f"[{result['score']:.3f}] {result['filename']}")
84-
print(f" {result['text']}")
85-
print("---")
86-
print()
87-
except KeyboardInterrupt:
75+
query = input("Enter search query (or Enter to quit): ")
76+
if query == '':
8877
break
78+
# Run the query function with the database connection pool and the query.
79+
results = search(pool, query)
80+
print("\nSearch results:")
81+
for result in results:
82+
print(f"[{result['score']:.3f}] {result['filename']}")
83+
print(f" {result['text']}")
84+
print("---")
85+
print()
8986

9087
if __name__ == "__main__":
9188
load_dotenv()

examples/pdf_embedding/main.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,12 @@ def _main():
111111
pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL"))
112112
# Run queries in a loop to demonstrate the query capabilities.
113113
while True:
114-
try:
115-
query = input("Enter search query (or Enter to quit): ")
116-
if query == '':
117-
break
118-
# Run the query function with the database connection pool and the query.
119-
results = search(pool, query)
120-
print(SEARCH_RESULTS_TEMPLATE.render(results=results))
121-
except KeyboardInterrupt:
114+
query = input("Enter search query (or Enter to quit): ")
115+
if query == '':
122116
break
117+
# Run the query function with the database connection pool and the query.
118+
results = search(pool, query)
119+
print(SEARCH_RESULTS_TEMPLATE.render(results=results))
123120

124121

125122
if __name__ == "__main__":

examples/text_embedding/main.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,17 @@ def _main():
6565
pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL"))
6666
# Run queries in a loop to demonstrate the query capabilities.
6767
while True:
68-
try:
69-
query = input("Enter search query (or Enter to quit): ")
70-
if query == '':
71-
break
72-
# Run the query function with the database connection pool and the query.
73-
results = search(pool, query)
74-
print("\nSearch results:")
75-
for result in results:
76-
print(f"[{result['score']:.3f}] {result['filename']}")
77-
print(f" {result['text']}")
78-
print("---")
79-
print()
80-
except KeyboardInterrupt:
68+
query = input("Enter search query (or Enter to quit): ")
69+
if query == '':
8170
break
71+
# Run the query function with the database connection pool and the query.
72+
results = search(pool, query)
73+
print("\nSearch results:")
74+
for result in results:
75+
print(f"[{result['score']:.3f}] {result['filename']}")
76+
print(f" {result['text']}")
77+
print("---")
78+
print()
8279

8380
if __name__ == "__main__":
8481
load_dotenv()

examples/text_embedding_qdrant/main.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,26 @@ def _main():
6666

6767
# Run queries in a loop to demonstrate the query capabilities.
6868
while True:
69-
try:
70-
query = input("Enter search query (or Enter to quit): ")
71-
if query == "":
72-
break
73-
74-
# Get the embedding for the query
75-
query_embedding = text_to_embedding.eval(query)
76-
77-
search_results = client.search(
78-
collection_name=QDRANT_COLLECTION,
79-
query_vector=("text_embedding", query_embedding),
80-
limit=10
81-
)
82-
print("\nSearch results:")
83-
for result in search_results:
84-
score = result.score
85-
payload = result.payload
86-
print(f"[{score:.3f}] {payload['filename']}")
87-
print(f" {payload['text']}")
88-
print("---")
89-
print()
90-
except KeyboardInterrupt:
69+
query = input("Enter search query (or Enter to quit): ")
70+
if query == "":
9171
break
72+
73+
# Get the embedding for the query
74+
query_embedding = text_to_embedding.eval(query)
75+
76+
search_results = client.search(
77+
collection_name=QDRANT_COLLECTION,
78+
query_vector=("text_embedding", query_embedding),
79+
limit=10
80+
)
81+
print("\nSearch results:")
82+
for result in search_results:
83+
score = result.score
84+
payload = result.payload
85+
print(f"[{score:.3f}] {payload['filename']}")
86+
print(f" {payload['text']}")
87+
print("---")
88+
print()
9289

9390

9491
if __name__ == "__main__":

0 commit comments

Comments
 (0)