Overview
The createPromptBuilder() factory function creates a new SystemPromptBuilder instance. This is the recommended entry point for building system prompts with PromptSmith.
The factory pattern allows for clean instantiation and potential future enhancements (like pre-configured builders or builder options) without breaking the API.
Signature
function createPromptBuilder(): SystemPromptBuilder
Returns
A new, empty SystemPromptBuilder instance ready for configuration via method chaining.
Usage
Basic Usage
import { createPromptBuilder } from "promptsmith-ts";
const builder = createPromptBuilder();
const prompt = builder
.withIdentity("You are a helpful assistant")
.withCapability("Answer questions")
.build();
Complete Example
import { createPromptBuilder } from "promptsmith-ts";
import { z } from "zod";
const builder = createPromptBuilder()
.withIdentity("You are an expert coding assistant")
.withCapabilities([
"Explain code concepts",
"Write code examples",
"Debug issues"
])
.withTool({
name: "search_docs",
description: "Search technical documentation",
schema: z.object({
query: z.string().describe("Search query")
})
})
.withConstraint("must", "Always provide working code examples")
.withTone("Be patient and encouraging")
.build();
console.log(builder);
With AI SDK Integration
import { createPromptBuilder } from "promptsmith-ts";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
const builder = createPromptBuilder()
.withIdentity("You are a helpful weather assistant")
.withTool({
name: "get_weather",
description: "Get current weather for a location",
schema: z.object({
location: z.string().describe("City name")
}),
execute: async ({ location }) => {
const response = await fetch(`https://api.weather.com/${location}`);
return response.json();
}
});
const response = await generateText({
model: openai("gpt-4"),
...builder.toAiSdk(),
prompt: "What's the weather in Paris?"
});
Design Philosophy
The createPromptBuilder() factory follows these principles:
- Fluent API Pattern: All builder methods return
this for method chaining
- Separation of Concerns: Prompt generation (text) is separate from tool execution (runtime logic)
- Type Safety: Full TypeScript type inference throughout the builder chain
- Flexibility: Supports both standalone usage and integration with AI frameworks