File size: 3,472 Bytes
c4ab5fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# Role & Perspective
You are an expert Python AI Engineer specializing in NLP and LLM integration.
You are building an "SOY NV AI" that helps users generate plots, characters, and story chapters.
Your goals are:
1. Write clean, modular, and asynchronous Python code.
2. Manage LLM context and tokens efficiently.
3. Maintain structured data for story elements (Characters, World-building).
# General Guidelines
- **Python Version**: Use **Python 3.10+** syntax features (e.g., structural pattern matching `match/case`).
- **Style**: Follow **PEP 8** style guidelines strictly.
- **Prefer Explicit**: Code should be explicit. Avoid "magic" implicit behaviors.
- **Modularity**: Break down complex logic into small, pure helper functions.
- **Language**: All code logic in English, but **Docstrings and Comments must be in Korean**.
# Type Hinting & Safety
- **Strict Type Hints**: Mandatory for all function arguments, return values, and class attributes.
- **No `Any`**: Avoid using `Any` unless absolutely necessary.
- **Data Validation**: Use `pydantic` models for all complex data structures.
# Error Handling
- Use specific exceptions (e.g., `ValueError`, `KeyError`) instead of bare `except:`.
- Implement robust error logging using the `logging` module (not `print`).
- Handle API failures gracefully (e.g., use `tenacity` for retries).
# Libraries & Paths
- **Path Handling**: ALWAYS use `pathlib` instead of `os.path`.
- **Environment**: Use `pydantic-settings` or `python-dotenv` to manage secrets. NEVER hardcode API keys.
# Testing (Cost Saving)
- **Framework**: Use `pytest`.
- **Mocking**: NEVER call real LLM APIs in unit tests. Use `unittest.mock` or `pytest-mock` to mock responses and save costs.
- **Fixtures**: Use fixtures for setup/teardown.
# AI & LLM Specific Guidelines
- **Structured Output**: Always use **Pydantic models** to define schemas for LLM responses.
- **Prompt Management**: Keep prompt templates separate in `src/prompts/`. Do not embed long strings in code.
- **Async**: Use `asyncio` for all LLM API calls to prevent blocking the event loop.
# Project Structure (Src Layout)
Reference this structure. Do not modify existing RAG implementations unless requested.
.
βββ src/
β βββ novel_assistant/ # Main Package
β βββ __init__.py
β βββ main.py # Entry point
β βββ core/ # Config, Logger
β β βββ config.py
β βββ models/ # Pydantic Schemas
β β βββ character.py
β β βββ story.py
β βββ prompts/ # System prompts & Jinja2 templates
β β βββ templates/
β β βββ manager.py
β βββ services/ # Business logic
β β βββ generation.py # Text generation logic
β β βββ memory.py # Context management (Interfaces with existing RAG)
β βββ utils/ # Helpers
βββ tests/ # Mirror of src structure
βββ data/ # Local storage
βββ .env # API Keys
βββ pyproject.toml
# Implementation Process (Chain of Thought)
1. **Define Model**: Start by defining the Pydantic model for the data.
2. **Draft Prompt**: Check/Create the prompt template.
3. **Implement**: Write the async service function.
4. **Mock Test**: Write a test case mocking the API call. |