1. Creating a Prompt Template
Portkey’s prompt playground enables you to experiment with various LLM providers. It acts as a definitive source of truth for your team, and it versions each snapshot of model parameters, allowing for easy rollback. We want to create a chat completion prompt withgpt4
that tells a story about any user-desired topic.
To do this:
- Go to www.portkey.ai
- Opens a Dashboard
- Click on Prompts and then the Create button.
- You are now on Prompt Playground.
System | You are a very good storyteller who covers various topics for the kids. You narrate them in very intriguing and interesting ways. You tell the story in less than 3 paragraphs. |
---|---|
User | Tell me a story about |
Max Tokens | 512 |
Temperature | 0.9 |
Frequency Penalty | -0.2 |
{{topic}}
. Portkey treats them as dynamic variables, so a string can be passed to this prompt at runtime. This prompt is much more useful since it generates stories on any topic.
Once you are happy with the Prompt Template, hit Save Prompt. The Prompts page displays saved prompt templates and their corresponding prompt ID, serving as a reference point in our code.
Next up, let’s see how to use the created prompt template to generate chat completions through OpenAI SDK.
2. Retrieving the prompt template
Fire up your code editor and import the request client,axios
. This will allow you to POST to the Portkey’s render endpoint and retrieve prompt details that can be used with OpenAI SDK.
We will use axios
to make a POST
call to /prompts/${PROMPT_ID}/render
endpoint along with headers (includes Portkey API Key) and body that includes the prompt variables required in the prompt template.
For more information about Render API, refer to the docs.
3. Sending requests through OpenAI SDK
This section will teach you to use the prompt details JS object we retrieved earlier and pass it as an argument to the instance of the OpenAI SDK when making the chat completions call. Let’s import the necessary libraries and create a client instance from the OpenAI SDK.portkey-ai
to use its utilities to change the base URL and the default headers. If you are wondering what virtual keys are, refer to Portkey Vault documentation.
The prompt details we retrieved are passed as an argument to the chat completions creation method.
Bonus: Using Portkey SDK
The official Portkey Client SDK has a prompts completions method that is similar to chat completions’ OpenAI signature. You can invoke a prompt template just by passing arguments topromptID
and variables
parameters.
Conclusion
We’ve now finished writing a some NodeJS program that retrieves the prompt details from the Prompt Playground using prompt ID. Then successfully made a chat completion call using OpenAI SDK to generate a story with the desired topic. We can use this approach to focus on improving prompt quality with all the LLMs supported, simply reference them at the code runtime.Show me the entire code
Show me the entire code