This project is a WebSocket-based chat application using FastAPI to provide real-time chat functionality. It integrates with a custom CodingAgent class to interact with the Gemini API for generating code responses based on user input. The project includes robust chat history storage and retrieval using Redis as an in-memory data store. It also includes safety filters for content moderation and custom tools for enhanced interactions.
- WebSocket Chat Interface: Enables real-time bi-directional communication with users.
- Code Generation with Gemini API: Uses the Gemini API to generate code responses based on user prompts.
- Chat History Storage: Stores chat messages and responses in Redis for session persistence.
- Safety Filters: Implements customizable safety settings for handling potentially harmful content.
- Custom Tool Integrations: Adds functionality with
search_custom_engineandscrape_websitetools.
- main.py: Contains the main application code with the WebSocket endpoint and FastAPI setup.
- CodingAgent Class:
- Manages interactions with the Gemini API.
- Retrieves chat history from Redis and stores interactions.
- Configures safety settings for content moderation.
- Logger: A custom logger that logs important application events and information, enhancing debugging and traceability.
- RedisMemory: Manages conversation history storage using Redis for efficient access.
- WebSocket Connection: When a client connects to
/chat, a WebSocket connection is established, and a new chat session is created with a uniquechat_id. - User Message Handling: When a message is received, it’s stored in Redis and passed to the
CodingAgent. - AI Response Generation: The
CodingAgentsends the user message to the Gemini API to generate a response, which is then sent back to the user. - Memory Management: The chat history is maintained in Redis for the current session and loaded when required.
- FastAPI: For building the WebSocket server and managing routes.
- Uvicorn: ASGI server to run FastAPI applications.
- Google Generative AI: For generating AI responses (Gemini API).
- Redis: Used for storing chat history and session management.
- Clone the repository:
git clone https://github.com/yafatek/ai-coding-assitant.git cd ai-coding-assitantInstall the required dependencies:
you need a Docker Desktop to run a Redis Instance
docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack:latestpip install -r requirements.txtpip install 'uvicorn[standard]' Configure environment variables, including the Gemini API key and Redis connection details.
Run the application:
uvicorn main:app --host 0.0.0.0 --port 11203version: '3'
services:
redis:
image: redis/redis-stack:latest
ports:
- "6379:6379"
- "8001:8001"
networks:
- fullstack_network
backend:
image: yafaorg/ai-assistant-backend:latest
ports:
- "11203:11203"
environment:
- GEMINI_API_KEY=your_gemini_api_key_here
- SEARCH_ENGIN_ID=your_search_engine_id_here
- GOOGLE_SEARCH_API_KEY=your_google_search_api_key_here
- REDIS_HOST=redis://redis:6379 # Connects to the Redis service
networks:
- fullstack_network
depends_on:
- redis
frontend:
image: yafaorg/ai-assistant-ui:latest
ports:
- "3000:3000"
networks:
- fullstack_network
networks:
fullstack_network:
driver: bridgeStart the server. Connect to the WebSocket endpoint at /chat. Send a text message, and the application will respond with AI-generated code from the Gemini API.
Contributions are welcome! Please fork the repository and create a pull request with your changes.
MIT License
Copyright (c) 2024 YAFATEK Solutions
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.