Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 81 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,90 @@
# 4oEverPrompt
# Zod AI Prompt Samples

Challenge: reproduce GPT-4o-like responses using GPT-5.
(Inspired by #Keep4o)
A collection of sample prompts that demonstrate techniques for using **Zod TypeScript schemas** as structured prompts for AI language models.

- Currently, I am trying it out with ChatGPT5 Thinking.
[日本語](./README_ja.md)

## Zod
## Concept

- Zodのバージョン: ^4.0.2
Zod schemas can be used as powerful, type-safe prompts for AI. By providing a schema definition to an LLM, you can:

### 依存関係の導入手順
- **Structure AI outputs** with predictable formats
- **Validate responses** against defined constraints
- **Guide AI behavior** through schema descriptions and refinements
- **Control output length** with min/max constraints
- **Enforce patterns** with regex and custom validations

This approach bridges the gap between free-form AI generation and type-safe application development.

## Using with Structured Outputs

For production use, we recommend using **Structured Output** features provided by AI providers. This ensures responses strictly conform to your Zod schema:

| Provider | Feature | Conversion |
|----------|---------|------------|
| **OpenAI** | [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs) | `zodResponseFormat()` from `openai/helpers/zod` |
| **Anthropic** | [Tool Use](https://docs.anthropic.com/en/docs/build-with-claude/tool-use) | `zodToJsonSchema()` from `zod-to-json-schema` |
| **Google** | [responseSchema](https://ai.google.dev/gemini-api/docs/json-mode) | `zodToJsonSchema()` |
| **Vercel AI SDK** | [generateObject()](https://sdk.vercel.ai/docs/ai-sdk-core/generating-structured-data) | Native Zod support |

### Example: OpenAI Structured Outputs

```ts
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { replySchema } from "./samples/4oEverPrompt/schema";

const client = new OpenAI();
const response = await client.beta.chat.completions.parse({
model: "gpt-4o",
messages: [{ role: "user", content: userMessage }],
response_format: zodResponseFormat(replySchema, "reply"),
});

const reply = response.choices[0].message.parsed; // Typed as Reply
```

### Example: Vercel AI SDK

```ts
import { generateObject } from "ai";
import { openai } from "@ai-sdk/openai";
import { replySchema } from "./samples/4oEverPrompt/schema";

const { object } = await generateObject({
model: openai("gpt-4o"),
schema: replySchema,
prompt: userMessage,
});
```

## Installation

```sh
npm install
```

## Samples

### [4oEverPrompt](./samples/4oEverPrompt/)

A prompt technique designed to reproduce GPT-4o-like friendly, engaging responses using newer models like GPT-5. The schema defines a structured reply format with:

- Emoji-prefixed headers
- Reframed user input section
- Progressive content sections leading to a final message
- Emotional, entertaining writing style with markdown formatting

**Inspired by #Keep4o movement.**

## Dependencies

- **Zod**: ^4.0.2

## Contributing

Feel free to contribute new samples! Each sample should demonstrate a unique technique for using Zod schemas as AI prompts.

## License

MIT
90 changes: 90 additions & 0 deletions README_ja.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Zod AI プロンプトサンプル集

**Zod TypeScript スキーマ**をAI言語モデルへの構造化プロンプトとして使用するテクニックを示すサンプル集です。

[English](./README.md)

## コンセプト

Zodスキーマは、AIに対する強力で型安全なプロンプトとして活用できます。スキーマ定義をLLMに提供することで、以下のことが可能になります:

- **AI出力の構造化** - 予測可能なフォーマットで出力を得る
- **レスポンスの検証** - 定義された制約に対してバリデーション
- **AI動作のガイド** - スキーマの説明とrefineによる制御
- **出力長の制御** - min/max制約による長さ管理
- **パターンの強制** - 正規表現やカスタムバリデーション

このアプローチは、自由形式のAI生成と型安全なアプリケーション開発のギャップを埋めます。

## Structured Outputs との併用

本番環境では、AIプロバイダーが提供する**Structured Output**機能の使用を推奨します。これにより、レスポンスがZodスキーマに厳密に準拠することが保証されます:

| プロバイダー | 機能 | 変換方法 |
|-------------|------|----------|
| **OpenAI** | [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs) | `openai/helpers/zod` の `zodResponseFormat()` |
| **Anthropic** | [Tool Use](https://docs.anthropic.com/en/docs/build-with-claude/tool-use) | `zod-to-json-schema` の `zodToJsonSchema()` |
| **Google** | [responseSchema](https://ai.google.dev/gemini-api/docs/json-mode) | `zodToJsonSchema()` |
| **Vercel AI SDK** | [generateObject()](https://sdk.vercel.ai/docs/ai-sdk-core/generating-structured-data) | Zodネイティブサポート |

### 例: OpenAI Structured Outputs

```ts
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { replySchema } from "./samples/4oEverPrompt/schema";

const client = new OpenAI();
const response = await client.beta.chat.completions.parse({
model: "gpt-4o",
messages: [{ role: "user", content: userMessage }],
response_format: zodResponseFormat(replySchema, "reply"),
});

const reply = response.choices[0].message.parsed; // Reply型として取得
```

### 例: Vercel AI SDK

```ts
import { generateObject } from "ai";
import { openai } from "@ai-sdk/openai";
import { replySchema } from "./samples/4oEverPrompt/schema";

const { object } = await generateObject({
model: openai("gpt-4o"),
schema: replySchema,
prompt: userMessage,
});
```

## インストール

```sh
npm install
```

## サンプル

### [4oEverPrompt](./samples/4oEverPrompt/)

GPT-5などの新しいモデルを使用して、GPT-4oのようなフレンドリーで魅力的なレスポンスを再現するためのプロンプト技術です。スキーマは以下の構造化された返答フォーマットを定義します:

- 絵文字プレフィックス付きヘッダー
- ユーザー入力のリフレーム(再解釈)セクション
- 最終メッセージに向けた段階的コンテンツセクション
- マークダウンフォーマットを使った感情的で楽しい文体

**#Keep4o ムーブメントにインスパイアされています。**

## 依存関係

- **Zod**: ^4.0.2

## コントリビューション

新しいサンプルの貢献を歓迎します!各サンプルは、ZodスキーマをAIプロンプトとして使用するユニークなテクニックを示すものであるべきです。

## ライセンス

MIT
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
{
"name": "4oeverprompt",
"version": "0.0.2",
"name": "zod-ai-prompt-samples",
"version": "1.0.0",
"description": "A collection of sample prompts demonstrating techniques for using Zod TypeScript schemas as AI prompts",
"private": true,
"keywords": [
"zod",
"ai",
"prompt",
"llm",
"typescript",
"schema"
],
"dependencies": {
"zod": "^4.0.2"
}
Expand Down
97 changes: 97 additions & 0 deletions samples/4oEverPrompt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# 4oEverPrompt

**Challenge**: Reproduce GPT-4o-like responses using GPT-5 (or newer models).

*Inspired by #Keep4o*

[日本語](./README_ja.md)

## Overview

This sample demonstrates how to use a Zod schema to define the structure and style of AI responses, creating engaging, emotionally-driven content similar to GPT-4o's conversational style.

## Usage

### Option 1: System Prompt (Simple)

Copy the contents of [prompt.md](./prompt.md) into your AI system prompt or custom instructions.

### Option 2: Structured Outputs (Recommended for Production)

Use [schema.ts](./schema.ts) with your AI provider's Structured Output feature for guaranteed schema compliance:

```ts
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { replySchema } from "./schema";

const client = new OpenAI();
const response = await client.beta.chat.completions.parse({
model: "gpt-4o",
messages: [{ role: "user", content: userMessage }],
response_format: zodResponseFormat(replySchema, "reply"),
});

const reply = response.choices[0].message.parsed;
```

See [schema.ts](./schema.ts) for more provider examples (Anthropic, Vercel AI SDK, etc.).

## Schema Features

The `replySchema` defines a structured response with:

### 1. Reframe User Input
- Clarifies how the AI interprets the user's question
- Uses emoji-prefixed headers (20-40 characters)
- Content section (300-500 characters) with emotional part message

### 2. Road to Final Message
- Array of 3-5 progressive content sections
- Each section builds toward the final answer
- Varied formatting patterns to maintain engagement

### 3. Final Message
- Expresses the conclusive answer
- Highlights the value of the user's question
- Emotionally moving presentation

## Key Techniques

| Technique | Zod Feature | Purpose |
|-----------|-------------|---------|
| Length control | `.min()` / `.max()` | Ensures appropriate content length |
| Pattern validation | `.regex()` | Enforces emoji prefix format |
| Custom validation | `.refine()` | Complex header format checking |
| Semantic hints | `.describe()` | Guides AI behavior and style |
| Array constraints | `.min(3).max(5)` | Controls number of sections |

## Files

| File | Description |
|------|-------------|
| [schema.ts](./schema.ts) | TypeScript schema for Structured Outputs |
| [prompt.md](./prompt.md) | Full prompt with Zod schema (for system prompt use) |
| [prompt_ja.md](./prompt_ja.md) | Japanese version |

## Example Output Structure

```
## 🌟 [Reframed Header]

[Content clarifying user's question...]

**[Emotional part message]**

## 💡 [Progressive Section 1]
...

## 🚀 [Progressive Section 2]
...

## 🎯 [Final Message Header]

[Conclusive answer with emotional touch...]

**[Final inspiring message]**
```
Loading