Twitter bot python webapp using OpenAI, tracery
Poetry can be used to manage dependencies locally. Poetry dependencies must be exported (synchronized) to requirements.txt if deploying to Azure app service for python applications,
as by default at least, it installs dependencies from requirements.txt when building the container.
To update requirements.txt, run
poetry export --without-hashes --without dev -f requirements.txt -o requirements.txt- Copy
.env.exampleto.env - Gain access to the Twitter API v1 to be able to write tweets, and enter API tokens in to
.env - Gain access to Open AI API and set token in
.envOPENAI_API_KEY. - Configure Open AI environmetn variables. SEe Open AI - GPT based bot.
- (Optional) Create tracery configuration for tracery-based bot. See Tracery-based bot.
This app uses the Chat Completion API to prompt Chat GPT with a user prompt and to receive the resposne.
The .env includes:
OPEN_AI_SYSTEM_PROMPT: the system prompt for initializing the APIOPEN_AI_USER_PROMPT_ARRAY: an array possible user prompts to engage the conversation. This is in the form of a python array as a string, and a random choise is selected when runniing the program. If only 1 prompt is desired, an array of a single item is permitted.OPEN_AI_TEMPERATURE- A value bewteen 0 and 2. THe higher the value, the more diverse and creative the resuls are.
This app also supports using Tracery to generate message and generate tweets as well. Tracery is a language for generating text based on rules and expansions. Tracery grammars are written in a format called JSON (or “javascript object notation”). JSON is a common format for exchanging data between computer programs written in different programming languages and on different kinds of computers.
Create a file named tracery.json to use the tracery bot function. See tracery.example.json as a template. It is similar to MadLibs.
You can deploy the servcie and run it periodically on Google Cloud, or other cloud providers. The author has found that Google Cloud is the less expensive (essentially free), and reliable at the lowest tier.
- Create a Cloud Function (serverless), and deploy the code there.
- Create a Cloud Scheduler to hit the URL of your Cloud Function at some periodicity. Authentication should be enabled, and restrict authentication within your Google Cloud project's servcie account.