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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class DIDx_Chatbot:
|
2 |
+
def __init__(self):
|
3 |
+
import openai
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
def get_balance():
|
8 |
+
import requests
|
9 |
+
response = requests.get(
|
10 |
+
f'https://newapi.didx.net/DidxApis/api/WebGetAccountBalance.php?UserID={user_id}&Password={password}')
|
11 |
+
return response.text
|
12 |
+
|
13 |
+
def get_available_dids_country():
|
14 |
+
import requests
|
15 |
+
response = requests.get(
|
16 |
+
f'https://newapi.didx.net/DidxApis/api/getDIDCountry.php?UserID={user_id}&Password={password}')
|
17 |
+
return response.text
|
18 |
+
|
19 |
+
def get_available_dids(CountryCode, AreaCode):
|
20 |
+
import requests
|
21 |
+
response = requests.get(
|
22 |
+
f'https://newapi.didx.net/DidxApis/api/getAvailableDIDS.php?UserID={user_id}&Password={password}&CountryCode={CountryCode}&AreaCode={AreaCode}')
|
23 |
+
return response.text
|
24 |
+
|
25 |
+
def get_purchased_dids():
|
26 |
+
import requests
|
27 |
+
response = requests.get(
|
28 |
+
f'https://newapi.didx.net/DidxApis/api/getMyPurchasedNumbers.php?UserID={user_id}&Password={password}')
|
29 |
+
return response.text
|
30 |
+
|
31 |
+
self.get_balance = get_balance
|
32 |
+
self.get_available_dids_country = get_available_dids_country
|
33 |
+
self.get_available_dids = get_available_dids
|
34 |
+
self.get_purchased_dids = get_purchased_dids
|
35 |
+
|
36 |
+
function_balance = {
|
37 |
+
"type": "function",
|
38 |
+
"function": {
|
39 |
+
"name": "get_balance",
|
40 |
+
"description": "Retrieve the available balance present in the account",
|
41 |
+
"parameters": {
|
42 |
+
"type": "object",
|
43 |
+
"properties": {
|
44 |
+
"Balance": {
|
45 |
+
"type": "string",
|
46 |
+
"description": "A balance in the amount of dollars"
|
47 |
+
},
|
48 |
+
"Status": {
|
49 |
+
"type": "string",
|
50 |
+
"description": "A balance status due or something else"
|
51 |
+
|
52 |
+
}
|
53 |
+
},
|
54 |
+
"required": ["account balance", "Status"]
|
55 |
+
}
|
56 |
+
}
|
57 |
+
}
|
58 |
+
function_dids_country = {
|
59 |
+
"type": "function",
|
60 |
+
"function": {
|
61 |
+
"name": "get_available_dids_country",
|
62 |
+
"description": "Retrieve the available DIDs country information",
|
63 |
+
"parameters": {
|
64 |
+
"type": "object",
|
65 |
+
"properties": {
|
66 |
+
"country_code": {
|
67 |
+
"type": "string",
|
68 |
+
"description": "Country code for available DIDs"
|
69 |
+
},
|
70 |
+
"description": {
|
71 |
+
"type": "string",
|
72 |
+
"description": "Country/City/County code for available DIDs"
|
73 |
+
|
74 |
+
},
|
75 |
+
"country_id": {
|
76 |
+
"type": "string",
|
77 |
+
"description": "Country/City/County code for available DIDs"
|
78 |
+
}
|
79 |
+
|
80 |
+
},
|
81 |
+
"required": ["country_code", "description", "country_id"]
|
82 |
+
}
|
83 |
+
}
|
84 |
+
}
|
85 |
+
function_dids = {
|
86 |
+
"type": "function",
|
87 |
+
"function": {
|
88 |
+
"name": "get_available_dids",
|
89 |
+
"description": "Retrieve all available DIDs list",
|
90 |
+
"parameters": {
|
91 |
+
"type": "object",
|
92 |
+
"properties": {
|
93 |
+
"CountryCode": {
|
94 |
+
"type": "string",
|
95 |
+
"description": "Country code for available DIDs. Default CountryCode is 1 for US"
|
96 |
+
},
|
97 |
+
"description": {
|
98 |
+
"type": "string",
|
99 |
+
"description": "A list of available DIDs"
|
100 |
+
|
101 |
+
},
|
102 |
+
"AreaCode": {
|
103 |
+
"type": "string",
|
104 |
+
"description": "Area Code for the available DIDs. Default AreaCode is -1 that will show all AreaCode"
|
105 |
+
}
|
106 |
+
|
107 |
+
},
|
108 |
+
"required": ["CountryCode", "description", "AreaCode"]
|
109 |
+
}
|
110 |
+
}
|
111 |
+
}
|
112 |
+
|
113 |
+
function_purchased_dids = {
|
114 |
+
"type": "function",
|
115 |
+
"function": {
|
116 |
+
"name": "get_purchased_dids",
|
117 |
+
"description": "Retrieve all purchased DIDs",
|
118 |
+
"parameters": {
|
119 |
+
"type": "object",
|
120 |
+
"properties": {
|
121 |
+
"DIDNumber": {
|
122 |
+
"type": "string",
|
123 |
+
"description": "Purchased DIDNumber"
|
124 |
+
},
|
125 |
+
"OurSetupCost": {
|
126 |
+
"type": "string",
|
127 |
+
"description": "Our Setup Cost"
|
128 |
+
|
129 |
+
},
|
130 |
+
"OurMonthlyCharges": {
|
131 |
+
"type": "string",
|
132 |
+
"description": "Our Monthly Charges"
|
133 |
+
},
|
134 |
+
"OurPerMinuteCharges": {
|
135 |
+
"type": "string",
|
136 |
+
"description": "Our Per-Minute Charges"
|
137 |
+
}
|
138 |
+
|
139 |
+
},
|
140 |
+
"required": ["CountryCode", "description", "AreaCode"]
|
141 |
+
}
|
142 |
+
}
|
143 |
+
}
|
144 |
+
|
145 |
+
|
146 |
+
import os
|
147 |
+
from dotenv import load_dotenv
|
148 |
+
|
149 |
+
# Load environment variables from .env file
|
150 |
+
load_dotenv()
|
151 |
+
|
152 |
+
self.client = openai.OpenAI(api_key=os.environ['openai_api_key'])
|
153 |
+
print(self.client)
|
154 |
+
# Step 1: Create an Assistant
|
155 |
+
self.assistant = self.client.beta.assistants.create(
|
156 |
+
name="DIDx Customer Support Chatbot",
|
157 |
+
instructions="You are a personal DIDx customer support chatbot.",
|
158 |
+
tools=[function_balance, function_dids_country, function_dids, function_purchased_dids],
|
159 |
+
model="gpt-4-1106-preview",
|
160 |
+
)
|
161 |
+
|
162 |
+
def user_auth(self, user, passw):
|
163 |
+
global user_id
|
164 |
+
global password
|
165 |
+
|
166 |
+
user_id = user
|
167 |
+
password = passw
|
168 |
+
|
169 |
+
def user_chat(self, query):
|
170 |
+
import time
|
171 |
+
# Step 2: Create a Thread
|
172 |
+
thread = self.client.beta.threads.create()
|
173 |
+
|
174 |
+
# Step 3: Add a Message to a Thread
|
175 |
+
message = self.client.beta.threads.messages.create(
|
176 |
+
thread_id=thread.id,
|
177 |
+
role="user",
|
178 |
+
content=query
|
179 |
+
)
|
180 |
+
|
181 |
+
# Step 4: Run the Assistant
|
182 |
+
run = self.client.beta.threads.runs.create(
|
183 |
+
thread_id=thread.id,
|
184 |
+
assistant_id=self.assistant.id,
|
185 |
+
instructions=""
|
186 |
+
)
|
187 |
+
answer = None
|
188 |
+
while True:
|
189 |
+
# Retrieve the run status
|
190 |
+
run_status = self.client.beta.threads.runs.retrieve(
|
191 |
+
thread_id=thread.id,
|
192 |
+
run_id=run.id
|
193 |
+
)
|
194 |
+
# print(run_status.model_dump_json(indent=4))
|
195 |
+
run_status.model_dump_json(indent=4)
|
196 |
+
|
197 |
+
# If run is completed, get messages
|
198 |
+
if run_status.status == 'completed':
|
199 |
+
messages = self.client.beta.threads.messages.list(
|
200 |
+
thread_id=thread.id
|
201 |
+
)
|
202 |
+
# Loop through messages and print content based on role
|
203 |
+
for msg in messages.data:
|
204 |
+
role = msg.role
|
205 |
+
content = msg.content[0].text.value
|
206 |
+
print(f"{role.capitalize()}: {content}")
|
207 |
+
answer = f"{role.capitalize()}: {content}"
|
208 |
+
break
|
209 |
+
break
|
210 |
+
elif run_status.status == 'requires_action':
|
211 |
+
# print("Function Calling")
|
212 |
+
required_actions = run_status.required_action.submit_tool_outputs.model_dump()
|
213 |
+
# print('required action test: ',required_actions)
|
214 |
+
tool_outputs = []
|
215 |
+
import json
|
216 |
+
for action in required_actions["tool_calls"]:
|
217 |
+
func_name = action['function']['name']
|
218 |
+
arguments = json.loads(action['function']['arguments'])
|
219 |
+
|
220 |
+
if func_name == "get_balance":
|
221 |
+
output = self.get_balance()
|
222 |
+
tool_outputs.append({
|
223 |
+
"tool_call_id": action['id'],
|
224 |
+
"output": output
|
225 |
+
})
|
226 |
+
|
227 |
+
elif func_name == 'get_available_dids_country':
|
228 |
+
output = self.get_available_dids_country()
|
229 |
+
tool_outputs.append({
|
230 |
+
"tool_call_id": action['id'],
|
231 |
+
"output": output
|
232 |
+
})
|
233 |
+
elif func_name == 'get_available_dids':
|
234 |
+
output = self.get_available_dids(CountryCode=arguments['CountryCode'],
|
235 |
+
AreaCode=arguments['AreaCode'])
|
236 |
+
tool_outputs.append({
|
237 |
+
"tool_call_id": action['id'],
|
238 |
+
"output": output
|
239 |
+
})
|
240 |
+
elif func_name == 'get_purchased_dids':
|
241 |
+
output = self.get_purchased_dids()
|
242 |
+
tool_outputs.append({
|
243 |
+
"tool_call_id": action['id'],
|
244 |
+
"output": output
|
245 |
+
})
|
246 |
+
|
247 |
+
else:
|
248 |
+
raise ValueError(f"Unknown function: {func_name}")
|
249 |
+
|
250 |
+
print("Submitting outputs back to the Assistant...")
|
251 |
+
self.client.beta.threads.runs.submit_tool_outputs(
|
252 |
+
thread_id=thread.id,
|
253 |
+
run_id=run.id,
|
254 |
+
tool_outputs=tool_outputs
|
255 |
+
)
|
256 |
+
else:
|
257 |
+
print("Waiting for the Assistant to process...")
|
258 |
+
time.sleep(5)
|
259 |
+
|
260 |
+
if answer is not None:
|
261 |
+
print(f'this is my answer : ', answer)
|
262 |
+
return answer
|