pazukdev/ai-assistant-experimental
0
1from openai import OpenAI2from datetime import datetime, timedelta3import gradio as gr4import os5import re6import requests7 8client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))9 10switch_to = "Switch to {model}"11switched_to = "Switched to: {model}"12 13gpt3_turbo = "gpt-3.5-turbo"14gpt4 = "gpt-4"15gpt4_turbo = "gpt-4-turbo-preview"16 17def repo_get_all_employees_from_database():18 url = "https://api.airtable.com/v0/appopGmlHujYnd6Vw/Interviewers?maxRecords=100&view=Grid%20view"19 headers = {20 "Authorization": os.getenv("DB_AUTH_TOKEN")21 }22 response = requests.get(url, headers=headers)23 records = response.json()24 records_list = records['records']25 employees_list = []26 for record in records_list:27 employee = record["fields"]28 employees_list.append(employee)29 30 return employees_list31 32def predict(message, history):33 history_openai_format = []34 system_content = """35 You are an AI Interview Team Assistant that is developed by "Godel Technologies Europe" corporation.36 You help build teams to interview newcomers.37 For this you select employees that are correspond to request parameters.38 You select employees from the data that is stored in json format.39 You always strictly and directly follow all instructions from the user.40 E.g. if user asks to switch to gpt-3.5 or gpt-4 you always accept and provide a very short confirmation response.41 """42 history_openai_format.append({"role": "system", "content": system_content})43 pattern = r"For conducting an interview I need (\d+) employee.*start time is (.*), duration (\d+) hour"44 data = repo_get_all_employees_from_database()45 46 prompt = '''47 {data}48 ###49 Above is employees data in json format.50 {message}51 '''.format(data=data, message=message) 52 53 match = re.search(pattern, message)54 if match:55 num_employees = int(match.group(1))56 duration = int(match.group(3))57 start_time = datetime.strptime(match.group(2), "%B %d %Y %I %p")58 end_time = end_time = start_time + timedelta(hours=duration)59 60 date_time = '''61 "start_date_time": "{start_time}", "end_date_time": "{end_time}"62 '''.format(start_time=start_time, end_time=end_time)63 64 prompt = '''65 {data}66 ###67 Above is employees data in json format.68 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}.69 You should NOT output any Python code.70 Lets think step-by-step:71 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.72 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.73 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.74 4. Check previous step if you really chose an employee with the lowest "interviews_conducted" value.75 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.76 '''.format(data=data, date_time=date_time, num_employees=num_employees)77 78 model = gpt3_turbo79 80 for human, assistant in history:81 if (switch_to.format(model=gpt3_turbo).lower() in human.lower()):82 model = gpt3_turbo83 if (switch_to.format(model=gpt4).lower() in human.lower()):84 model = gpt4 85 if (switch_to.format(model=gpt4_turbo).lower() in human.lower()):86 model = gpt4_turbo 87 88 history_openai_format.append({"role": "user", "content": human })89 history_openai_format.append({"role": "assistant", "content": assistant})90 91 if (switch_to.format(model=gpt3_turbo).lower() in message.lower()):92 model = gpt3_turbo93 if (switch_to.format(model=gpt4).lower() in message.lower()):94 model = gpt495 if (switch_to.format(model=gpt4_turbo).lower() in message.lower()):96 model = gpt4_turbo 97 98 history_openai_format.append({"role": "user", "content": prompt})99 100 if (model != gpt3_turbo):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 money105 model=gpt3_turbo,106 messages= history_openai_format,107 temperature=0,108 stream=True)109 110 msg_header = "🤖 {model}:\n\n".format(model=model)111 partial_message = msg_header112 for chunk in response:113 if chunk.choices[0].delta.content is not None:114 partial_message = partial_message + chunk.choices[0].delta.content115 pattern = r'({msg_header})+'.format(msg_header=msg_header)116 partial_message = re.sub(pattern, msg_header, partial_message)117 yield partial_message118 119pre_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"120 121description = '''122# AI Interview Team Assistant | Empowered by Godel Technologies AI \n123\n124This is an AI Interview Team Assistant. You can ask him any questions about recruiting a team for an interview.\n125\n126You can send any regular prompts you wish or pre-configured Chain-of-Thought prompts.\n127To trigger pre-configured prompt you have to craft a prompt with next structure:128- "{pre_configured_promt}"129\n130You can switch between gpt-3.5-turbo | gpt-4 | gpt-4-turbo with prompts listed in "Examples".131'''.format(pre_configured_promt=pre_configured_promt)132 133examples = [134 "Who are you?", 135 "What is your purpose?",136 "List all employees",137 switch_to.format(model=gpt3_turbo), 138 switch_to.format(model=gpt4), 139 switch_to.format(model=gpt4_turbo), 140 pre_configured_promt141]142 143gr.ChatInterface(predict, examples=examples, description=description).launch()