How to configure prompts for chat completion with context injection?

After months of watching tutorial videos and studying documentation, examples, blogs and papers, learning how to extract and chunk and embed my data, learning how to make the correct api calls to do all of this, and doing all of this in PHP, I’ve finally come to the point where I’m ready to do my chat completion.

I figure there are dozens of you who have already gone way past this point, so I will hopefully get a little assistance here.

Below is my flowchart. I understand each step, what I need to do and how, except for one thing: How do I configure the prompt consisting of my question and it’s associated context texts in order to submit a chat completion request to the OpenAI model?

So, this is what I need to submit:

a. The question
b. The texts (3) that provide the context for the question.

So, let’s say this is my starting point:

	$data = array(
        "model" => "gpt-3.5-turbo",
		"messages" => array(
			array("role" => "system", "content" => "You are a helpful assistant. Your goal is to answer the following question, using the associated texts as context, as truthfully as you can."),
			array("role" => "user", "content" => $prompt),
		),

How the heck to I format $prompt?

$prompt = '
Question: "My question here."
Context document 1: "My first context text."
Context document 2: "My second context text"
Context document 3: "My 3rd context text"
';

How do you guys who’ve been doing this for a while handle this?

Think of the prompt as chatting with the AI.

The system message could be reconfigured to something like “You are a helpful question-answering system”. Simple and to the point. Provide the instruction as the next user prompt. “Given some context, provide a truthful answer to the question given below”. You can provide the question and the context individually or as a single document you have given and then get the system to generate the output for you.

While the system message it important, gpt-3.5 is not as good as gpt-4 in giving importance to the system message. By providing your instructions in a seperate message, they are more likely to permeate.

1 Like

Hi @SomebodySysop

Here’s a working example.

1 Like