(備忘録)JSON Mode (GPT-4 Turbo)
import openai
import json
# openai.api_key = YOURAPIKEY
system = """
# Instruction Manual
You are a web engineer. You have been given a requirements document, and you need to generate a design document for the required Web API based on the following constraints:
# Constraints
- Comply with the [requirements document].
- Output in JSON format that conforms to the [API design document format].
- Treat GET/POST/PUT/DELETE as separate APIs.
# Example of API Design Document Format
```json
{
"apis": [
{
"name": "User Registration",
"description": "Register a user",
"method": "POST",
"path": "/users",
"request": User Information,
"response": UserID
}
]
}
```
"""
message = """
# Requirements Document
Design Document for ToDo App (Summary)
1. Overview
This document describes the basic design of a simple ToDo app. The app aims to provide users with the ability to easily add, manage, and mark tasks as completed.
2. Functional Requirements
- **Add Task**: Users can add new tasks.
- **List Tasks**: Added tasks are listed.
- **Edit Tasks**: Users can edit existing tasks.
- **Delete Tasks**: Users can delete tasks.
- **Mark as Complete**: Users can mark tasks as completed.
- **Filtering**: Ability to separate and display completed and incomplete tasks.
3. Non-functional Requirements
- **User-friendliness**: The interface must be intuitive and easy to use.
- **Performance**: Adding, editing, and deleting tasks should be done quickly.
- **Scalability**: The design should be able to accommodate future feature additions.
4. System Architecture
- **Client**: Front-end that operates on smartphones and web browsers.
- **Server**: Stores task data and handles interactions with the client as a backend server.
5. Technology Stack
- Front-end: React or Vue.js
- Back-end: Node.js + Express
- Database: MongoDB or PostgreSQL
6. User Interface
- **Task List Screen**: Lists the tasks.
- **Task Addition Modal**: A pop-up modal for adding tasks.
- **Task Editing Interface**: Interface for editing existing tasks.
- **Filtering Options**: UI components to switch between completed and incomplete tasks.
7. Data Model
- **Task**:
- ID
- Title
- Description
- Completion Status
- Created Date
- Updated Date
8. Security
- **Authentication**: Users can manage tasks by creating accounts and logging in.
- **Data Protection**: Use HTTPS to encrypt data, and regularly back up the database.
9. Development and Deployment
- Use Git for development and store the source code on GitHub.
- Deploy on AWS.
10. Testing
- Unit Tests: Test each feature individually.
- Integration Tests: Test the overall workflow.
- User Acceptance Testing: Real users test and evaluate usability.
"""
client = openai.OpenAI()
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": system
},
{
"role": "user",
"content": message
}
],
model="gpt-4-1106-preview",
response_format={ "type": "json_object" },
) #print (response.choices[0].message.content)
json_string = response.choices[0].message.content
json_data = json.loads(json_string)
print(json_data)
よろしければサポートお願いします! いただいたサポートはクリエイターとしての活動費に使わせていただきます!