Dynamic Forms is a Python-based command-line interface (CLI) application built to explore how large language models can dynamically drive data collection. The current implementation focuses on asking users about a product and using the OpenAI API to generate relevant follow-up questions based on their answers, essentially creating an adaptive form that changes in real-time.
- About the Project
- Key Features
- Tech Stack
- System Architecture
- Folder Structure
- Important Code Concepts
- Architectural Decisions
- Main User Flows
- Setup Instructions
- Configuration Notes
- Future Improvements
- Learning Outcomes
- License
This project addresses the limitations of static forms by replacing rigid, predefined questions with a dynamic conversational flow. Instead of a standard questionnaire where every user sees the same fields, this script uses OpenAI's GPT models to analyze the user's initial product review and generate contextually appropriate follow-up questions.
The current stage is a minimal terminal prototype meant to validate the interaction loop. It currently focuses solely on user input and API integration, running entirely in the console without a frontend or database.
The app takes a user's initial input ("How was the product?") and feeds it into the OpenAI API to return the next logical question, adapting the conversation in real-time.
The script maintains an infinite loop, allowing the conversation to continue indefinitely as long as the user keeps answering. The loop breaks cleanly when the user types "exit".
The generate_prompt function passes previous responses back into the prompt string, attempting to retain a minimal level of conversational context between turns.
| Layer | Technology | Purpose |
|---|---|---|
| Language | Python 3 | Core scripting and execution |
| AI Integration | OpenAI Python SDK | Communicating with the OpenAI API |
| Configuration | Local config.py |
Storing API credentials securely |
The architecture is extremely straightforward, designed specifically to test the core logic of dynamic question generation.
Terminal / User
↓ (Input)
Python CLI Loop (`main.py`)
↓ (Prompt generation)
OpenAI API (`text-davinci-003`)
↓ (Next question)
Python CLI Loop
↓ (Print to terminal)
Terminal / User.
main.py Main entry point containing the core interaction loop
config.py (Expected locally) Contains the OpenAI API key
README.md Project documentation
LICENSE / MIT-LICENSE.txt
The script maintains a basic form of state by passing the prev_response variable back into the generate_prompt function. This ensures the OpenAI API has some context of what was just asked, rather than treating every request as a blank slate.
The project currently uses the text-davinci-003 engine via openai.Completion.create. It configures specific parameters such as max_tokens=60 and temperature=0.5 to keep the generated questions concise and reasonably focused.
The project is built as a command-line tool. This approach makes sense for the current prototype stage because it isolates the core problem—testing the AI prompt generation logic—without the overhead of building a web frontend or setting up React components.
The codebase expects a config.py file to hold the api_key. While environment variables (.env) are more standard for production applications, using a local, unversioned python file is a quick way to inject secrets for local scripting.
Instead of using complex prompt templates or a framework like LangChain, the script concatenates strings to build the prompt (f"{prev_response.strip()} User response: {user_response} \n"). This keeps the dependencies minimal, though it limits how complex the context window can grow.
- The user runs
main.py. - The terminal asks: "How was the product?"
- The user provides an initial review.
- The script sends the review to OpenAI and generates a follow-up question.
- The terminal prints the generated question.
- The user answers the new question.
- This loop continues until the user types "exit", at which point the script terminates.
- Python 3.x installed
- An active OpenAI API key
- The
openaipython package
Clone the repository and install the required dependencies:
git clone <repository-url>
cd <repository-folder>
pip install openaiThe script does not use traditional environment variables. Instead, it expects a config.py file in the root directory.
You must create this file manually:
# config.py
api_key = "your-openai-api-key-here"Note: Ensure config.py is added to your .gitignore so you do not accidentally commit your API key.
Execute the main script directly via python:
python main.pymain.py: Contains themax_tokens(set to 60) andtemperature(set to 0.5) parameters. Adjust these if you want longer or more varied follow-up questions.config.py: Required to run the script, as it provides theopenai.api_key.
Automated tests are not currently included in this repository.
Future testing efforts should focus on:
- Mocking the OpenAI API to test the continuous loop without incurring API costs.
- Verifying the loop break condition when "exit" is inputted.
- Testing context string formatting when multiple conversational turns occur.
No deployment-specific configuration was found. Since this is a standalone Python CLI script, it is intended to be run locally. To deploy it as a service, the logic would need to be wrapped in a web framework (like Flask or FastAPI) and hosted on a platform like Render or Heroku.
- Update API Endpoints: The script currently uses the
text-davinci-003engine, which is an older completion model. Migrating to the newergpt-3.5-turboorgpt-4chat completions API would improve response quality and context handling. - Persistent Storage: Right now, the collected answers disappear when the script stops. Saving the Q&A pairs to a local JSON file, SQLite database, or Supabase backend would turn this into a true data collection tool.
- Web Interface: Wrapping the logic in an Express backend or a FastAPI service, paired with a React frontend, would make the dynamic form usable by non-technical users.
- Better Context Management: The current prompt concatenation only remembers the immediate previous response. Implementing a rolling memory buffer would allow the model to refer back to earlier answers.
This project demonstrates how to quickly prototype AI-driven logic before committing to a full stack architecture. It shows a practical implementation of the OpenAI SDK, handling interactive continuous loops in Python, and understanding how to pass conversational context back into a stateless API. It highlights a pragmatic decision to test the core value (dynamic questions) locally in the terminal rather than getting bogged down in UI development.
This project is licensed under the MIT License. See the LICENSE or MIT-LICENSE.txt file for details.