-
Notifications
You must be signed in to change notification settings - Fork 3
Chains
The following example shows how we can use a chain to run prompts with simulated user input. This user input will be generated by OpenAI based on the context of the chat completions.
Take the following example as the first prompt in the chain. Call it structured-story.prompt. There are three possible values you can configure for substitution.
- story
- character
- characterAction
The response will be in JSON Format.
PREVIOUS SCENE
${story}
CHARACTER
Role: ${character}
Main Character Name: ${mainCharacterName}
If no main character name is given, choose one based on role
CHARACTER ACTION
${characterAction}
Write me a story based on the character role. If character name, action and the previous scene
are given also use those. Write two sentences only.
RESPONSE
The response must only be in JSON using the following structure.
Only use these fields. {"mainCharacterName": "", "story": ""}
We can see from the above prompt that the previous scene and characterAction should change every request, while the mainCharacterName and character should remain the same through the prompt chain. It's clear that we can use the story in the JSON response in the next prompt request, but how do we get the characterAction?
In a real application the characterAction would come from the user input. We can ask the AI to give us an action to simulate a user response.
So create a second prompt called character-action.prompt.
Give me an action for ${mainCharacterName} for the following story:
${story}
The response must be in JSON using the following structure. Only use these fields. {"characterAction": ""}
Now create an project file called project.yaml
---
projectName: experiment-chain-multiple
projectVersion: '1.4'
apiKeyFile: "../../api_key"
blocks:
- blockId: chain-1
pluginName: ExperimentGptPlugin
configuration:
requestParams:
model: gpt-3.5-turbo
temperature: 1.2
top_p: 1
max_tokens: 500
executions:
- id: exp-1
chainRuns: 2
promptChain:
- structured-story.prompt
- character-action.prompt
excludesMessageHistory:
- character-action.prompt
fixJson: true
responseFormat: json
properties:
rank: Commander in Starfleet
show: Star Trek
mainCharacterName: ''
story: ''
characterAction: ''
# Block demonstrates the use of importing properties
- blockId: chain-2
pluginName: ExperimentGptPlugin
configuration:
requestParams:
model: gpt-3.5-turbo
temperature: 1.2
top_p: 1
max_tokens: 500
executions:
- id: exp-1
chainRuns: 2
promptChain:
- structured-story.prompt
- character-action.prompt
excludesMessageHistory:
- character-action.prompt
fixJson: true
responseFormat: json
properties:
rank: Commander in Starfleet
mainCharacterName: ''
story: ''
characterAction: ''
# Import properties from a properties file
import:
propertiesFile: properties.json
properties:
rank: 3
show: 2In the project file above, notice that we have added both prompts to the chain. Since the chain runs 2 times, it will run the prompts in the following order
- structured-story.prompt
- character-action.prompt
- structured-story.prompt
- character-action.prompt
We also have the character-action.prompt excluded from the message history. What this means is that this prompt and its result will not be part of the main chat context. It's a one-shot. This will be clearer in the explanation below.
The first request will look like
The response will be in JSON Format.
PREVIOUS SCENE
CHARACTER
Role: Commander in Starfleet
Main Character Name:
If no main character name is given, choose one based on role
CHARACTER ACTION
Write me a story based on the character role. If character name, action and the previous scene
are given also use those. Write two sentences only.
RESPONSE
The response must only be in JSON using the following structure.
Only use these fields. {"mainCharacterName": "", "story": ""}
Since only the character property was given in the project file, it's the only one that is non-blank in the prompt.
The following is an example of a response. The Klingons are threatening the Federation.
{
"mainCharacterName": "Captain Kirk",
"story": "As soon as Captain Kirk received news of a possible threat to the Federation
from the Klingons, he swiftly ordered his crew to high alert, and set course towards the
Neutral Zone to investigate."
}
For the second request, let's ask AI for the characterAction using the character-action.prompt
Give me an action for Captain Kirk for the following story:
As soon as Captain Kirk received news of a possible threat to the Federation
from the Klingons, he swiftly ordered his crew to high alert, and set course towards the
Neutral Zone to investigate
The response must be in JSON using the following structure. Only use these fields. {"characterAction": ""}
The response is the following. AI has generated a plausible user response for testing.
{
"characterAction": "Captain Kirk ordered his crew to high alert and set course towards the Neutral Zone to investigate the threat from the Klingons."
}Now we do the third request using the structured-story.prompt. Notice how we substitute in the characterAction generated from the previous prompt.
The response will be in JSON Format.
PREVIOUS SCENE
As soon as Captain Kirk received news of a possible threat to the Federation
from the Klingons, he swiftly ordered his crew to high alert, and set course towards the
Neutral Zone to investigate
CHARACTER
Role: Commander in Starfleet
Main Character Name: Captain Kirk
If no main character name is given, choose one based on role
CHARACTER ACTION
Captain Kirk ordered his crew to high alert and set course towards the Neutral Zone to investigate the threat from the Klingons.
Write me a story based on the character role. If character name, action and the previous scene
are given also use those. Write two sentences only.
RESPONSE
The response must only be in JSON using the following structure.
Only use these fields. {"mainCharacterName": "", "story": ""}
Based on the character action of "entering the Neutral Zone", we get the next scene.
{
"mainCharacterName": "Captain Kirk",
"story": "As news of a possible threat from the Klingons reached him, Captain Kirk swiftly
ordered his crew to high alert and set course towards the Neutral Zone to investigate. Determined
to protect the Federation from any harm, the brave commander led his crew forward, ready to face
whatever danger lay ahead."
}
This example shows how we can use chaining to simulate user input in a chain.
To run the experiment
air run -b exp-1
For each run, you will get a record of the request sent and response received:
{
"model": "gpt-3.5-turbo",
"temperature": 1.2,
"top_p": 1,
"max_tokens": 500,
"messages": [
{
"role": "user",
"content": "Write me a story about Commander in Starfleet. One Sentence Only."
}
]
}The response allows you to determine tokens used.
{
"id": "chatcmpl-74WgfENKxOmQwRwtpoJ6IFELyzNTL",
"object": "chat.completion",
"created": 1681313317,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 22,
"completion_tokens": 27,
"total_tokens": 49
},
"choices": [
{
"message": {
"role": "assistant",
"content": "As Commander of the USS Enterprise, Jean-Luc Picard boldly leads his crew through perilous missions and treks through the galaxy."
},
"finish_reason": "stop",
"index": 0
}
]
}The metrics.csv file will give you the performance times and token usage for each experiment. In this case, there was an alternating run of two prompts. The story prompt takes 3.9 and 5.7 seconds to run.
request_id, prompt_name, request_time, prompt_tokens, completion_tokens, total_tokens
chatcmpl-774QSUZEM0qGzIHSkjdc1YB6SnqqU, structured-story.prompt, 3872, 126, 55, 181
chatcmpl-774QWC735h8zqC48NdDvQBbAK4wtI, character-action.prompt, 2361, 91, 30, 121
chatcmpl-774QYzgnx9x3UjxPjd4ef4lotUPI1, structured-story.prompt, 5668, 190, 72, 262
chatcmpl-774QelDpcSp02xIJSH2kpdj1WyNsJ, character-action.prompt, 2057, 111, 23, 134