首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

OpenAI Responses API|加速集成与响应优化的深度指南

介绍

这是 OpenAI Responses API 指南!本系列将引导您了解刚刚发布 的新 OpenAI Responses API 的关键功能和特性。

我们将涵盖的内容

1.介绍:开始使用 OpenAI Responses API。

2.文本提示:为 API 制作有效的文本提示。

3.对话状态:管理对话状态,实现流畅交互。

4.函数调用:将自定义函数集成到 API 中。

5.结构化输出:从 API 生成结构化输出。

6.网页搜索:将网页搜索功能集成到应用中。

7.推理:增强 API 的推理能力。

8.文件搜索:在上传文件中搜索内容以生成上下文响应。

最重要的信息

向后兼容性

Responses API 是 Chat Completions 的超集,意味着您能用 Chat Completions 做的事情,都能在 Responses API 中完成,且还有更多功能。

关键新特性

简化接口:为不同交互类型提供更简单的接口。

原生网页搜索:内置网页搜索支持。

developer 角色:新增developer角色,实现更高级控制。

改进的推理模型:更好地支持推理模型。

文件/向量搜索:内置文件和向量搜索功能。

简化的对话状态管理:更简便的对话状态管理。

可用工具

网页搜索:在模型响应生成中包含互联网数据。

文件搜索:搜索上传文件内容以获取上下文。

计算机使用:创建能控制计算机界面的代理工作流。

函数调用:使模型调用您定义的自定义代码,访问更多数据和能力。

实施注意事项

API 结构变化:API 结构发生变化,但核心 AI 工程原则不变。

单次 API 调用:以往需多次调用完成的功能,现在可通过单次调用实现。

基础模式:检索、工具和记忆管理的基础模式仍然适用。

新 Agent SDK

OpenAI 发布了新的 Agent SDK,将替代 Swarm。该 SDK 为使用 Responses API 构建 AI 代理提供了标准化方式。了解更多请访问:OpenAI Agent SDK。

代码讲解

1. 介绍

from openai import OpenAI

client = OpenAI()

# Basic text example with the Chat Completions API

response = client.chat.completions.create(

  model="gpt-4o",

  messages=[

      {

          "role": "user",

          "content": "Write a one-sentence bedtime story about a unicorn.",

      }

  ],

)

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

# Basic text example with the Responses API

response = client.responses.create(

  model="gpt-4o", input="Write a one-sentence bedtime story about a unicorn."

)

print(response.output_text)

# Image example

response = client.responses.create(

  model="gpt-4o",

  input=[

      {"role": "user", "content": "what teams are playing in this image?"},

      {

          "role": "user",

          "content": [

              {

                  "type": "input_image",

                  "image_url": "https://upload.wikimedia.org/wikipedia/commons/3/3b/LeBron_James_Layup_%28Cleveland_vs_Brooklyn_2018%29.jpg",

              }

          ],

      },

  ],

)

print(response.output_text)

# Streaming

stream = client.responses.create(

  model="gpt-4o",

  input="Say 'double bubble bath' ten times fast.",

  stream=True,

)

text_chunks = []

for event in stream:

  ifhasattr(event, "type") and"text.delta"in event.type:

      text_chunks.append(event.delta)

      print(event.delta, end="", flush=True)2. 文本提示

from openai import OpenAI

client = OpenAI()

# Introducing instructions

response = client.responses.create(

  model="gpt-4o",

  instructions="Talk like a pirate.",

  input="Are semicolons optional in JavaScript?",

)

print(response.output_text)

# Chain of command (hierarchical instructions)

response = client.responses.create(

  model="gpt-4o",

  input=[

      {"role": "system", "content": "Talk like a pirate."},

      {"role": "developer", "content": "don't talk like a pirate."},

      {"role": "user", "content": "Are semicolons optional in JavaScript?"},

  ],

)

print(response.output_text)  # talks like a pirate

response = client.responses.create(

  model="gpt-4o",

  input=[

      {"role": "system", "content": "Don't talk like a pirate."},

      {"role": "developer", "content": "Talk like a pirate."},

      {"role": "user", "content": "Are semicolons optional in JavaScript?"},

  ],

)

print(response.output_text)  # doesn't talk like a pirate3. 对话状态

from openai import OpenAI

client = OpenAI()

# Manual conversation state with role specifications

response = client.responses.create(

  model="gpt-4o-mini",

  input=[

      {"role": "user", "content": "knock knock."},

      {"role": "assistant", "content": "Who's there?"},

      {"role": "user", "content": "Orange."},

  ],

)

print(response.output_text)

# Dynamic conversation state

history = [{"role": "user", "content": "tell me a joke"}]

response = client.responses.create(model="gpt-4o-mini", input=history, store=False)

print(response.output_text)

history += [

  {"role": output.role, "content": output.content} for output in response.output

]

history.append({"role": "user", "content": "tell me another"})

second_response = client.responses.create(

  model="gpt-4o-mini", input=history, store=False

)

print(second_response.output_text)

# OpenAI APIs for conversation state (default is to store)

response = client.responses.create(

  model="gpt-4o-mini",

  input="tell me a joke",

)

print(response.output_text)

second_response = client.responses.create(

  model="gpt-4o-mini",

  previous_response_id=response.id,

  input=[{"role": "user", "content": "explain why this is funny."}],

)

print(second_response.output_text)4. 函数调用

from openai import OpenAI

client = OpenAI()

tools = [

  {

      "type": "function",

      "name": "send_email",

      "description": "Send an email to a given recipient with a subject and message.",

      "parameters": {

          "type": "object",

          "properties": {

              "to": {"type": "string", "description": "The recipient email address."},

              "subject": {"type": "string", "description": "Email subject line."},

              "body": {"type": "string", "description": "Body of the email message."},

          },

          "required": ["to", "subject", "body"],

          "additionalProperties": False,

      },

  }

]

response = client.responses.create(

  model="gpt-4o",

  input="Can you send an email to ilan@example.com and katia@example.com saying hi?",

  tools=tools,

)

print(response.output)

print(response.output[0].model_dump_json(indent=2))

print(response.output[1].model_dump_json(indent=2))5. 结构化输出

import json

from typing importList

from openai import OpenAI

from pydantic import BaseModel

client = OpenAI()

# Using a JSON Schema

response = client.responses.create(

  model="gpt-4o",

  input=[

      {"role": "system", "content": "Extract the event information."},

      {

          "role": "user",

          "content": "Alice and Bob are going to a science fair on Friday.",

      },

  ],

  text={

      "format": {

          "type": "json_schema",

          "name": "calendar_event",

          "schema": {

              "type": "object",

              "properties": {

                  "name": {"type": "string"},

                  "date": {"type": "string"},

                  "participants": {"type": "array", "items": {"type": "string"}},

              },

              "required": ["name", "date", "participants"],

              "additionalProperties": False,

          },

          "strict": True,

      }

  },

)

event = json.loads(response.output_text)

# Using a Pydantic model (and simple response format)

classCalendarEvent(BaseModel):

  name: str

  date: str

  participants: List[str]

response = client.responses.parse(

  model="gpt-4o",

  input="Alice and Bob are going to a science fair on Friday",

  instructions="Extract the event information",

  text_format=CalendarEvent,

)

response_model = response.output[0].content[0].parsed

print(type(response_model))

print(response_model.model_dump_json(indent=2))

from openai import OpenAI

client = OpenAI()

# Basic web search

response = client.responses.create(

  model="gpt-4o",

  tools=[

      {

          "type": "web_search_preview",

      }

  ],

  input="What are the best restaurants around de Dam?",

)

print(response.output_text)

# Basic web search with location

response = client.responses.create(

  model="gpt-4o",

  tools=[

      {

          "type": "web_search_preview",

          "user_location": {

              "type": "approximate",

              "country": "NL",

              "city": "Amsterdam",

          },

      }

  ],

  input="What are the best restaurants around de Dam?",

)

print(response.output_text)

print(response.output[1].content[0].annotations)

print(response.output[1].content[0].annotations[0].url)

from openai import OpenAI

client = OpenAI()

prompt = """

Write a bash script that takes a matrix represented as a string with

format '[1,2],[3,4],[5,6],[7,8]' and prints the transpose in the same format.

"""

response = client.responses.create(

  model="o3-mini",

  reasoning={"effort": "medium"},

  input=[{"role": "user", "content": prompt}],

)

print(response.output_text)ConclusionDocumentation Resources

Official OpenAI Documentation: OpenAI Responses API Documentation

• https://github.com/Jaimboh/OpenAI-Responses-API

tags: #llm-applications #openai #api #chatgpt #llm

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OvULeuux5_nMBSQ-FP2IZIEQ0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

相关快讯

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券