2025年3月11号,OpenAI召开发布会,正式发布全新一代底层调用API:Responses API,并计划在未来一段时间,逐渐代替Chat.Completion API,在如今OpenAI的大模型调用方法已成为业内标准的今天,这一次更新,无疑将对未来的大模型开发生态带来重大影响。
随着发布会一同发布的,还有三项Agent tools和一个开源Agent SDK,分别是:
- Web Search:允许用户联网搜索相关信息;
- File Search:允许用户将文件上传到OpenAI服务器上,并在模型对话时,实时对其进行检索;
- computer use:允许用户借助大模型功能,来操作当前电脑或浏览器。
- Agent SDK:swarm的升级版,一个开源的Multi-Agent开发框架。
对此,我曾专门录制视频进行入门介绍,感兴趣的同学可以戳此观看:https://www.bilibili.com/video/BV1SVQEYqERV/
本届公开课,我们就围绕OpenAI本次更新内容进行详细讲解。
-
课程资料及相关参考材料:OpenAI注册指南&公开课课件&国内反向代理地址,扫码即可领取。
!pip install openai
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Requirement already satisfied: openai in /root/miniconda3/lib/python3.12/site-packages (1.66.3)
Requirement already satisfied: anyio<5,>=3.5.0in /root/miniconda3/lib/python3.12/site-packages (from openai) (4.6.2.post1)
Requirement already satisfied: distro<2,>=1.7.0in /root/miniconda3/lib/python3.12/site-packages (from openai) (1.9.0)
Requirement already satisfied: httpx<1,>=0.23.0in /root/miniconda3/lib/python3.12/site-packages (from openai) (0.27.2)
Requirement already satisfied: jiter<1,>=0.4.0in /root/miniconda3/lib/python3.12/site-packages (from openai) (0.8.2)
Requirement already satisfied: pydantic<3,>=1.9.0in /root/miniconda3/lib/python3.12/site-packages (from openai) (2.10.6)
Requirement already satisfied: sniffio in /root/miniconda3/lib/python3.12/site-packages (from openai) (1.3.1)
Requirement already satisfied: tqdm>4in /root/miniconda3/lib/python3.12/site-packages (from openai) (4.67.1)
Requirement already satisfied: typing-extensions<5,>=4.11in /root/miniconda3/lib/python3.12/site-packages (from openai) (4.12.2)
Requirement already satisfied: idna>=2.8in /root/miniconda3/lib/python3.12/site-packages (from anyio<5,>=3.5.0->openai) (3.7)
Requirement already satisfied: certifi in /root/miniconda3/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai) (2025.1.31)
Requirement already satisfied: httpcore==1.* in /root/miniconda3/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai) (1.0.7)
Requirement already satisfied: h11<0.15,>=0.13in /root/miniconda3/lib/python3.12/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)
Requirement already satisfied: annotated-types>=0.6.0in /root/miniconda3/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)
Requirement already satisfied: pydantic-core==2.27.2in /root/miniconda3/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (2.27.2)
[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv[0m[33m
[0m
import openai
openai.__version__
'1.66.3'
from openai import OpenAI
-
传统chat.completion API调用方法回顾
openai_api_key = "YOUR_API_KEY"
base_url = "国内反向代理地址"
# 实例化客户端
client = OpenAI(api_key=openai_api_key,
base_url=base_url)
# 调用 GPT-4o-mini 模型
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "你好,好久不见!"}
]
)
response
ChatCompletion(id='chatcmpl-BAxh80cMoWVaAYMsZx5BwWGyC6ref', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='你好!很高兴见到你!你最近怎么样?有什么我可以帮助你的吗?', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1741952446, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_06737a9306', usage=CompletionUsage(completion_tokens=21, prompt_tokens=13, total_tokens=34, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))
-
GPT4o&o1模型公开课
-
GPT4o入门:https://www.bilibili.com/video/BV1Kh22YAE6Z/
-
OpenAI o1模型入门:https://www.bilibili.com/video/BV1t82UY6Ezh/
一、Responses API基本调用方法介绍
Responses API 是 OpenAI 为智能代理(Agents)提供的全新 API 基础构件,它结合了 Chat Completions API 的简洁性 与 Assistants API 的内置工具能力,使得代理能够更智能地执行任务。
📌 核心特点
-
✅ 简洁易用:继承了 Chat Completions API 的易用性。
-
✅ 增强功能:支持内置工具(Tools),如函数调用(Function Calling)、Web 搜索、文件搜索、计算机控制等。
-
✅ 适用于代理(Agents):可用于构建智能化任务执行系统。
🔗 未来发展:Responses API 旨在成为 OpenAI 代理系统的核心 API,结合 Agents SDK,提供更灵活的任务编排能力。
1.文本生成
使用 OpenAI API,你可以通过一个 简单的提示(prompt) 让模型生成文本,类似于 ChatGPT 的工作方式。
response = client.responses.create(
model="gpt-4o",
input="你好,好久不见,请介绍下你自己!"
)
response
Response(id='resp_67d3e10ecbcc8190bdaeb00e8c76c7360e6b5df3e25d4c00', created_at=1741938958.0, error=None, incomplete_details=None, instructions=None, metadata={}, model='gpt-4o-2024-08-06', object='response', output=[ResponseOutputMessage(id='msg_67d3e10f2a0481909e4046740f0ef58a0e6b5df3e25d4c00', content=[ResponseOutputText(annotations=[], text='你好!我是一款先进的人工智能助手,可以帮助回答问题、提供建议和协助你处理各种信息。我很高兴能够与你交流!如果你有什么问题或者需要帮助,随时告诉我哦。', type='output_text')], role='assistant', status='completed', type='message')], parallel_tool_calls=True, temperature=1.0, tool_choice='auto', tools=[], top_p=1.0, max_output_tokens=None, previous_response_id=None, reasoning=Reasoning(effort=None, generate_summary=None), status='completed', text=ResponseTextConfig(format=ResponseFormatText(type='text')), truncation='disabled', usage=ResponseUsage(input_tokens=36, output_tokens=47, output_tokens_details=OutputTokensDetails(reasoning_tokens=0), total_tokens=83, input_tokens_details={'cached_tokens': 0}), user=None, store=True)
response.output_text
'你好!我是一款先进的人工智能助手,可以帮助回答问题、提供建议和协助你处理各种信息。我很高兴能够与你交流!如果你有什么问题或者需要帮助,随时告诉我哦。'
2. 响应结构
OpenAI 的 API 响应包含一个内容数组(output),每个内容项具有以下结构:
[
{
"id": "msg_67b73f697ba4819183a15cc17d011509",
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "你好!我是一款先进的人工智能助手,可以帮助回答问题、提供建议和协助你处理各种信息。我很高兴能够与你交流!如果你有什么问题或者需要帮助,随时告诉我哦。",
"annotations": []
}
]
}
]
📌 重要说明:
-
output 可能包含多个结果,在多轮对话或批量生成时尤其明显。
-
一些 SDK 提供 output_text 属性,可以直接获取所有文本输出,方便访问文本数据。
-
除了纯文本,模型还可以返回 JSON 结构化数据(称为 Structured Outputs)。
response.output
[ResponseOutputMessage(id='msg_67d3e10f2a0481909e4046740f0ef58a0e6b5df3e25d4c00', content=[ResponseOutputText(annotations=[], text='你好!我是一款先进的人工智能助手,可以帮助回答问题、提供建议和协助你处理各种信息。我很高兴能够与你交流!如果你有什么问题或者需要帮助,随时告诉我哦。', type='output_text')], role='assistant', status='completed', type='message')]
response.output[0]
ResponseOutputMessage(id='msg_67d3e10f2a0481909e4046740f0ef58a0e6b5df3e25d4c00', content=[ResponseOutputText(annotations=[], text='你好!我是一款先进的人工智能助手,可以帮助回答问题、提供建议和协助你处理各种信息。我很高兴能够与你交流!如果你有什么问题或者需要帮助,随时告诉我哦。', type='output_text')], role='assistant', status='completed', type='message')
response.output[0].content
[ResponseOutputText(annotations=[], text='你好!我是一款先进的人工智能助手,可以帮助回答问题、提供建议和协助你处理各种信息。我很高兴能够与你交流!如果你有什么问题或者需要帮助,随时告诉我哦。', type='output_text')]
response.output[0].content[0]
ResponseOutputText(annotations=[], text='你好!我是一款先进的人工智能助手,可以帮助回答问题、提供建议和协助你处理各种信息。我很高兴能够与你交流!如果你有什么问题或者需要帮助,随时告诉我哦。', type='output_text')
response.output[0].content[0].text
'你好!我是一款先进的人工智能助手,可以帮助回答问题、提供建议和协助你处理各种信息。我很高兴能够与你交流!如果你有什么问题或者需要帮助,随时告诉我哦。'
3. 消息角色与指令控制
我们可以使用不同的方式给模型提供指令:
-
使用 instructions 参数 提供全局行为指令,如语气、目标等。(权重最高)
-
使用 input 数组,指定不同角色的消息。
response = client.responses.create(
model="gpt-4o",
instructions="用海盗的口吻说话。",
input="JavaScript 中的分号是可选的吗?",
)
print(response.output_text)
啊,船长,当涉及到JavaScript那片汪洋时,分号这玩意儿确是可选的!不加分号,JavaScript会用它的“自动分号插入”这门巫术帮你上一道,但它有时也会出岔子,让你误入险境。因此,明智的海盗总在适当之处加上分号,以防意外啦!⚓️
📌 在 instructions 中定义“说话像海盗”后,模型会以海盗风格回答。
此外,也可以使用 input 数组还可以指定多种角色,例如使用developer角色,developer 角色类似于 系统设定,用户输入 user 角色的内容,最终 模型按 developer 设定风格回答。
response = client.responses.create(
model="gpt-4o",
input=[
{
"role": "developer",
"content": "用海盗的口吻说话。"
},
{
"role": "user",
"content": "JavaScript 中的分号是可选的吗?"
}
]
)
print(response.output_text)
啊,船长!在 JavaScript 这片汪洋中,分号虽说是可选的,但有时就像海上的避风锚。若是不慎,就可能被自动插入机制给搅了局。聪明的海盗大都在每个语句后头放上分号,以免让风暴打翻了船。小心驶得万年船,啊!
4. 消息角色的优先级
OpenAI 规定不同角色的优先级如下:
角色 | 优先级 | 说明 |
developer | 最高 | 由开发者提供的指令,优先级最高,类似 system。 |
user | 次高 | 由最终用户提供的输入,次于 developer。 |
assistant | 最低 | 由模型生成的响应。 |
5.推理模型调用
-
查看可以调用的模型
models_list = client.models.list()
models_list.data
[Model(id='gpt-4o-mini-audio-preview-2024-12-17', created=1734115920, object='model', owned_by='system'),
Model(id='dall-e-3', created=1698785189, object='model', owned_by='system'),
Model(id='dall-e-2', created=1698798177, object='model', owned_by='system'),
Model(id='gpt-4o-audio-preview-2024-10-01', created=1727389042, object='model', owned_by='system'),
Model(id='gpt-4o-audio-preview', created=1727460443, object='model', owned_by='system'),
Model(id='gpt-4o-mini-realtime-preview-2024-12-17', created=1734112601, object='model', owned_by='system'),
Model(id='gpt-4o-mini-realtime-preview', created=1734387380, object='model', owned_by='system'),
Model(id='o1-mini-2024-09-12', created=1725648979, object='model', owned_by='system'),
Model(id='o1-mini', created=1725649008, object='model', owned_by='system'),
Model(id='omni-moderation-latest', created=1731689265, object='model', owned_by='system'),
Model(id='gpt-4o-mini', created=1721172741, object='model', owned_by='system'),
Model(id='gpt-4o-mini-audio-preview', created=1734387424, object='model', owned_by='system'),
Model(id='omni-moderation-2024-09-26', created=1732734466, object='model', owned_by='system'),
Model(id='gpt-4o-realtime-preview-2024-10-01', created=1727131766, object='model', owned_by='system'),
Model(id='babbage-002', created=1692634615, object='model', owned_by='system'),
Model(id='tts-1-hd-1106', created=1699053533, object='model', owned_by='system'),
Model(id='text-embedding-3-large', created=1705953180, object='model', owned_by='system'),
Model(id='gpt-4o-audio-preview-2024-12-17', created=1734034239, object='model', owned_by='system'),
Model(id='gpt-4', created=1687882411, object='model', owned_by='openai'),
Model(id='gpt-4-0125-preview', created=1706037612, object='model', owned_by='system'),
Model(id='gpt-4o-2024-05-13', created=1715368132, object='model', owned_by='system'),
Model(id='tts-1-hd', created=1699046015, object='model', owned_by='system'),
Model(id='gpt-4-turbo-preview', created=1706037777, object='model', owned_by='system'),
Model(id='o1-preview', created=1725648897, object='model', owned_by='system'),
Model(id='o1-preview-2024-09-12', created=1725648865, object='model', owned_by='system'),
Model(id='gpt-3.5-turbo-instruct-0914', created=1694122472, object='model', owned_by='system'),
Model(id='gpt-4o-mini-search-preview', created=1741391161, object='model', owned_by='system'),
Model(id='o1-2024-12-17', created=1734326976, object='model', owned_by='system'),
Model(id='o1', created=1734375816, object='model', owned_by='system'),
Model(id='tts-1-1106', created=1699053241, object='model', owned_by='system'),
Model(id='davinci-002', created=1692634301, object='model', owned_by='system'),
Model(id='gpt-3.5-turbo-1106', created=1698959748, object='model', owned_by='system'),
Model(id='o3-mini-2025-01-31', created=1738010200, object='model', owned_by='system'),
Model(id='gpt-4o-search-preview', created=1741388720, object='model', owned_by='system'),
Model(id='gpt-4-turbo', created=1712361441, object='model', owned_by='system'),
Model(id='o3-mini', created=1737146383, object='model', owned_by='system'),
Model(id='gpt-3.5-turbo-instruct', created=1692901427, object='model', owned_by='system'),
Model(id='gpt-4o-mini-search-preview-2025-03-11', created=1741390858, object='model', owned_by='system'),
Model(id='gpt-3.5-turbo-0125', created=1706048358, object='model', owned_by='system'),
Model(id='gpt-4o-2024-08-06', created=1722814719, object='model', owned_by='system'),
Model(id='gpt-4o-realtime-preview-2024-12-17', created=1733945430, object='model', owned_by='system'),
Model(id='gpt-3.5-turbo', created=1677610602, object='model', owned_by='openai'),
Model(id='gpt-4-turbo-2024-04-09', created=1712601677, object='model', owned_by='system'),
Model(id='gpt-4o-realtime-preview', created=1727659998, object='model', owned_by='system'),
Model(id='gpt-3.5-turbo-16k', created=1683758102, object='model', owned_by='openai-internal'),
Model(id='gpt-4o', created=1715367049, object='model', owned_by='system'),
Model(id='text-embedding-3-small', created=1705948997, object='model', owned_by='system'),
Model(id='chatgpt-4o-latest', created=1723515131, object='model', owned_by='system'),
Model(id='gpt-4-1106-preview', created=1698957206, object='model', owned_by='system'),
Model(id='text-embedding-ada-002', created=1671217299, object='model', owned_by='openai-internal'),
Model(id='whisper-1', created=1677532384, object='model', owned_by='openai-internal'),
Model(id='gpt-4-0613', created=1686588896, object='model', owned_by='openai'),
Model(id='tts-1', created=1681940951, object='model', owned_by='openai-internal'),
Model(id='gpt-4.5-preview', created=1740623059, object='model', owned_by='system'),
Model(id='computer-use-preview-2025-03-11', created=1741377021, object='model', owned_by='system'),
Model(id='computer-use-preview', created=1734655677, object='model', owned_by='system'),
Model(id='gpt-4.5-preview-2025-02-27', created=1740623304, object='model', owned_by='system'),
Model(id='gpt-4o-search-preview-2025-03-11', created=1741388170, object='model', owned_by='system'),
Model(id='gpt-4o-2024-11-20', created=1739331543, object='model', owned_by='system'),
Model(id='gpt-4o-mini-2024-07-18', created=1721172717, object='model', owned_by='system')]
-
借助o3模型进行调用
response = client.responses.create(
model="o3-mini",
input="你好,请帮我编写一个贪吃蛇小游戏,并能在html中运行。"
)
from IPython.display import display, Code, Markdown
response.output_text
'下面提供一个完整的 HTML 文件示例,实现了一个简单的贪吃蛇小游戏。你只需将下面的代码保存为一个 .html 文件(比如 snake.html),然后用浏览器打开即可运行。\n\n--------------------------------------------------\n<!DOCTYPE html>\n<html lang="zh-CN">\n<head>\n <meta charset="UTF-8">\n <title>贪吃蛇小游戏</title>\n <style>\n /* 设置画布样式 */\n canvas {\n background-color: #000;\n display: block;\n margin: 50px auto;\n border: 1px solid #444;\n }\n </style>\n</head>\n<body>\n <canvas id="gameCanvas" width="400" height="400"></canvas>\n <script>\n // 获取画布及其上下文\n const canvas = document.getElementById("gameCanvas");\n const ctx = canvas.getContext("2d");\n\n // 定义网格大小(每个方块大小)\n const gridSize = 20;\n\n // 定义蛇(由一个包含 x, y 的坐标对象组成的数组)\n let snake = [\n { x: 160, y: 160 },\n { x: 140, y: 160 },\n { x: 120, y: 160 }\n ];\n\n // 定义蛇的移动方向(初始向右移动)\n let dx = gridSize;\n let dy = 0;\n\n // 定义苹果的坐标\n let apple = { x: 0, y: 0 };\n\n // 控制游戏速度(利用 requestAnimationFrame 的“计数器”来降低帧率)\n let count = 0;\n\n // 随机生成苹果位置(保证在画布网格上)\n function randomApple() {\n apple.x = Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize;\n apple.y = Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize;\n }\n randomApple();\n\n // 游戏主循环\n function game() {\n requestAnimationFrame(game);\n\n // 控制帧率,每4帧更新一次(可以根据需要调整)\n if (++count < 4) {\n return;\n }\n count = 0;\n\n // 计算蛇头的新位置\n const head = { x: snake[0].x + dx, y: snake[0].y + dy };\n\n // 将新位置加入蛇头\n snake.unshift(head);\n\n // 判断是否吃到苹果\n if (head.x === apple.x && head.y === apple.y) {\n // 吃到苹果后重新生成苹果位置(不移除蛇尾,蛇身增长)\n randomApple();\n } else {\n // 没有吃到苹果则移除蛇尾(保持长度不变)\n snake.pop();\n }\n\n // 边界检测:如果蛇头碰到画布边界,则重置游戏\n if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height) {\n resetGame();\n return;\n }\n\n // 检测蛇是否撞到自己\n for (let i = 1; i < snake.length; i++) {\n if (head.x === snake[i].x && head.y === snake[i].y) {\n resetGame();\n return;\n }\n }\n\n // 绘制背景\n ctx.fillStyle = "black";\n ctx.fillRect(0, 0, canvas.width, canvas.height);\n\n // 绘制苹果(红色方块)\n ctx.fillStyle = "red";\n ctx.fillRect(apple.x, apple.y, gridSize - 2, gridSize - 2);\n\n // 绘制蛇(绿色方块)\n ctx.fillStyle = "lime";\n snake.forEach(part => {\n ctx.fillRect(part.x, part.y, gridSize - 2, gridSize - 2);\n });\n }\n\n // 键盘事件监听,根据上下左右方向键控制蛇的移动\n document.addEventListener("keydown", e => {\n // 避免蛇反向移动(上下或左右同时只能改变一次)\n if (e.key === "ArrowUp" && dy === 0) {\n dx = 0;\n dy = -gridSize;\n } else if (e.key === "ArrowDown" && dy === 0) {\n dx = 0;\n dy = gridSize;\n } else if (e.key === "ArrowLeft" && dx === 0) {\n dx = -gridSize;\n dy = 0;\n } else if (e.key === "ArrowRight" && dx === 0) {\n dx = gridSize;\n dy = 0;\n }\n });\n\n // 重置游戏,初始化蛇和苹果的状态\n function resetGame() {\n snake = [\n { x: 160, y: 160 },\n { x: 140, y: 160 },\n { x: 120, y: 160 }\n ];\n dx = gridSize;\n dy = 0;\n randomApple();\n }\n\n // 开始游戏主循环\n requestAnimationFrame(game);\n </script>\n</body>\n</html>\n--------------------------------------------------\n\n代码说明:\n1. 在 head 部分引入了简单的 CSS 设置,将 Canvas 居中显示并设置黑色背景。\n2. 使用 Canvas 绘制蛇(用绿色小方块)和苹果(用红色小方块)。\n3. 通过 requestAnimationFrame 进行游戏主循环,并利用计数器来控制更新速率。\n4. 通过监听键盘的 Arrow 按键控制蛇的移动方向,同时避免了直接反向移动的情况。\n5. 如果蛇碰到边界或者碰到自己,调用 resetGame() 重置游戏状态。\n\n你可以根据需要对代码进行进一步扩展,比如添加得分显示、难度调节等。希望这个示例能帮助你实现一个简单的贪吃蛇游戏!'
print(response.output_text)
下面提供一个完整的 HTML 文件示例,实现了一个简单的贪吃蛇小游戏。你只需将下面的代码保存为一个 .html 文件(比如 snake.html),然后用浏览器打开即可运行。
--------------------------------------------------
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>贪吃蛇小游戏</title>
<style>
/* 设置画布样式 */
canvas {
background-color: #000;
display: block;
margin: 50px auto;
border: 1px solid #444;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
// 获取画布及其上下文
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// 定义网格大小(每个方块大小)
const gridSize = 20;
// 定义蛇(由一个包含 x, y 的坐标对象组成的数组)
let snake = [
{ x: 160, y: 160 },
{ x: 140, y: 160 },
{ x: 120, y: 160 }
];
// 定义蛇的移动方向(初始向右移动)
let dx = gridSize;
let dy = 0;
// 定义苹果的坐标
let apple = { x: 0, y: 0 };
// 控制游戏速度(利用 requestAnimationFrame 的“计数器”来降低帧率)
let count = 0;
// 随机生成苹果位置(保证在画布网格上)
function randomApple() {
apple.x = Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize;
apple.y = Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize;
}
randomApple();
// 游戏主循环
function game() {
requestAnimationFrame(game);
// 控制帧率,每4帧更新一次(可以根据需要调整)
if (++count < 4) {
return;
}
count = 0;
// 计算蛇头的新位置
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
// 将新位置加入蛇头
snake.unshift(head);
// 判断是否吃到苹果
if (head.x === apple.x && head.y === apple.y) {
// 吃到苹果后重新生成苹果位置(不移除蛇尾,蛇身增长)
randomApple();
} else {
// 没有吃到苹果则移除蛇尾(保持长度不变)
snake.pop();
}
// 边界检测:如果蛇头碰到画布边界,则重置游戏
if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height) {
resetGame();
return;
}
// 检测蛇是否撞到自己
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
resetGame();
return;
}
}
// 绘制背景
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制苹果(红色方块)
ctx.fillStyle = "red";
ctx.fillRect(apple.x, apple.y, gridSize - 2, gridSize - 2);
// 绘制蛇(绿色方块)
ctx.fillStyle = "lime";
snake.forEach(part => {
ctx.fillRect(part.x, part.y, gridSize - 2, gridSize - 2);
});
}
// 键盘事件监听,根据上下左右方向键控制蛇的移动
document.addEventListener("keydown", e => {
// 避免蛇反向移动(上下或左右同时只能改变一次)
if (e.key === "ArrowUp" && dy === 0) {
dx = 0;
dy = -gridSize;
} elseif (e.key === "ArrowDown" && dy === 0) {
dx = 0;
dy = gridSize;
} elseif (e.key === "ArrowLeft" && dx === 0) {
dx = -gridSize;
dy = 0;
} elseif (e.key === "ArrowRight" && dx === 0) {
dx = gridSize;
dy = 0;
}
});
// 重置游戏,初始化蛇和苹果的状态
function resetGame() {
snake = [
{ x: 160, y: 160 },
{ x: 140, y: 160 },
{ x: 120, y: 160 }
];
dx = gridSize;
dy = 0;
randomApple();
}
// 开始游戏主循环
requestAnimationFrame(game);
</script>
</body>
</html>
--------------------------------------------------
代码说明:
1. 在 head 部分引入了简单的 CSS 设置,将 Canvas 居中显示并设置黑色背景。
2. 使用 Canvas 绘制蛇(用绿色小方块)和苹果(用红色小方块)。
3. 通过 requestAnimationFrame 进行游戏主循环,并利用计数器来控制更新速率。
4. 通过监听键盘的 Arrow 按键控制蛇的移动方向,同时避免了直接反向移动的情况。
5. 如果蛇碰到边界或者碰到自己,调用 resetGame() 重置游戏状态。
你可以根据需要对代码进行进一步扩展,比如添加得分显示、难度调节等。希望这个示例能帮助你实现一个简单的贪吃蛇游戏!
-
responses API参数列表
参数名 | 类型 | 必填/可选 | 默认值 | 说明 |
model | string | 必填 | 无 | 指定要使用的模型 ID,例如 gpt-4o 或 gpt-4o-mini。 |
store | boolean or null | 可选 | false | 是否存储本次对话的输出,供模型精炼或评估产品使用。 |
metadata | object or null | 可选 | null | 开发者自定义的标签和值,用于过滤仪表盘中的补全结果。 |
frequency_penalty | number or null | 可选 | 0 | 数值在 -2.0 到 2.0 之间,正值减少重复生成内容的可能性。 |
logit_bias | map | 可选 | null | 调整某些特定 tokens 出现的可能性,值在 -100 到 100 之间。 |
logprobs | boolean or null | 可选 | false | 是否返回生成的每个 token 的对数概率。 |
top_logprobs | integer or null | 可选 | null | 指定返回最有可能出现的前几个 tokens 及其概率,需开启 logprobs。 |
max_completion_tokens | integer or null | 可选 | null | 指定模型生成的最大 token 数,包括可见文本和推理 tokens。 |
n | integer or null | 可选 | 1 | 每个输入生成的对话补全选项数量,值越大,生成的回复越多。 |
presence_penalty | number or null | 可选 | 0 | 数值在 -2.0 到 2.0 之间,正值鼓励生成新的主题和内容。 |
response_format | object | 可选 | null | 指定生成结果的格式,可以设置为 json_schema 以确保结构化输出,或 json_object 用于 JSON 格式。 |
seed | integer or null | 可选 | null | 保持生成的一致性,重复相同请求将尽量生成相同的结果。 |
service_tier | string or null | 可选 | auto | 指定服务延迟等级,适用于付费订阅用户,默认为 auto。 |
stop | string / array / null | 可选 | null | 最多指定 4 个序列,API 遇到这些序列时会停止生成进一步的 tokens。 |
stream | boolean or null | 可选 | false | 是否启用流式响应,若启用,生成的 tokens 将逐步返回。 |
stream_options | object or null | 可选 | null | 流式响应的选项,仅当 stream 为 true 时设置。 |
temperature | number or null | 可选 | 1 | 控制生成输出的随机性,值越高生成的文本越随机。建议调整此值或 top_p,而不是同时调整。 |
top_p | number or null | 可选 | 1 | 使用核采样方法,选择最有可能的 tokens,总概率达到 top_p 百分比。建议与 temperature 二选一。 |
tools | array | 可选 | null | 模型可以调用的工具列表,目前仅支持函数调用。 |
user | string | 可选 | null | 表示最终用户的唯一标识符,用于监控和检测滥用行为。 |
参数解释:
-
模型和输出相关参数:
-
model 是必填参数,决定使用哪个模型(如 gpt-4o 或 gpt-4o-mini)。
-
store 控制是否存储生成的对话结果,便于后续模型训练或评估。
-
metadata 用于添加开发者自定义的标签,便于在仪表盘中过滤补全结果。
-
max_completion_tokens 和 n 控制生成内容的数量和长度,帮助管理生成成本。
-
-
生成行为控制:
-
frequency_penalty 和 presence_penalty 都用于影响生成结果的内容重复度和新颖性。
-
logit_bias 是用于调整特定 token 出现概率的高级控制工具。
-
temperature 和 top_p 通过不同的方式控制生成结果的随机性,建议选其一进行调整。
-
-
高级功能:
-
logprobs 和 top_logprobs 用于返回每个 token 的概率信息,适合对模型输出进行更细粒度分析。
-
stream 启用后会实时返回生成的结果,适用于需要逐步展示内容的场景。
-
tools 允许模型调用外部工具(如函数),适用于扩展模型的功能。
-
-
服务和用户相关参数:
-
service_tier 控制服务的延迟和稳定性,适合高性能要求的付费用户。
-
user 用于标识最终用户,有助于监控使用行为,防止滥用。
-
三、Web Search(网页搜索)功能实现
OpenAI Agents SDK 支持网页搜索,允许模型在生成回答之前查询最新的信息,类似于 ChatGPT 的搜索功能,并提供清晰的引用来源。
-
官网地址:https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses
response = client.responses.create(
model="gpt-4o",
tools=[{"type": "web_search_preview"}], # 启用 Web 搜索工具
input="今天有什么正面的新闻吗?"
)
print(response.output_text)
以下是近期的一些正面新闻:
1. **香港政商界对降息持积极态度**:2024年9月19日,美国联邦储备委员会四年来首次降息,香港金融管理局随即下调基本利率。香港政商界人士认为,此举有助于降低企业资金成本,对香港经济和资产市场产生正面影响。 ([news.youth.cn](https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai))
2. **中国青年对欧洲及德国评价积极**:2022年7月发布的《中国青年的欧洲观》报告显示,中国青年普遍对欧洲,特别是德国,持积极正面的评价,认为中欧及中德关系总体友好互利,并对未来关系发展持乐观态度。 ([news.youth.cn](https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai))
3. **正面舆情引领新闻报道新路径**:近年来,天津“跳水大爷”、广东龙舟赛“摇头哥”、淄博烧烤等正面舆情事件增多,体现了网络生态的积极变化,为媒体进行正面报道提供了新的思路,有助于克服“强行正能量”等问题。 ([news.qq.com](https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai))
希望这些新闻能为您带来积极的感受。
📌 效果:
-
该 API 请求会调用 web_search_preview,允许模型在回答前搜索最新的新闻。
-
但模型可以自行决定是否使用该工具。
response
Response(id='resp_67d3e43ec15c8190be1d4ef786c5000d04483d504f8a42a4', created_at=1741939774.0, error=None, incomplete_details=None, instructions=None, metadata={}, model='gpt-4o-2024-08-06', object='response', output=[ResponseFunctionWebSearch(id='ws_67d3e43f2d348190af9efa09bfe0101304483d504f8a42a4', status='completed', type='web_search_call'), ResponseOutputMessage(id='msg_67d3e4414c3481908cf889203e6abe8f04483d504f8a42a4', content=[ResponseOutputText(annotations=[AnnotationURLCitation(end_index=215, start_index=122, title='香港政商界:降息对香港经济和资产市场影响正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai'), AnnotationURLCitation(end_index=411, start_index=320, title='中国青年对欧洲及德国总体评价积极正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai'), AnnotationURLCitation(end_index=597, start_index=519, title='正面舆情:新闻规律回归与正面报道新路径_腾讯新闻', type='url_citation', url='https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai')], text='以下是近期的一些正面新闻:\n\n1. **香港政商界对降息持积极态度**:2024年9月19日,美国联邦储备委员会四年来首次降息,香港金融管理局随即下调基本利率。香港政商界人士认为,此举有助于降低企业资金成本,对香港经济和资产市场产生正面影响。 ([news.youth.cn](https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai))\n\n2. **中国青年对欧洲及德国评价积极**:2022年7月发布的《中国青年的欧洲观》报告显示,中国青年普遍对欧洲,特别是德国,持积极正面的评价,认为中欧及中德关系总体友好互利,并对未来关系发展持乐观态度。 ([news.youth.cn](https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai))\n\n3. **正面舆情引领新闻报道新路径**:近年来,天津“跳水大爷”、广东龙舟赛“摇头哥”、淄博烧烤等正面舆情事件增多,体现了网络生态的积极变化,为媒体进行正面报道提供了新的思路,有助于克服“强行正能量”等问题。 ([news.qq.com](https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai))\n\n希望这些新闻能为您带来积极的感受。 ', type='output_text')], role='assistant', status='completed', type='message')], parallel_tool_calls=True, temperature=1.0, tool_choice='auto', tools=[WebSearchTool(type='web_search_preview', search_context_size='medium', user_location=UserLocation(type='approximate', city=None, country='US', region=None, timezone=None))], top_p=1.0, max_output_tokens=None, previous_response_id=None, reasoning=Reasoning(effort=None, generate_summary=None), status='completed', text=ResponseTextConfig(format=ResponseFormatText(type='text')), truncation='disabled', usage=ResponseUsage(input_tokens=326, output_tokens=364, output_tokens_details=OutputTokensDetails(reasoning_tokens=0), total_tokens=690, input_tokens_details={'cached_tokens': 0}), user=None, store=True)
response1 = client.responses.create(
model="gpt-4o",
tools=[{"type": "web_search_preview"}], # 启用 Web 搜索工具
input="请帮我讲个笑话吧。"
)
print(response1.output_text)
当然可以!你听说过那个关于小蘑菇的笑话吗?
为什么小蘑菇去派对?
因为它是一朵“欢人”!(Fun-guy,fungi)
希望你喜欢!😊
2. 强制使用 Web 搜索
如果希望确保模型一定使用 Web 搜索(避免它仅使用内部知识回答),可以设置 tool_choice 参数:
tool_choice={"type": "web_search_preview"}
📌 作用:
-
让 Web 搜索始终执行,而不是让模型决定是否使用搜索工具。
-
提升一致性,但可能会增加查询时间。
3. 输出格式与引用
如果模型调用了 Web 搜索,API 响应将包含两部分:
-
Web 搜索调用的 ID
-
模型的回答,并带有网页来源的引用信息
📌 示例输出:
[
{
"type": "web_search_call",
"id": "ws_67c9fa0502748190b7dd390736892e100be649c1a5ff9609",
"status": "completed"
},
{
"id": "msg_67c9fa077e288190af08fdffda2e34f20be649c1a5ff9609",
"type": "message",
"status": "completed",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "On March 6, 2025, several news...",
"annotations": [
{
"type": "url_citation",
"start_index": 2606,
"end_index": 2758,
"url": "https://...",
"title": "Title..."
}
]
}
]
}
]
response
Response(id='resp_67d3e43ec15c8190be1d4ef786c5000d04483d504f8a42a4', created_at=1741939774.0, error=None, incomplete_details=None, instructions=None, metadata={}, model='gpt-4o-2024-08-06', object='response', output=[ResponseFunctionWebSearch(id='ws_67d3e43f2d348190af9efa09bfe0101304483d504f8a42a4', status='completed', type='web_search_call'), ResponseOutputMessage(id='msg_67d3e4414c3481908cf889203e6abe8f04483d504f8a42a4', content=[ResponseOutputText(annotations=[AnnotationURLCitation(end_index=215, start_index=122, title='香港政商界:降息对香港经济和资产市场影响正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai'), AnnotationURLCitation(end_index=411, start_index=320, title='中国青年对欧洲及德国总体评价积极正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai'), AnnotationURLCitation(end_index=597, start_index=519, title='正面舆情:新闻规律回归与正面报道新路径_腾讯新闻', type='url_citation', url='https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai')], text='以下是近期的一些正面新闻:\n\n1. **香港政商界对降息持积极态度**:2024年9月19日,美国联邦储备委员会四年来首次降息,香港金融管理局随即下调基本利率。香港政商界人士认为,此举有助于降低企业资金成本,对香港经济和资产市场产生正面影响。 ([news.youth.cn](https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai))\n\n2. **中国青年对欧洲及德国评价积极**:2022年7月发布的《中国青年的欧洲观》报告显示,中国青年普遍对欧洲,特别是德国,持积极正面的评价,认为中欧及中德关系总体友好互利,并对未来关系发展持乐观态度。 ([news.youth.cn](https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai))\n\n3. **正面舆情引领新闻报道新路径**:近年来,天津“跳水大爷”、广东龙舟赛“摇头哥”、淄博烧烤等正面舆情事件增多,体现了网络生态的积极变化,为媒体进行正面报道提供了新的思路,有助于克服“强行正能量”等问题。 ([news.qq.com](https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai))\n\n希望这些新闻能为您带来积极的感受。 ', type='output_text')], role='assistant', status='completed', type='message')], parallel_tool_calls=True, temperature=1.0, tool_choice='auto', tools=[WebSearchTool(type='web_search_preview', search_context_size='medium', user_location=UserLocation(type='approximate', city=None, country='US', region=None, timezone=None))], top_p=1.0, max_output_tokens=None, previous_response_id=None, reasoning=Reasoning(effort=None, generate_summary=None), status='completed', text=ResponseTextConfig(format=ResponseFormatText(type='text')), truncation='disabled', usage=ResponseUsage(input_tokens=326, output_tokens=364, output_tokens_details=OutputTokensDetails(reasoning_tokens=0), total_tokens=690, input_tokens_details={'cached_tokens': 0}), user=None, store=True)
response.output
[ResponseFunctionWebSearch(id='ws_67d3e43f2d348190af9efa09bfe0101304483d504f8a42a4', status='completed', type='web_search_call'),
ResponseOutputMessage(id='msg_67d3e4414c3481908cf889203e6abe8f04483d504f8a42a4', content=[ResponseOutputText(annotations=[AnnotationURLCitation(end_index=215, start_index=122, title='香港政商界:降息对香港经济和资产市场影响正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai'), AnnotationURLCitation(end_index=411, start_index=320, title='中国青年对欧洲及德国总体评价积极正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai'), AnnotationURLCitation(end_index=597, start_index=519, title='正面舆情:新闻规律回归与正面报道新路径_腾讯新闻', type='url_citation', url='https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai')], text='以下是近期的一些正面新闻:\n\n1. **香港政商界对降息持积极态度**:2024年9月19日,美国联邦储备委员会四年来首次降息,香港金融管理局随即下调基本利率。香港政商界人士认为,此举有助于降低企业资金成本,对香港经济和资产市场产生正面影响。 ([news.youth.cn](https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai))\n\n2. **中国青年对欧洲及德国评价积极**:2022年7月发布的《中国青年的欧洲观》报告显示,中国青年普遍对欧洲,特别是德国,持积极正面的评价,认为中欧及中德关系总体友好互利,并对未来关系发展持乐观态度。 ([news.youth.cn](https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai))\n\n3. **正面舆情引领新闻报道新路径**:近年来,天津“跳水大爷”、广东龙舟赛“摇头哥”、淄博烧烤等正面舆情事件增多,体现了网络生态的积极变化,为媒体进行正面报道提供了新的思路,有助于克服“强行正能量”等问题。 ([news.qq.com](https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai))\n\n希望这些新闻能为您带来积极的感受。 ', type='output_text')], role='assistant', status='completed', type='message')]
len(response.output)
2
response.output[0]
ResponseFunctionWebSearch(id='ws_67d3e43f2d348190af9efa09bfe0101304483d504f8a42a4', status='completed', type='web_search_call')
response.output[1]
ResponseOutputMessage(id='msg_67d3e4414c3481908cf889203e6abe8f04483d504f8a42a4', content=[ResponseOutputText(annotations=[AnnotationURLCitation(end_index=215, start_index=122, title='香港政商界:降息对香港经济和资产市场影响正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai'), AnnotationURLCitation(end_index=411, start_index=320, title='中国青年对欧洲及德国总体评价积极正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai'), AnnotationURLCitation(end_index=597, start_index=519, title='正面舆情:新闻规律回归与正面报道新路径_腾讯新闻', type='url_citation', url='https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai')], text='以下是近期的一些正面新闻:\n\n1. **香港政商界对降息持积极态度**:2024年9月19日,美国联邦储备委员会四年来首次降息,香港金融管理局随即下调基本利率。香港政商界人士认为,此举有助于降低企业资金成本,对香港经济和资产市场产生正面影响。 ([news.youth.cn](https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai))\n\n2. **中国青年对欧洲及德国评价积极**:2022年7月发布的《中国青年的欧洲观》报告显示,中国青年普遍对欧洲,特别是德国,持积极正面的评价,认为中欧及中德关系总体友好互利,并对未来关系发展持乐观态度。 ([news.youth.cn](https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai))\n\n3. **正面舆情引领新闻报道新路径**:近年来,天津“跳水大爷”、广东龙舟赛“摇头哥”、淄博烧烤等正面舆情事件增多,体现了网络生态的积极变化,为媒体进行正面报道提供了新的思路,有助于克服“强行正能量”等问题。 ([news.qq.com](https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai))\n\n希望这些新闻能为您带来积极的感受。 ', type='output_text')], role='assistant', status='completed', type='message')
response.output[1].content
[ResponseOutputText(annotations=[AnnotationURLCitation(end_index=215, start_index=122, title='香港政商界:降息对香港经济和资产市场影响正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai'), AnnotationURLCitation(end_index=411, start_index=320, title='中国青年对欧洲及德国总体评价积极正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai'), AnnotationURLCitation(end_index=597, start_index=519, title='正面舆情:新闻规律回归与正面报道新路径_腾讯新闻', type='url_citation', url='https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai')], text='以下是近期的一些正面新闻:\n\n1. **香港政商界对降息持积极态度**:2024年9月19日,美国联邦储备委员会四年来首次降息,香港金融管理局随即下调基本利率。香港政商界人士认为,此举有助于降低企业资金成本,对香港经济和资产市场产生正面影响。 ([news.youth.cn](https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai))\n\n2. **中国青年对欧洲及德国评价积极**:2022年7月发布的《中国青年的欧洲观》报告显示,中国青年普遍对欧洲,特别是德国,持积极正面的评价,认为中欧及中德关系总体友好互利,并对未来关系发展持乐观态度。 ([news.youth.cn](https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai))\n\n3. **正面舆情引领新闻报道新路径**:近年来,天津“跳水大爷”、广东龙舟赛“摇头哥”、淄博烧烤等正面舆情事件增多,体现了网络生态的积极变化,为媒体进行正面报道提供了新的思路,有助于克服“强行正能量”等问题。 ([news.qq.com](https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai))\n\n希望这些新闻能为您带来积极的感受。 ', type='output_text')]
response.output[1].content[0]
ResponseOutputText(annotations=[AnnotationURLCitation(end_index=215, start_index=122, title='香港政商界:降息对香港经济和资产市场影响正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai'), AnnotationURLCitation(end_index=411, start_index=320, title='中国青年对欧洲及德国总体评价积极正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai'), AnnotationURLCitation(end_index=597, start_index=519, title='正面舆情:新闻规律回归与正面报道新路径_腾讯新闻', type='url_citation', url='https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai')], text='以下是近期的一些正面新闻:\n\n1. **香港政商界对降息持积极态度**:2024年9月19日,美国联邦储备委员会四年来首次降息,香港金融管理局随即下调基本利率。香港政商界人士认为,此举有助于降低企业资金成本,对香港经济和资产市场产生正面影响。 ([news.youth.cn](https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai))\n\n2. **中国青年对欧洲及德国评价积极**:2022年7月发布的《中国青年的欧洲观》报告显示,中国青年普遍对欧洲,特别是德国,持积极正面的评价,认为中欧及中德关系总体友好互利,并对未来关系发展持乐观态度。 ([news.youth.cn](https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai))\n\n3. **正面舆情引领新闻报道新路径**:近年来,天津“跳水大爷”、广东龙舟赛“摇头哥”、淄博烧烤等正面舆情事件增多,体现了网络生态的积极变化,为媒体进行正面报道提供了新的思路,有助于克服“强行正能量”等问题。 ([news.qq.com](https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai))\n\n希望这些新闻能为您带来积极的感受。 ', type='output_text')
response.output[1].content[0].annotations
[AnnotationURLCitation(end_index=215, start_index=122, title='香港政商界:降息对香港经济和资产市场影响正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai'),
AnnotationURLCitation(end_index=411, start_index=320, title='中国青年对欧洲及德国总体评价积极正面_新闻频道_中国青年网', type='url_citation', url='https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai'),
AnnotationURLCitation(end_index=597, start_index=519, title='正面舆情:新闻规律回归与正面报道新路径_腾讯新闻', type='url_citation', url='https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai')]
response.output[1].content[0].text
'以下是近期的一些正面新闻:\n\n1. **香港政商界对降息持积极态度**:2024年9月19日,美国联邦储备委员会四年来首次降息,香港金融管理局随即下调基本利率。香港政商界人士认为,此举有助于降低企业资金成本,对香港经济和资产市场产生正面影响。 ([news.youth.cn](https://news.youth.cn/jsxw/202409/t20240919_15529696.htm?utm_source=openai))\n\n2. **中国青年对欧洲及德国评价积极**:2022年7月发布的《中国青年的欧洲观》报告显示,中国青年普遍对欧洲,特别是德国,持积极正面的评价,认为中欧及中德关系总体友好互利,并对未来关系发展持乐观态度。 ([news.youth.cn](https://news.youth.cn/gj/202207/t20220726_13871411.htm?utm_source=openai))\n\n3. **正面舆情引领新闻报道新路径**:近年来,天津“跳水大爷”、广东龙舟赛“摇头哥”、淄博烧烤等正面舆情事件增多,体现了网络生态的积极变化,为媒体进行正面报道提供了新的思路,有助于克服“强行正能量”等问题。 ([news.qq.com](https://news.qq.com/rain/a/20240908A00ZCQ00?utm_source=openai))\n\n希望这些新闻能为您带来积极的感受。 '
4. 指定位置搜索
Web 搜索可以根据用户的位置优化搜索结果。你可以指定:
-
country(国家):两字母 ISO 代码,如 "US"(美国)、"GB"(英国)。
-
city(城市):如 "London"(伦敦)。
-
region(地区):如 "California"(加州)。
-
timezone(时区):如 "America/Chicago"(芝加哥时间)。
response = client.responses.create(
model="gpt-4o",
tools=[{
"type": "web_search_preview",
"user_location": {
"type": "approximate",
"country": "CN",
"city": "Beijing",
"region": "Beijing",
}
}],
input="北京三里屯附近最好吃的餐厅有哪些?",
)
print(response.output_text)
北京三里屯地区汇聚了众多美食餐厅,以下是一些备受好评的餐厅供您参考:
**[一坐一忘云南菜(三里屯店)](https://www.google.com/maps/search/%E4%B8%80%E5%9D%90%E4%B8%80%E5%BF%98%E4%BA%91%E5%8D%97%E8%8F%9C%EF%BC%88%E4%B8%89%E9%87%8C%E5%B1%AF%E5%BA%97%EF%BC%89%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
创立于2006年的云南菜品牌,餐厅面积700多平方米,可同时容纳140多人就餐。招牌菜包括香茅草烤鲈鱼、丽江腊排骨锅、普洱酸菜酥红豆等。
**[京雅堂](https://www.google.com/maps/search/%E4%BA%AC%E9%9B%85%E5%A0%82%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
以特色北京烤鸭闻名,鸭皮酥脆,肉质细嫩。其他推荐菜品有三杯罗勒鳕鱼煲、凉菜话梅小番茄、椿苗炒虾仁等。人均消费约239元。
**[大董(工体店)](https://www.google.com/maps/search/%E5%A4%A7%E8%91%A3%EF%BC%88%E5%B7%A5%E4%BD%93%E5%BA%97%EF%BC%89%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
以创新烤鸭和精致菜品著称,推荐菜品有董式烧海参、樱桃鹅肝、扬州炒饭等。人均消费约308元。
**[奶奶家·幸福里](https://www.google.com/maps/search/%E5%A5%B6%E5%A5%B6%E5%AE%B6%C2%B7%E5%B9%B8%E7%A6%8F%E9%87%8C%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
提供家常菜,推荐菜品有鸡丝凉面、宫保鸡腿肉、鸡软骨疙瘩汤等。人均消费约52元。
**[和盛斋老北京菜馆(三里屯店)](https://www.google.com/maps/search/%E5%92%8C%E7%9B%9B%E6%96%8B%E8%80%81%E5%8C%97%E4%BA%AC%E8%8F%9C%E9%A6%86%EF%BC%88%E4%B8%89%E9%87%8C%E5%B1%AF%E5%BA%97%EF%BC%89%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
主打老北京风味菜肴,推荐菜品有炸酱面、青菜豆腐、豌豆黄、稣焖鲫鱼等。人均消费约55元。
**[1949全鸭季(三里屯店)](https://www.google.com/maps/search/1949%E5%85%A8%E9%B8%AD%E5%AD%A3%EF%BC%88%E4%B8%89%E9%87%8C%E5%B1%AF%E5%BA%97%EF%BC%89%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
以烤鸭闻名,鸭皮酥脆,肉质细嫩。其他推荐菜品有肠粉、萝卜糕、辣油拌笋等。人均消费约403元。
**[东田私家菜(三里屯店)](https://www.google.com/maps/search/%E4%B8%9C%E7%94%B0%E7%A7%81%E5%AE%B6%E8%8F%9C%EF%BC%88%E4%B8%89%E9%87%8C%E5%B1%AF%E5%BA%97%EF%BC%89%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
提供京味儿私家菜,推荐菜品有烙饼卷带鱼、腊八蒜猪肝、干锅有机花菜等。人均消费约98元。
**[唐廊(工体店)](https://www.google.com/maps/search/%E5%94%90%E5%BB%8A%EF%BC%88%E5%B7%A5%E4%BD%93%E5%BA%97%EF%BC%89%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
提供中式菜肴,推荐菜品有烤鸭、宫保虾球、牛仔粒等。人均消费约199元。
**[三里屯面馆](https://www.google.com/maps/search/%E4%B8%89%E9%87%8C%E5%B1%AF%E9%9D%A2%E9%A6%86%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
以拌面闻名,推荐菜品有茄子肉丁鸡蛋拌面、辣爆小公鸡拌面等。
**[MustGuette红邮筒餐厅](https://www.google.com/maps/search/MustGuette%E7%BA%A2%E9%82%AE%E7%AD%92%E9%A4%90%E5%8E%85%2C+%E5%8C%97%E4%BA%AC%2C+%E4%B8%AD%E5%9B%BD)**
_北京, 中国_
伦敦主题餐厅,推荐菜品有大虾牛油果沙拉、经典牛肉堡、铁板鸡翅等。
以上餐厅各具特色,您可以根据个人口味和喜好选择尝试。
篇幅有限,此部分为完整课件的上半部分,课件还有文件搜索(File Search)+计算机使用(Computer Use)的详细讲解,加入社区即可获取⬇️
为每个人提供最有价值的技术赋能!【公益】大模型技术社区已经上线!
内容完全免费,涵盖20多套工业级方案 + 10多个企业实战项目 + 400万开发者筛选的实战精华~不定期开展大模型硬核技术直播公开课,对标市面千元价值品质,社区成员限时免费听喔!
📍完整视频讲解+学习课件+项目源码包获取⬇️请点击原文进入赋范大模型技术社区即可领取~