Skip to content

Prompts

sisbell edited this page May 8, 2023 · 1 revision

Chained prompts are useful when you want to use the results from one prompt in the next prompt. In the following case we will use a single prompt feeding back into itself. This technique can be used for maintaining a strong context between requests, something particularly useful for generating stories.

Prompt

Create the prompt file.

Write me a story about ${character}. The main character is ${mainCharacterName}. 
If no main character is given, choose one. Write one sentence only.

The response should be in JSON using the following structure. 
Only use these fields. {"mainCharacterName": "", "story": ""}

Project File

Now create the project file: project.yaml. Note that we are defining the character value as "Commander in Starfleet" but are not defining any mainCharacterName. We will let the AI do this for us.

---
projectName: experiment-chain-single
projectVersion: '1.1'
apiKeyFile: "../../api_key"
blocks:
  - blockId: single-1
    pluginName: ExperimentGptPlugin
    configuration:
      requestParams:
        model: gpt-3.5-turbo
        temperature: 1.2
        top_p: 1
        max_tokens: 500
    executions:
      - id: exp-1
        systemMessageFile: "../system-message.txt"
        responseFormat: json
        promptChain:
          - simple-story.prompt
        properties:
          character: Commander in Starfleet
          mainCharacterName: ''

Since we are chaining requests on the single prompt, it's important to set the response_format field to "json". This is how the response knows how to map itself to the properties in the next request.

You may also choose to set fixJson to true. This will try to cleanup any extra text the AI may add in addition to the JSON response. For example, the following case is common:

"As an AI assistant...{"foo" :" "bar"}"

CLI

To run the experiment command

air run -b exp-1

Response

On the first (chain) request, the prompt sent to OpenAI will be

Write me a story about Commander in Starfleet. The main character is . 
If no main character is given, choose one. Write one sentence only.

The response should be in JSON using the following structure. 
Only use these fields. {"mainCharacterName": "", "story": ""}

The content of the response looks like

{
  "mainCharacterName": "Kiera", 
  "story": "Commander Kiera was a respected officer in Starfleet, known for her exceptional leadership skills and bravery in the face of danger."
}

On the second (chain) request, we use the mainCharacterName from the above JSON and substitute it into the prompt.

Write me a story about Commander in Starfleet. The main character is Kiera. If no main character is given, choose one. Write one sentence only.
The response should be in JSON using the following structure. Only use these fields. {"mainCharacterName": "Kiera", "story" :""}

So you can see once the story starts with "Commander Kiera", it will now continue on with that same character.

If we set the chain_run to 3, then there would be another call of the prompt above.

Clone this wiki locally