Hi,
I have been building Python code that integrates GPT API with Gmail API so that content is extracted from an unread email and is sent to GPT to produce an email response based on the content.
I am facing two issues - 1) the number of tokens for sending and receiving the message seems to be much higher than expected e.g. a one-line email equating to 2000 tokens.
- Whilst I have managed to get the response from GPT to print in the terminal, it does not appear as an email in the drafts folder.
import os
import base64
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import openai
Set up your Gmail API credentials
SCOPES = [‘Can’t include links - but modify included here’]
def get_gmail_credentials():
creds = None
if os.path.exists(‘token.json’):
creds = Credentials.from_authorized_user_file(‘token.json’, SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
return creds
Initialize the Gmail service
def initialize_gmail_service():
creds = get_gmail_credentials()
return build(‘gmail’, ‘v1’, credentials=creds)
Function to get the entire email message
def get_email_message(service, message_id):
try:
message = service.users().messages().get(userId=‘me’, id=message_id, format=‘raw’).execute()
message_raw = base64.urlsafe_b64decode(message[‘raw’].encode(‘UTF-8’)).decode(‘UTF-8’)
return message_raw
except Exception as e:
print(f"An error occurred: {str(e)}")
return None
Your OpenAI settings
openai.api_key = ‘YOUR OPEN AI KEY’ # Replace with your OpenAI API key
chatgpt_model = ‘gpt-3.5-turbo’ # Choose an appropriate model
Your conversation with OpenAI
messages = [
{“role”: “system”, “content”: “You are composing a response to an email.”},
{“role”: “user”, “content”: “Compose an email response based on the content of the unread message:”},
]
Get the list of unread emails
Function to get unread emails
def get_unread_emails(service):
query = “is:unread is:inbox”
response = service.users().messages().list(userId=‘me’, q=query).execute()
messages =
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId='me', q=query, pageToken=page_token).execute()
if 'messages' in response:
messages.extend(response['messages'])
return messages
def main():
service = initialize_gmail_service()
unread_emails = get_unread_emails(service)
if unread_emails:
# Choose the first unread email (you can modify the selection logic as needed)
first_unread_email = unread_emails[0]
message_id = first_unread_email['id']
# Fetch the entire email message
email_message = get_email_message(service, message_id)
if email_message:
# Include the email message in the conversation with OpenAI
messages.append({"role": "assistant", "content": email_message})
# Use OpenAI to generate a response
response = openai.ChatCompletion.create(
model=chatgpt_model,
messages=messages,
temperature=0.7,
max_tokens=1000 # Adjust the max tokens as needed
)
# Extract the response from OpenAI
response_content = response['choices'][0]['message']['content']
# You can use the response_content to send an email or take other actions
print("OpenAI's response based on the email content:")
print(response_content)
else:
print("Failed to retrieve the email message.")
else:
print("No unread emails found in the inbox.")
if name == “main”:
main()