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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from tavily import TavilyClient
|
3 |
+
from ics import Calendar, Event
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
import yagmail
|
6 |
+
from groq import Groq
|
7 |
+
import smtplib
|
8 |
+
from email.mime.multipart import MIMEMultipart
|
9 |
+
from email.mime.text import MIMEText
|
10 |
+
from email.mime.base import MIMEBase
|
11 |
+
from email import encoders
|
12 |
+
import json
|
13 |
+
from openai import OpenAI
|
14 |
+
import os
|
15 |
+
|
16 |
+
def web_information_retrieval(user_query):
|
17 |
+
tavily_client = TavilyClient(api_key=os.getenv('TAVILY_API_KEY'))
|
18 |
+
response = tavily_client.search(user_query, include_answer=True)['answer']
|
19 |
+
return response
|
20 |
+
|
21 |
+
def information_retrieval(user_query):
|
22 |
+
client = Groq(api_key=os.getenv('GROQ_API_KEY'))
|
23 |
+
chat_completion = client.chat.completions.create(
|
24 |
+
messages=[
|
25 |
+
{"role": "user", "content": user_query}
|
26 |
+
],
|
27 |
+
model="llama3-8b-8192",
|
28 |
+
)
|
29 |
+
return chat_completion.choices[0].message.content
|
30 |
+
|
31 |
+
def e_commerce_search(user_query):
|
32 |
+
tavily_client = TavilyClient(api_key=os.getenv('TAVILY_API_KEY'))
|
33 |
+
response = tavily_client.search(
|
34 |
+
user_query,
|
35 |
+
search_depth="advanced",
|
36 |
+
include_domains=[
|
37 |
+
'http://amazon.in', 'http://flipkart.com', 'https://www.myntra.com/',
|
38 |
+
'https://www.nykaa.com/', 'http://ajio.com'
|
39 |
+
],
|
40 |
+
include_images=True
|
41 |
+
)
|
42 |
+
return response
|
43 |
+
|
44 |
+
def travel_and_navigation(user_query):
|
45 |
+
tavily_client = TavilyClient(api_key=os.getenv('TAVILY_API_KEY'))
|
46 |
+
response = tavily_client.search(
|
47 |
+
user_query,
|
48 |
+
search_depth="advanced",
|
49 |
+
include_domains=['https://delhimetrorail.info/', 'http://goibibo.com', 'http://makemytrip.com'],
|
50 |
+
include_images=True
|
51 |
+
)
|
52 |
+
return response
|
53 |
+
|
54 |
+
def send_calendar_invite(title, description, location, start_time, end_time, attendee_emails, sender_email='[email protected]', sender_password='wdpv atip epfw tksx'):
|
55 |
+
cal = Calendar()
|
56 |
+
event = Event()
|
57 |
+
event.name = title
|
58 |
+
event.description = description
|
59 |
+
event.location = location
|
60 |
+
event.begin = start_time
|
61 |
+
event.end = end_time
|
62 |
+
for email in attendee_emails:
|
63 |
+
event.add_attendee(email)
|
64 |
+
cal.events.add(event)
|
65 |
+
|
66 |
+
ics_content = str(cal)
|
67 |
+
|
68 |
+
subject = f'Calendar Invite: {title}'
|
69 |
+
body = f'Please find attached the calendar invite for the event: {title}'
|
70 |
+
|
71 |
+
msg = MIMEMultipart()
|
72 |
+
msg['From'] = sender_email
|
73 |
+
msg['To'] = ', '.join(attendee_emails)
|
74 |
+
msg['Subject'] = subject
|
75 |
+
|
76 |
+
msg.attach(MIMEText(body, 'plain'))
|
77 |
+
|
78 |
+
part = MIMEBase('application', 'octet-stream')
|
79 |
+
part.set_payload(ics_content)
|
80 |
+
encoders.encode_base64(part)
|
81 |
+
part.add_header('Content-Disposition', 'attachment; filename=invite.ics')
|
82 |
+
msg.attach(part)
|
83 |
+
|
84 |
+
try:
|
85 |
+
with smtplib.SMTP('smtp.gmail.com', 587) as server:
|
86 |
+
server.starttls()
|
87 |
+
server.login(sender_email, sender_password)
|
88 |
+
server.send_message(msg)
|
89 |
+
st.write(f'Calendar invite sent successfully to: {", ".join(attendee_emails)}')
|
90 |
+
except Exception as e:
|
91 |
+
st.write(f'Failed to send calendar invite: {e}')
|
92 |
+
|
93 |
+
from openai import OpenAI
|
94 |
+
import json
|
95 |
+
|
96 |
+
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
97 |
+
|
98 |
+
def run_conversation(user_query):
|
99 |
+
messages = [{"role": "user", "content": user_query}]
|
100 |
+
tools = [
|
101 |
+
{
|
102 |
+
"type": "function",
|
103 |
+
"function": {
|
104 |
+
"name": "web_information_retrieval",
|
105 |
+
"description": "Retrieve web information based on user query",
|
106 |
+
"parameters": {
|
107 |
+
"type": "object",
|
108 |
+
"properties": {
|
109 |
+
"user_query": {
|
110 |
+
"type": "string",
|
111 |
+
"description": "User query to search web information",
|
112 |
+
},
|
113 |
+
},
|
114 |
+
"required": ["user_query"],
|
115 |
+
},
|
116 |
+
},
|
117 |
+
},
|
118 |
+
{
|
119 |
+
"type": "function",
|
120 |
+
"function": {
|
121 |
+
"name": "information_retrieval",
|
122 |
+
"description": "Retrieve general information based on user query without internet",
|
123 |
+
"parameters": {
|
124 |
+
"type": "object",
|
125 |
+
"properties": {
|
126 |
+
"user_query": {
|
127 |
+
"type": "string",
|
128 |
+
"description": "User query to retrieve information",
|
129 |
+
},
|
130 |
+
},
|
131 |
+
"required": ["user_query"],
|
132 |
+
},
|
133 |
+
},
|
134 |
+
},
|
135 |
+
{
|
136 |
+
"type": "function",
|
137 |
+
"function": {
|
138 |
+
"name": "e_commerce_search",
|
139 |
+
"description": "Search e-commerce sites based on user query",
|
140 |
+
"parameters": {
|
141 |
+
"type": "object",
|
142 |
+
"properties": {
|
143 |
+
"user_query": {
|
144 |
+
"type": "string",
|
145 |
+
"description": "User query to search e-commerce sites",
|
146 |
+
},
|
147 |
+
},
|
148 |
+
"required": ["user_query"],
|
149 |
+
},
|
150 |
+
},
|
151 |
+
},
|
152 |
+
{
|
153 |
+
"type": "function",
|
154 |
+
"function": {
|
155 |
+
"name": "travel_and_navigation",
|
156 |
+
"description": "Search travel and navigation sites based on user query",
|
157 |
+
"parameters": {
|
158 |
+
"type": "object",
|
159 |
+
"properties": {
|
160 |
+
"user_query": {
|
161 |
+
"type": "string",
|
162 |
+
"description": "User query to search travel and navigation sites",
|
163 |
+
},
|
164 |
+
},
|
165 |
+
"required": ["user_query"],
|
166 |
+
},
|
167 |
+
},
|
168 |
+
},
|
169 |
+
{
|
170 |
+
"type": "function",
|
171 |
+
"function": {
|
172 |
+
"name": "send_calendar_invite",
|
173 |
+
"description": "Send a calendar invite for an event",
|
174 |
+
"parameters": {
|
175 |
+
"type": "object",
|
176 |
+
"properties": {
|
177 |
+
"title": {
|
178 |
+
"type": "string",
|
179 |
+
"description": "Title of the event",
|
180 |
+
},
|
181 |
+
"description": {
|
182 |
+
"type": "string",
|
183 |
+
"description": "Description of the event",
|
184 |
+
},
|
185 |
+
"location": {
|
186 |
+
"type": "string",
|
187 |
+
"description": "Location of the event",
|
188 |
+
},
|
189 |
+
"start_time": {
|
190 |
+
"type": "string",
|
191 |
+
"description": "Start time of the event",
|
192 |
+
},
|
193 |
+
"end_time": {
|
194 |
+
"type": "string",
|
195 |
+
"description": "End time of the event",
|
196 |
+
},
|
197 |
+
"attendee_emails": {
|
198 |
+
"type": "array",
|
199 |
+
"items": {
|
200 |
+
"type": "string",
|
201 |
+
"description": "Emails of attendees",
|
202 |
+
},
|
203 |
+
},
|
204 |
+
"sender_email": {
|
205 |
+
"type": "string",
|
206 |
+
"description": "Sender's email address",
|
207 |
+
},
|
208 |
+
"sender_password": {
|
209 |
+
"type": "string",
|
210 |
+
"description": "Sender's email password",
|
211 |
+
},
|
212 |
+
},
|
213 |
+
"required": ["title", "location", "start_time", "end_time", "attendee_emails"],
|
214 |
+
},
|
215 |
+
},
|
216 |
+
},
|
217 |
+
]
|
218 |
+
response = client.chat.completions.create(
|
219 |
+
model="gpt-4o",
|
220 |
+
messages=messages,
|
221 |
+
tools=tools,
|
222 |
+
tool_choice="auto",
|
223 |
+
)
|
224 |
+
response_message = response.choices[0].message
|
225 |
+
tool_calls = response_message.tool_calls
|
226 |
+
|
227 |
+
if tool_calls:
|
228 |
+
available_functions = {
|
229 |
+
"web_information_retrieval": web_information_retrieval,
|
230 |
+
"information_retrieval": information_retrieval,
|
231 |
+
"e_commerce_search": e_commerce_search,
|
232 |
+
"travel_and_navigation": travel_and_navigation,
|
233 |
+
"send_calendar_invite": send_calendar_invite,
|
234 |
+
}
|
235 |
+
|
236 |
+
messages.append(response_message)
|
237 |
+
for tool_call in tool_calls:
|
238 |
+
function_name = tool_call.function.name
|
239 |
+
function_to_call = available_functions[function_name]
|
240 |
+
function_args = json.loads(tool_call.function.arguments)
|
241 |
+
if function_name == "send_calendar_invite":
|
242 |
+
function_response = function_to_call(
|
243 |
+
title=function_args.get("title"),
|
244 |
+
description=function_args.get("description"),
|
245 |
+
location=function_args.get("location"),
|
246 |
+
start_time=function_args.get("start_time"),
|
247 |
+
end_time=function_args.get("end_time"),
|
248 |
+
attendee_emails=function_args.get("attendee_emails"),
|
249 |
+
)
|
250 |
+
else:
|
251 |
+
function_response = function_to_call(
|
252 |
+
user_query=function_args.get("user_query"),
|
253 |
+
)
|
254 |
+
|
255 |
+
if function_name in ["e_commerce_search", "travel_and_navigation"]:
|
256 |
+
for i, result in enumerate(function_response['results']):
|
257 |
+
st.write(f"### [{result['title']}]({result['url']})")
|
258 |
+
st.write(result['content'])
|
259 |
+
if i < len(function_response['images']):
|
260 |
+
st.image(function_response['images'][i])
|
261 |
+
|
262 |
+
messages.append(
|
263 |
+
{
|
264 |
+
"tool_call_id": tool_call.id,
|
265 |
+
"role": "tool",
|
266 |
+
"name": function_name,
|
267 |
+
"content": function_response,
|
268 |
+
}
|
269 |
+
)
|
270 |
+
return function_response
|
271 |
+
|
272 |
+
st.title("Glucon_D Chatbot")
|
273 |
+
user_query = st.text_input("Enter your query")
|
274 |
+
if st.button("Run"):
|
275 |
+
if user_query:
|
276 |
+
response = run_conversation(user_query)
|