Skip to content

Instantly share code, notes, and snippets.

@HiromuKato
Created June 15, 2023 17:18
Show Gist options
  • Save HiromuKato/e24fb3edcc0bc0fab2b378f0427b5979 to your computer and use it in GitHub Desktop.
Save HiromuKato/e24fb3edcc0bc0fab2b378f0427b5979 to your computer and use it in GitHub Desktop.
OpenAI function calling sample
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "U42o6gBFmSLq"
},
"source": [
"# OpenAI API サンプル"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "abM0swaVmi4o"
},
"source": [
"[API reference](https://platform.openai.com/docs/api-reference)\n",
"\n",
"[Function calling and other API updates](https://openai.com/blog/function-calling-and-other-api-updates)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 6676,
"status": "ok",
"timestamp": 1686841781591,
"user_tz": -540
},
"id": "EZ65pE94mKGy",
"outputId": "dd5fe14f-771c-4f73-ff9f-b6a957add60c",
"scrolled": true
},
"outputs": [],
"source": [
"!pip install openai"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 260,
"status": "ok",
"timestamp": 1686841932872,
"user_tz": -540
},
"id": "LVAK5ITomRbz",
"outputId": "7041b54d-8e80-462d-d82f-db1535a52822",
"scrolled": true
},
"outputs": [],
"source": [
"import os\n",
"import openai\n",
"\n",
"openai.api_key = \"ここに API key を入力する\";\n",
"#openai.Model.list()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zaJrCLMurIHG"
},
"source": [
"## Chat Completions API (function calling)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 1272,
"status": "ok",
"timestamp": 1686845421083,
"user_tz": -540
},
"id": "ZwEyZ3sgmRm8",
"outputId": "da0c7709-a400-4b1e-9768-c393ac3134d8"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"name\": \"get_current_weather\",\n",
" \"arguments\": \"{\\n \\\"location\\\": \\\"Boston, MA\\\"\\n}\"\n",
"}\n"
]
}
],
"source": [
"# OpenAI への1回目の問合せ\n",
"# レスポンスとして 指定した JSON を返してもらう\n",
"\n",
"# 自然言語で問合せ\n",
"prompt = \"What is the weather like in Boston?\";\n",
"\n",
"completion = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": prompt}\n",
" ],\n",
" functions=[\n",
" {\n",
" \"name\": \"get_current_weather\",\n",
" \"description\": \"Get current weather.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city and state, e.g. San Francisco, CA\"\n",
" },\n",
" \"unit\": {\n",
" \"type\": \"string\",\n",
" \"enum\": [\"celsius\", \"fahrenheit\"]\n",
" }\n",
" },\n",
" \"required\": [\"location\"]\n",
" }\n",
" }\n",
" ],\n",
" function_call={\"name\": \"get_current_weather\"},\n",
")\n",
"\n",
"#print(completion)\n",
"openai_response = completion.choices[0].message.function_call\n",
"print(openai_response)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"id": "fyNQRdExmRpM"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ \"temperature\": 22, \"unit\": \"celsius\", \"description\": \"Sunny\" }\n"
]
}
],
"source": [
"# ThirdParty の API への問合せ\n",
"# OpenAIのレスポンスのをもとに、天気取得APIへ問合せを行ったとして、\n",
"# 以下のレスポンスが返ってきたとする\n",
"# { \"temperature\": 22, \"unit\": \"celsius\", \"description\": \"Sunny\" }\n",
"\n",
"thirdparty_response = r'{ \"temperature\": 22, \"unit\": \"celsius\", \"description\": \"Sunny\" }'\n",
"print(thirdparty_response)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 1290,
"status": "ok",
"timestamp": 1686845427793,
"user_tz": -540
},
"id": "U8D2JWZtmRrs",
"outputId": "370588ed-5853-4a50-ac84-3c659df373bd"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weather in Boston is currently sunny with a temperature of 22 degrees Celsius.\n"
]
}
],
"source": [
"# OpenAI への2回目の問合せ\n",
"# OpenAI の1回目の問合せのレスポンスと、ThirdPartyのレスポンスをもとに\n",
"# ユーザーに最終的なレスポンスを返す\n",
"\n",
"completion = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=[\n",
" {\"role\": \"user\", \"content\": \"What is the weather like in Boston?\"},\n",
" {\"role\": \"assistant\", \"content\": \"\", \"function_call\": openai_response},\n",
" {\"role\": \"function\", \"name\": \"get_current_weather\", \"content\": thirdparty_response}\n",
" ]\n",
")\n",
"\n",
"#print(completion)\n",
"print(completion.choices[0].message.content)"
]
}
],
"metadata": {
"celltoolbar": "Edit Metadata",
"colab": {
"authorship_tag": "ABX9TyNl9BBde09t0+htQTALkaXn",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment