Note: This example demonstrates the pattern for integrating Prompt Fusion with agent-style frameworks using an instructions parameter. The @openai/agents package shown is conceptual - adapt this pattern to your actual OpenAI implementation (Assistants API, function calling, etc.).
Shows how to:
- Fuse prompts into a single
instructionsstring for agent initialization - Create an
AdaptiveAgentclass that switches personas dynamically - Use template-based configuration for different agent roles
The key difference from LangChain: fusion happens once at initialization or when switching personas, not per-message.
- Node.js 18+
- OpenAI API key
- Your chosen OpenAI implementation (Assistants API, custom agent framework, etc.)
For OpenAI Assistants API (real implementation):
npm install openaiFor the conceptual pattern shown in the example:
# This package doesn't exist - adapt the pattern to your framework
npm install openaiCreate a .env file:
OPENAI_API_KEY=your_openai_api_key_hereSince @openai/agents is conceptual, here's how to adapt this to the real Assistants API:
import OpenAI from 'openai';
import PromptFusionEngine from '../../core/promptFusionEngine.js';
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const fusionEngine = new PromptFusionEngine();
// Fuse your prompts
const fusedInstructions = fusionEngine.semanticWeightedFusion(
baseInstructions,
brainInstructions,
personaInstructions['analyst'],
{ base: 0.2, brain: 0.3, persona: 0.5 }
);
// Create Assistant with fused instructions
const assistant = await openai.beta.assistants.create({
name: "Customer Analytics Agent",
instructions: fusedInstructions,
model: "gpt-4o",
tools: [{ type: "code_interpreter" }]
});
// Create thread and run
const thread = await openai.beta.threads.create();
await openai.beta.threads.messages.create(thread.id, {
role: "user",
content: "Analyze customer retention trends"
});
const run = await openai.beta.threads.runs.create(thread.id, {
assistant_id: assistant.id
});function createFusionAgent(personaType) {
const fusedInstructions = createFusedInstructions(
baseInstructions,
brainInstructions,
personaContent,
personaType !== null
);
return new Agent({
instructions: fusedInstructions, // Fused once here
// ... other config
});
}The AdaptiveAgent class recreates the agent when persona changes:
class AdaptiveAgent {
switchPersona(personaType) {
this.currentAgent = this.createAgent(personaType); // Re-fuse & recreate
}
}const weights = personaActive
? { base: 0.2, brain: 0.3, persona: 0.5 } // Persona dominates
: { base: 0.4, brain: 0.6, persona: 0.0 }; // Brain dominatesUse initialization-time fusion when:
- Your framework uses a static
instructionsparameter - Persona changes are infrequent
- You want to minimize per-message overhead
Use per-message fusion (like LangChain example) when:
- Persona can change between messages
- You need dynamic context injection
- Framework supports message-level hooks
const config: AgentConfig = {
role: "Data Scientist",
domain: "Healthcare Analytics",
constraints: [
"HIPAA compliant data handling",
"Anonymize all patient data"
],
guidelines: [
"Use evidence-based approaches",
"Cite medical literature"
]
};
const agent = createConfiguredAgent(config);const personaInstructions = {
analyst: `...`,
researcher: `...`,
engineer: `ROLE: Software Engineer
SPECIALIZATION:
- Code architecture and design patterns
- Performance optimization
- Technical documentation`
};| Aspect | LangChain (messageModifier) | OpenAI SDK (instructions) |
|---|---|---|
| Fusion timing | Per-message | At initialization |
| Overhead | Minimal per-call | One-time |
| Persona switching | Instant | Requires re-init |
| Use case | Dynamic multi-turn | Static or infrequent changes |
For production use with OpenAI:
- Assistants API: Fuse prompts → create assistant with
instructions - Function Calling: Fuse prompts → pass as
systemmessage - Custom Framework: Adapt the fusion pattern to your architecture