TextGPT bridges the gap between telephony and Conversational AI by integrating the Twilio telephony system with OpenAI's GPT model. It enables SMS messages to be processed and responded to by GPT ChatCompletion, providing an AI-driven conversational experience all built on a scaleable and easily deployable platform.
Using prompt engineering, TextGPT now provides the option for the AI personality to create a contextual response as well as occasionally create an image prompt relating to the subject matter. This image prompt is then sent to Dall-E 2 for generation. This image, along with the GPT response, is sent to the user as a MMS message.
- Send and Receive SMS and MMS messages via Twilio API
- Process and respond to messages using OpenAI's API featuring a wealth of models (including GPT 3.5 turbo and GPT 4).
- Process and respond to GPT personality generated image prompts using Dall-E 2 to generate images. There is currently a 3 image cooldown before another is sent, if many are sent in a row by the AI.
- Image storing using AWS S3.
- Scalable and serverless architecture using AWS Lambda functions.
- Reliable message processing using AWS SQS queues.
- Secure storage of sensitive data with AWS Secrets Manager.
- Detailed logging for monitoring and troubleshooting.
- Conversation context stored using AWS DynamoDB (Can reply to "Tell me more about 'that'")
- Infrastructure as Code
- 100% Code Coverage with Jest testcases
TextGPT is built using the AWS Cloud Development Kit (CDK) and consists of several AWS Lambda functions, each serving a specific function:
-
receiveSms: Triggered when an SMS message is received. It determines the Telephony origination, parses the message, and enqueues it into an SQS queue for further processing.
-
queryGpt: Retrieves messages from the SQS queue, constructs a prompt based on the message content and conversation history (stored in DynamoDB), and generates a prompt to send to the openAI API for GPT 3.5 turbo. The AI-generated response is then run through a delimiter check to see if an image prompt has been included. If so, it is processed and enqueued into a separate image SQS queue for processing by the Image Processor, and the new response is added to the Conversations DynamoDB table. If there is no delimited prompt, the response is enqueued on the sms SQS queue.
-
sendSms: Retrieves the either the bundled message for the sender, or a system error message from one of three SQS queues, and either sends it as an SMS reply or, in the case of a delimited image prompt being returned, sends as a MMS reply using the Twilio API.
-
imageProcessor: Retrieves a delimited prompt, generated by the AI assistant, to be sent to Dall-E 2 to generate an image. Upon successful generation, adds the iamge to the original message body and send as a MMS to the user. The generated image is then stored in S3.
All logging is done through AWS CloudWatch and follows the format:
Logging Level
ConversationId
-- Lambda
-- Log Content
-- Logged Parameters
Example:
INFO SMb1f9a545ebbc89d7669b438753a82a6c -- SendSMS -- Sending message to Twilio
The ConversationId
is consistent across logs for the same message, making it easier to trace its progression through the system.
Featuring over 45 comprehensive testcases, 100% of the testGpt codebase is covered by Jest test cases and ready for CI/CD checks.
![Screenshot 2023-06-29 at 4 03 42 PM](https://private-user-images.githubusercontent.com/27901095/249886387-a29adc88-ae82-4eef-87f3-aad4268897e8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1ODM0MDksIm5iZiI6MTcyNDU4MzEwOSwicGF0aCI6Ii8yNzkwMTA5NS8yNDk4ODYzODctYTI5YWRjODgtYWU4Mi00ZWVmLTg3ZjMtYWFkNDI2ODg5N2U4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDEwNTE0OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTJkY2NkNjgzYjVlM2U3ZTk3YTEwYTk4NDQ0ODIzMjMxYWI0ZDFmZTUwYTVkMGE5NGI1MjIzNThhNDk3ODczNDImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.vM8CiGU-10oRpzOO0cEvoOCVrYd7vbLLyoYB2yssr2A)
![Screenshot 2023-07-13 at 11 37 28 AM](https://private-user-images.githubusercontent.com/27901095/253330997-04307be6-cb0f-4656-9d81-412140749e02.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1ODM0MDksIm5iZiI6MTcyNDU4MzEwOSwicGF0aCI6Ii8yNzkwMTA5NS8yNTMzMzA5OTctMDQzMDdiZTYtY2IwZi00NjU2LTlkODEtNDEyMTQwNzQ5ZTAyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDEwNTE0OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWViY2E2MDBkMzJkYTA2YzdiN2Y0MDZkZjEyMWE5YWY1Y2YyNjA0NmQyNTdhM2EyYjJlNWMxN2U5N2RlYzczNWUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.bQlWCKaUdHLyM9PnnLZMywX9RNi8ndEMghuiC7FK8rU)
By default, TextGPT uses the gpt-3.5-turbo
model from OpenAI. To use a different model, visit the OpenAI Models Documentation to select a model, and then update the model name in the model
element of the config.ts at the stack level.
- AWS Account
- Twilio Account
- OpenAI Account
- Node.js and npm
- AWS CDK CLI
- Sign up for a Twilio account and go to the console.
- Obtain a phone number and take note of it.
- Configure the Lambda webhook: In the Twilio console, navigate to Phone Numbers -> Manage -> Active Numbers. Under Messaging Configuration, find the "A message comes in" field and ensure it's set to "Webhook". Add your
receiveSms
Lambda URL to this field.
- Create a secret in AWS Secrets Manager named
ChatGPTSecrets
. - Add the following key-value pairs to the secret:
twilioAccountSid
,twilioAuthToken
, andopenAiApiKey
. Use the configuration values from the Twilio console forthe Twilio keys, and for OpenAI, generate a new secret API key in the account settings panel. - Ensure that the IAM policy for your Lambda functions includes permissions to access the secret. Make sure the resource ARN in the IAM policy matches the full ARN of the secret.
- You can toggle the timeout settings for the Lambda functions as needed in the
./lib/config.ts
file. - All the necessary IAM policies and roles are set up in the stack defined in
./lib/text_gpt-stack.ts
.
- Clone the TextGPT repository to your local machine.
- Navigate to the project directory and run
npm install
to install dependencies. - Bootstrap the environment with
cdk bootstrap
. - Build the CDK stack by running
cdk build
. - Deploy the stack to your AWS account by running
cdk deploy
. This command also creates the necessary resources in your AWS account.
- Monitor the logs in AWS CloudWatch for any issues or to trace the flow of messages using the ConversationId
- If the Lambda functions are not able to access the secrets, verify that the IAM policy associated with the Lambda functions has the correct permissions and resource ARN.
- Ensure that your Twilio webhook is correctly configured to point to the
receiveSms
Lambda function URL. - If no image is being generated, ensure the
imageCooldown
configuration is set, and an appropriate value (3 works nicely)
- Implement a cache system to avoid unecessary calls to the DB for conversation history (redis or elasticache)
- Create an abstraction and mapping processor for the receiveSms lambda to become telephony provider agnostic
- Allow for
variations
andedits
for previously generated images from Dall-E - Replace GPT with a local, trained, LLaMA.cpp instance
- Replace Dall-E with a local, trained, Stable Diffusion instance
- Implement logging framework
- OpenAI for the ChatGPT model.
- AWS for the Cloud Development Kit (CDK) and Lambda services.
- Twilio for the telephony services.