New models and developer products announced at DevDay
APIでの利用が可能に。Mandrill.pngを送信。
b64=$(base64 Mandrill.png | tr -d '\\n')
payload='{
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "この画像、どう思う?"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,'"$b64"'"
}
}
]
}
],
"max_tokens": 300
}'
echo $payload | curl <https://api.openai.com/v1/chat/completions> \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer $OPENAI_API_KEY" \\
--data-binary @-
{
"id": "chatcmpl-8ISJDxrAR1oCfS6O6WqmwCl6uq3Vr",
"object": "chat.completion",
"created": 1699409415,
"model": "gpt-4-1106-vision-preview",
"usage": {
"prompt_tokens": 271,
"completion_tokens": 186,
"total_tokens": 457
},
"choices": [
{
"message": {
"role": "assistant",
"content": "この画像は、鮮やかな色彩のマンドリルの顔をクローズアップしたものです。マンドリルは、その特徴的な赤と青の顔色と、黄色がかった毛皮で知られる霊長類です。彼らは社会性が高く、アフリカの熱帯雨林に生息しています。画像は非常に鮮明で、マンドリルの顔の細部をはっきりと捉えています。自然の美しさと野生動物の多様性を感じさせるすばらしい写真ですね。"
},
"finish_details": {
"type": "stop",
"stop": "<|fim_suffix|>"
},
"index": 0
}
]
}
https://platform.openai.com/docs/guides/function-calling/parallel-function-calling
function callingのスキーマは複雑になりがちなのでPythonを使用します。
既存のfunction callingとは違い、toolとして差別化されている。
import openai # 1.1.1
import json
funcs = [
{
"type": "function", "function": {
"name": "buy_item",
"description": "商品を買う",
"parameters": {
"type": "object",
"properties": {
"item": {
"type": "string",
"description": "商品名",
},
"quantity": {
"type": "number",
"description": "購入する商品の数",
}
},
"required": ["item","quantity"],
},
}
}
]
message = """牛乳を1つ買ってきてちょうだい。卵も6つお願い"""
client = openai.OpenAI()
response = client.chat.completions.create(
messages=[
{
"role": "user",
"content": message
}
],
model="gpt-4-1106-preview",
tools=funcs
)
tool_calls = response.choices[0].message.tool_calls
for call in tool_calls:
args = json.loads(call.function.arguments)
print(args["item"], args["quantity"])
https://platform.openai.com/docs/guides/text-generation/json-mode