Skip to content

Commit

Permalink
Add proper sorting of context documents based on similarity score
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenwindflower committed Apr 4, 2023
1 parent 7b07238 commit a676a05
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Binary file modified __pycache__/question_answerer.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/vector_store.cpython-311.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

if __name__ == "__main__":
qa = QuestionAnswerer()
qa.answer_question()
answer = qa.answer_question()
print(answer)
20 changes: 9 additions & 11 deletions question_answerer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

template = """
Answer the question as truthfully as possible using the provided context,
and if the answer is not contained within the text below, say "I don't know."
Answer the question as truthfully as possible using the provided context, and assume
that the question is about dbt, analytics engineering, or data. Prefer code snippets
over prose where possible and relevant.
If the answer is not contained within the text below, say "I don't know."\n
\n
Context:\n
{context} \n
\n
Context:
\n
{context}
\n
\n
Question:
\n
{question}
Question: \n
{question}
"""


Expand All @@ -37,4 +35,4 @@ def answer_question(self) -> str:
question = input("Ask a question: ")
context = self.db.rank_and_truncate_documents(question)
answer = self.llm(prompt.format(question=question, context=context))
print(answer)
return answer
3 changes: 2 additions & 1 deletion vector_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def rank_and_truncate_documents(self, question: str, max_tokens: int = 3000):
chosen_sections = ""
chosen_sections_len = 0

for result in results:
for result in sorted(results, key=lambda x: x[1], reverse=True):
print(results)
result_content = result[0].page_content.replace("\n", " ")
if (
chosen_sections_len
Expand Down

0 comments on commit a676a05

Please sign in to comment.