Spaces:
Build error
Build error
Update functions.py
Browse files- functions.py +0 -23
functions.py
CHANGED
|
@@ -3,29 +3,6 @@ import numpy as np
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
def vector_similarity(x: list[float], y: list[float]) -> float:
|
| 7 |
-
"""
|
| 8 |
-
Returns the similarity between two vectors.
|
| 9 |
-
|
| 10 |
-
Because OpenAI Embeddings are normalized to length 1, the cosine similarity is the same as the dot product.
|
| 11 |
-
"""
|
| 12 |
-
return np.dot(np.array(x), np.array(y))
|
| 13 |
-
|
| 14 |
-
def select_document_section_by_query_similarity(query: str, contexts: dict[(str, str), np.array]) -> list[(float, (str, str))]:
|
| 15 |
-
"""
|
| 16 |
-
Find the query embedding for the supplied query, and compare it against all of the pre-calculated document embeddings
|
| 17 |
-
to find the most relevant sections.
|
| 18 |
-
|
| 19 |
-
Return the list of document sections, sorted by relevance in descending order.
|
| 20 |
-
"""
|
| 21 |
-
query_embedding = get_embedding(query)
|
| 22 |
-
|
| 23 |
-
document_similarities = sorted([
|
| 24 |
-
(vector_similarity(query_embedding, doc_embedding), doc_index) for doc_index, doc_embedding in contexts.items()
|
| 25 |
-
], reverse=True)
|
| 26 |
-
|
| 27 |
-
return document_similarities[0]
|
| 28 |
-
|
| 29 |
def construct_prompt(query: str, context_embeddings: dict, df: pd.DataFrame) -> tuple[str, str]:
|
| 30 |
"""
|
| 31 |
Constructs a prompt for the language model based on the most relevant service description.
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def construct_prompt(query: str, context_embeddings: dict, df: pd.DataFrame) -> tuple[str, str]:
|
| 7 |
"""
|
| 8 |
Constructs a prompt for the language model based on the most relevant service description.
|