Functions.Templates/Templates-v2/OpenAI-Chat-Python/blueprint_body.py (27 lines of code) (raw):
@$(BLUEPRINT_FILENAME).function_name("CreateChatBot")
@$(BLUEPRINT_FILENAME).route(route="chats/{chatID}", methods=["PUT"], auth_level=func.AuthLevel.$(AUTHLEVEL_INPUT))
@$(BLUEPRINT_FILENAME).assistant_create_output(arg_name="requests")
def create_chat_bot(req: func.HttpRequest, requests: func.Out[str]) -> func.HttpResponse:
chatID = req.route_params.get("chatID")
input_json = req.get_json()
logging.info(
f"Creating chat ${chatID} from input parameters ${json.dumps(input_json)}")
create_request = {
"id": chatID,
"instructions": input_json.get("instructions")
}
requests.set(json.dumps(create_request))
response_json = {"chatId": chatID}
return func.HttpResponse(json.dumps(response_json), status_code=202, mimetype="$(BLUEPRINT_FILENAME)lication/json")
@$(BLUEPRINT_FILENAME).function_name("GetChatState")
@$(BLUEPRINT_FILENAME).route(route="chats/{chatID}", methods=["GET"], auth_level=func.AuthLevel.$(AUTHLEVEL_INPUT))
@$(BLUEPRINT_FILENAME).assistant_query_input(arg_name="state", id="{chatID}", timestamp_utc="{Query.timestampUTC}")
def get_chat_state(req: func.HttpRequest, state: str) -> func.HttpResponse:
return func.HttpResponse(state, status_code=200, mimetype="$(BLUEPRINT_FILENAME)lication/json")
@$(BLUEPRINT_FILENAME).function_name("PostUserResponse")
@$(BLUEPRINT_FILENAME).route(route="chats/{chatID}", methods=["POST"], auth_level=func.AuthLevel.$(AUTHLEVEL_INPUT))
@$(BLUEPRINT_FILENAME).assistant_post_input(arg_name="state", id="{chatID}", user_message="{Query.message}", model="$(CHAT_MODEL_NAME)")
def post_user_response(req: func.HttpRequest, state: str) -> func.HttpResponse:
# Parse the JSON string into a dictionary
data = json.loads(state)
# Extract the content of the recentMessage
recent_message_content = data['recentMessages'][0]['content']
return func.HttpResponse(recent_message_content, status_code=200, mimetype="text/plain")