OpenAI API

client.responses.create: 新的调用模型接口,提供了获取历史会话内容的能力,还有并行调用工具,网络搜索,文件上传等等。

推荐使用instructions属性来输入system prompt。 新接口传入图片链接时,如果不填detail参数,image_url后面直接放url地址,否则要嵌套一层字典。

from openai import OpenAI
client = OpenAI(api_key="OPENAI_API_KEY")

response = client.responses.create(
  model="gpt-4o",
  instructions="你是一个精准的图像解析专家,风格严谨、细节丰富。",
  input=[
    {
      "role": "user",
      "content": [
        {"type":"input_image","image_url":"https://.../img1.jpg"},
        {"type":"input_image",
         "image_url": {"url": "https://.../img2.jpg",
                            "detail": "high"  # 可选参数
                           }
        },
        {"type":"input_text","text":"请综合描述这两张图。"}
      ]
    }
  ]
)

现在又增加了开发者角色,用于与system角色区分。并且,system提示词也可以放在input里并标明system role,但并不推荐。

response = client.responses.create(
  model="gpt-4o",
  input=[
    {"role":"system","content":[{"type":"input_text","text":"你是图像解析助手。"}]},
    {"role":"developer","content":[{"type":"input_text","text":"请用简练的三个要点总结。"}]},
    {"role":"user","content":[
        {"type":"input_image","image_url":"..."},
        {"type":"input_text","text":"描述这张图。"}
    ]}
  ]
)

client.chat.completions.create: 传统聊天接口。就算不填detail参数,image_url后也要嵌套一层字典。

from openai import OpenAI

client = OpenAI(api_key="YOUR_API_KEY")

completion = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        { "role": "system", "content": "你是一个图像理解专家,请解析以下图片。" },
        {
            "role": "user",
            "content": [
                { "type": "text", "text": "这是第一张图片:" },
                { "type": "image_url", "image_url": { "url": "https://example.com/image1.jpg" , 
                                                                "detail": "high"   # 可选属性,控制图像清晰度
                                                                } 
                },
                { "type": "text", "text": "还有第二张图片:" },
                { "type": "image_url", "image_url": { "url": "https://example.com/image2.jpg" } },
                { "type": "text", "text": "请综述这两张图所呈现的内容。" }
            ]
        }
    ],
    max_tokens=500,
    temperature=0.7,
)

print(completion.choices[0].message.content)

注意,两种接口都支持developer角色。它的指令优先级介于system role与user role之间,使得对模型指令进一步细分,增强模型安全使用的能力。

目前OpenAI推荐使用Response API。
此API返回的对象有很多方法, 常用的model_dump或to_dict方法,以dict类型展示对象属性值,model_dump_json方法以json形式返回内容但没有进行格式缩进,或想格式化显示,则使用to_json方法。

posted @ 2025-06-27 10:29  RolandHe  阅读(106)  评论(0)    收藏  举报