Skip to content

Instantly share code, notes, and snippets.

@neerajks77
Created September 22, 2023 14:10
Show Gist options
  • Save neerajks77/ef82604f7e27c432a41cb9ad627c9050 to your computer and use it in GitHub Desktop.
Save neerajks77/ef82604f7e27c432a41cb9ad627c9050 to your computer and use it in GitHub Desktop.
Updated Function Calling.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyP60hQJvOY4f73i35exCPbH",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/neerajks77/ef82604f7e27c432a41cb9ad627c9050/updated-function-calling.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "QEiEdJ5kmyAd"
},
"outputs": [],
"source": [
"!pip install openai\n",
"!pip install python-dotenv\n",
"!pip install requests"
]
},
{
"cell_type": "code",
"source": [
"import openai\n",
"import json\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"import os\n",
"import requests\n",
"from flask import Flask, jsonify, make_response"
],
"metadata": {
"id": "5S17sxdxSCt7"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"OPENAI_API_KEY= \"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\"\n",
"openai.api_key = OPENAI_API_KEY\n",
"openai.organization = \"org-GUYW20sXDhuEummGG82g2qxS\""
],
"metadata": {
"id": "3NLcMoTZSb-x"
},
"execution_count": 23,
"outputs": []
},
{
"cell_type": "code",
"source": [
"message = []\n",
"response = ''\n",
"system_message = f\"\"\"\n",
" You are a customer service assistant for cloud operations managed services. \\\n",
" Respond in a friendly and helpful tone, with concise answers in not more than 200 words. \\\n",
" Please provide sources for the answer, and also provide url sources. \\\n",
" Don't make assumptions about what values to use with parameters inside function_descriptions. \\\n",
" Ask for clarification if a user request is ambiguous and the required parameter values are not provided.\n",
" \"\"\"\n",
"message = [{\"role\": \"system\", \"content\": system_message}]\n",
"\n",
"function_descriptions = [\n",
" {\n",
" \"name\": \"set_mailbox_quota\",\n",
" \"description\": \"\"\"\n",
" Raise a service request for setting the mailbox quota of a user on exchange online with the following parameters -\n",
" email id, issue warning quota, prohibit email send quota, and prohibit email receive quota.\n",
" \"\"\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"emailid\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The email id of the user whose mailbox quota is to be changed\",\n",
" },\n",
" \"issue_warning_quota\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The mailbox size limit at which a quota warning notification is sent to the user\",\n",
" },\n",
" \"prohibit_send_quota\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The mailbox size limit beyond which the user cannot send emails\",\n",
" },\n",
" \"prohibit_receive_quota\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The mailbox size limit beyond which the user cannot receive emails\",\n",
" }\n",
" },\n",
" \"required\": [\"emailid\", \"issue_warning_quota\", \"prohibit_send_quota\", \"prohibit_receive_quota\"],\n",
" }\n",
" },\n",
" {\n",
" \"name\": \"get_shared_mailbox\",\n",
" \"description\": \"\"\"\n",
" Raise a shared mailbox service request with the following parameters -\n",
" Shared Mailbox name, mailbox alias, email id, permissions for full access, send as, and Calendar Access.\n",
" \"\"\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"shared_mailbox_name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The name of the shared mailbox\", #e.g., Employee Onboarding, Payroll Processing, Trainings\",\n",
" },\n",
" \"alias\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The required alias for the shared mailbox\", #e.g., Onboardings, Payroll, trainings\",\n",
" },\n",
" \"email_id\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The required email id for the shared mailbox\",\n",
" },\n",
" \"owner\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Email id of the intended owner for the shared mailbox\",\n",
" },\n",
" \"full_access\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Is full access required for the shared mailbox\", # \"e.g., yes, no\",\n",
" \"enum\": [\"yes\", \"no\"]\n",
" },\n",
" \"send_as\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Is send as required for the shared mailbox\", #e.g., yes, no\",\n",
" \"enum\": [\"yes\", \"no\"]\n",
" },\n",
" \"calendar_access\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Is calendar access required for the shared mailbox\", # e.g., yes, no\",\n",
" \"enum\": [\"yes\", \"no\"]\n",
" }\n",
" },\n",
" \"required\": [\"shared_mailbox_name\", \"alias\", \"email_id\", \"owner\", \"full_access\", \"send_as\", \"calendar_access\"],\n",
" }\n",
" }\n",
" ]"
],
"metadata": {
"id": "WmAm9EgbUCqs"
},
"execution_count": 24,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def set_mailbox_quota(emailid, issue_warning_quota, prohibit_send_quota, prohibit_receive_quota):\n",
" \"\"\"Set mailbox quota for the user\"\"\"\n",
" mailbox_quota = {\n",
" \"emailid\": emailid,\n",
" \"issue_warning_quota\": issue_warning_quota,\n",
" \"prohibit_send_quota\": prohibit_send_quota ,\n",
" \"prohibit_receive_quota\": prohibit_receive_quota,\n",
" }\n",
" response = requests.post(\n",
" \"https://6779d4d5-f434-47f3-a2e7-dc378700a4a9.webhook.cid.azure-automation.net/webhooks?token=NzsHCC%2f%2fV%2bI358Ly%2b%2foTeMHI%2f6H8HzBJi5e4UbvVIkM%3d\",\n",
" data = json.dumps(mailbox_quota)\n",
" )\n",
" print(response)\n",
" return response"
],
"metadata": {
"id": "GG4hP7O5an3B"
},
"execution_count": 25,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def get_shared_mailbox(shared_mailbox_name, alias, email_id, owner, full_access, send_as, calendar_access):\n",
" \"\"\"Create a shared mailbox with the users input\"\"\"\n",
" shared_mailbox = {\n",
" \"shared_mailbox_name\": shared_mailbox_name,\n",
" \"alias\": alias,\n",
" \"email_id\": email_id,\n",
" \"owner\": owner,\n",
" \"full_access\": full_access,\n",
" \"send_as\": send_as,\n",
" \"calendar_access\": calendar_access\n",
" }\n",
" response = requests.post(\n",
" \"https://d64986a4-a285-4f1a-b77c-8b7f87bed388.webhook.eus.azure-automation.net/webhooks?token=7i3NRlFtavoPKWPvh7%2fPhw1bbtWpPGSvIt3WPW1HZH8%3d\",\n",
" data = json.dumps(shared_mailbox)\n",
" )\n",
" print(response)\n",
" return response"
],
"metadata": {
"id": "-RCtmIEobCH3"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def get_results():\n",
" # Step 2: Answer the user question\n",
" response = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=message,\n",
" functions=function_descriptions,\n",
" function_call=\"auto\", # auto is default\n",
" )\n",
" responsemessage = response.choices[0].message\n",
" if \"content\" in responsemessage and \"function_call\" not in responsemessage:\n",
" message.append(\n",
" {\n",
" \"role\": responsemessage[\"role\"],\n",
" \"content\": responsemessage[\"content\"],\n",
" }\n",
" )\n",
" return responsemessage\n",
" else:\n",
" return finalprocess(responsemessage)"
],
"metadata": {
"id": "xkhjUOzDWded"
},
"execution_count": 19,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def finalprocess(response):\n",
" if response.get(\"function_call\"):\n",
" function_name = str(response[\"function_call\"][\"name\"]) #eval(firstoutput.function_call.name)\n",
"\n",
" available_functions = {\n",
" \"get_shared_mailbox\": get_shared_mailbox,\n",
" \"set_mailbox_quota\": set_mailbox_quota\n",
" }\n",
"\n",
" function_to_call = available_functions[function_name]\n",
" function_args = json.loads(response[\"function_call\"][\"arguments\"])\n",
" function_response = function_to_call(**function_args)\n",
" message.append( # extend conversation with assistant's reply\n",
" {\n",
" \"role\": response[\"role\"],\n",
" \"name\": response[\"function_call\"][\"name\"],\n",
" \"content\": response[\"function_call\"][\"arguments\"],\n",
" }\n",
" )\n",
" message.append(\n",
" {\n",
" \"role\": \"function\",\n",
" \"name\": function_name,\n",
" \"content\": json.dumps(str(function_response)),\n",
" }\n",
" ) # extend conversation with function response\n",
"\n",
" second_response = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages = message,\n",
" )\n",
" print(second_response[\"choices\"][0][\"message\"])\n",
" return second_response[\"choices\"][0][\"message\"]"
],
"metadata": {
"id": "nqtxnJyOlEvN"
},
"execution_count": 26,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#query = \"I want to raise a service request for setting the mailbox quota for a user\"\n",
"#query = \"Email id of the user is neeraj@azure-training.com\"\n",
"#query = \"Issue warning quota at 51 GB, Prohibit send quota at 52 GB, and Prohibit receive quota at 53 GB\"\n",
"message.append(\n",
" {\"role\": \"user\", \"content\": query}\n",
")\n",
"\n",
"response = get_results()\n",
"print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "x1XJtDVfU4qk",
"outputId": "b877774a-b58b-441f-8104-59e3def4f5db"
},
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<Response [202]>\n",
"{\n",
" \"role\": \"assistant\",\n",
" \"content\": \"Great! I have raised a service request to set the mailbox quota for the user \\\"neeraj@azure-training.com\\\" with the following values:\\n- Issue warning quota: 51 GB\\n- Prohibit email send quota: 52 GB\\n- Prohibit email receive quota: 53 GB\\n\\nThe request has been submitted successfully. The mailbox quota will be updated accordingly. You will receive a confirmation once the changes have been implemented.\\n\\nIf you have any further questions or need assistance with anything else, feel free to ask.\"\n",
"}\n",
"{\n",
" \"role\": \"assistant\",\n",
" \"content\": \"Great! I have raised a service request to set the mailbox quota for the user \\\"neeraj@azure-training.com\\\" with the following values:\\n- Issue warning quota: 51 GB\\n- Prohibit email send quota: 52 GB\\n- Prohibit email receive quota: 53 GB\\n\\nThe request has been submitted successfully. The mailbox quota will be updated accordingly. You will receive a confirmation once the changes have been implemented.\\n\\nIf you have any further questions or need assistance with anything else, feel free to ask.\"\n",
"}\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment