Updating notebooks to use new Python SDK (#837)

pull/863/head
jhills20 6 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"
]
},
{
@ -179,21 +182,21 @@
" for place in results[:2]: # limit to top 2 results\n",
" place_id = place.get(\"place_id\")\n",
" place_details = get_place_details(place_id, API_KEY) # Get the details of the place\n",
" \n",
"\n",
" place_name = place_details.get(\"name\", \"N/A\")\n",
" place_types = next((t for t in place_details.get(\"types\", []) if t not in [\"food\", \"point_of_interest\"]), \"N/A\") # Get the first type of the place, excluding \"food\" and \"point_of_interest\"\n",
" place_rating = place_details.get(\"rating\", \"N/A\") # Get the rating of the place\n",
" total_ratings = place_details.get(\"user_ratings_total\", \"N/A\") # Get the total number of ratings\n",
" place_address = place_details.get(\"vicinity\", \"N/A\") # Get the vicinity of the place\n",
" \n",
"\n",
" if ',' in place_address: # If the address contains a comma\n",
" street_address = place_address.split(',')[0] # Split by comma and keep only the first part\n",
" else:\n",
" street_address = place_address\n",
" \n",
"\n",
" # Prepare the output string for this place\n",
" place_info = f\"{place_name} is a {place_types} located at {street_address}. It has a rating of {place_rating} based on {total_ratings} user reviews.\"\n",
" \n",
"\n",
" places.append(place_info)\n",
"\n",
" return places\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": [
@ -233,13 +236,13 @@
" customer_profile = fetch_customer_profile(user_id)\n",
" if customer_profile is None:\n",
" return \"I couldn't find your profile. Could you please verify your user ID?\"\n",
" \n",
"\n",
" customer_profile_str = json.dumps(customer_profile)\n",
"\n",
" 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",
"\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,22 +302,23 @@
},
{
"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"
]
}
],
"source": [
"user_id = \"user1234\" \n",
"user_input = \"I'm hungry\" \n",
"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