8000 improved connection reuse · Bubby2015/azure-sql-db-openai@b25a5f5 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b25a5f5

Browse files
committed
improved connection reuse
1 parent 8a87680 commit b25a5f5

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

python/hybrid_search.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,37 @@
1919
model = SentenceTransformer('multi-qa-MiniLM-L6-cos-v1')
2020
embeddings = model.encode(sentences)
2121

22+
conn = get_mssql_connection()
23+
2224
print('Cleaning up the database...')
2325
try:
24-
conn = get_mssql_connection()
25-
conn.execute("DELETE FROM dbo.document_embeddings;")
26-
conn.execute("DELETE FROM dbo.documents;")
27-
conn.commit();
26+
cursor = conn.cursor()
27+
cursor.execute("DELETE FROM dbo.document_embeddings;")
28+
cursor.execute("DELETE FROM dbo.documents;")
29+
cursor.commit();
2830
finally:
29-
conn.close()
31+
cursor.close()
3032

3133
print('Saving documents and embeddings in the database...')
3234
try:
33-
conn = get_mssql_connection()
3435
cursor = conn.cursor()
3536

36-
for content, embedding in zip(sentences, embeddings):
37+
for id, (content, embedding) in enumerate(zip(sentences, embeddings)):
3738
cursor.execute(f"""
38-
INSERT INTO dbo.documents (content, embedding) VALUES (?, ?);
39-
INSERT INTO dbo.document_embeddings SELECT SCOPE_IDENTITY(), CAST([key] AS INT), CAST([value] AS FLOAT) FROM OPENJSON(?);
39+
DECLARE @id INT = ?;
40+
DECLARE @content NVARCHAR(MAX) = ?;
41+
DECLARE @embedding NVARCHAR(MAX) = ?;
42+
INSERT INTO dbo.documents (id, content, embedding) VALUES (@id, @content, @embedding);
43+
INSERT INTO dbo.document_embeddings SELECT @id, CAST([key] AS INT), CAST([value] AS FLOAT) FROM OPENJSON(@embedding);
4044
""",
45+
id,
4146
content,
42-
json.dumps(embedding.tolist()),
4347
json.dumps(embedding.tolist())
4448
)
4549

46-
cursor.close()
47-
conn.commit()
50+
cursor.commit()
4851
finally:
49-
conn.close()
52+
cursor.close()
5053

5154
print('Searching for similar documents...')
5255
print('Getting embeddings...')
@@ -56,7 +59,6 @@
5659
print('Querying database...')
5760
k = 5
5861
try:
59-
conn = get_mssql_connection()
6062
cursor = conn.cursor()
6163

6264
results = cursor.execute(f"""
@@ -95,9 +97,7 @@
9597
)
9698

9799
for row in results:
98-
print('document:', row[0], 'RRF score:', row[1])
100+
print(f'Document: {row[0]} -> RRF score: {row[1]:0.4}')
99101

100-
cursor.close()
101-
conn.commit()
102102
finally:
103-
conn.close()
103+
cursor.close()

0 commit comments

Comments
 (0)
0