muhammadnasar commited on
Commit
225ce96
1 Parent(s): a4c8c44

Create main.py

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