Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from pydantic.v1 import BaseModel, Field
|
| 6 |
+
from langchain_openai import ChatOpenAI
|
| 7 |
+
from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser
|
| 8 |
+
from langchain.agents import AgentExecutor, create_openai_functions_agent
|
| 9 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 10 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
| 11 |
+
from langchain.tools import StructuredTool
|
| 12 |
+
|
| 13 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 14 |
+
|
| 15 |
+
def repo_get_all_employees_from_database():
|
| 16 |
+
url = "https://api.airtable.com/v0/appopGmlHujYnd6Vw/Interviewers?maxRecords=100&view=Grid%20view"
|
| 17 |
+
headers = {
|
| 18 |
+
"Authorization": os.getenv("DB_AUTH_TOKEN")
|
| 19 |
+
}
|
| 20 |
+
response = requests.get(url, headers=headers)
|
| 21 |
+
records = response.json()
|
| 22 |
+
records_list = records['records']
|
| 23 |
+
employees_list = []
|
| 24 |
+
for record in records_list:
|
| 25 |
+
employee = record["fields"]
|
| 26 |
+
employees_list.append(employee)
|
| 27 |
+
|
| 28 |
+
return employees_list
|
| 29 |
+
|
| 30 |
+
def get_all_employees() -> str:
|
| 31 |
+
"""
|
| 32 |
+
A function to get a list of all employees from database.
|
| 33 |
+
Returns:
|
| 34 |
+
str: A list of all employees in json.
|
| 35 |
+
"""
|
| 36 |
+
return repo_get_all_employees_from_database()
|
| 37 |
+
|
| 38 |
+
def get_employees(number_of_employees: int, start_time: str, duration_hours: int) -> str:
|
| 39 |
+
"""
|
| 40 |
+
A function to get a required number_of_employees that are available from tart_time during specified duration_hours.
|
| 41 |
+
Args:
|
| 42 |
+
number_of_employees (int): Required number of employees.
|
| 43 |
+
start_time (str): Required start time.
|
| 44 |
+
duration_hours (int): Required duration of the availability in hours.
|
| 45 |
+
Returns:
|
| 46 |
+
str: Employees list in json.
|
| 47 |
+
"""
|
| 48 |
+
method_return_value_stub = '''
|
| 49 |
+
{
|
| 50 |
+
"employees": [
|
| 51 |
+
{
|
| 52 |
+
"id": 100,
|
| 53 |
+
"name": "Lana Kane"
|
| 54 |
+
}
|
| 55 |
+
]
|
| 56 |
+
}
|
| 57 |
+
'''
|
| 58 |
+
return method_return_value_stub
|
| 59 |
+
|
| 60 |
+
class GetAllEmployees(BaseModel):
|
| 61 |
+
"""
|
| 62 |
+
Pydantic arguments schema for get_all_employees function
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
class GetEmployees(BaseModel):
|
| 66 |
+
"""
|
| 67 |
+
Pydantic arguments schema for get_employees function
|
| 68 |
+
"""
|
| 69 |
+
number_of_employees: int = Field(..., description="Required number of employees")
|
| 70 |
+
start_time: str = Field(..., description="Required start time")
|
| 71 |
+
duration_hours: int = Field(..., description="Required duration of the availability in hours")
|
| 72 |
+
|
| 73 |
+
llm = ChatOpenAI(temperature=1.0, model_name="gpt-3.5-turbo", openai_api_key=OPENAI_API_KEY)
|
| 74 |
+
|
| 75 |
+
tools = [
|
| 76 |
+
StructuredTool.from_function(
|
| 77 |
+
func=get_all_employees,
|
| 78 |
+
args_schema=GetAllEmployees,
|
| 79 |
+
description="A function to get a list of all employees from database."
|
| 80 |
+
),
|
| 81 |
+
StructuredTool.from_function(
|
| 82 |
+
func=get_employees,
|
| 83 |
+
args_schema=GetEmployees,
|
| 84 |
+
description="A function to get a required number_of_employees that are available from tart_time during specified duration_hours."
|
| 85 |
+
)
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
system_content = """
|
| 89 |
+
You are an AI Interview Team Assistant that is developed by "Godel Technologies Europe" corporation.
|
| 90 |
+
You help to choose employees who can interview newcomers.
|
| 91 |
+
For this you select employees that are correspond to request parameters.
|
| 92 |
+
You select employees from the data that is stored in json format.
|
| 93 |
+
You always strictly and directly follow all instructions from the user.
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
def predict(message, history):
|
| 97 |
+
chat_history = []
|
| 98 |
+
|
| 99 |
+
for human, assistant in history:
|
| 100 |
+
chat_history.extend([HumanMessage(content=human), AIMessage(content=assistant)])
|
| 101 |
+
|
| 102 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 103 |
+
[
|
| 104 |
+
("system", system_content),
|
| 105 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 106 |
+
("user", message),
|
| 107 |
+
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 108 |
+
]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
agent = create_openai_functions_agent(llm, tools, prompt)
|
| 112 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
| 113 |
+
gpt_response = agent_executor.invoke({"input": message, "chat_history": chat_history})
|
| 114 |
+
gpt_output = gpt_response["output"]
|
| 115 |
+
chat_history.extend([HumanMessage(content=message), AIMessage(content=gpt_output)])
|
| 116 |
+
return gpt_output
|
| 117 |
+
|
| 118 |
+
examples = [
|
| 119 |
+
"Who are you?",
|
| 120 |
+
"What is your purpose?",
|
| 121 |
+
"List all employees",
|
| 122 |
+
"I need 1 employee in given time slot: start time is March 11 2024 2 pm, duration 1 hour"
|
| 123 |
+
]
|
| 124 |
+
|
| 125 |
+
description = '''
|
| 126 |
+
# AI Interview Team Assistant | Empowered by Godel Technologies AI \n
|
| 127 |
+
\n
|
| 128 |
+
This is an AI Interview Team Assistant. You can ask him any questions about recruiting a team for an interview.\n
|
| 129 |
+
'''
|
| 130 |
+
|
| 131 |
+
gr.ChatInterface(predict, examples=examples, description=description).launch()
|