This script generates a personalized survey based on user responses using OpenAI's GPT models and the Qualtrics API. It can be used to make personalized questionairres for psychological assessment through prompt engineering.
This specific case was to assess preferences for certain variations of activities that we have prior information of preferences for through the user responses. It extracts activities from the user's input, creates pairs of related activities, formulates survey questions, and constructs a survey in Qualtrics. The survey link is then provided for distribution.
- Activity Extraction: Uses GPT models to extract activities from user responses.
- Activity Pair Generation: Creates pairs like stressful vs. relaxing and social vs. solitary versions of activities.
- Survey Question Creation: Formulates survey questions based on the generated activity pairs.
- Qualtrics Integration: Interacts with the Qualtrics API to create and activate surveys, add questions, and generate distribution links.
- Automated Workflow: Provides an end-to-end solution from data extraction to survey generation.
- Python: Version 3.7 or higher.
- OpenAI Account: With API access to GPT models (e.g.,
gpt-4o-miniorgpt-4o). - Qualtrics Account: With API access and permissions to create and manage surveys.
- Python Packages:
langchain_openairequestspython-dotenv
git clone https://github.com/yourusername/yourrepository.git
cd yourrepositorypython3 -m venv venv
source venv/bin/activate # For Windows: venv\Scripts\activatepip install -r requirements.txtIf a requirements.txt file is not available, install the packages manually:
pip install langchain_openai requests python-dotenvCreate a .env file in the project directory with the following content:
OPENAI_API_KEY=your_openai_api_key
QUALTRICS_API_TOKEN=your_qualtrics_api_tokenReplace your_openai_api_key and your_qualtrics_api_token with your actual API keys.
Run the script using:
python your_script_name.pyReplace your_script_name.py with the actual name of the Python script (qualsurv.py or personalized_survey.py).
The script includes an example in the __main__ block that demonstrates how to generate a survey using sample user responses.
Ensure the following environment variables are set:
OPENAI_API_KEY: Your OpenAI API key.QUALTRICS_API_TOKEN: Your Qualtrics API token.
These can be set in the .env file or directly in your environment.
- OpenAI Model Access: Confirm that your OpenAI account has access to the GPT model specified in the script.
- Qualtrics Data Center: The
base_urlin theQualtricsAPIclass is set tohttps://yul1.qualtrics.com/API/v3. Update this if your Qualtrics account is hosted in a different data center. - API Permissions: Your Qualtrics API token must have the necessary permissions to create surveys, add questions, and activate surveys via the API.
- Dependencies: All required Python packages must be installed. Use the
pip installcommands provided in the setup.
- Missing Environment Variables: If you encounter errors about missing environment variables, ensure that the
.envfile is properly configured and that the variable names are correct. - Invalid API Keys: Double-check that your OpenAI and Qualtrics API keys are valid and have not expired.
- API Errors: Review the console output for detailed error messages from API responses. Common issues include incorrect payload formats or insufficient permissions.
- Survey Activation Issues: If the survey fails to activate, verify that the
activate_surveymethod uses the correct endpoint (/surveys/{surveyId}) and payload ({"isActive": True}).
This project is licensed under the MIT License.
If you encounter any issues, refer to the troubleshooting section or consult the relevant API documentation for OpenAI and Qualtrics.