File size: 4,478 Bytes
96258d3
 
 
 
 
e3430e7
96258d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec7074d
96258d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec7074d
96258d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import streamlit as st
import openai
import json
import requests
import os
# openai.api_key = ""

def send_email(email_id,subject,content):
  import smtplib
  from email.mime.text import MIMEText
  from email.mime.multipart import MIMEMultipart
  
  # Email configuration
  sender_email = 'Your gmail id'
  receiver_email = email_id
  password = 'Use an app-specific password for security'
  subject = subject
  message_body = content
  
  # Create a MIMEText object for the email message
  message = MIMEMultipart()
  message['From'] = sender_email
  message['To'] = receiver_email
  message['Subject'] = subject
  message.attach(MIMEText(message_body, 'plain'))
  
  # Connect to the SMTP server (for Gmail, use 'smtp.gmail.com')
  smtp_server = 'smtp.gmail.com'
  smtp_port = 587  # Use 465 for SSL or 587 for TLS
  
  try:
      server = smtplib.SMTP(smtp_server, smtp_port)
      server.starttls()
      server.login(sender_email, password)
      server.sendmail(sender_email, receiver_email, message.as_string())
      server.quit()
      return('Email sent successfully')
  except Exception as e:
      return('Error sending email:', str(e))
  # Attach the message body

def run_conversation():
    functions = [
        {
            name: send_email,
            description: send an email to given email address.,
            parameters: {
                type: object,
                properties: {
                    email_id:{
                        type:string,
                        description:The id of email.
                    },
                    subject: {
                        type: string,
                        description: The subject of the email.,
                    },
                    content:
                    {type: string, description: The content of the email.},
                },
                required: [email_id,subject,content],
            },
        }]
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=st.session_state.messages,
        functions=functions,
        function_call=auto,
    )
    response_message = response[choices][0][message]
    # Step 2: check if GPT wanted to call a function
    if response_message.get(function_call):
        # Step 3: call the function
        # Note: the JSON response may not always be valid; be sure to handle errors
        available_functions = {
            send_email: send_email
        }  # only one function in this example, but you can have multiple
        function_name = response_message[function_call][name]
        function_to_call = available_functions[function_name]
        function_args = json.loads(response_message[function_call][arguments])
        function_response=function_to_call(
        email_id=function_args.get(email_id),
        subject=function_args.get(subject),
        content=function_args.get(content))
        # Step 4: send the info on the function call and function response to GPT
        # extend conversation with assistant's reply
        st.session_state.messages.append(response_message)
        st.session_state.messages.append(
            {
                role: function,
                name: function_name,
                content: function_response,
            })
        import time
        time.sleep(5)
        second_response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=st.session_state.messages)
        second_response=second_response[choices][0][message][content]
        st.session_state.messages.append({role: assistant, content: second_response})
        return second_response
    else:
        response_message = response_message[content]
        st.session_state.messages.append({role:assistant,content:response_message})
        return response_message


if messages not in st.session_state.keys():
  st.session_state.messages = [{role: assistant, content: WellCome To AI Email Sender!}]

if prompt := st.chat_input(Send Query...): # Prompt for user input and save to chat history
    st.session_state.messages.append({role: user, content: prompt})
  
for message in st.session_state.messages: # Display the prior chat messages
      with st.chat_message(message[role]):
        st.write(message[content])


if st.session_state.messages[-1][role] != assistant:
  with st.chat_message(assistant):
    with st.spinner(Thinking...):
      response_message = run_conversation()
      st.write(response_message)