pazukdev commited on
Commit
cf93143
·
verified ·
1 Parent(s): 30ae1f6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ from datetime import datetime, timedelta
3
+ import gradio as gr
4
+ import os
5
+ import re
6
+ import requests
7
+
8
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
9
+
10
+ switch_to = "Switch to {model}"
11
+ switched_to = "Switched to: {model}"
12
+
13
+ gpt3_turbo = "gpt-3.5-turbo"
14
+ gpt4 = "gpt-4"
15
+ gpt4_turbo = "gpt-4-turbo-preview"
16
+
17
+ model = gpt3_turbo
18
+
19
+ def repo_get_all_employees_from_database():
20
+ url = "https://api.airtable.com/v0/appopGmlHujYnd6Vw/Interviewers?maxRecords=100&view=Grid%20view"
21
+ headers = {
22
+ "Authorization": os.getenv("DB_AUTH_TOKEN")
23
+ }
24
+ response = requests.get(url, headers=headers)
25
+ records = response.json()
26
+ records_list = records['records']
27
+ employees_list = []
28
+ for record in records_list:
29
+ employee = record["fields"]
30
+ employees_list.append(employee)
31
+
32
+ return employees_list
33
+
34
+ def predict(message, history):
35
+ history_openai_format = []
36
+ system_content = """
37
+ You are an AI Interview Team Assistant that is developed by "Godel Technologies Europe" corporation.
38
+ You help build teams to interview newcomers.
39
+ For this you select employees that are correspond to request parameters.
40
+ You select employees from the data that is stored in json format.
41
+ You always strictly and directly follow all instructions from the user.
42
+ E.g. if user asks to switch to gpt-3.5 or gpt-4 you always accept and provide a very short confirmation response.
43
+ """
44
+ history_openai_format.append({"role": "system", "content": system_content})
45
+ pattern = r"For conducting an interview I need (\d+) employee.*start time is (.*), duration (\d+) hour"
46
+ data = repo_get_all_employees_from_database()
47
+
48
+ prompt = '''
49
+ {data}
50
+ ###
51
+ Above is employees data in json format.
52
+ {message}
53
+ '''.format(data=data, message=message)
54
+
55
+ match = re.search(pattern, message)
56
+ if match:
57
+ num_employees = int(match.group(1))
58
+ duration = int(match.group(3))
59
+ start_time = datetime.strptime(match.group(2), "%B %d %Y %I %p")
60
+ end_time = end_time = start_time + timedelta(hours=duration)
61
+
62
+ date_time = '''
63
+ "start_date_time": "{start_time}", "end_date_time": "{end_time}"
64
+ '''.format(start_time=start_time, end_time=end_time)
65
+
66
+ prompt = '''
67
+ {data}
68
+ ###
69
+ Above is employees data in json format.
70
+ Please choose {num_employees} employee with the lowest "interviews_conducted" value but whose "busy_dat_time_slots" doesn't contain the "given_date_time_slot" which is: {date_time}.
71
+ You should NOT output any Python code.
72
+ Lets think step-by-step:
73
+ 1. Remove the employees whose "busy_date_time_slots" CONTAINS the "given_date_time_slot" specified above. Provide a list of names of remaining employees.
74
+ 2. Double check your filtration. It's very important NOT to include into the remained employees list an employee whose "busy_date_time_slots" CONTAINS the "given_date_time_slot" . Type a "given_date_time_slot" value and then check that no one of remaining employees has no "given_date_time_slot" value in "busy_dat_time_slots". If someone contains - replase him.
75
+ 3. Provide a list of names of remaining employees along with their "interviews_conducted" values and choose {num_employees} employee with the lowest "interviews_conducted" value.
76
+ 4. Check previous step if you really chose an employee with the lowest "interviews_conducted" value.
77
+ 5. At the end print ids and names of finally selected employees in json format. Please remember that in your output should be maximum {num_employees} employee.
78
+ '''.format(data=data, date_time=date_time, num_employees=num_employees)
79
+
80
+ for human, assistant in history:
81
+ history_openai_format.append({"role": "user", "content": human })
82
+ history_openai_format.append({"role": "assistant", "content": assistant})
83
+ history_openai_format.append({"role": "user", "content": prompt})
84
+
85
+ global model
86
+ switched = False
87
+
88
+ if (switch_to.format(model=gpt3_turbo).lower() in message.lower()):
89
+ model = gpt3_turbo
90
+ switched = True
91
+
92
+ if (switch_to.format(model=gpt4).lower() in message.lower()):
93
+ model = gpt4
94
+ switched = True
95
+
96
+ if (switch_to.format(model=gpt4_turbo).lower() in message.lower()):
97
+ model = gpt4_turbo
98
+ switched = True
99
+
100
+ if (switched):
101
+ print(switched_to.format(model=model))
102
+
103
+ response = client.chat.completions.create(
104
+ # model=model, # gpt-4 and gpt-4-turbo-preview are temporarily disabled to save money
105
+ model=gpt3_turbo,
106
+ messages= history_openai_format,
107
+ temperature=0,
108
+ stream=True)
109
+
110
+ partial_message = "🤖 {model}:\n\n".format(model=model)
111
+ for chunk in response:
112
+ if chunk.choices[0].delta.content is not None:
113
+ partial_message = partial_message + chunk.choices[0].delta.content
114
+ yield partial_message
115
+
116
+ pre_configured_promt = "For conducting an interview I need 1 employee in given time slot: start time is March 11 2024 2 pm, duration 1 hour"
117
+
118
+ description = '''
119
+ # AI Interview Team Assistant | Empowered by Godel Technologies AI \n
120
+ \n
121
+ This is an AI Interview Team Assistant. You can ask him any questions about recruiting a team for an interview.\n
122
+ \n
123
+ You can send any regular prompts you wish or pre-configured Chain-of-Thought prompts.\n
124
+ To trigger pre-configured prompt you have to craft a prompt with next structure:
125
+ - "{pre_configured_promt}"
126
+ \n
127
+ You can switch between gpt-3.5-turbo | gpt-4 | gpt-4-turbo with prompts listed in "Examples".
128
+ '''.format(pre_configured_promt=pre_configured_promt)
129
+
130
+ examples = [
131
+ "Who are you?",
132
+ "What is your purpose?",
133
+ "List all employees",
134
+ switch_to.format(model=gpt3_turbo),
135
+ switch_to.format(model=gpt4),
136
+ switch_to.format(model=gpt4_turbo),
137
+ pre_configured_promt
138
+ ]
139
+
140
+ gr.ChatInterface(predict, examples=examples, description=description).launch()