Updating notebooks to use new Python SDK (#837)

This commit is contained in:
jhills20 2023-11-14 13:31:13 -08:00 committed by GitHub
parent a1747ab388
commit bd58636a8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 54 deletions

View file

@ -59,8 +59,11 @@
"source": [ "source": [
"import json\n", "import json\n",
"import openai\n", "import openai\n",
"from openai import OpenAI\n",
"import os\n", "import os\n",
"import requests" "import requests\n",
"\n",
"client = OpenAI()\n"
] ]
}, },
{ {
@ -141,7 +144,7 @@
" else:\n", " else:\n",
" print(f\"Google Place Details API request failed with status code {response.status_code}\")\n", " print(f\"Google Place Details API request failed with status code {response.status_code}\")\n",
" print(f\"Response content: {response.content}\")\n", " print(f\"Response content: {response.content}\")\n",
" return None" " return None\n"
] ]
}, },
{ {
@ -203,7 +206,7 @@
" return []\n", " return []\n",
" except Exception as e:\n", " except Exception as e:\n",
" print(f\"Error during the Google Places API call: {e}\")\n", " print(f\"Error during the Google Places API call: {e}\")\n",
" return []" " return []\n"
] ]
}, },
{ {
@ -225,7 +228,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -239,7 +242,7 @@
" food_preference = customer_profile.get('preferences', {}).get('food', [])[0] if customer_profile.get('preferences', {}).get('food') else None\n", " food_preference = customer_profile.get('preferences', {}).get('food', [])[0] if customer_profile.get('preferences', {}).get('food') else None\n",
"\n", "\n",
"\n", "\n",
" response = openai.ChatCompletion.create(\n", " response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n", " model=\"gpt-3.5-turbo\",\n",
" messages=[\n", " messages=[\n",
" {\n", " {\n",
@ -272,10 +275,12 @@
" ],\n", " ],\n",
" )\n", " )\n",
"\n", "\n",
" if 'message' in response.choices[0] and 'function_call' in response.choices[0]['message']:\n", " print(response.choices[0].message.function_call)\n",
" function_call = response.choices[0]['message']['function_call']\n", "\n",
" if function_call[\"name\"] == \"call_google_places_api\":\n", " if response.choices[0].finish_reason=='function_call':\n",
" place_type = json.loads(function_call[\"arguments\"])[\"place_type\"]\n", " function_call = response.choices[0].message.function_call\n",
" if function_call.name == \"call_google_places_api\":\n",
" place_type = json.loads(function_call.arguments)[\"place_type\"]\n",
" places = call_google_places_api(user_id, place_type, food_preference)\n", " places = call_google_places_api(user_id, place_type, food_preference)\n",
" if places: # If the list of places is not empty\n", " if places: # If the list of places is not empty\n",
" return f\"Here are some places you might be interested in: {' '.join(places)}\"\n", " return f\"Here are some places you might be interested in: {' '.join(places)}\"\n",
@ -297,14 +302,15 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Here are some places you might be interested in: Mona Lisa Restaurant is a restaurant located at 353 Columbus Avenue #3907. It has a rating of 4.3 based on 1784 user reviews. Tommaso's Ristorante Italiano is a restaurant located at 1042 Kearny Street. It has a rating of 4.5 based on 732 user reviews.\n" "FunctionCall(arguments='{\\n \"place_type\": \"restaurant\"\\n}', name='call_google_places_api')\n",
"I couldn't find any places of interest nearby.\n"
] ]
} }
], ],
@ -312,7 +318,7 @@
"user_id = \"user1234\"\n", "user_id = \"user1234\"\n",
"user_input = \"I'm hungry\"\n", "user_input = \"I'm hungry\"\n",
"output = provide_user_specific_recommendations(user_input, user_id)\n", "output = provide_user_specific_recommendations(user_input, user_id)\n",
"print(output)" "print(output)\n"
] ]
} }
], ],
@ -332,7 +338,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.9.9" "version": "3.9.16"
}, },
"orig_nbformat": 4 "orig_nbformat": 4
}, },

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long