community[patch]: surrealdb handle for empty metadata and allow collection names with complex characters (#17374)

- **Description:** Handle for empty metadata and allow collection names
with complex characters
  - **Issue:** #17057
  - **Dependencies:** `surrealdb`

---------

Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
pull/17307/head^2
Marcus Virginia 2 months ago committed by GitHub
parent 0df76bee37
commit 69bb96c80f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -220,26 +220,38 @@ class SurrealDBStore(VectorStore):
"k": k, "k": k,
"score_threshold": kwargs.get("score_threshold", 0), "score_threshold": kwargs.get("score_threshold", 0),
} }
query = """select id, text, metadata, query = f"""
vector::similarity::cosine(embedding,{embedding}) as similarity select
from {collection} id,
where vector::similarity::cosine(embedding,{embedding}) >= {score_threshold} text,
order by similarity desc LIMIT {k} metadata,
""".format(**args) vector::similarity::cosine(embedding, $embedding) as similarity
results = await self.sdb.query(query) from {args["collection"]}
where vector::similarity::cosine(embedding, $embedding) >= $score_threshold
order by similarity desc LIMIT $k;
"""
results = await self.sdb.query(query, args)
if len(results) == 0: if len(results) == 0:
return [] return []
result = results[0]
if result["status"] != "OK":
from surrealdb.ws import SurrealException
err = result.get("result", "Unknown Error")
raise SurrealException(err)
return [ return [
( (
Document( Document(
page_content=result["text"], page_content=doc["text"],
metadata={"id": result["id"], **result["metadata"]}, metadata={"id": doc["id"], **(doc.get("metadata", None) or {})},
), ),
result["similarity"], doc["similarity"],
) )
for result in results[0]["result"] for doc in result["result"]
] ]
async def asimilarity_search_with_relevance_scores( async def asimilarity_search_with_relevance_scores(

Loading…
Cancel
Save