Last active
December 8, 2023 04:45
-
-
Save mif-git/79ef7cfe91fe3ff083b1067a22c36379 to your computer and use it in GitHub Desktop.
ChatGPT function calling sample with Product search API
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
import openai | |
import random | |
import json | |
openai.api_key = "YOUR_OPEN_API_KEY" | |
def search_printer_products(model_name=None, manufacturer=None, release_date_from=None, release_date_to=None, | |
price_min=None, price_max=None, paper_size=None, color_printing=None, | |
tray_capacity_min=None, tray_capacity_max=None, ink_cost_min=None, ink_cost_max=None, | |
width_min=None, width_max=None, depth_min=None, depth_max=None, | |
height_min=None, height_max=None, color_print_speed_min=None, color_print_speed_max=None, | |
mono_print_speed_min=None, mono_print_speed_max=None, scan_function=None, | |
duplex_printing=None, copy_function=None, fax_function=None, features=None): | |
""" | |
指定された条件に基づいてプリンター製品を検索するためのパラメータを整理し、文字列として返します。 | |
""" | |
params = { | |
'model_name': model_name, | |
'manufacturer': manufacturer, | |
'release_date_from': release_date_from, | |
'release_date_to': release_date_to, | |
'price_min': price_min, | |
'price_max': price_max, | |
'paper_size': paper_size, | |
'color_printing': color_printing, | |
'tray_capacity_min': tray_capacity_min, | |
'tray_capacity_max': tray_capacity_max, | |
'ink_cost_min': ink_cost_min, | |
'ink_cost_max': ink_cost_max, | |
'width_min': width_min, | |
'width_max': width_max, | |
'depth_min': depth_min, | |
'depth_max': depth_max, | |
'height_min': height_min, | |
'height_max': height_max, | |
'color_print_speed_min': color_print_speed_min, | |
'color_print_speed_max': color_print_speed_max, | |
'mono_print_speed_min': mono_print_speed_min, | |
'mono_print_speed_max': mono_print_speed_max, | |
'scan_function': scan_function, | |
'duplex_printing': duplex_printing, | |
'copy_function': copy_function, | |
'fax_function': fax_function, | |
'features': features | |
} | |
# Noneでないパラメータのみを含める | |
params = {k: v for k, v in params.items() if v is not None} | |
return str(params) | |
my_functions = [ | |
{ | |
"name": "search_printer_products", | |
"description": "Searches for printer products based on specified criteria and returns their information.", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"model_name": { | |
"type": "string", "description": "The model name of the printer." | |
}, | |
"manufacturer": { | |
"type": "string", "enum": ["Company A", "Company B", "Company C", "Company D"], "description": "The name of the printer's manufacturer." | |
}, | |
"release_date_from": { | |
"type": "string", "description": "The start range of the release date (YYYY-MM-DD)." | |
}, | |
"release_date_to": { | |
"type": "string", "description": "The end range of the release date (YYYY-MM-DD)." | |
}, | |
"price_min": { | |
"type": "number", "description": "The minimum price of the printer (in yen)." | |
}, | |
"price_max": { | |
"type": "number", "description": "The maximum price of the printer (in yen)." | |
}, | |
"paper_size": { | |
"type": "string", "description": "The compatible paper size." | |
}, | |
"color_printing": { | |
"type": "boolean", "description": "Whether color printing is available or not." | |
}, | |
"tray_capacity_min": { | |
"type": "number", "description": "The minimum tray capacity (in sheets)." | |
}, | |
"tray_capacity_max": { | |
"type": "number", "description": "The maximum tray capacity (in sheets)." | |
}, | |
"ink_cost_min": { | |
"type": "number", "description": "The minimum ink cost (in yen)." | |
}, | |
"ink_cost_max": { | |
"type": "number", "description": "The maximum ink cost (in yen)." | |
}, | |
"width_min": { | |
"type": "number", "description": "The minimum width (in mm)." | |
}, | |
"width_max": { | |
"type": "number", "description": "The maximum width (in mm)." | |
}, | |
"depth_min": { | |
"type": "number", "description": "The minimum depth (in mm)." | |
}, | |
"depth_max": { | |
"type": "number", "description": "The maximum depth (in mm)." | |
}, | |
"height_min": { | |
"type": "number", "description": "The minimum height (in mm)." | |
}, | |
"height_max": { | |
"type": "number", "description": "The maximum height (in mm)." | |
}, | |
"color_print_speed_min": { | |
"type": "number", "description": "The minimum color print speed (pages per minute)." | |
}, | |
"color_print_speed_max": { | |
"type": "number", "description": "The maximum color print speed (pages per minute)." | |
}, | |
"mono_print_speed_min": { | |
"type": "number", "description": "The minimum monochrome print speed (pages per minute)." | |
}, | |
"mono_print_speed_max": { | |
"type": "number", "description": "The maximum monochrome print speed (pages per minute)." | |
}, | |
"scan_function": { | |
"type": "boolean", "description": "Whether scanning function is available or not." | |
}, | |
"duplex_printing": { | |
"type": "boolean", "description": "Whether duplex printing function is available or not." | |
}, | |
"copy_function": { | |
"type": "boolean", "description": "Whether copy function is available or not." | |
}, | |
"fax_function": { | |
"type": "boolean", "description": "Whether fax function is available or not." | |
}, | |
"features": { | |
"type": "string", "description": "The features of the printer (free-form text)." | |
} | |
}, | |
"required": [] | |
} | |
} | |
] | |
# 会話履歴を保持するリスト | |
conversation_history = [] | |
while True: | |
prompt = input("input message: ") | |
if prompt == "終了": | |
break | |
# 会話履歴にユーザーのメッセージを追加 | |
conversation_history.append({"role": "user", "content": prompt}) | |
# print(conversation_history) | |
# 会話履歴が10件を超えた場合、最も古いメッセージを削除 | |
if len(conversation_history) > 10: | |
conversation_history.pop(0) | |
# ChatGPTの応答を取得 | |
completion = openai.ChatCompletion.create( | |
model="gpt-4-1106-preview", | |
messages=conversation_history, | |
functions=my_functions | |
) | |
response = completion.choices[0] | |
if "function_call" in response["message"]: | |
# 関数の呼び出し処理 | |
# print("function_call") | |
function_call = response["message"]["function_call"] | |
function_name = function_call["name"] | |
function_args = json.loads(function_call.get("arguments", "{}")) | |
# ここで関数に応じた処理を実装 | |
if function_name == "search_printer_products": | |
print("プリンター提案機能を開始します。") | |
# 引数を取得 | |
model_name = function_args.get("model_name") | |
manufacturer = function_args.get("manufacturer") | |
release_date_from = function_args.get("release_date_from") | |
release_date_to = function_args.get("release_date_to") | |
price_min = function_args.get("price_min") | |
price_max = function_args.get("price_max") | |
paper_size = function_args.get("paper_size") | |
color_printing = function_args.get("color_printing") | |
tray_capacity_min = function_args.get("tray_capacity_min") | |
tray_capacity_max = function_args.get("tray_capacity_max") | |
ink_cost_min = function_args.get("ink_cost_min") | |
ink_cost_max = function_args.get("ink_cost_max") | |
width_min = function_args.get("width_min") | |
width_max = function_args.get("width_max") | |
depth_min = function_args.get("depth_min") | |
depth_max = function_args.get("depth_max") | |
height_min = function_args.get("height_min") | |
height_max = function_args.get("height_max") | |
color_print_speed_min = function_args.get("color_print_speed_min") | |
color_print_speed_max = function_args.get("color_print_speed_max") | |
mono_print_speed_min = function_args.get("mono_print_speed_min") | |
mono_print_speed_max = function_args.get("mono_print_speed_max") | |
scan_function = function_args.get("scan_function") | |
duplex_printing = function_args.get("duplex_printing") | |
copy_function = function_args.get("copy_function") | |
fax_function = function_args.get("fax_function") | |
features = function_args.get("features") | |
# 関数を呼び出し | |
function_response = search_printer_products( | |
model_name, manufacturer, release_date_from, release_date_to, | |
price_min, price_max, paper_size, color_printing, | |
tray_capacity_min, tray_capacity_max, ink_cost_min, ink_cost_max, | |
width_min, width_max, depth_min, depth_max, | |
height_min, height_max, color_print_speed_min, color_print_speed_max, | |
mono_print_speed_min, mono_print_speed_max, scan_function, | |
duplex_printing, copy_function, fax_function, features | |
) | |
conversation_history.append({"role": "assistant", "content": function_response}) | |
# 結果を出力 | |
print(f"Function response: {function_response}") | |
# 他の関数に対する処理もここに追加 | |
else: | |
# 通常のメッセージ処理 | |
message = response.get("message", {}).get("content", "") | |
print(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment