Typescript library used to generate the stories for StoryBot
npm install --save story-gpt
import { createStory } from "story-gpt";
import { OpenAI } from "openai";
const story = await createStory("A story about a happy horse", new OpenAI({apiKey: ">my api key<"}));
console.log("The story is named %s and it's tells the following story:", story.title, story.content);
console.log("See the cover picture for the story here:", story.image);
createStory(prompt, openai)Main utility function that creates a complete story with title and image.
Parameters:
prompt (string): The prompt to generate the story fromopenai (OpenAI): The authenticated OpenAI clientReturns: Promise<StoryPayload> containing:
prompt: The original prompttitle: Generated story titlecontent: The story contenttemperature: Temperature value used for generationimage: URL to the generated image (Note: this link expires, so download it)Example:
import { createStory } from "story-gpt";
import { OpenAI } from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const story = await createStory("A tale of a brave knight", openai);
console.log(story.title); // e.g., "The Knight's Quest"
Story ClassClass for generating and managing stories.
Story.generateStory(prompt, openai, chatModel?, logger?)Static method to generate a story from a prompt.
Parameters:
prompt (string): The prompt to generate the story fromopenai (OpenAI): The authenticated OpenAI clientchatModel (string, optional): The model to use. Defaults to gpt-5-mini. Note: gpt-5-mini only supports temperature=1logger (ILogger, optional): Logger instance for debugging. Defaults to consoleReturns: Promise<Story>
Example:
import { Story } from "story-gpt";
import { OpenAI } from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const story = await Story.generateStory("A mystery in the old mansion", openai);
console.log(story.content); // The generated story text
story.generateTitle()Generates a title for the story.
Returns: Promise<string> - The generated title
story.generateImage(size?, model?)Generates an image for the story using DALL-E.
Parameters:
size (ImageSize, optional): Image size. Defaults to "1024x1024". Options: '256x256' | '512x512' | '1024x1024' | '1792x1024' | '1024x1792'model (Model, optional): DALL-E model. Defaults to "dall-e-3". Options: 'dall-e-2' | 'dall-e-3'Returns: Promise<string> - URL to the generated image
verifyPrompt(prompt, openai, chatModel?)Utility function that verifies if a prompt contains potentially harmful content and if it qualifies as a story.
Parameters:
prompt (string): The prompt to analyzeopenai (OpenAI): The authenticated OpenAI clientchatModel (string, optional): The model to use. Defaults to gpt-5-miniReturns: Promise<StoryResult> where StoryResult is:
{ validStory: true } if the prompt is valid{ validStory: false, reasonForRejection: string } if invalidExample:
import { verifyPrompt } from "story-gpt";
import { OpenAI } from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const result = await verifyPrompt("Write a story about a dragon", openai);
if (result.validStory) {
    console.log("Prompt is valid!");
} else {
    console.log("Rejected:", result.reasonForRejection);
}
ImageGenerator ClassClass for generating images using OpenAI's DALL-E.
new ImageGenerator(openai, logger)Parameters:
openai (OpenAI): The authenticated OpenAI clientlogger (ILogger): Logger instance for debuggingimageGenerator.generateImage(prompt, size?, model?)Generates a single image from a text prompt.
Parameters:
prompt (string): The text prompt describing the imagesize (ImageSize, optional): Image size. Defaults to "512x512"model (Model, optional): DALL-E model. Defaults to "dall-e-3"Returns: Promise<string> - URL to the generated image
Example:
import { ImageGenerator } from "story-gpt";
import { OpenAI } from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const imageGen = new ImageGenerator(openai, console);
const imageUrl = await imageGen.generateImage("A sunset over mountains", "1024x1024", "dall-e-3");
imageGenerator.generateImages(prompt, numberOfImages, size?, model?)Generates multiple images from a text prompt.
Parameters:
prompt (string): The text prompt describing the imagesnumberOfImages (number): Number of images to generate (1-5)size (ImageSize, optional): Image size. Defaults to "512x512"model (Model, optional): DALL-E model. Defaults to "dall-e-3"Returns: Promise<string[]> - Array of URLs to the generated images
ChatAssistant ClassAssistant class for having conversations while keeping the history.
new ChatAssistant(openai, temperature, chatModel?)Parameters:
openai (OpenAI): The authenticated OpenAI clienttemperature (number): Temperature for response generation (0-2). Note: gpt-5-mini only supports temperature=1chatModel (string, optional): The model to use. Defaults to gpt-5-minichatAssistant.chat(...messages)Starts a conversation and returns an answer with the ability to continue the conversation.
Parameters:
messages (ChatCompletionMessageParam[]): Thread messages, usually the system message and a first messageReturns: Promise<Conversation> with:
answer: The response from the assistantchat(message): Function to continue the conversationExample:
import { ChatAssistant } from "story-gpt";
import { OpenAI } from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const assistant = new ChatAssistant(openai, 1, "gpt-5-mini");
const conv = await assistant.chat({role: "user", content: "Where is Argentina?"});
console.log("Answer is", conv.answer.content);
const followUp = await conv.chat({role: "user", content: "And how big is it?"});
console.log("Argentina size is:", followUp.answer.content);
gpt-5-mini model only supports the default temperature value (1). When using this model, the library automatically omits the temperature parameter to avoid API errors.The repository includes comprehensive end-to-end tests that verify all endpoints of the library. These tests run automatically on every push and pull request.
To run the E2E tests locally:
# Build the project
npm run build
# Set your OpenAI API key
export OPENAI_TOKEN=your_openai_api_key_here
# Run the E2E tests
node e2e-test.mjs
The E2E tests cover:
createStory()Story.generateStory()verifyPrompt()ImageGenerator.generateImage()