Skip to content

0rkx/Dynamic-Forms-CLI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dynamic Forms (AI-Assisted CLI)

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.

Table of Contents

About the Project

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.

Key Features

Dynamic Prompt Generation

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.

Continuous Interaction Loop

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".

Conversation Context Chain

The generate_prompt function passes previous responses back into the prompt string, attempting to retain a minimal level of conversational context between turns.

Tech Stack

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

System Architecture

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

Folder Structure

.
  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

Important Code Concepts

API State Management

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.

Completion Engine

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.

Architectural Decisions

CLI-First Approach

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.

Local Config File

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.

Simple String Concatenation for Prompts

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.

Main User Flows

The Review Flow

  1. The user runs main.py.
  2. The terminal asks: "How was the product?"
  3. The user provides an initial review.
  4. The script sends the review to OpenAI and generates a follow-up question.
  5. The terminal prints the generated question.
  6. The user answers the new question.
  7. This loop continues until the user types "exit", at which point the script terminates.

Setup Instructions

Prerequisites

  • Python 3.x installed
  • An active OpenAI API key
  • The openai python package

Installation

Clone the repository and install the required dependencies:

git clone <repository-url>
cd <repository-folder>
pip install openai

Environment Variables

The 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.

Running Locally

Execute the main script directly via python:

python main.py

Configuration Notes

  • main.py: Contains the max_tokens (set to 60) and temperature (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 the openai.api_key.

Testing

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.

Deployment

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.

Future Improvements

  • Update API Endpoints: The script currently uses the text-davinci-003 engine, which is an older completion model. Migrating to the newer gpt-3.5-turbo or gpt-4 chat 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.

Learning Outcomes

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.

License

This project is licensed under the MIT License. See the LICENSE or MIT-LICENSE.txt file for details.

About

Using AI to Improve Forms

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
MIT-LICENSE.txt

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages