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

merge dev #201

Merged
merged 6 commits into from
Feb 20, 2024
Merged
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
2 changes: 0 additions & 2 deletions web/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const metadata: Metadata = {
};

function Footer() {
const year = new Date().getFullYear();
return (
<footer className='flex flex-col w-full p-6 pt-2'>
<div className='font-regular flex gap-x-[2px] text-[12px] text-indigo-800/70'>
Expand All @@ -32,7 +31,6 @@ function Footer() {
rel='noopener noreferrer'>
polywrap
</a>
<span>© {year}</span>
</div>
</footer>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from langchain.text_splitter import CharacterTextSplitter

from chromadb import EphemeralClient
Expand All @@ -10,6 +11,7 @@
from fund_public_goods.lib.strategy.utils.strings_to_numbers import strings_to_numbers
from langchain_openai import OpenAIEmbeddings
from langchain.vectorstores.chroma import Chroma
import openai


reranking_prompt_template = """
Expand All @@ -22,9 +24,18 @@
formatted as a comma-separated list without any quotation marks.
It is crucial to return these IDs exactly as they appear, with no modifications.

You will return the list of IDs as a string with the following format:
You will return a JSON object with the following format:
'''
34,23,5,1,44,53,13
{{
"project_ids": number[]
}}
'''

Example response:
'''
{{
"project_ids": [32, 25, 4, 8]
}}
'''

In your assessment, it is essential to discern the genuine alignment of each project with the user's specific requirements.
Expand All @@ -39,37 +50,35 @@
"""

def rerank_top_projects(prompt: str, projects: list[Projects]) -> list[Projects]:
reranking_prompt = ChatPromptTemplate.from_messages([
("system", reranking_prompt_template),
separator = "\n-----\n"
formatted_projects = separator.join([
f"ID: {i} - Description: {projects[i].description}\n"
for i in range(len(projects))
])
formatted_prompt = reranking_prompt_template.format(prompt=prompt, separator=separator, projects=formatted_projects)

llm = ChatOpenAI(model="gpt-4-1106-preview") # type: ignore

reranking_chain = reranking_prompt | llm | StrOutputParser()
response = openai.chat.completions.create(
model="gpt-4-1106-preview",
response_format={"type": "json_object"},
messages=[
{"role": "user", "content": formatted_prompt}
]
)

separator = "\n-----\n"

top_ids_res = reranking_chain.invoke({
"prompt": prompt,
"separator": separator,
"projects": separator.join([
f"ID: {i} - Description: {projects[i].description}\n"
for i in range(len(projects))
])
})
top_ids_split = top_ids_res.split(',')
top_ids = strings_to_numbers(top_ids_split)
raw_response = str(response.choices[0].message.content)
json_response = json.loads(raw_response)
top_ids = json_response['project_ids']
reranked_projects: list[Projects] = []

for i in range(len(top_ids)):
id = top_ids[i]
if id is None:
raise Exception(
f"The LLM has responded with a non-number at index {i}. Llm response ({top_ids_res}). Response split ({top_ids_split})"
f"The LLM has responded with a non-number at index {i}. Llm response ({json_response}). RAW response ({raw_response})"
)
if id > len(projects) or id < 0:
raise Exception(
f"ID {id} not found in projects array (len {len(projects)}). Llm response ({top_ids_res}). Response split ({top_ids_split})"
f"ID {id} not found in projects array (len {len(projects)}). Llm response ({json_response}). RAW response ({raw_response})"
)
reranked_projects.append(projects[id])

Expand Down
Loading