Fuction calling 例

‘‘‘ import openai import json from dotenv import load_dotenv load_dotenv() import os

model_name = "gpt-4-0613"

model_name = "gpt-3.5-turbo-0613"

openai.api_key = "APIキー"

functions=[ { "name":"whoisheroine", "description":"", "parameters":{ "type":"object", "properties":{ "heroine":{ "type":"string", "description":"与えられた小説を解析し、主人公の名前を返します。" }, "appearance":{ "type":"string", "description":"与えられた小説を解析し、主人公の外見を返します。" }, "personality":{ "type":"string", "description":"与えられた小説を解析し、主人公の性格を返します。" }, "other":{ "type":"string", "description":"与えられた小説を解析し、主人公のその他の情報を返します。" } } }, "type":"object", } ]

arg = """ 真夏の猫は知っている

どこが涼しいかを

革張りのソファーの上

板張りの廊下

アスファルトの道路

だから君も酔って火照った体を冷やすなら

猫の後をつけるといい。

冷たいアスファルトの上に横たわるのは

最高だとわかるから。

""" question = f'「{arg}」について、この小説の登場人物を判定し、名前をheroineに、外見をappearance' \ f'に、性格をpersonarityに、その他の情報をotherに入れてください。' \ f'複数人の登場人物がいます。全ての人物について同様の内容をリストアップして返してください。'

print(question) res = openai.ChatCompletion.create( model=model_name, messages=[{ "role":"user", "content":question }], functions=functions, function_call = "auto" ) res_json = json.loads(res["choices"][0]["message"]["function_call"]["arguments"]) print(res_json) print("END")

‘‘‘