box gpt cook book (#1341)

Co-authored-by: Aaron Wilkowitz <aaronwilkowitz@openai.com>
Co-authored-by: Aaron Wilkowitz <157151487+aaronwilkowitz-openai@users.noreply.github.com>
This commit is contained in:
Keelan Schule 2024-08-07 15:28:41 -07:00 committed by GitHub
parent 872a322868
commit cd91aa66c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 1003 additions and 0 deletions

View File

@ -0,0 +1,997 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GPT Action Library: Box"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This page provides an instruction & guide for developers building a GPT Action for a specific application. Before you proceed, make sure to first familiarize yourself with the following information: \n",
"- [Introduction to GPT Actions](https://platform.openai.com/docs/actions)\n",
"- [Introduction to GPT Actions Library](https://platform.openai.com/docs/actions/actions-library)\n",
"- [Example of Buliding a GPT Action from Scratch](https://platform.openai.com/docs/actions/getting-started)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This guide provides details on how to connect chatGPT with a Box.com account, the GPT requires two actions to pull data from Box. The GPT will interact with the Box API directly but requires middleware (ie Azure function) to properly format the response from Box to download and read the file contents. The azure function action is transparent to the end user, meaning the user will not need to explicity call the action. \n",
"\n",
"- Action 1 : Box API Action - Leverages the Box API to query data from Box \n",
"- Action 2 : Azure function - Formats response from Box enabling chatGPT to download the file directly from Box\n",
"\n",
"### Value + Example Business Use Cases\n",
"\n",
"Existing Box customers can leverage these guidelines to query details about files, contents of files and any metadata related. This enables a OpenAI powered analysis of any content stored in Box such as visualizing data sets and creating summaries across multiple folders and files. This GPT can access folders, files and business process data such as metadata in Box. Additionally Box admins can use this GPT action for visibility into audit trails and health checks."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Application Information"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Application Key Links"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check out these links from Box and Azure before you get started:\n",
"\n",
"**Box Action**\n",
"- Application Website: https://app.box.com \n",
"- Application API Documentation: https://developer.box.com/reference/\n",
"\n",
"</br>\n",
"\n",
"**Azure Function**\n",
"- Application Website: https://learn.microsoft.com/en-us/azure/azure-functions/\n",
"- Application API Documentation: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference/\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Application Prerequisites"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before you get started, make sure you go through the following steps in your Box environment:\n",
"- This requires a Box developer account to get started : https://developer.box.com/\n",
"- Follow the Box Developer site to create a custom app with OAuth 2.0 authentication type : https://developer.box.com/guides/getting-started/first-application/ \n",
"- Navigate to **Configuration** tab for the following values\n",
" - OAuth 2.0 Credentials (**Client ID** / **Client Secret**) You will need both of these values for the chatGPT configuration\n",
" - OAuth 2.0 **Redirect URIs** : You will fill this value in from chatGPT action configuration below\n",
" - Application scopes (**Read all files and folders in Box**, **Manage Enterprise properties**) \n",
"\n",
"You will want to keep this window open, the Redirct URIs needs to be filled in from the gpt configuration.\n",
"\n",
"\n",
"\n",
"![gpt_actions_box_boxconfig1.png.png](../../../images/gpt_actions_box_boxconfig1.png)\n",
"\n",
"</br>\n",
"\n",
"\n",
"### Middleware information : required for Action 2\n",
"\n",
"Make sure you go through the following steps in your Azure environment:\n",
"- Azure Portal with access to create Azure Function Apps and Azure Entra App Registrations\n",
"- There is a detailed section in this guide related to deploying and designing the function required to wrap the response from Box in order to view the contents of the file. Without the function the GPT will only be able to query data about the file and not the contents. Be sure to read this section after creating the first action.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ChatGPT Steps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Custom GPT Instructions "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you've created a Custom GPT, copy the text below in the Instructions panel. Have questions? Check out [Getting Started Example](https://platform.openai.com/docs/actions/getting-started) to see how this step works in more detail."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"**context** \n",
"\n",
"This GPT will connect to your Box.com account to search files and folders, providing accurate and helpful responses based on the user's queries. It will assist with finding, organizing, and retrieving information stored in Box.com. Ensure secure and private handling of any accessed data. Avoid performing any actions that could modify or delete files unless explicitly instructed. Prioritize clarity and efficiency in responses. Use simple language for ease of understanding. Ask for clarification if a request is ambiguous or if additional details are needed to perform a search. Maintain a professional and friendly tone, ensuring users feel comfortable and supported.\n",
"\n",
"\n",
"Please use this website for instructions using the box API : https://developer.box.com/reference/ each endpoint can be found from this reference documentation\n",
"\n",
"Users can search with the Box search endpoint or Box metadata search endpoint\n",
"\n",
"**instructions**\n",
"When retrieving file information from Box provide as much details as possible and format into a table when more than one file is returned, include the modified date, created date and any other headers you might find valuable\n",
"\n",
"Provide insights to files and suggest patterns for users, gives example queries and suggestions when appropriate\n",
"\n",
"When a user wants to compare files retrieve the file for the user with out asking"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Action 1 : Box API Action"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you've created a Custom GPT, you will need to create 2 actions. Copy the text below in the 1st Actions panel, this will be for the Box action. Have questions? Check out [Getting Started Example](https://platform.openai.com/docs/actions/getting-started) to see how this step works in more detail."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "yaml"
}
},
"outputs": [],
"source": [
"{\n",
" \"openapi\": \"3.1.0\",\n",
" \"info\": {\n",
" \"title\": \"Box.com API\",\n",
" \"description\": \"API for Box.com services\",\n",
" \"version\": \"v1.0.0\"\n",
" },\n",
" \"servers\": [\n",
" {\n",
" \"url\": \"https://api.box.com/2.0\"\n",
" }\n",
" ],\n",
" \"paths\": {\n",
" \"/folders/{folder_id}\": {\n",
" \"get\": {\n",
" \"summary\": \"Get Folder Items\",\n",
" \"operationId\": \"getFolderItems\",\n",
" \"parameters\": [\n",
" {\n",
" \"name\": \"folder_id\",\n",
" \"in\": \"path\",\n",
" \"required\": true,\n",
" \"schema\": {\n",
" \"type\": \"string\"\n",
" },\n",
" \"description\": \"The ID of the folder\"\n",
" }\n",
" ],\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"A list of items in the folder\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/FolderItems\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"read:folders\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"/files/{file_id}\": {\n",
" \"get\": {\n",
" \"summary\": \"Get File Information\",\n",
" \"operationId\": \"getFileInfo\",\n",
" \"parameters\": [\n",
" {\n",
" \"name\": \"file_id\",\n",
" \"in\": \"path\",\n",
" \"required\": true,\n",
" \"schema\": {\n",
" \"type\": \"string\"\n",
" },\n",
" \"description\": \"The ID of the file\"\n",
" }\n",
" ],\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"File information\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/FileInfo\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"read:files\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"/folders\": {\n",
" \"get\": {\n",
" \"summary\": \"List All Folders\",\n",
" \"operationId\": \"listAllFolders\",\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"A list of all folders\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/FoldersList\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"read:folders\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"/events\": {\n",
" \"get\": {\n",
" \"summary\": \"Get User Events\",\n",
" \"operationId\": \"getUserEvents\",\n",
" \"parameters\": [\n",
" {\n",
" \"name\": \"stream_type\",\n",
" \"in\": \"query\",\n",
" \"required\": true,\n",
" \"schema\": {\n",
" \"type\": \"string\"\n",
" },\n",
" \"description\": \"The type of stream\"\n",
" }\n",
" ],\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"User events\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/UserEvents\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"read:events\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"/admin_events\": {\n",
" \"get\": {\n",
" \"summary\": \"Get Admin Events\",\n",
" \"operationId\": \"getAdminEvents\",\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"Admin events\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/AdminEvents\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"read:events\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"/search\": {\n",
" \"get\": {\n",
" \"summary\": \"Search\",\n",
" \"operationId\": \"search\",\n",
" \"parameters\": [\n",
" {\n",
" \"name\": \"query\",\n",
" \"in\": \"query\",\n",
" \"required\": true,\n",
" \"schema\": {\n",
" \"type\": \"string\"\n",
" },\n",
" \"description\": \"Search query\"\n",
" }\n",
" ],\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"Search results\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/SearchResults\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"search:items\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"/metadata_templates\": {\n",
" \"get\": {\n",
" \"summary\": \"Get Metadata Templates\",\n",
" \"operationId\": \"getMetadataTemplates\",\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"Metadata templates\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/MetadataTemplates\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"read:metadata_templates\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"/metadata_templates/enterprise\": {\n",
" \"get\": {\n",
" \"summary\": \"Get Enterprise Metadata Templates\",\n",
" \"operationId\": \"getEnterpriseMetadataTemplates\",\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"Enterprise metadata templates\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/MetadataTemplates\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"read:metadata_templates\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"/files/{file_id}/metadata\": {\n",
" \"get\": {\n",
" \"summary\": \"Get All Metadata for a File\",\n",
" \"operationId\": \"getAllMetadataForFile\",\n",
" \"parameters\": [\n",
" {\n",
" \"name\": \"file_id\",\n",
" \"in\": \"path\",\n",
" \"required\": true,\n",
" \"schema\": {\n",
" \"type\": \"string\"\n",
" },\n",
" \"description\": \"The ID of the file\"\n",
" }\n",
" ],\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"All metadata instances for the file\",\n",
" \"content\": {\n",
" \"application/json\": {\n",
" \"schema\": {\n",
" \"$ref\": \"#/components/schemas/MetadataInstances\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"security\": [\n",
" {\n",
" \"OAuth2\": [\n",
" \"read:metadata\"\n",
" ]\n",
" }\n",
" ]\n",
" }\n",
" }\n",
" },\n",
" \"components\": {\n",
" \"schemas\": {\n",
" \"FolderItems\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"total_count\": {\n",
" \"type\": \"integer\",\n",
" \"description\": \"The total number of items in the folder\"\n",
" },\n",
" \"entries\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"type\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The type of the item (e.g., file, folder)\"\n",
" },\n",
" \"id\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The ID of the item\"\n",
" },\n",
" \"name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The name of the item\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"FileInfo\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"id\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The ID of the file\"\n",
" },\n",
" \"name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The name of the file\"\n",
" },\n",
" \"size\": {\n",
" \"type\": \"integer\",\n",
" \"description\": \"The size of the file in bytes\"\n",
" },\n",
" \"created_at\": {\n",
" \"type\": \"string\",\n",
" \"format\": \"date-time\",\n",
" \"description\": \"The creation time of the file\"\n",
" },\n",
" \"modified_at\": {\n",
" \"type\": \"string\",\n",
" \"format\": \"date-time\",\n",
" \"description\": \"The last modification time of the file\"\n",
" }\n",
" }\n",
" },\n",
" \"FoldersList\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"id\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The ID of the folder\"\n",
" },\n",
" \"name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The name of the folder\"\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"UserEvents\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"entries\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"event_id\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The ID of the event\"\n",
" },\n",
" \"event_type\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The type of the event\"\n",
" },\n",
" \"created_at\": {\n",
" \"type\": \"string\",\n",
" \"format\": \"date-time\",\n",
" \"description\": \"The time the event occurred\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"AdminEvents\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"entries\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"event_id\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The ID of the event\"\n",
" },\n",
" \"event_type\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The type of the event\"\n",
" },\n",
" \"created_at\": {\n",
" \"type\": \"string\",\n",
" \"format\": \"date-time\",\n",
" \"description\": \"The time the event occurred\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"SearchResults\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"total_count\": {\n",
" \"type\": \"integer\",\n",
" \"description\": \"The total number of search results\"\n",
" },\n",
" \"entries\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"type\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The type of the item (e.g., file, folder)\"\n",
" },\n",
" \"id\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The ID of the item\"\n",
" },\n",
" \"name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The name of the item\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"MetadataTemplates\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"templateKey\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The key of the metadata template\"\n",
" },\n",
" \"displayName\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The display name of the metadata template\"\n",
" },\n",
" \"scope\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The scope of the metadata template\"\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"MetadataInstances\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"templateKey\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The key of the metadata template\"\n",
" },\n",
" \"type\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The type of the metadata instance\"\n",
" },\n",
" \"attributes\": {\n",
" \"type\": \"object\",\n",
" \"additionalProperties\": {\n",
" \"type\": \"string\"\n",
" },\n",
" \"description\": \"Attributes of the metadata instance\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"securitySchemes\": {\n",
" \"OAuth2\": {\n",
" \"type\": \"oauth2\",\n",
" \"flows\": {\n",
" \"authorizationCode\": {\n",
" \"authorizationUrl\": \"https://account.box.com/api/oauth2/authorize\",\n",
" \"tokenUrl\": \"https://api.box.com/oauth2/token\",\n",
" \"scopes\": {\n",
" \"read:folders\": \"Read folders\",\n",
" \"read:files\": \"Read files\",\n",
" \"search:items\": \"Search items\",\n",
" \"read:metadata\": \"Read metadata\",\n",
" \"read:metadata_templates\": \"Read metadata templates\",\n",
" \"read:events\": \"Read events\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Note : this schema above does not contain all possible API endpoints, be sure to edit the schema to produce the appropriate actions from [Box Developer documentation](https://developer.box.com)**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Authentication Instructions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below are instructions on setting up authentication with this 3rd party application. Have questions? Check out [Getting Started Example](https://platform.openai.com/docs/actions/getting-started) to see how this step works in more detail."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### In ChatGPT"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In ChatGPT, click on \"Authentication\" and choose OAuth "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"<br> ![gptactions_box_gptauth.png](../../../images/gpt_actions_box_gptauth.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**OAuth Connection**\n",
"\n",
"- Client ID - value from Box custom app you created earlier\n",
"- Client Secret - value from Box custom app you created earlier \n",
"- Authorization URL - : https://account.box.com/api/oauth2/authorize?response_type=code&client_id=[client ID from above]&redirect_uri=[use a placeholder like chat.openai.com/aip//oauth/callback for now, youll update this later when you create the Action in ChatGPT]\n",
"- Token URL : https:api.box.com/oauth2/token\n",
"</br>\n",
"</br>\n",
"You need to save the configuration and navigate back to the gpt Configuration tab to copy the Callback URL, edit the configuration for the Box action Authorization URL and format the URL as https://account.box.com/api/oauth2/authorize?response_type=code&client_id=[client_ID]&redirect_uri=[callBack URL]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Post-Action Steps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Update the Box.com custom application\n",
"</br>\n",
"- Copy the CallBack URL from the gpt and add a OAuth 2.0 Redirect URIs in Box.com\n",
"</br>\n",
"</br>\n",
"![gpt_actions_box_boxconfig1.png.png](../../../images/gpt_actions_box_boxconfig1.png)\n",
"\n",
"</br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Action 2 : Azure Function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have the GPT created and authenticating against Box.com, we can create the azure function to handle the response formatting enabling the GPT to download the files from Box. \n",
"\n",
"Follow this [Azure Cookbook Guide](https://cookbook.openai.com/examples/azure/functions) for further details deploying an Azure function. Below you will find sample code to add to the function. \n",
"\n",
"This code is meant to be directional - while it should work out of the box, it is designed to be customized to your need.</br>\n",
"\n",
"</br>\n",
"\n",
"**Data flow**\n",
"\n",
"![gpt_actions_box_azureflow.png](../../../images/gpt_actions_box_azuredataflow.png)\n",
"\n",
"</br></br>\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that you have the azure function created, add the sample code below:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"function_app.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "yaml"
}
},
"outputs": [],
"source": [
"import azure.functions as func\n",
"from boxsdk import Client, JWTAuth\n",
"\n",
"import requests\n",
"import base64\n",
"import json\n",
"import jwt\n",
"import logging\n",
"\n",
"app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)\n",
"\n",
"logger = logging.getLogger(__name__)\n",
"logger.setLevel(logging.INFO)\n",
"\n",
"@app.route(route=\"box_retrieval\")\n",
"def box_retrieval(req: func.HttpRequest) -> func.HttpResponse:\n",
" logger.info('Starting box_retrieval function')\n",
" file_ids = req.params.get('file_ids')\n",
" auth_header = req.headers.get('Authorization')\n",
"\n",
" if not file_ids or not auth_header:\n",
" logger.error('Missing file_ids or Authorization header')\n",
" return func.HttpResponse(\n",
" \"Missing file_id or Authorization header.\",\n",
" status_code=400\n",
" )\n",
" \n",
" file_ids = file_ids.split(\",\") # Assuming file_ids are passed as a comma-separated string\n",
" if len(file_ids) == 0 or len(file_ids) > 10:\n",
" logger.error('file_ids list is empty or contains more than 10 IDs')\n",
" return func.HttpResponse(\n",
" \"file_ids list is empty or contains more than 10 IDs.\",\n",
" status_code=400\n",
" )\n",
"\n",
" try:\n",
" # Decode JWT to extract the email\n",
" token = auth_header.split(\" \")[1]\n",
" decoded_token = jwt.decode(token, options={\"verify_signature\": False})\n",
" upn = decoded_token['upn']\n",
" user_email = get_user_mapping(upn)\n",
" logger.info(f'User email extracted: {user_email}')\n",
"\n",
" config = JWTAuth.from_settings_file('jwt_config.json')\n",
" sdk = Client(config)\n",
" logger.info('Authenticated with Box API')\n",
"\n",
" # Use the user email to get the user ID\n",
" users = sdk.users(filter_term=user_email)\n",
" user = next(users)\n",
" user_id = user.id\n",
" logger.info(f'User ID obtained: {user_id}')\n",
"\n",
" openai_file_responses = []\n",
" for file_id in file_ids:\n",
" # Perform as_user call to get the file representation\n",
" my_file = sdk.as_user(user).file(file_id).get()\n",
" file_url = my_file.get_download_url()\n",
" openai_file_responses.append(file_url)\n",
" \n",
" response_body = json.dumps({'openaiFileResponse': openai_file_responses})\n",
"\n",
" return func.HttpResponse(\n",
" response_body,\n",
" status_code=200,\n",
" mimetype=\"application/json\"\n",
" )\n",
"\n",
" except Exception as e:\n",
" return func.HttpResponse(\n",
" f\"An error occurred: {str(e)}\",\n",
" status_code=500\n",
" )\n",
" \n",
"def get_user_mapping(upn):\n",
" # In our case, the user's authentication email into Azure AD is the same as their email in Box\n",
" # If that is not the case, map the email in Box to the email in Azure AD\n",
" return upn"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"jwt_config.json.sample"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "yaml"
}
},
"outputs": [],
"source": [
"{\n",
" \"boxAppSettings\": {\n",
" \"clientID\": \"12345\",\n",
" \"clientSecret\": \"abcde\",\n",
" \"appAuth\": {\n",
" \"publicKeyID\": \"123\",\n",
" \"privateKey\": \"-----BEGIN ENCRYPTED PRIVATE KEY-----\\nvwxyz==\\n-----END ENCRYPTED PRIVATE KEY-----\\n\",\n",
" \"passphrase\": \"lmnop\"\n",
" }\n",
" },\n",
" \"enterpriseID\": \"09876\"\n",
" }"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"requirements.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "yaml"
}
},
"outputs": [],
"source": [
"boxsdk[jwt]\n",
"azure-functions\n",
"requests\n",
"pyjwt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make sure to follow the rest of the Azure guide for post authentication steps and chatGPT configuration : [Azure Cookbook Guide](https://cookbook.openai.com/examples/azure/functions)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### FAQ & Troubleshooting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- *Schema calls the wrong project or dataset:* If ChatGPT calls the wrong project or dataset, consider updating your instructions to make it more explicit either (a) which project / dataset should be called or (b) to require the user provide those exact details before it runs the query\n",
"- Box can return a large set of data in the event stream which can cause errors, "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Are there integrations that youd like us to prioritize? Are there errors in our integrations? File a PR or issue in our github, and well take a look.*\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

View File

@ -1453,6 +1453,12 @@
date: 2024-07-31
authors:
- evanweiss-openai
- title: GPT Actions library - Box
path: examples/chatgpt/gpt_actions_library/gpt_action_box.ipynb
date: 2024-08-02
authors:
- keelan-openai
tags:
- gpt-actions-library
- chatgpt