Updating notebooks to use new Python SDK (#837)

pull/863/head
jhills20 7 months ago committed by GitHub
parent a1747ab388
commit bd58636a8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -59,8 +59,11 @@
"source": [
"import json\n",
"import openai\n",
"from openai import OpenAI\n",
"import os\n",
"import requests"
"import requests\n",
"\n",
"client = OpenAI()\n"
]
},
{
@ -141,7 +144,7 @@
" else:\n",
" print(f\"Google Place Details API request failed with status code {response.status_code}\")\n",
" print(f\"Response content: {response.content}\")\n",
" return None"
" return None\n"
]
},
{
@ -203,7 +206,7 @@
" return []\n",
" except Exception as e:\n",
" print(f\"Error during the Google Places API call: {e}\")\n",
" return []"
" return []\n"
]
},
{
@ -225,7 +228,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@ -239,7 +242,7 @@
" food_preference = customer_profile.get('preferences', {}).get('food', [])[0] if customer_profile.get('preferences', {}).get('food') else None\n",
"\n",
"\n",
" response = openai.ChatCompletion.create(\n",
" response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\n",
@ -272,10 +275,12 @@
" ],\n",
" )\n",
"\n",
" if 'message' in response.choices[0] and 'function_call' in response.choices[0]['message']:\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",
" print(response.choices[0].message.function_call)\n",
"\n",
" if response.choices[0].finish_reason=='function_call':\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",
" 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",
@ -297,14 +302,15 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"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_input = \"I'm hungry\"\n",
"output = provide_user_specific_recommendations(user_input, user_id)\n",
"print(output)"
"print(output)\n"
]
}
],
@ -332,7 +338,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
"version": "3.9.16"
},
"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
Loading…
Cancel
Save