muhammadnasar commited on
Commit
879a8ae
·
1 Parent(s): 16fe4b0

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +20 -0
  2. bot.py +133 -0
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from bot import supertec_bot
3
+
4
+
5
+ didx_chatbot = supertec_bot()
6
+
7
+ st.title("SuperTech Bot")
8
+ user_input = st.text_input('How can I help you?')
9
+ with st.spinner('Sit back and relax. It takes a while.'):
10
+ if st.button('Ask'):
11
+ if user_input:
12
+ answer = didx_chatbot.user_chat(user_input)
13
+ st.write(answer)
14
+
15
+
16
+
17
+
18
+
19
+
20
+
bot.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class supertec_bot:
2
+ def __init__(self):
3
+ import openai
4
+
5
+
6
+
7
+ def get_data():
8
+ import requests
9
+ headers = {
10
+ 'User-Agent': 'Mozilla/5.0', # You can try different User-Agent strings if needed
11
+ }
12
+
13
+ payload = {'company_token': 'II@tNfQ70O'}
14
+
15
+ response = requests.post("https://superteclabs.com/apis2/retrieveallusers.php", data=payload,
16
+ headers=headers)
17
+
18
+ data = response.json()
19
+ return response.text
20
+
21
+ self.get_data = get_data
22
+
23
+ function_balance = {
24
+ "type": "function",
25
+ "function": {
26
+ "name": "get_data",
27
+ "description": "Retrieve the data from this ",
28
+ "parameters": {
29
+ "type": "object",
30
+ "properties": {
31
+ "query": {
32
+ "type": "string",
33
+ "description": "show me the all data "
34
+ },
35
+ },
36
+ "required": ["query"]
37
+ }
38
+ }
39
+ }
40
+
41
+ import os
42
+ from dotenv import load_dotenv
43
+
44
+ # Load environment variables from .env file
45
+ load_dotenv()
46
+
47
+ self.client = openai.OpenAI(api_key=os.environ['openai_api_key'])
48
+ print(self.client)
49
+ # Step 1: Create an Assistant
50
+ # self.assistant = self.client.beta.assistants.create(
51
+ # name="library Support Chatbot",
52
+ # instructions="You are a personal Supertech support chatbot.",
53
+ # tools=[function_balance],
54
+ # model="gpt-3.5-turbo",
55
+ # )
56
+
57
+
58
+
59
+
60
+ def user_chat(self,query):
61
+ import time
62
+ # Step 2: Create a Thread
63
+ thread = self.client.beta.threads.create()
64
+
65
+ # Step 3: Add a Message to a Thread
66
+ message = self.client.beta.threads.messages.create(
67
+ thread_id=thread.id,
68
+ role="user",
69
+ content=query
70
+ )
71
+
72
+ # Step 4: Run the Assistant
73
+ run = self.client.beta.threads.runs.create(
74
+ thread_id=thread.id,
75
+ assistant_id="asst_tbdEcvmCNA4uFRPcxyVt8KNR",
76
+ instructions=""
77
+ )
78
+ answer = None
79
+ while True:
80
+ # Retrieve the run status
81
+ run_status = self.client.beta.threads.runs.retrieve(
82
+ thread_id=thread.id,
83
+ run_id=run.id
84
+ )
85
+ # print(run_status.model_dump_json(indent=4))
86
+ run_status.model_dump_json(indent=4)
87
+
88
+ # If run is completed, get messages
89
+ if run_status.status == 'completed':
90
+ messages = self.client.beta.threads.messages.list(
91
+ thread_id=thread.id
92
+ )
93
+ # Loop through messages and print content based on role
94
+ for msg in messages.data:
95
+ role = msg.role
96
+ content = msg.content[0].text.value
97
+ print(f"{role.capitalize()}: {content}")
98
+ answer = f"{role.capitalize()}: {content}"
99
+ break
100
+ break
101
+ elif run_status.status == 'requires_action':
102
+ # print("Function Calling")
103
+ required_actions = run_status.required_action.submit_tool_outputs.model_dump()
104
+ # print('required action test: ',required_actions)
105
+ tool_outputs = []
106
+ import json
107
+ for action in required_actions["tool_calls"]:
108
+ func_name = action['function']['name']
109
+ arguments = json.loads(action['function']['arguments'])
110
+
111
+ if func_name == "get_data":
112
+ output = self.get_data()
113
+ tool_outputs.append({
114
+ "tool_call_id": action['id'],
115
+ "output": output
116
+ })
117
+
118
+ else:
119
+ raise ValueError(f"Unknown function: {func_name}")
120
+
121
+ print("Submitting outputs back to the Assistant...")
122
+ self.client.beta.threads.runs.submit_tool_outputs(
123
+ thread_id=thread.id,
124
+ run_id=run.id,
125
+ tool_outputs=tool_outputs
126
+ )
127
+ else:
128
+ print("Waiting for the Assistant to process...")
129
+ time.sleep(5)
130
+
131
+ if answer is not None:
132
+ print(f'this is my answer : ', answer)
133
+ return answer