After you create a prompt, you can deploy it to your codebase, and then you can call it through the API.

1. Find the Prompt ID

You should first find the Prompt ID of the prompt you want to deploy. You can find the Prompt ID in the Overview panel on the Prompts page.

2. Connect the prompt to codebase

Integrate the prompt to the codebase by adding the following code to your codebase.

from openai import OpenAI

client = OpenAI(
  base_url="https://api.keywordsai.co/api/", # switch to the Keywords AI base URL
  api_key="YOUR_API_KEY", # switch to your Keywords AI API key
)

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": "Tell me a long story"}
    ],
    extra_body={"prompt": {"prompt_id":"042f5f",
                "variables":{"task_description":"Square a number", "specific_library":"math"},
                "version": "1",
                "override": True,
                }
    }
)

Since OpenAI forces you to pass the model and messages fields, you should pass the an override field to the prompt field, which will override the model and messages fields and use the prompt you created in Keywords AI.

3. Deploy the prompt

Once you have connected the prompt to your codebase, you can deploy the prompt on the platform. You can deploy the prompt by clicking on the Deploy button on the Prompts page.

Override prompt messages in the API

You can override the prompt messages in the API by passing the override_params and override_config field to the prompt field. You can choose to override the whole prompt messages or append a new message to the prompt messages.

Example:

request_body = {
    "prompt": {
        "prompt_id": "xxxxxx",
        "override_config": {"messages_override_mode": "append"}, # append or override
        "override_params": {"messages": [{"role": "user", "content": "5"}]},
    }
}

In this example, the override_config specifies how the new messages should be handled:

  • When messages_override_mode is set to "append", the new message {"role": "user", "content": "5"} will be added to the end of the existing prompt messages
  • When set to "override", the new messages will completely replace the original prompt messages

4. View the logs with the prompt

You can view the logs with the prompt by clicking a log on the Logs page or filtering the logs with the prompt name.

Optional parameters

echo
boolean
default:
true

with echo on, the response body will have an extra field

  "prompt_message": [an array of messages]
override
boolean
default:
true

Turn on override to use params in override_params instead of the params in the prompt.

{
  "override": true,
}

override_params
boolean
default:
true

You can put any OpenAI chat/completions parameters here to override the prompt’s parameters. This will only work if override is set to true.

{
  "override_params": {
    "temperature": 0.5,
    "max_tokens": 100
  }
}
override_config
object

This parameter allows you to control how you can override the parameters in the prompt.

Troubleshooting

Enable stream when you’re using OpenAI SDK

If you’re using OpenAI SDK and want to connect with the prompt you created, you have to specify stream=True in the call body.