API Reference
- Introduction
- Authentication
- Headers
- Errors
- Supported Providers
- SDKs
- API Details
Chat Completions
Portkey Endpoints
Embeddings
Other APIs
Completions
Moderations
Fine-tuning
Assistants
- Assistants
- Threads
- Messages
- Runs
- Run Steps
Prompt Render
Renders a prompt template with its variable values filled in
curl -X POST "https://api.portkey.ai/v1/prompts/YOUR_PROMPT_ID/render" \
-H "Content-Type: application/json" \
-H "x-portkey-api-key: $PORTKEY_API_KEY" \
-d '{
"variables": {
"user_input": "Hello world"
},
"max_tokens": 250,
"presence_penalty": 0.2
}'
{
"success": true,
"data": {
"messages": [
{
"content": "<string>",
"role": "system",
"name": "<string>"
}
],
"model": "gpt-4-turbo",
"frequency_penalty": 0,
"logit_bias": null,
"logprobs": false,
"top_logprobs": 10,
"max_tokens": 123,
"n": 1,
"presence_penalty": 0,
"response_format": {
"type": "text"
},
"seed": 0,
"stop": "<string>",
"stream": false,
"stream_options": null,
"temperature": 1,
"top_p": 1,
"tools": [
{
"type": "function",
"function": {
"description": "<string>",
"name": "<string>",
"parameters": {}
}
}
],
"tool_choice": "none",
"parallel_tool_calls": true,
"user": "user-1234",
"function_call": "none",
"functions": [
{
"description": "<string>",
"name": "<string>",
"parameters": {}
}
]
}
}
Given a prompt ID, variable values, and optionally any hyperparameters, this API returns a JSON object containing the raw prompt template.
Note: Unlike inference requests, Prompt Render API calls are processed through Portkey’s Control Plane services.
Here’s how you can take the output from the render API
and use it for making a separate LLM call. We’ll take example of OpenAI SDKs, but you can use it simlarly for any other frameworks like Langchain etc. as well.
from portkey_ai import Portkey
from openai import OpenAI
# Retrieving the Prompt from Portkey
portkey = Portkey(
api_key="PORTKEY_API_KEY"
)
render_response = portkey.prompts.render(
prompt_id="PROMPT_ID",
variables={ "movie":"Dune 2" }
)
PROMPT_TEMPLATE = render_response.data
# Making a Call to OpenAI with the Retrieved Prompt
openai = OpenAI(
api_key = "OPENAI_API_KEY",
base_url = "https://api.portkey.ai/v1",
default_headers = {
'x-portkey-provider': 'openai',
'x-portkey-api-key': 'PORTKEY_API_KEY',
'Content-Type': 'application/json',
}
)
chat_complete = openai.chat.completions.create(**PROMPT_TEMPLATE)
print(chat_complete.choices[0].message.content)
Authorizations
Path Parameters
The unique identifier of the prompt template to render
Body
Note: Although hyperparameters are shown grouped here (like messages, max_tokens, temperature, etc.), they should only be passed at the root level, alongside 'variables' and 'stream'.
Variables to substitute in the prompt template
Note: All hyperparameters are optional. Pass them at the root level, and not nested under hyperparameters
. Their grouping here is for educational purposes only.
A list of messages comprising the conversation so far. Example Python code.
The contents of the system message.
The role of the messages author, in this case system
.
system
An optional name for the participant. Provides the model information to differentiate between participants of the same role.
ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.
"gpt-4-turbo"
Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
See more information about frequency and presence penalties.
-2 <= x <= 2
Modify the likelihood of specified tokens appearing in the completion.
Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content
of message
.
An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs
must be set to true
if this parameter is used.
0 <= x <= 20
The maximum number of tokens that can be generated in the chat completion.
The total length of input tokens and generated tokens is limited by the model's context length. Example Python code for counting tokens.
How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n
as 1
to minimize costs.
1 <= x <= 128
1
Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
See more information about frequency and presence penalties.
-2 <= x <= 2
An object specifying the format that the model must output.
Setting to { "type": "json_schema", "json_schema": {...} }
enables Structured Outputs which ensures the model will match your
supplied JSON schema. Works across all the providers that support this functionality. OpenAI & Azure OpenAI, Gemini & Vertex AI.
Setting to { "type": "json_object" }
enables the older JSON mode, which ensures the message the model generates is valid JSON.
Using json_schema
is preferred for models that support it.
The type of response format being defined. Always text
.
text
This feature is in Beta.
If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed
and parameters should return the same result.
Determinism is not guaranteed, and you should refer to the system_fingerprint
response parameter to monitor changes in the backend.
-9223372036854776000 <= x <= 9223372036854776000
Up to 4 sequences where the API will stop generating further tokens.
If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE]
message. Example Python code.
Options for streaming response. Only set this when you set stream: true
.
If set, an additional chunk will be streamed before the data: [DONE]
message. The usage
field on this chunk shows the token usage statistics for the entire request, and the choices
field will always be an empty array. All other chunks will also include a usage
field, but with a null value.
What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or top_p
but not both.
0 <= x <= 2
1
An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or temperature
but not both.
0 <= x <= 1
1
A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.
The type of the tool. Currently, only function
is supported.
function
The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
A description of what the function does, used by the model to choose when and how to call the function.
The parameters the functions accepts, described as a JSON Schema object. See the guide for examples, and the JSON Schema reference for documentation about the format.
Omitting parameters
defines a function with an empty parameter list.
Controls which (if any) tool is called by the model.
none
means the model will not call any tool and instead generates a message.
auto
means the model can pick between generating a message or calling one or more tools.
required
means the model must call one or more tools.
Specifying a particular tool via {"type": "function", "function": {"name": "my_function"}}
forces the model to call that tool.
none
is the default when no tools are present. auto
is the default if tools are present.
none
, auto
, required
Whether to enable parallel function calling during tool use.
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
"user-1234"
Deprecated in favor of tool_choice
.
Controls which (if any) function is called by the model.
none
means the model will not call a function and instead generates a message.
auto
means the model can pick between generating a message or calling a function.
Specifying a particular function via {"name": "my_function"}
forces the model to call that function.
none
is the default when no functions are present. auto
is the default if functions are present.
none
, auto
Deprecated in favor of tools
.
A list of functions the model may generate JSON inputs for.
The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
A description of what the function does, used by the model to choose when and how to call the function.
The parameters the functions accepts, described as a JSON Schema object. See the guide for examples, and the JSON Schema reference for documentation about the format.
Omitting parameters
defines a function with an empty parameter list.
Response
Indicates if the render was successful
A list of messages comprising the conversation so far. Example Python code.
The contents of the system message.
The role of the messages author, in this case system
.
system
An optional name for the participant. Provides the model information to differentiate between participants of the same role.
ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.
"gpt-4-turbo"
Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
See more information about frequency and presence penalties.
-2 <= x <= 2
Modify the likelihood of specified tokens appearing in the completion.
Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content
of message
.
An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs
must be set to true
if this parameter is used.
0 <= x <= 20
The maximum number of tokens that can be generated in the chat completion.
The total length of input tokens and generated tokens is limited by the model's context length. Example Python code for counting tokens.
How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n
as 1
to minimize costs.
1 <= x <= 128
1
Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
See more information about frequency and presence penalties.
-2 <= x <= 2
An object specifying the format that the model must output.
Setting to { "type": "json_schema", "json_schema": {...} }
enables Structured Outputs which ensures the model will match your
supplied JSON schema. Works across all the providers that support this functionality. OpenAI & Azure OpenAI, Gemini & Vertex AI.
Setting to { "type": "json_object" }
enables the older JSON mode, which ensures the message the model generates is valid JSON.
Using json_schema
is preferred for models that support it.
The type of response format being defined. Always text
.
text
This feature is in Beta.
If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed
and parameters should return the same result.
Determinism is not guaranteed, and you should refer to the system_fingerprint
response parameter to monitor changes in the backend.
-9223372036854776000 <= x <= 9223372036854776000
Up to 4 sequences where the API will stop generating further tokens.
If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE]
message. Example Python code.
Options for streaming response. Only set this when you set stream: true
.
If set, an additional chunk will be streamed before the data: [DONE]
message. The usage
field on this chunk shows the token usage statistics for the entire request, and the choices
field will always be an empty array. All other chunks will also include a usage
field, but with a null value.
What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or top_p
but not both.
0 <= x <= 2
1
An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or temperature
but not both.
0 <= x <= 1
1
A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.
The type of the tool. Currently, only function
is supported.
function
The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
A description of what the function does, used by the model to choose when and how to call the function.
The parameters the functions accepts, described as a JSON Schema object. See the guide for examples, and the JSON Schema reference for documentation about the format.
Omitting parameters
defines a function with an empty parameter list.
Controls which (if any) tool is called by the model.
none
means the model will not call any tool and instead generates a message.
auto
means the model can pick between generating a message or calling one or more tools.
required
means the model must call one or more tools.
Specifying a particular tool via {"type": "function", "function": {"name": "my_function"}}
forces the model to call that tool.
none
is the default when no tools are present. auto
is the default if tools are present.
none
, auto
, required
Whether to enable parallel function calling during tool use.
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
"user-1234"
Deprecated in favor of tool_choice
.
Controls which (if any) function is called by the model.
none
means the model will not call a function and instead generates a message.
auto
means the model can pick between generating a message or calling a function.
Specifying a particular function via {"name": "my_function"}
forces the model to call that function.
none
is the default when no functions are present. auto
is the default if functions are present.
none
, auto
Deprecated in favor of tools
.
A list of functions the model may generate JSON inputs for.
The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
A description of what the function does, used by the model to choose when and how to call the function.
The parameters the functions accepts, described as a JSON Schema object. See the guide for examples, and the JSON Schema reference for documentation about the format.
Omitting parameters
defines a function with an empty parameter list.
Was this page helpful?
curl -X POST "https://api.portkey.ai/v1/prompts/YOUR_PROMPT_ID/render" \
-H "Content-Type: application/json" \
-H "x-portkey-api-key: $PORTKEY_API_KEY" \
-d '{
"variables": {
"user_input": "Hello world"
},
"max_tokens": 250,
"presence_penalty": 0.2
}'
{
"success": true,
"data": {
"messages": [
{
"content": "<string>",
"role": "system",
"name": "<string>"
}
],
"model": "gpt-4-turbo",
"frequency_penalty": 0,
"logit_bias": null,
"logprobs": false,
"top_logprobs": 10,
"max_tokens": 123,
"n": 1,
"presence_penalty": 0,
"response_format": {
"type": "text"
},
"seed": 0,
"stop": "<string>",
"stream": false,
"stream_options": null,
"temperature": 1,
"top_p": 1,
"tools": [
{
"type": "function",
"function": {
"description": "<string>",
"name": "<string>",
"parameters": {}
}
}
],
"tool_choice": "none",
"parallel_tool_calls": true,
"user": "user-1234",
"function_call": "none",
"functions": [
{
"description": "<string>",
"name": "<string>",
"parameters": {}
}
]
}
}