A sophisticated AI assistant hosted on Google Cloud Run, using FastAPI backend and integrated with Telegram. This bot leverages OpenAI's GPT-4 model to process and respond to text messages, images, and voice inputs.
- Features
- Prerequisites
- Installation
- Configuration
- Deployment
- Usage
- Project Structure
- Troubleshooting
- Contributing
- License
- Text message processing and responses
- Image analysis and description
- Voice message transcription and voice response
- Text-to-speech for voice responses
- Hosted on Google Cloud Run for 24/7 availability
- FastAPI backend for efficient API handling
- Telegram bot integration for user interaction and easy access through telegram
Certainly! I'll add a new category to the README to explain the additional features you've implemented for your personal needs. Here's how we can incorporate this information:
This bot includes some specialized features tailored for personal use:
The bot can fetch and summarize recent news from specific regions using GPT function calls. This feature allows users to stay updated on current events in France or Alsace.
- The bot uses a custom tool defined in the GPT model's function calling capability.
- Users can request news by asking about current events in France or Alsace.
- The bot processes the request and calls the appropriate function to fetch relevant news.
self.tools = [
{
"type": "function",
"function": {
"name": "get_news",
"description": "récupère les dernières actualités, en France ou spécifiquement en Alsace",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"enum": ["France", "Alsace"],
"description": "Le périmètre de recherche pour récupérer les actualités",
},
},
"required": ["location"],
},
},
}
]To get news, you can ask the bot questions like:
- "What's the latest news from France?"
- "Tell me about recent events in Alsace."
- "Any important headlines from France today?"
The bot will then use the appropriate function to fetch and summarize relevant news articles based on the specified location.
Note: This feature requires appropriate backend implementation to actually fetch and process the news data. Ensure that the necessary APIs or web scraping mechanisms are in place to support this functionality.
This bot implements a sophisticated persistence mechanism to maintain context across conversations and improve response relevance over time.
The bot stores all messages along with their embeddings in a Google Cloud Storage bucket. This allows for efficient retrieval of relevant past conversations.
- For each message, the bot generates an embedding using OpenAI's text-embedding-ada-002 model.
- The message and its embedding are stored together in the Google Cloud Storage bucket.
- When a new message comes in, the bot computes its embedding and finds the most similar past messages.
- These relevant past messages are included in the system prompt to provide context for the AI's response.
# Generate embedding for the message
response = client.embeddings.create(
model="text-embedding-ada-002",
input=message_obj["content"]
)
# Prepare the message-embedding pair for storage
emb_mess_pair = {
"embedding": json.dumps(response.data[0].embedding),
"message": json.dumps(message_obj)
}
# Store the pair in Google Cloud Storage
# (Implementation details for storage not shown here)When processing a new message:
- The bot computes the embedding for the incoming message.
- It then calculates the cosine similarity between this embedding and all stored embeddings.
- The most similar past messages are retrieved and incorporated into the system prompt.
This approach allows the bot to:
- Maintain long-term memory of conversations
- Provide more contextually relevant responses
- Improve continuity in multi-turn conversations
- Improved Consistency: The bot can refer to past conversations, maintaining consistency in its responses over time.
- Personalization: By remembering user-specific information, the bot can provide more tailored responses.
- Enhanced Context Understanding: The AI can draw upon a broader context when formulating responses, leading to more informed and relevant answers.
- Ensure proper security measures are in place to protect stored messages and embeddings.
- Regularly monitor and manage the storage usage in your Google Cloud Storage bucket.
- Consider implementing a retention policy or periodic cleanup to manage the growth of stored data.
This feature works automatically in the background. Users will experience more contextually aware and personalized responses without needing to explicitly reference past conversations.
Note: The exact implementation of similarity calculation and the method of incorporating past messages into the prompt may vary based on specific requirements and optimizations.
- Python 3.10 or higher
- Google Cloud Platform account
- OpenAI API account
- Telegram Bot Token
-
Clone the repository:
git clone https://github.com/yourusername/personal-ai-assistant-bot.git cd personal-ai-assistant-bot -
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate` -
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the project root and add the following environment variables:OPENAI_API_KEY=your_openai_api_key TELEGRAM_TOKEN=your_telegram_bot_token -
Obtain API keys:
-
Install and set up the Google Cloud SDK
-
Authenticate with Google Cloud:
gcloud auth login -
Set your project ID:
gcloud config set project YOUR_PROJECT_ID -
Build and deploy the container to Google Cloud Run:
gcloud run deploy personal-ai-assistant --source . --platform managed --region your-preferred-region -
Follow the prompts to complete the deployment
-
Start a chat with your Telegram bot
-
Send text messages, images, or voice messages to interact with the AI assistant
-
The bot will process your input and respond accordingly, using text or voice as appropriate
telegram-elza-bot/
├── main.py
├── requirements.txt
├── Dockerfile
├── .gcloudignore
├── .env.example
├── .gitignore
├── README.md
├── LICENSE
└── start.bat
- If you encounter issues with API rate limits, check your usage and consider upgrading your plan
- For deployment problems, review the Google Cloud Run logs for detailed error messages
- Ensure all required environment variables are correctly set in your
.envfile and in your Google Cloud Run configuration
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.