Spaces:
Sleeping
Sleeping
Commit
·
bbfb383
0
Parent(s):
Initial commit
Browse files- .gitignore +4 -0
- Dockerfile +12 -0
- app/__init__.py +5 -0
- app/agent.py +52 -0
- app/config.py +10 -0
- app/main.py +69 -0
- requirements.txt +8 -0
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
.env
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY app/ ./app/
|
| 9 |
+
|
| 10 |
+
ENV PYTHONPATH=/app
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gmail Agent API using Gemini and Composio
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
__version__ = "1.0.0"
|
app/agent.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/agent.py
|
| 2 |
+
from llama_index.core import Settings
|
| 3 |
+
from llama_index.llms.gemini import Gemini
|
| 4 |
+
from llama_index.core.agent import FunctionCallingAgentWorker
|
| 5 |
+
from composio_llamaindex import ComposioToolSet, App
|
| 6 |
+
from llama_index.core.llms import ChatMessage
|
| 7 |
+
|
| 8 |
+
class GmailAgent:
|
| 9 |
+
def __init__(self, google_api_key: str, composio_api_key: str):
|
| 10 |
+
# Set up Gemini LLM
|
| 11 |
+
Settings.llm = Gemini(
|
| 12 |
+
model="models/gemini-1.5-pro",
|
| 13 |
+
api_key=google_api_key
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Initialize Composio ToolSet
|
| 17 |
+
self.composio_toolset = ComposioToolSet(api_key=composio_api_key)
|
| 18 |
+
self.gmail_tools = self.composio_toolset.get_tools(apps=[App.GMAIL])
|
| 19 |
+
|
| 20 |
+
# Set up agent
|
| 21 |
+
prefix_messages = [
|
| 22 |
+
ChatMessage(
|
| 23 |
+
role="system",
|
| 24 |
+
content="""You are a Gmail Assistant that can:
|
| 25 |
+
- Read and analyze email content
|
| 26 |
+
- Draft professional replies
|
| 27 |
+
- Manage email threads
|
| 28 |
+
Use the available Gmail tools to perform these actions."""
|
| 29 |
+
)
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
agent_worker = FunctionCallingAgentWorker.from_tools(
|
| 33 |
+
tools=self.gmail_tools,
|
| 34 |
+
prefix_messages=prefix_messages,
|
| 35 |
+
verbose=True
|
| 36 |
+
)
|
| 37 |
+
self.agent = agent_worker.as_agent()
|
| 38 |
+
|
| 39 |
+
def authenticate_gmail(self):
|
| 40 |
+
request = self.composio_toolset.initiate_connection(app=App.GMAIL)
|
| 41 |
+
return request.redirectUrl
|
| 42 |
+
|
| 43 |
+
async def process_email(self, thread_id: str, email_content: str, sender_email: str):
|
| 44 |
+
prompt = f"""Analyze this email and create an appropriate reply:
|
| 45 |
+
Thread ID: {thread_id}
|
| 46 |
+
Sender: {sender_email}
|
| 47 |
+
Content: {email_content}
|
| 48 |
+
|
| 49 |
+
Please draft a professional response and use the appropriate Gmail tool to send it."""
|
| 50 |
+
|
| 51 |
+
response = await self.agent.achat(prompt)
|
| 52 |
+
return str(response)
|
app/config.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/config.py
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
class Config:
|
| 8 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 9 |
+
COMPOSIO_API_KEY = os.getenv("COMPOSIO_API_KEY")
|
| 10 |
+
ALLOWED_ORIGINS = ["*"] # Adjust for production
|
app/main.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/main.py
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from .config import Config
|
| 6 |
+
from .agent import GmailAgent
|
| 7 |
+
from app import __version__
|
| 8 |
+
|
| 9 |
+
app = FastAPI(
|
| 10 |
+
title="Gmail Agent API",
|
| 11 |
+
description="API for Gmail management using Gemini and Composio",
|
| 12 |
+
version=__version__
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Add CORS middleware
|
| 16 |
+
app.add_middleware(
|
| 17 |
+
CORSMiddleware,
|
| 18 |
+
allow_origins=Config.ALLOWED_ORIGINS,
|
| 19 |
+
allow_credentials=True,
|
| 20 |
+
allow_methods=["*"],
|
| 21 |
+
allow_headers=["*"],
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Initialize Gmail Agent
|
| 25 |
+
gmail_agent = GmailAgent(
|
| 26 |
+
google_api_key=Config.GOOGLE_API_KEY,
|
| 27 |
+
composio_api_key=Config.COMPOSIO_API_KEY
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
class EmailRequest(BaseModel):
|
| 31 |
+
thread_id: str
|
| 32 |
+
email_content: str
|
| 33 |
+
sender_email: str
|
| 34 |
+
|
| 35 |
+
@app.get("/")
|
| 36 |
+
async def root():
|
| 37 |
+
"""
|
| 38 |
+
Root endpoint that provides API information and health check
|
| 39 |
+
"""
|
| 40 |
+
return {
|
| 41 |
+
"status": "online",
|
| 42 |
+
"version": __version__,
|
| 43 |
+
"name": "Gmail Agent API",
|
| 44 |
+
"description": "Gmail Agent API using Gemini and Composio"
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
@app.get("/health")
|
| 48 |
+
async def health_check():
|
| 49 |
+
return {"status": "healthy"}
|
| 50 |
+
|
| 51 |
+
@app.get("/authenticate")
|
| 52 |
+
async def get_auth_url():
|
| 53 |
+
try:
|
| 54 |
+
auth_url = gmail_agent.authenticate_gmail()
|
| 55 |
+
return {"auth_url": auth_url}
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 58 |
+
|
| 59 |
+
@app.post("/process-email")
|
| 60 |
+
async def process_email(request: EmailRequest):
|
| 61 |
+
try:
|
| 62 |
+
response = await gmail_agent.process_email(
|
| 63 |
+
thread_id=request.thread_id,
|
| 64 |
+
email_content=request.email_content,
|
| 65 |
+
sender_email=request.sender_email
|
| 66 |
+
)
|
| 67 |
+
return {"response": response}
|
| 68 |
+
except Exception as e:
|
| 69 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
python-dotenv
|
| 4 |
+
composio-llamaindex
|
| 5 |
+
llama-index
|
| 6 |
+
llama-index-llms-gemini
|
| 7 |
+
pydantic
|
| 8 |
+
python-multipart
|