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

Removed double fetching of projects from Supabase #222

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from fund_public_goods.db.entities import Projects
from fund_public_goods.db.tables.projects import get_projects_by_ids
from fund_public_goods.lib.strategy.models.answer import Answer
from fund_public_goods.lib.strategy.utils.get_top_matching_projects import get_top_matching_projects


def fetch_matching_projects(prompt: str) -> list[tuple[Projects, list[Answer]]]:
matching_projects = get_top_matching_projects(prompt)[:10]
matched_ids = [p.id for p in matching_projects]

matching_projects_with_answers = get_projects_by_ids(matched_ids)

return matching_projects_with_answers
return matching_projects
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
Projects: {projects}
"""

def rerank_top_projects(prompt: str, projects: list[Projects]) -> list[Projects]:
def rerank_top_projects(prompt: str, projects: list[tuple[Projects, list[Answer]]]) -> list[tuple[Projects, list[Answer]]]:
separator = "\n-----\n"
formatted_projects = separator.join([
f"ID: {i} - Description: {projects[i].description}\n"
f"ID: {i} - Description: {projects[i][0].description}\n"
for i in range(len(projects))
])
formatted_prompt = reranking_prompt_template.format(prompt=prompt, separator=separator, projects=formatted_projects)
Expand All @@ -56,7 +56,7 @@ def rerank_top_projects(prompt: str, projects: list[Projects]) -> list[Projects]
raw_response = str(response.choices[0].message.content)
json_response = json.loads(raw_response)
top_ids = json_response['project_ids']
reranked_projects: list[Projects] = []
reranked_projects: list[tuple[Projects, list[Answer]]] = []

for i in range(len(top_ids)):
id = top_ids[i]
Expand All @@ -82,7 +82,7 @@ def remove_duplicates_and_preserve_order(lst: list[str]) -> list[str]:
return result


def get_top_matching_projects(prompt: str) -> list[Projects]:
def get_top_matching_projects(prompt: str) -> list[tuple[Projects, list[Answer]]]:
env = load_env()
vectorstore = Pinecone(
index_name=env.pinecone_index,
Expand All @@ -99,6 +99,6 @@ def get_top_matching_projects(prompt: str) -> list[Projects]:

matched_projects: list[tuple[Projects, list[Answer]]] = get_projects_by_ids(total_unique_ids[:target_unique_ids])

reranked_projects = rerank_top_projects(prompt=prompt, projects=[p for (p, _) in matched_projects])
reranked_projects = rerank_top_projects(prompt=prompt, projects=matched_projects)

return reranked_projects
Loading