diff --git a/notebooks/Learning_with_Gemini_and_ChatGPT.ipynb b/notebooks/Learning_with_Gemini_and_ChatGPT.ipynb new file mode 100644 index 00000000..ccb3a44f --- /dev/null +++ b/notebooks/Learning_with_Gemini_and_ChatGPT.ipynb @@ -0,0 +1,248 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "Use Gemini and ChatGPT to learn from two capable teachers\n", + "\n", + "Use Google's latest model release, Gemini, to teach you what you want to know and compare those with ChatGPT's responses.\n", + "\n", + "The models are specifically prompted not to generate extra text to make it easier to compare any differences." + ], + "metadata": { + "id": "ZWhWniBGu3_Y" + } + }, + { + "cell_type": "code", + "source": [ + "#@title Configure Gemini API key\n", + "\n", + "# access your Gemini API key\n", + "\n", + "import google.generativeai as genai\n", + "from google.colab import userdata\n", + "\n", + "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", + "\n", + "try:\n", + " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", + " genai.configure(api_key=GOOGLE_API_KEY)\n", + "except userdata.SecretNotFoundError as e:\n", + " print(f'''Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://makersuite.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}''')\n", + " raise e\n", + "except userdata.NotebookAccessError as e:\n", + " print(f'''You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.''')\n", + " raise e\n", + "except Exception as e:\n", + " # unknown error\n", + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", + " raise e\n", + "\n", + "model = genai.GenerativeModel('gemini-pro')" + ], + "metadata": { + "cellView": "form", + "id": "HAg5Vlk-TjdX" + }, + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#@title Configure OpenAI API key\n", + "\n", + "# access your OpenAI API key\n", + "\n", + "# installing llmx first isn't necessary but avoids a confusing error when installing openai\n", + "!pip install -q llmx\n", + "!pip install -q openai\n", + "from openai import OpenAI\n", + "\n", + "openai_api_secret_name = 'OPENAI_API_KEY' # @param {type: \"string\"}\n", + "\n", + "try:\n", + " OPENAI_API_KEY=userdata.get(openai_api_secret_name)\n", + " client = OpenAI(\n", + " api_key=OPENAI_API_KEY\n", + " )\n", + "except userdata.SecretNotFoundError as e:\n", + " print(f'''Secret not found\\n\\nThis expects you to create a secret named {openai_api_secret_name} in Colab\\n\\nVisit https://platform.openai.com/api-keys to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {openai_api_secret_name}''')\n", + " raise e\n", + "except userdata.NotebookAccessError as e:\n", + " print(f'''You need to grant this notebook access to the {openai_api_secret_name} secret in order for the notebook to access Gemini on your behalf.''')\n", + " raise e\n", + "except Exception as e:\n", + " # unknown error\n", + " print(f\"There was an unknown error. Ensure you have a secret {openai_api_secret_name} stored in Colab and it's a valid key from https://platform.openai.com/api-keys\")\n", + " raise e\n" + ], + "metadata": { + "id": "yFv1abRcv2P2", + "cellView": "form" + }, + "execution_count": 17, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#@title Ask a question!\n", + "\n", + "text = 'Write a python function that calculates the distance between any two latitudes and longitudes on earth' # @param {type:\"string\"}\n", + "\n", + "# ask Gemini\n", + "model = genai.GenerativeModel('gemini-pro')\n", + "chat = model.start_chat(history=[])\n", + "\n", + "response = chat.send_message('%s -- Please answer as concisely as you can, avoiding any extra conversation or text' % text)\n", + "gemini_response = response.text\n", + "\n", + "# ask ChatGPT\n", + "completion = client.chat.completions.create(\n", + " model=\"gpt-3.5-turbo\",\n", + " messages=[\n", + " {\"role\": \"user\", \"content\": '%s -- Please answer as concisely as you can, avoiding any extra conversation or text' % text}\n", + " ]\n", + ")\n", + "\n", + "openai_response = completion.choices[0].message.content\n", + "\n", + "# render the diff\n", + "\n", + "# importing these every execution is unnecessary but avoids another notebook cell\n", + "from IPython.display import HTML\n", + "import difflib\n", + "\n", + "# omit the legend to slim down the UI\n", + "difflib.HtmlDiff._legend = ''\n", + "\n", + "HTML(difflib.HtmlDiff().make_file(gemini_response.splitlines(), openai_response.splitlines(), 'Gemini', 'ChatGPT'))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 923 + }, + "cellView": "form", + "id": "timFyiyRYEqz", + "outputId": "781353b5-53c4-46ae-f01f-0aea2d46a1fe" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "

Gemini
ChatGPT
n1```pythonn
2import math1import math
32
n4def distance(lat1, lon1, lat2, lon2):n3def calculate_distance(lat1, lon1, lat2, lon2):
5  \"\"\"Calculates the distance between two latitudes and longitudes.4    # Convert degrees to radians
5    lat1 = math.radians(lat1)
6    lon1 = math.radians(lon1)
7    lat2 = math.radians(lat2)
8    lon2 = math.radians(lon2)
69
n7  Args:n10    # Calculate differences
8    lat1: The latitude of the first point.11    dlat = lat2 - lat1
9    lon1: The longitude of the first point.12    dlon = lon2 - lon1
10    lat2: The latitude of the second point.
11    lon2: The longitude of the second point.
1213
n13  Returns:n14    # Calculate distance using Haversine formula
14    The distance between the two points in kilometers.15    a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
15  \"\"\"16    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
17    distance = 6371 * c  # Earth's radius in kilometers
1618
t17  # Convert the latitudes and longitudes to radians.t
18  lat1 = math.radians(lat1)
19  lon1 = math.radians(lon1)
20  lat2 = math.radians(lat2)
21  lon2 = math.radians(lon2)
22 
23  # Calculate the difference between the latitudes and longitudes.
24  dlat = lat2 - lat1
25  dlon = lon2 - lon1
26 
27  # Calculate the square of the sine of the half of the difference in latitude.
28  sin_dlat_half = math.sin(dlat / 2) ** 2
29 
30  # Calculate the square of the sine of the half of the difference in longitude.
31  sin_dlon_half = math.sin(dlon / 2) ** 2
32 
33  # Calculate the square of the great-circle distance between the two points.
34  a = sin_dlat_half + math.cos(lat1) * math.cos(lat2) * sin_dlon_half
35 
36  # Calculate the great-circle distance between the two points.
37  c = 2 * math.asin(math.sqrt(a))
38 
39  # Calculate the Earth's radius in kilometers.
40  r = 6371
41 
42  # Calculate the distance between the two points in kilometers.
43  distance = r * c
44 
45  return distance19    return distance
46```
\n", + "\n", + "\n", + "" + ] + }, + "metadata": {}, + "execution_count": 18 + } + ] + } + ] +} \ No newline at end of file