plano/demos/hr_agent/main.py

104 lines
3.1 KiB
Python
Raw Permalink Normal View History

2024-10-22 17:01:23 -07:00
import os
import json
import pandas as pd
2024-10-12 17:58:35 -07:00
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
2024-10-22 17:01:23 -07:00
from typing import Optional
2024-10-12 17:58:35 -07:00
from enum import Enum
2024-10-22 17:01:23 -07:00
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
2024-10-12 17:58:35 -07:00
app = FastAPI()
2024-10-22 17:01:23 -07:00
workforce_data_df = None
2024-10-12 17:58:35 -07:00
2024-10-22 17:01:23 -07:00
with open("workforce_data.json") as file:
workforce_data = json.load(file)
workforce_data_df = pd.json_normalize(
workforce_data, record_path=["regions"], meta=["point_in_time", "satisfaction"]
)
2024-10-18 15:07:28 -07:00
2024-10-22 17:01:23 -07:00
# Define the request model
class WorkforceRequset(BaseModel):
region: str
staffing_type: str
point_in_time: Optional[int] = None
2024-10-18 15:07:28 -07:00
2024-10-22 17:01:23 -07:00
class SlackRequest(BaseModel):
slack_message: str
2024-10-12 17:58:35 -07:00
2024-10-22 17:01:23 -07:00
class WorkforceResponse(BaseModel):
2024-10-12 17:58:35 -07:00
region: str
staffing_type: str
2024-10-22 17:01:23 -07:00
headcount: int
satisfaction: float
2024-10-18 15:07:28 -07:00
2024-10-12 17:58:35 -07:00
# Post method for device summary
2024-10-22 17:01:23 -07:00
@app.post("/agent/workforce")
def get_workforce(request: WorkforceRequset):
2024-10-12 17:58:35 -07:00
"""
2024-10-22 17:01:23 -07:00
Endpoint to workforce data by region, staffing type at a given point in time.
2024-10-12 17:58:35 -07:00
"""
2024-10-22 17:01:23 -07:00
region = request.region.lower()
staffing_type = request.staffing_type.lower()
point_in_time = request.point_in_time if request.point_in_time else 0
2024-10-12 17:58:35 -07:00
response = {
2024-10-22 17:01:23 -07:00
"region": region,
"staffing_type": f"Staffing agency: {staffing_type}",
"headcount": f"Headcount: {int(workforce_data_df[(workforce_data_df['region']==region) & (workforce_data_df['point_in_time']==point_in_time)][staffing_type].values[0])}",
"satisfaction": f"Satisifaction: {float(workforce_data_df[(workforce_data_df['region']==region) & (workforce_data_df['point_in_time']==point_in_time)]['satisfaction'].values[0])}",
2024-10-18 15:07:28 -07:00
}
2024-10-12 17:58:35 -07:00
return response
2024-10-18 15:07:28 -07:00
2024-10-22 17:01:23 -07:00
@app.post("/agent/slack_message")
def send_slack_message(request: SlackRequest):
"""
Endpoint that sends slack message
"""
slack_message = request.slack_message
# Load the bot token from an environment variable or replace it directly
slack_token = os.getenv(
"SLACK_BOT_TOKEN"
) # Replace with your token if needed: 'xoxb-your-token'
client = WebClient(token=slack_token)
channel = "hr_agent_demo"
try:
# Send the message
response = client.chat_postMessage(channel=channel, text=slack_message)
return f"Message sent to {channel}: {response['message']['text']}"
except SlackApiError as e:
print(f"Error sending message: {e.response['error']}")
2024-10-12 17:58:35 -07:00
@app.post("/agent/hr_qa")
async def general_hr_qa():
"""
This method handles Q/A related to general issues in HR.
It forwards the conversation to the OpenAI client via a local proxy and returns the response.
"""
return {
"choices": [
{
"message": {
"role": "assistant",
"content": "I am a helpful HR agent, and I can help you plan for workforce related questions",
},
"finish_reason": "completed",
"index": 0,
}
],
"model": "hr_agent",
"usage": {"completion_tokens": 0},
}
2024-10-18 15:07:28 -07:00
2024-10-12 17:58:35 -07:00
if __name__ == "__main__":
app.run(debug=True)