Examine The Context
By encouraging our model to examine each source in the provided context, we can help mitigate the impact of irrelevant context. This improves reasoning performance and the final output. This is known as Thread Of Thought 1.
We can implement Thread Of Thought using the following template.
Thread Of Thought template
[ Input Prompt ]
Proceed through the context systematically, zeroing in on areas that could provide the answers we’re seeking
We can implement this using instructor
as seen below.
import instructor
from openai import OpenAI
from pydantic import BaseModel, Field
from textwrap import dedent
client = instructor.from_openai(OpenAI())
class ThreadOfThoughtResponse(BaseModel):
analysis: list[str] = Field(
description="""An explanation for each relevant source explaining
its relevance and content""",
)
correct_answer: int
def analyze_context_and_generate_response(query: str, context: list[str]):
return client.chat.completions.create(
model="gpt-4o",
response_model=ThreadOfThoughtResponse,
messages=[
{
"role": "system",
"content": dedent(
f"""
You are an expert Question Answerer.
Here are all of the sources that you should refer to
for context:
{'\n'.join(context)}
"""
),
},
{
"role": "user",
"content": query,
},
{
"role": "assistant",
"content": dedent(
"""
Navigate through the context incrementally,
identifying and summarizing relevant portions.
"""
),
},
],
)
if __name__ == "__main__":
context = [
"The price of a house was $100,000 in 2024",
"""The Great Wall of China is not visible from space
with the naked eye""",
"""Honey never spoils; archaeologists have found pots
of honey in ancient Egyptian tombs that are over
3,000 years old""",
"""The world's oldest known living tree is over 5,000
years old and is located in California""",
"The price of a house was $80,000 in 2023",
]
query = "What was the increase in the price of a house from 2023 to 2024"
response = analyze_context_and_generate_response(query, context)
print(response.model_dump_json(indent=2))
"""
{
"analysis": [
"The price of a house was $80,000 in 2023",
"The price of a house was $100,000 in 2024"
],
"correct_answer": 20000
}
"""
Useful Tips¶
Here are some alternative phrases that you can add to your prompt to generate a thread of thought before your model generates a response.
- In a step-by-step manner, go through the context, surfacing important information that could be useful.
- Walk me through this lengthy document segment by segment, focusing on each part's significance.
- Guide me through the context part by part, providing insights along the way.
- Divide the document into manageable parts and guide me through each one, providing insights as we move along.
- Let's go through this document piece by piece, paying close attention to each section.
- Take me through the context bit by bit, making sure we capture all important aspects.
- Examine the document in chunks, evaluating each part critically before moving to the next.
- Analyze the context by breaking it down into sections, summarizing each as we move forward.
- Navigate through the context incrementally, identifying and summarizing relevant portions.
- Proceed through the context systematically, zeroing in on areas that could provide the answers we're seeking.
- Take me through this long document step-by-step, making sure not to miss any important details.
- Analyze this extensive document in sections, summarizing each one and noting any key points.
- Navigate through this long document by breaking it into smaller parts and summarizing each, so we don't miss anything.
- Let's navigate through the context section by section, identifying key elements in each part.
- Let's dissect the context into smaller pieces, reviewing each one for its importance and relevance.
- Carefully analyze the context piece by piece, highlighting relevant points for each question.
- Read the context in sections, concentrating on gathering insights that answer the question at hand.
- Let's read through the document section by section, analyzing each part carefully as we go.
- Let's dissect this document bit by bit, making sure to understand the nuances of each section.
- Systematically work through this document, summarizing and analyzing each portion as we go.
- Let's explore the context step-by-step, carefully examining each segment.
- Systematically go through the context, focusing on each part individually.
- Methodically examine the context, focusing on key segments that may answer the query.
- Progressively sift through the context, ensuring we capture all pertinent details.
- Take a modular approach to the context, summarizing each part before drawing any conclusions.
- Examine each segment of the context meticulously, and let's discuss the findings.
- Approach the context incrementally, taking the time to understand each portion fully.
- Let's scrutinize the context in chunks, keeping an eye out for information that answers our queries.
- Walk me through this context in manageable parts step by step, summarizing and analyzing as we go.
- Let's take a segmented approach to the context, carefully evaluating each part for its relevance to the questions posed.