Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: could make searching code easier to understand #65

Open
eddotman opened this issue Nov 8, 2022 · 0 comments
Open

refactor: could make searching code easier to understand #65

eddotman opened this issue Nov 8, 2022 · 0 comments
Labels
refactor Better design while preserving functionality

Comments

@eddotman
Copy link
Collaborator

eddotman commented Nov 8, 2022

self._measure_similarity(embedded_query, d.embedding)
for d in self.documents
]
max_similarity = max(similarities)
if max_similarity < threshold:
logging.warning(
f"Max search similarity {max_similarity} below threshold {threshold}; "
"no document returned."
)
return None
logging.info(f"Search result found for query: {query}")
nearest_idx = similarities.index(max_similarity)
nearest_doc = self.documents[nearest_idx]
return nearest_doc

Right now there are some list comprehensions & reliance on list order preservation that make this hard to interpret.

The code could be made more explicit:

# Search for the most similar doc
most_similar_document = None
top_similarity_score = 0
for doc in documents:
    similarity_score = self._measure_similarity(embedded_query, doc.embedding)
    if similarity_score > top_similarity_score:
        most_similar_document = doc

# Make sure it's similar enough
if top_similarity_score < threshold:
    logging.warning(
        f"Max search similarity {max_similarity} below threshold {threshold}; "
        "no document returned."
    )
    return None

return most_similar_document

Shoutout to @mkozakov and Jacob-GK for flagging this!

@eddotman eddotman added the refactor Better design while preserving functionality label Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Better design while preserving functionality
Projects
None yet
Development

No branches or pull requests

1 participant