{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GPT-4 interaction using the OpenAI API\n", "\n", "(C) 2024 by [Damir Cavar](http://damir.cavar.me/)\n", "\n", "The basic API example is documented on the [OpenAI](https://openai.com/) website. The code below is extended and based on the example provided by [OpenAI](https://openai.com/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Download:** This and various other Jupyter notebooks are available from my [GitHub repo](https://github.com/dcavar/python-tutorial-for-ipython)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Version:** 1.0, March 2024" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**License:** [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/) ([CA BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Prerequisites:**\n", "\n", "The code requires the `openai` module for Python:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -U openai" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will need to create a file `secret.py` in the same location as this notebook and set the following variable in it:\n", "\n", " api_key = \"...\"\n", "\n", "Make sure that you replace the three dots with your [OpenAI](https://openai.com/) API key.\n", "\n", "Alternatively, follow the instructions on the [OpenAI](https://openai.com/) site and set the environmental variable with the API key. Should you prefer to use some alternative method to provide the API key to the code, you might need to comment out `from secret import apikey` below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "This is a simple notebook that shows how to use the [OpenAI](https://openai.com/) endpoint for [GPT](https://chat.openai.com/), in particular the newly released [GPT-4 turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from openai import OpenAI\n", "from secret import apikey\n", "gpt_model = \"gpt-4-turbo-preview\"\n", "gpt_model = \"gpt-4o-2024-08-06\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following code creates an OpenAI `client` that connects with GPT-4." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "client = OpenAI(api_key=apikey)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following function performs the API call and returns the resulting message content." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def call_gpt4(prompt: str, instructions: str) -> str:\n", " chat_completion = client.chat.completions.create(\n", " messages=[ {\n", " \"role\": \"user\",\n", " \"content\": f'{instructions} {prompt}.'\n", " } ],\n", " model=gpt_model\n", " )\n", " return chat_completion.choices[0].message.content.strip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `instructions` text provides a context and instructions to GPT-4. The `prompt` contains the full textual prompt." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'When someone says, \"I bought the blue car,\" there are several conversational implicatures that can be deduced from this statement. Conversational implicatures are not explicitly stated but are inferred from the context and the way the statement is made, based on the cooperative principle proposed by H. P. Grice. The cooperative principle suggests that speakers and listeners in a conversation cooperate with each other, adhering to certain conversational maxims, making the communication effective and meaningful.\\n\\nFrom the claim, \"I bought the blue car,\" the following implicatures might be derived:\\n\\n1. **Selection from options**: The speaker had a choice of cars of different colors and chose the blue one. This implies a decision-making process where blue was preferred over other colors.\\n\\n2. **Ownership change**: By stating the purchase, it implies that the speaker did not own the car previously but now does. The act of buying indicates a transfer of ownership.\\n\\n3. **Exclusivity of Choice**: The statement suggests that the speaker bought the blue car specifically, possibly excluding other cars they were considering. It implies the finality of choice – the speaker ended up purchasing the blue one and not any other color.\\n\\n4. **Financial transaction**: The use of \"bought\" implies that a financial transaction took place. The speaker exchanged money (or other forms of payment) for the car. This also suggests that the speaker had the necessary resources to make the purchase.\\n\\n5. **Possession**: The speaker now possesses the blue car. This is more explicit than implied, but the fact that they are specifying the action of buying brings attention to the new ownership.\\n\\n6. **Act of decision making**: It suggests that the speaker made a conscious choice, indicating preference, decision-making ability, and possibly even deliberation over various options.\\n\\n7. **Kinds of actions involved**: It may imply that the speaker visited a car dealership, engaged in negotiations or considerations, and completed a transaction. It suggests a series of actions leading up to the purchase.\\n\\n8. **Recent or Relevant Context**: The statement might be relevant to the immediate conversation or context, implying why the speaker is bringing up this fact. It could be in response to a question about cars, a discussion about recent purchases, or a way to inform someone of a change in their transportation situation.\\n\\nIt\\'s important to remember that the specifics of these implicatures could vary depending on additional context or how the statement fits into a broader conversation. Conversational implicature depends heavily on the assumed shared knowledge between the speaker and the listener, as well as the context in which the statement is made.'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "instructions = \"Answer the following question.\"\n", "prompt = \"\"\"What are the implicatures of the claim:\n", "'I bought the blue car.'\n", "\"\"\"\n", "call_gpt4(prompt, instructions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(C) 2024 by [Damir Cavar](http://damir.cavar.me/)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }