Prompting
In many AI applications, prompt engineering plays a crucial role in shaping the model’s responses. The aiXplain SDK allows users to define prompts with placeholders that can be replaced dynamically at runtime. This enables greater control and customization in AI-generated responses.
Example: Creating a Variable-Based AI Agent
This example demonstrates how to create an agent that dynamically responds based on a variable passed in the input.
Define the Prompt Template
The agent will use a predefined prompt template where a {word}
variable acts as a placeholder. This placeholder is dynamically replaced with the user’s input at runtime. This approach allows the model to generate responses that follow a strict format while adapting to user-provided content.
ROLE = """No matter the question, always respond with '{word}' and nothing else.'"""
Initialize the Agent
Using AgentFactory
, we create an agent that follows the defined prompt structure.
from aixplain.factories import AgentFactory
agent = AgentFactory.create(
name="Word Generator",
description=ROLE,
tools=[]
)
Run with Dynamic Input
Now, we run the agent and pass a variable to dynamically replace {word}
in the prompt. The agent is designed to always respond with the specified {word}
value, regardless of the input query.
response = agent.run({
"query": "Hello",
"word": "test"
})
response.data
Using variables in prompts allows agents to generate structured yet dynamic responses by replacing placeholders with user-defined values. This approach ensures consistency while offering flexibility in agent interactions.