-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
3,125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,374 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "59762bba-eee1-45e2-b74e-1cc46d4905ac", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"/usr/local/opt/[email protected]/bin/python3.11\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import sys\n", | ||
"print(sys.executable)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "2bb23777-e922-469b-9e16-4d43e669d344", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Requirement already satisfied: openai in /usr/local/lib/python3.11/site-packages (1.6.1)\n", | ||
"Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/site-packages (from openai) (3.6.2)\n", | ||
"Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/site-packages (from openai) (1.9.0)\n", | ||
"Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/site-packages (from openai) (0.26.0)\n", | ||
"Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.11/site-packages (from openai) (2.5.3)\n", | ||
"Requirement already satisfied: sniffio in /usr/local/lib/python3.11/site-packages (from openai) (1.3.0)\n", | ||
"Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/site-packages (from openai) (4.65.0)\n", | ||
"Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.11/site-packages (from openai) (4.9.0)\n", | ||
"Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai) (3.4)\n", | ||
"Requirement already satisfied: certifi in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai) (2023.5.7)\n", | ||
"Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai) (1.0.2)\n", | ||
"Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n", | ||
"Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->openai) (0.6.0)\n", | ||
"Requirement already satisfied: pydantic-core==2.14.6 in /usr/local/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->openai) (2.14.6)\n", | ||
"\n", | ||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.2\u001b[0m\n", | ||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.11 -m pip install --upgrade pip\u001b[0m\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!pip install openai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "74ec6927-c50a-45d1-a5d9-b34bc1a1ff39", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"import openai\n", | ||
"\n", | ||
"openai.api_key = os.environ['OPENAI_API_KEY']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "b75555e6-3154-42bb-b27d-fe70aaa9ca3e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def chat_completion(messages, tools=None, tool_choice=None, model=\"gpt-3.5-turbo-0613\"):\n", | ||
" response = openai.ChatCompletion.create(\n", | ||
" model=model,\n", | ||
" messages=messages,\n", | ||
" tools=tools,\n", | ||
" tool_choice=tool_choice,\n", | ||
" temperature=0,\n", | ||
" )\n", | ||
" return response" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "68685d92-7b64-43eb-909b-c4cb5756506e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def build_message(role: str, content: str):\n", | ||
" return {\"role\": role, \"content\": content}\n", | ||
"\n", | ||
"def sys_message(content: str):\n", | ||
" return build_message(role=\"system\", content=content)\n", | ||
"\n", | ||
"def user_message(content: str):\n", | ||
" return build_message(role=\"user\", content=content)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"id": "3693431a-3a2a-4b67-813a-1c1ef6772c4c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The capital city of Australia is Canberra.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(chat_completion([user_message(\"What's the capital city of Australia\")]).choices[0].message.content)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e5f91820-83c3-4a7c-ae51-5f229fc74672", | ||
"metadata": {}, | ||
"source": [ | ||
"## Defining function" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"id": "6dfe620d-3694-4055-8b5f-26041ac86fe3", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"func_get_current_weather = {\n", | ||
" \"type\": \"function\",\n", | ||
" \"function\": {\n", | ||
" \"name\": \"get_current_weather\",\n", | ||
" \"description\": \"Get the current weather for a City\",\n", | ||
" \"parameters\": {\n", | ||
" \"type\": \"object\",\n", | ||
" \"properties\": {\n", | ||
" \"city\": {\n", | ||
" \"type\": \"string\",\n", | ||
" \"description\": \"The city, e.g. San Francisco\",\n", | ||
" }\n", | ||
" },\n", | ||
" \"required\": [\"city\"],\n", | ||
" },\n", | ||
" }\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"id": "99cb8116-64b3-4758-bf96-b95ed0cabaff", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"tools = [func_get_current_weather]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 17, | ||
"id": "f62aa1d7-3a6e-472b-a885-7fcac65c6bcc", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<OpenAIObject at 0x10c56d190> JSON: {\n", | ||
" \"content\": \"Sure, I can help you with that. Could you please tell me the name of the city you want to know the weather for?\",\n", | ||
" \"role\": \"assistant\"\n", | ||
"}" | ||
] | ||
}, | ||
"execution_count": 17, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"messages = []\n", | ||
"messages.append(sys_message(\"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"))\n", | ||
"messages.append(user_message(\"What's the weather like today\"))\n", | ||
"\n", | ||
"response = chat_completion(messages, tools=tools)\n", | ||
"assistant_message = response.choices[0].message\n", | ||
"messages.append(assistant_message)\n", | ||
"assistant_message" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 19, | ||
"id": "f9bf92e6-c370-47b9-aeec-84974bcd49fb", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<OpenAIObject at 0x10c56cdd0> JSON: {\n", | ||
" \"content\": null,\n", | ||
" \"role\": \"assistant\",\n", | ||
" \"tool_calls\": [\n", | ||
" {\n", | ||
" \"function\": {\n", | ||
" \"arguments\": \"{\\n \\\"city\\\": \\\"Melbourne\\\"\\n}\",\n", | ||
" \"name\": \"get_current_weather\"\n", | ||
" },\n", | ||
" \"id\": \"call_6DRFtk59mt9pBIeNKMqBulKV\",\n", | ||
" \"type\": \"function\"\n", | ||
" }\n", | ||
" ]\n", | ||
"}" | ||
] | ||
}, | ||
"execution_count": 19, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"messages.append(user_message(\"I'm in Melbourne, Australia\"))\n", | ||
"\n", | ||
"response = chat_completion(messages, tools=tools)\n", | ||
"assistant_message = response.choices[0].message\n", | ||
"messages.append(assistant_message)\n", | ||
"\n", | ||
"assistant_message" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 24, | ||
"id": "620501b0-1572-45f1-9254-8d749a9332ca", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import requests\n", | ||
"\n", | ||
"def get_current_weather(params):\n", | ||
" url = f\"http://wttr.in/{params['city']}?format=3\"\n", | ||
" response = requests.get(url)\n", | ||
" return response.text" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 25, | ||
"id": "466cd08f-6502-4977-ba47-26521d4e9484", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'Melbourne: ⛅️ +15°C\\n'" | ||
] | ||
}, | ||
"execution_count": 25, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"get_current_weather({\"city\": \"Melbourne\"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 27, | ||
"id": "3c7f7fdb-e96d-4c8c-9325-f5c67087d6cb", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[{'role': 'system', 'content': \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"}, {'role': 'user', 'content': \"What's the weather like today\"}, <OpenAIObject at 0x10c56d190> JSON: {\n", | ||
" \"content\": \"Sure, I can help you with that. Could you please tell me the name of the city you want to know the weather for?\",\n", | ||
" \"role\": \"assistant\"\n", | ||
"}, {'role': 'user', 'content': \"I'm in Melbourne, Australia\"}, {'role': 'user', 'content': \"I'm in Melbourne, Australia\"}, <OpenAIObject at 0x10c56cdd0> JSON: {\n", | ||
" \"content\": null,\n", | ||
" \"role\": \"assistant\",\n", | ||
" \"tool_calls\": [\n", | ||
" {\n", | ||
" \"function\": {\n", | ||
" \"arguments\": \"{\\n \\\"city\\\": \\\"Melbourne\\\"\\n}\",\n", | ||
" \"name\": \"get_current_weather\"\n", | ||
" },\n", | ||
" \"id\": \"call_6DRFtk59mt9pBIeNKMqBulKV\",\n", | ||
" \"type\": \"function\"\n", | ||
" }\n", | ||
" ]\n", | ||
"}, {'role': 'tool', 'tool_call_id': 'call_6DRFtk59mt9pBIeNKMqBulKV', 'name': 'get_current_weather', 'content': 'Melbourne: ⛅️ +15°C\\n'}]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import json\n", | ||
"\n", | ||
"if assistant_message.tool_calls[0].function.name == \"get_current_weather\":\n", | ||
" params = json.loads(assistant_message.tool_calls[0].function.arguments)\n", | ||
" result = get_current_weather(params)\n", | ||
" messages.append({\n", | ||
" \"role\": \"tool\", \n", | ||
" \"tool_call_id\": assistant_message.tool_calls[0].id, \n", | ||
" \"name\": assistant_message.tool_calls[0].function.name,\n", | ||
" \"content\": result\n", | ||
" })\n", | ||
"\n", | ||
"print(messages)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 28, | ||
"id": "612af0d0-8b2e-46c7-ae95-3009f2efceb8", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{\n", | ||
" \"content\": \"The current weather in Melbourne is partly cloudy with a temperature of 15\\u00b0C. While it may not be ideal for camping, it is still possible depending on your preferences. It's always a good idea to check the weather forecast for the evening and make sure you have appropriate camping gear.\",\n", | ||
" \"role\": \"assistant\"\n", | ||
"}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"messages.append(user_message(\"Is it a good weather for camping tonight?\"))\n", | ||
"response = chat_completion(messages, tools=tools)\n", | ||
"assistant_message = response.choices[0].message\n", | ||
"\n", | ||
"print(assistant_message)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1c6072da-2f95-45da-a210-d53fdf9e5f7f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python (Global)", | ||
"language": "python", | ||
"name": "global-python" | ||
}, | ||
"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.11.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.