This tutorial covers dynamic context injection for chatbots to provide accurate, up-to-date answers by sourcing relevant information at runtime.
In this tutorial, we’ll cover dynamic context injection—a technique that ensures your chatbot always provides accurate, up-to-date answers by pulling relevant information at runtime. You’ll start with a basic chat application using the OpenAI Chat Completion endpoint and evolve it into a robust system that sources context dynamically.
First, observe how GPT-3.5-turbo handles a query about the 95th Academy Awards (March 2023) without any added context. Because its training data cuts off in September 2021, it won’t know about later events.
import osimport openaiopenai.api_key = os.getenv("OPENAI_API_KEY")response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": "You answer questions about the 95th Academy Awards held in March 2023. Answer only if you are sure." }, { "role": "user", "content": "Which movie won the Best Picture award?" } ])print(response.choices[0].message.content)
Expected response:
I’m sorry, but I don’t have information on events after September 2021.
Without external context, the model defaults to stating its knowledge cutoff.
To work around the cutoff, you can paste relevant excerpts from a trusted source directly into the prompt. For instance, from Good Morning America:
On Hollywood’s biggest night, Everything Everywhere All at Once reigned supreme, winning seven Oscars, including Best Picture.
Inject this snippet into your user message:
import osimport openaiopenai.api_key = os.getenv("OPENAI_API_KEY")response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": "You answer questions about the 95th Academy Awards held in March 2023. Answer only if you are sure." }, { "role": "user", "content": ( "On Hollywood’s biggest night, \"Everything Everywhere All at Once\" reigned supreme, " "winning seven Oscars, including Best Picture. " "Which movie won the Best Picture award?" ) } ])print(response.choices[0].message.content)
Output:
"Everything Everywhere All at Once" won the Best Picture award at the 95th Academy Awards.
Static context can quickly bloat your prompt and is tedious to maintain as information changes.