Answer
Finally, we use these similarity scores to sort and filter the results. scored_articles = zip(articles, cosine_similarities) # Sort articles by cosine similarity sorted_articles = sorted(scored_articles, key=lambda x: x[1], reverse=True) # Print top 5 articles print("Top 5 articles:", "\n") for article, score in sorted_articles[0:5]: print("Title:", article["title"]) print("Description:", article["description"]) print("Content:", article["content"][0:100] + "...") print("Score:", score) print() Awesome! These results look a lot more relevant to our original query. Now, let's use the top 5 results to generate a final answer. 3. Answer formatted_top_results = [ { "title": article["title"], "description": article["description"], "url": article["url"], } for article, _score in sorted_articles[0:5] ] ANSWER_INPUT = f"""
Generate an answer to the user's question based on the given search results. TOP_RESULTS: {formatted_top_results} USER_QUESTION: {USER_QUESTION} Include as much information as possible in the answer. Reference the relevant search result urls as markdown links.""" completion = openai.ChatCompletion.create( model=GPT_MODEL, messages=[{"role": "user", "content": ANSWER_INPUT}], temperature=0.5, stream=True, ) text = "" for chunk in completion: text += chunk.choices[0].delta.get("content", "") display.clear_output(wait=True) display.display(display.Markdown(text))