OpenAI 讲解如何为个人智能体实现时间感知与异步输入
Time awareness and async input for personal agents | OpenAI Developers
OpenAI 介绍如何利用 Agents API 的时间感知与异步函数工具,让个人智能体在截止时间内持续工作并接收用户补充信息。时间感知会定期提醒模型当前时间,使其临近期限时优先核验发现并撰写结果;异步工具则允许应用等待用户回答的同时继续处理任务。文章以会议简报智能体为例,给出了配置、处理异步问题、返回工具结果及提取最终答案的代码。
推荐理由
精选AI 评分 75
文章以限时会议简报为例,展示智能体如何边等待用户补充信息边推进任务,并按截止时间调整工作重点。
主题
正文 · AI 翻译
如果你正在构建一个类似 dot 的个人智能体,通常会希望它能在截止时间前向用户交付结果。
假设 15 分钟后我要与一位潜在客户开会。我让智能体研究这家公司并准备一份简报,同时在会议开始前留出三分钟让我阅读。这是我在工作日准备会议时相当常见的时间安排 😬。
智能体注意到,我没有说明这次会议是技术评估,还是关于定价的讨论。这一区别会影响它应调查哪些细节,以及简报中应包含哪些内容。
智能体决定询问会议的重点。在等待我回复期间,它应如何利用剩余时间、在收到回复时将其纳入工作,并且即使我始终没有回答,也能交付一份有用的简报?
时间感知与异步输入
时间感知是 Agents API 最近新增的功能,它会定期提醒模型当前时间,使模型能够根据截止时间调整工作。异步函数工具则允许智能体继续工作,同时应用程序等待最终用户的回答。
距离会议还有 15 分钟,因此智能体有 12 分钟准备我的简报。随着截止时间临近,智能体可以优先核查最有把握的发现并撰写简报,而不是开启另一条研究线索。相反,如果给出更长的时间,模型就会知道自己可以进行更深入、更充分的研究。
像“我 15 分钟后要开会。请提前三分钟给我一份简报,让我有时间审阅”这样的请求设定了截止时间。当搜索和用户输入仍在等待中时,时间感知功能会持续让模型了解当前时间。
下午 4:00 简报应于 4:12 前完成 开始撰写简报 简报到期 会议 智能体 正在研究公司 向你提问 会议重点未明确 笔记 Pied Piper 会议简报 公司背景 近期公告 会议将在 15 分钟后开始。
为了展示其工作方式,我们将设置一个帮助用户准备会议的智能体。在图示中,我于 UTC 时间下午 4 点请求在下午 4:12 前收到一份简报,为下午 4:15 的会议做准备。我们将了解如何配置智能体、提供时间更新、响应其工具调用,以及处理其最终结果。
你可以直接跳到你偏好的编程语言对应的完整示例代码。
创建智能体
为了帮助用户准备会议,我们的智能体需要研究公司、询问缺失的会议背景信息,并跟踪准备简报所剩的时间。要完成这些任务,智能体需要网页搜索、用于请求用户输入的异步工具,以及时间感知功能。
我们使用 SDK 的智能体创建方法,通过以下配置创建智能体:
将 experimental.time_awareness 设为 true,以便定期提醒模型当前时间。
以实时模式启用内置的 web_search 工具,使智能体能够在网上搜索最新信息。
定义一个名为 request_user_input_async 的函数工具。将 async 设为 true,这样在你的应用程序收集回复时,智能体可以继续工作。
将 OPENAI_API_KEY 设置为具有 Agents API 访问权限的项目所提供的密钥。按照 SDK 库指南安装官方 SDK,并创建下文使用的客户端。有关所需权限,请参阅 Agents API 快速入门的先决条件。创建会议准备智能体 Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38const requests = new AbortController();
const agent = await client.beta.agents.create( { name: "Personal assistant", model: "gpt-6.2-astra", instructions: "You are a personal assistant. Deliver useful, well-sourced answers within the user's time constraints. " + "Ask for clarification when it would materially improve the result, and keep making progress while a reply is pending. " + "State important uncertainties.", experimental: { time_awareness: true }, tools: [ { type: "web_search", mode: "live" }, { type: "function", name: "request_user_input_async", description: "Ask the user for context. The result contains their reply or reports that the question expired.", async: true, parameters: { type: "object", properties: { question: { type: "string" }, choices: { type: "array", items: { type: "string" }, description: "Optional suggested replies to display with the question.", }, }, required: ["question"], additionalProperties: false, }, }, ], }, { signal: requests.signal } );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30async def create_agent(client: AsyncOpenAI): return await client.beta.agents.create( name="Personal assistant", model="gpt-6.2-astra", experimental={"time_awareness": True}, instructions=( "You are a personal assistant. Deliver useful, well-sourced answers " "within the user's time constraints. Ask for clarification when it " "would materially improve the result, and keep making progress while " "a reply is pending. State important uncertainties." ), tools=[ {"type": "web_search", "mode": "live"}, { "type": "function", "name": "request_user_input_async", "description": "Ask for missing context while continuing independent work.", "async": True, "parameters": { "type": "object", "properties": { "question": {"type": "string"}, "choices": {"type": "array", "items": {"type": "string"}}, }, "required": ["question"], "additionalProperties": False, }, }, ], )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26func createAgent(ctx context.Context, client *openai.Client) (*openai.Agent, error) { return client.Beta.Agents.New(ctx, openai.BetaAgentNewParams{ Name: openai.String("Personal assistant"), Model: "gpt-6.2-astra", Instructions: openai.String("You are a personal assistant. Deliver useful, well-sourced answers within the user's time constraints. " + "Ask for clarification when it would materially improve the result, and keep making progress while a reply is pending. " + "State important uncertainties."), Experimental: openai.BetaAgentNewParamsExperimental{TimeAwareness: openai.Bool(true)}, Tools: []openai.PersistedAgentToolParamUnion{ {OfParamWebSearch: &openai.PersistedAgentToolParamWebSearch{Mode: "live"}}, {OfParamFunction: &openai.PersistedAgentToolParamFunction{ Name: "request_user_input_async", Description: "Ask for missing context while continuing independent work.", Async: openai.Bool(true), Parameters: map[string]any{ "type": "object", "properties": map[string]any{ "question": map[string]any{"type": "string"}, "choices": map[string]any{"type": "array", "items": map[string]any{"type": "string"}}, }, "required": []string{"question"}, "additionalProperties": false, }, }}, }, }) }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50var agent = client .beta() .agents() .create( AgentCreateParams.builder() .name("Personal assistant") .model("gpt-6.2-astra") .instructions( "You are a personal assistant. Deliver useful, well-sourced answers" + " within the user's time constraints. Ask for clarification when it" + " would materially improve the result, and keep making progress" + " while a reply is pending. State important uncertainties.") .experimental( AgentCreateParams.Experimental.builder().timeAwareness(true).build()) .addTool( PersistedAgentToolParam.WebSearch.builder() .mode(PersistedAgentToolParam.WebSearch.Mode.LIVE) .build()) .addTool( PersistedAgentToolParam.Function.builder() .name("request_user_input_async") .description( "Ask the user for context. The result contains their reply or" + " reports that the question expired.") .async(true) .parameters( PersistedAgentToolParam.Function.Parameters.builder() .putAdditionalProperty("type", JsonValue.from("object")) .putAdditionalProperty( "properties", JsonValue.from( Map.of( "question", Map.of("type", "string"), "choices", Map.of( "type", "array", "items", Map.of("type", "string"), "description", "Optional suggested replies to" + " display with the question.")))) .putAdditionalProperty( "required", JsonValue.from(List.of("question"))) .putAdditionalProperty( "additionalProperties", JsonValue.from(false)) .build()) .build()) .build());
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35def create_agent(client) client.beta.agents.create( name: "Personal assistant", model: "gpt-6.2-astra", experimental: { time_awareness: true }, instructions: "You are a personal assistant. Deliver useful, well-sourced answers " \ "within the user's time constraints. Ask for clarification when it would " \ "materially improve the result, and keep making progress while a reply is " \ "pending. State important uncertainties.", tools: [ { type: "web_search", mode: "live" }, { type: "function", name: "request_user_input_async", description: "Ask for missing context while continuing independent work.", async: true, parameters: { type: "object", properties: { question: { type: "string" }, choices: { type: "array", items: { type: "string" } } }, required: ["question"], additionalProperties: false } } ] ) end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33agent=$(curl --fail-with-body --silent --show-error --max-time 30 \ https://api.openai.com/v1/agents \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: agents=v1" \ -H "Content-Type: application/json" \ --data-binary @- "$work/events" & stream_pid=$!
显示待处理的问题
当智能体询问会议的重点时,应用程序需要在公司研究继续进行的同时显示该问题。在单独的任务中收集回复,可以让应用程序继续接收智能体的更新。
当模型通过工具调用要求澄清时,由你的应用程序代码负责收集用户输入。
监视会话事件流,查找包含 request_user_input_async 函数调用的 agent.session.turn.item.done 事件。从 item.arguments 中读取问题和任何建议选项。会话的 required_actions 列表描述的是阻塞操作,因此等待其中出现条目可能会错过异步问题。
保存会话 ID 以及调用的 turn_id 和 call_id,以便将回复与问题关联起来。跟踪待处理的问题,确保每个问题只显示和处理一次,并在智能体运行结束时取消其输入处理程序:处理待处理的问题Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23if (event.type === "agent.session.created") { sessionId = event.session.id; } if (event.type === "agent.session.turn.item.done") { const call = event.item; if ( call.type === "function_call" && call.name === "request_user_input_async" ) { const key = `${call.turn_id}:${call.call_id}`; if (!seenCalls.has(key)) { seenCalls.add(key); // Start collecting the reply without blocking the event stream. const task = collectReply(event.session_id, call).catch( (error: unknown) => { replyError = error; requests.abort(); } ); replyTasks.add(task); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16item = event.item if ( isinstance(item, AgentFunctionCallItem) and item.name == "request_user_input_async" and item.call_id not in questions ): questions[item.call_id] = asyncio.create_task( respond( client, event.session_id, item, replies, prompt_lock, reply_cutoff, ) )
1 2 3 4 5 6 7 8 9item := event.AsAgentSessionTurnItemDone().Item if item.Type == "function_call" && item.Name == "request_user_input_async" { call := item.AsFunctionCall() key := call.TurnID + "/" + call.CallID if !seen[key] { seen[key] = true pending = append(pending, question{call: call}) } }
1 2 3 4 5 6 7 8 9 10 11 12 13event.created().ifPresent(created -> sessionId = created.session().id()); if (event.turnItemDone().isPresent()) { var item = event.turnItemDone().get().item(); if (item.functionCall().isPresent()) { var call = item.functionCall().get(); String key = call.turnId() + ":" + call.callId(); if (call.name().equals("request_user_input_async") && seenCalls.add(key)) { // Collect the reply on another thread while consuming session events. replyWorker.submit( () -> collectReply(event.turnItemDone().get().sessionId(), call)); } } }
1 2 3 4 5 6 7 8 9 10 11item = event.item if item.is_a?(OpenAI::Models::Beta::AgentFunctionCallItem) && item.name == "request_user_input_async" && !questions.key?(item.call_id) questions[item.call_id] = Thread.new(item, event.session_id) do |call, id| answer = console_reply(call.arguments, input_lock, cutoff) return_result(sessions, id, call, answer) rescue OpenAI::Errors::ConflictError # The turn can finish while the reply is in flight. nil end end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25# Buffer terminal replies independently of the event stream. next_reply=1 ( number=1 while IFS= read -r line || [ -n "$line" ]; do printf '%s\n' "$line" >"$work/incoming" mv "$work/incoming" "$work/reply-$number" number=$((number + 1)) done ) /dev/null; then call=$(jq -c '.item' /dev/null; then seen=$(jq -c --arg key "$key" '.[$key] = true' &2 fi fi
问题以函数调用事件的形式到达:
{ "type": "agent.session.turn.item.done", "item": { "type": "function_call", "name": "request_user_input_async", "arguments": { "question": "这次会议主要是技术评估还是商务讨论?", "choices": ["技术评估", "商务讨论"] }, "turn_id": "turn_789abcd", "call_id": "call_012ef34" } }
返回工具结果
用户对智能体问题的回答有助于智能体根据正确的会议目标来准备简报。如果用户始终没有回答,智能体仍然需要时间完成撰写,因此应用程序会在下午 4:10(即交付前两分钟)关闭未回答的问题。
在智能体运行期间,为每个待处理的调用返回一个工具结果:
收到回复时,将 status 设为 answered,并把键入的文本或选定的选项放入 answer。
到达截止时间时,如果同一 call_id 仍处于待处理状态且用户尚未回复,则返回 {"status":"no_answer"}。
通过 SDK 的会话事件方法发送结果,并使用保存的标识符将结果与问题匹配。提交结果时继续消费事件流。如果智能体提前完成,则在界面中关闭待处理的问题。返回回复或过期结果Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24async function returnResult( sessionId: string, call: AgentFunctionCallItem, answer: string | undefined ) { const result = answer ? { status: "answered", answer } : { status: "no_answer" }; await client.beta.agents.sessions.events.create( sessionId, { events: [ { type: "agent.session.input.tool_result", turn_id: call.turn_id, call_id: call.call_id, success: true, output: JSON.stringify(result), }, ], }, { signal: requests.signal } ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15result = ( {"status": "answered", "answer": answer} if answer else {"status": "no_answer"} ) await client.beta.agents.sessions.events.create( session_id, events=[ { "type": "agent.session.input.tool_result", "turn_id": call.turn_id, "call_id": call.call_id, "success": True, "output": json.dumps(result), } ], )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18func returnResult(ctx context.Context, client *openai.Client, sessionID string, call openai.AgentFunctionCallItem, answer string) error { result := map[string]string{"status": "no_answer"} if answer != "" { result = map[string]string{"status": "answered", "answer": answer} } output, err := json.Marshal(result) if err != nil { return err } return client.Beta.Agents.Sessions.Events.New(ctx, sessionID, openai.BetaAgentSessionEventNewParams{ Events: []openai.AgentSessionInputParamUnion{{ OfParamAgentSessionInputToolResult: &openai.AgentSessionInputParamAgentSessionInputToolResult{ TurnID: call.TurnID, CallID: call.CallID, Success: true, Output: openai.AgentFunctionCallOutputParamUnion{OfString: openai.String(string(output))}, }, }}, }) }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23private void returnResult(String sessionId, AgentFunctionCallItem call, String answer) throws Exception { var result = answer == null || answer.isBlank() ? Map.of("status", "no_answer") : Map.of("status", "answered", "answer", answer); client .beta() .agents() .sessions() .events() .create( EventCreateParams.builder() .sessionId(sessionId) .addEvent( AgentSessionInputParam.AgentSessionInputToolResult.builder() .turnId(call.turnId()) .callId(call.callId()) .success(true) .output(new JsonMapper().writeValueAsString(result)) .build()) .build()); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22def return_result(sessions, session_id, call, answer) result = if answer.nil? || answer.empty? { status: "no_answer" } else { status: "answered", answer: answer } end sessions.events.create( session_id, events: [ { type: "agent.session.input.tool_result", turn_id: call.turn_id, call_id: call.call_id, success: true, output: JSON.generate(result) } ] ) end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16return_result() { local turn_id=$1 call_id=$2 answer=$3 result payload result=$(jq -nc --arg answer "$answer" ' if $answer == "" then {status: "no_answer"} else {status: "answered", answer: $answer} end') payload=$(jq -nc --arg turn "$turn_id" --arg call "$call_id" --arg output "$result" '{ events: [{type: "agent.session.input.tool_result", turn_id: $turn, call_id: $call, success: true, output: $output}] }') curl --fail-with-body --silent --show-error --max-time 30 \ "https://api.openai.com/v1/agents/sessions/$session_id/events" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: agents=v1" \ -H "Content-Type: application/json" \ --data-binary "$payload" >/dev/null }
检索并交付答案
简报准备好后立即交付一份简明版本,至少为用户留出三分钟在会议前阅读。如果先到截止时间,则返回最新完成的简报,或者报告尚无经过核实的发现可用。
当 agent.session.turn.completed 到达时,使用 SDK 的回合项目方法检索已保存的项目。找到 phase 为 final_answer 的已完成智能体消息。
如果数据流提前关闭,请列出会话中的回合以检查已保存的状态。有关重新连接和检索已保存工作的信息,请参阅数据流恢复。检索最终简报Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20async function retrieveAnswer(sessionId: string, turnId: string) { for await (const item of client.beta.agents.sessions.turns.items.list( turnId, { session_id: sessionId, order: "desc" }, { signal: requests.signal } )) { if ( item.type === "message" && item.role === "assistant" && item.status === "completed" && item.phase === "final_answer" ) { return item.content .filter((part) => part.type === "output_text") .map((part) => part.text) .join("\n"); } } throw new Error("The completed turn has no final answer"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20def final_text(item: AgentSessionItem | AgentOutputItem) -> str: if ( item.type == "message" and item.role == "assistant" and item.status == "completed" and item.phase == "final_answer" ): return "\n".join( part.text for part in item.content if part.type == "output_text" ) return ""
async def retrieve_answer(client: AsyncOpenAI, session_id: str, turn_id: str) -> str: async for item in client.beta.agents.sessions.turns.items.list( turn_id, session_id=session_id, order="desc" ): if text := final_text(item): return text return ""
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17func retrieveAnswer(ctx context.Context, client *openai.Client, sessionID, turnID string) (string, error) { items := client.Beta.Agents.Sessions.Turns.Items.ListAutoPaging(ctx, sessionID, turnID, openai.BetaAgentSessionTurnItemListParams{Order: "desc"}) for items.Next() { item := items.Current() if item.Type == "message" && item.Role == "assistant" && item.Status == "completed" && item.Phase == "final_answer" { var parts []string for _, part := range item.AsMessage().Content { if part.Type == "output_text" { parts = append(parts, part.Text) } } return strings.Join(parts, "\n"), nil } } return "", items.Err() }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28private String retrieveAnswer(String sessionId, String turnId) { var items = client .beta() .agents() .sessions() .turns() .items() .list( ItemListParams.builder() .sessionId(sessionId) .turnId(turnId) .order(ItemListParams.Order.DESC) .build()); for (var item : items.autoPager()) { if (item.message().isEmpty()) continue; var message = item.message().get(); if (message.role().toString().equals("assistant") && message.status().toString().equals("completed") && message.phase().map(phase -> phase.toString().equals("final_answer")).orElse(false)) { return message.content().stream() .flatMap(part -> part.outputText().stream()) .map(part -> part.text()) .collect(Collectors.joining("\n")); } } throw new IllegalStateException("已完成的轮次没有最终答案"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16def final_text(item) return unless item.type.to_s == "message" && item.role.to_s == "assistant" && item.status.to_s == "completed" && item.phase.to_s == "final_answer"
text = item.content.select { |part| part.type.to_s == "output_text" }.map(&:text).join("\n") text unless text.empty? end
def retrieve_answer(sessions, session_id, turn_id) page = sessions.turns.items.list(turn_id, session_id: session_id, order: "desc") page.auto_paging_each do |item| text = final_text(item) return text if text && !text.empty? end nil end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18retrieve_answer() { local turn_id=$1 page after="" text local -a page_args=() while :; do page=$(curl --fail-with-body --silent --show-error --max-time 30 --get \ "https://api.openai.com/v1/agents/sessions/$session_id/turns/$turn_id/items" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Beta: agents=v1" \ --data-urlencode order=desc ${page_args[@]+"${page_args[@]}"}) text=$(jq -r '[.data[] | select(.type == "message" and .role == "assistant" and .status == "completed" and .phase == "final_answer") | [.content[] | select(.type == "output_text") | .text] | join("\n")][0] // empty' void>(); const seenCalls = new Set (); const replyTasks = new Set >(); let agentId: string | undefined; let sessionId: string | undefined; let turnActive = false; let delivered = false; let deadlineReached = false; let finalAnswer = ""; let replyError: unknown;
terminal.on("line", (line) => { const waiting = waitingReplies.values().next().value; if (waiting) { waiting( Date.now() { return new Promise((resolve) => { const key = `${call.turn_id}:${call.call_id}`; let timer: ReturnType | undefined; const finish = (answer?: string) => { clearTimeout(timer); waitingReplies.delete(key); resolve(answer); }; waitingReplies.set(key, finish); timer = setTimeout(finish, Math.max(0, replyCutoff.getTime() - Date.now())); if (Date.now() >= replyCutoff.getTime()) finish(); else if (bufferedReplies.length) finish(bufferedReplies.shift() || undefined); }); }
async function returnResult( sessionId: string, call: AgentFunctionCallItem, answer: string | undefined ) { const result = answer ? { status: "answered", answer } : { status: "no_answer" }; await client.beta.agents.sessions.events.create( sessionId, { events: [ { type: "agent.session.input.tool_result", turn_id: call.turn_id, call_id: call.call_id, success: true, output: JSON.stringify(result), }, ], }, { signal: requests.signal } ); }
async function collectReply(sessionId: string, call: AgentFunctionCallItem) { const arguments_ = call.arguments; if ( typeof arguments_ !== "object" || arguments_ === null || !("question" in arguments_) || typeof arguments_.question !== "string" ) { throw new Error("问题工具未提供问题"); } console.error(arguments_.question); if ("choices" in arguments_ && Array.isArray(arguments_.choices)) { console.error(arguments_.choices.join(" / ")); } const answer = await readReply(call); if (turnActive && !requests.signal.aborted) { await returnResult(sessionId, call, answer); } }
async function retrieveAnswer(sessionId: string, turnId: string) { for await (const item of client.beta.agents.sessions.turns.items.list( turnId, { session_id: sessionId, order: "desc" }, { signal: requests.signal } )) { if ( item.type === "message" && item.role === "assistant" && item.status === "completed" && item.phase === "final_answer" ) { return item.content .filter((part) => part.type === "output_text") .map((part) => part.text) .join("\n"); } } throw new Error("已完成的轮次没有最终答案"); }
const deliveryTimer = setTimeout( () => { deadlineReached = true; deliver( finalAnswer || "在简报截止时间前没有核实任何结果。" ); turnActive = false; closeQuestions(); requests.abort(); }, Math.max(0, briefingBy.getTime() - Date.now()) );
try { const agent = await client.beta.agents.create( { name: "个人助理", model: "gpt-6.2-astra", instructions: "你是一名个人助理。请在用户的时间限制内提供实用且来源可靠的答案。" + "当澄清能实质性改善结果时,请询问用户;在等待回复期间也要继续推进工作。" + "说明重要的不确定性。", experimental: { time_awareness: true }, tools: [ { type: "web_search", mode: "live" }, { type: "function", name: "request_user_input_async", description: "向用户询问背景信息。结果包含用户的回复,或报告问题已过期。", async: true, parameters: { type: "object", properties: { question: { type: "string" }, choices: { type: "array", items: { type: "string" }, description: "可选的建议回复,将与问题一同显示。", }, }, required: ["question"], additionalProperties: false, }, }, ], }, { signal: requests.signal } ); agentId = agent.id; const stream = await client.beta.agents.sessions.create( { agent_id: agent.id, environment: { type: "none" }, stream: true, input: `我将在 ${meetingAt.toISOString()} 与 Pied Piper(example.com)会面,讨论我们的客户支持软件。` + "请帮我快速了解他们的业务、近期产品公告,以及我应该提出的问题。" + `请在 ${briefingBy.toISOString()} 前给我发送一份简明的简报,以便我在会前有时间阅读。`, }, { signal: requests.signal } ); turnActive = true; for await (const event of stream) { if (event.type === "agent.session.created") { sessionId = event.session.id; } if (event.type === "agent.session.turn.item.done") { const call = event.item; if ( call.type === "function_call" && call.name === "request_user_input_async" ) { const key = `${call.turn_id}:${call.call_id}`; if (!seenCalls.has(key)) { seenCalls.add(key); // 开始收集回复,同时不阻塞事件流。 const task = collectReply(event.session_id, call).catch( (error: unknown) => { replyError = error; requests.abort(); } ); replyTasks.add(task); } } } if ( event.type === "agent.session.turn.item.done" && event.item.type === "message" && event.item.role === "assistant" && event.item.phase === "final_answer" && event.item.status === "completed" ) { finalAnswer = event.item.content.map((part) => part.text).join("\n"); } if (event.type === "agent.session.turn.completed") { turnActive = false; closeQuestions(); finalAnswer = await retrieveAnswer(event.session_id, event.turn.id); deliver(finalAnswer); break; } if ( event.type === "agent.session.turn.failed" || event.type === "agent.session.turn.cancelled" || event.type === "agent.session.failed" || event.type === "agent.session.environment.failed" || event.type === "error" ) { throw new Error(`智能体生命周期失败:${event.type}`); } } if (replyError) throw replyError; if (!delivered && !deadlineReached) { throw new Error("轮次完成前,流已关闭"); } } catch (error) { if (!deadlineReached) throw replyError || error; } finally { clearTimeout(deliveryTimer); const mustCancel = turnActive || deadlineReached; turnActive = false; closeQuestions(); requests.abort(); await Promise.allSettled(replyTasks); try { if (sessionId) { try { if (mustCancel) { await client.beta.agents.sessions.events.create(sessionId, { events: [{ type: "agent.session.input.cancel" }], }); } } finally { // 取消操作可能会让会话短暂保持忙碌状态。 await client.beta.agents.sessions.delete(sessionId, { maxRetries: 5, timeout: 10_000, }); } } } finally { if (agentId) await client.beta.agents.delete(agentId); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239import asyncio import json import os import sys from contextlib import AsyncExitStack, suppress from datetime import UTC, datetime, timedelta from threading import Thread
from openai import AsyncOpenAI, ConflictError from openai.types.beta import AgentFunctionCallItem, AgentOutputItem, AgentSessionItem
client = AsyncOpenAI(max_retries=0)
async def create_agent(client: AsyncOpenAI): return await client.beta.agents.create( name="个人助理", model="gpt-6.2-astra", experimental={"time_awareness": True}, instructions=( "你是一名个人助理。请在用户的时间限制内提供有用且来源可靠的回答。" "如果澄清问题能实质性改善结果,就提出问题;在等待回复期间继续推进工作。" "说明重要的不确定性。" ), tools=[ {"type": "web_search", "mode": "live"}, { "type": "function", "name": "request_user_input_async", "description": "在继续独立工作的同时询问缺失的背景信息。", "async": True, "parameters": { "type": "object", "properties": { "question": {"type": "string"}, "choices": {"type": "array", "items": {"type": "string"}}, }, "required": ["question"], "additionalProperties": False, }, }, ], )
def console_replies() -> asyncio.Queue[str]: loop = asyncio.get_running_loop() replies: asyncio.Queue[str] = asyncio.Queue()
def read_lines(): with os.fdopen( os.dup(sys.stdin.fileno()), encoding=sys.stdin.encoding ) as terminal: for line in terminal: try: loop.call_soon_threadsafe(replies.put_nowait, line.strip()) except RuntimeError: return # 会话及其事件循环已关闭。
# 待处理的终端读取不得妨碍程序在交付结果后退出。Thread(target=read_lines, daemon=True).start() return replies
async def respond(client, session_id, call, replies, prompt_lock, cutoff): loop = asyncio.get_running_loop() try: async with asyncio.timeout_at(cutoff), prompt_lock: if loop.time() >= cutoff: raise TimeoutError print(call.arguments["question"], file=sys.stderr, flush=True) for choice in call.arguments.get("choices", []): print(f" {choice}", file=sys.stderr, flush=True) reply = await replies.get() answer = reply if loop.time() str: if ( item.type == "message" and item.role == "assistant" and item.status == "completed" and item.phase == "final_answer" ): return "\n".join( part.text for part in item.content if part.type == "output_text" ) return ""
async def retrieve_answer(client: AsyncOpenAI, session_id: str, turn_id: str) -> str: async for item in client.beta.agents.sessions.turns.items.list( turn_id, session_id=session_id, order="desc" ): if text := final_text(item): return text return ""
async def main(): async with client, AsyncExitStack() as cleanup: agent = await create_agent(client) cleanup.push_async_callback(client.beta.agents.delete, agent.id) sessions = client.beta.agents.sessions session_id = None active = False saved_answer = "" questions = {} prompt_lock = asyncio.Lock() replies = console_replies() loop = asyncio.get_running_loop() started = loop.time() reply_cutoff = started + 10 * 60 try: async with asyncio.timeout_at(started + 12 * 60): now = datetime.now(UTC) meeting = (now + timedelta(minutes=15)).isoformat(timespec="seconds") delivery = (now + timedelta(minutes=12)).isoformat(timespec="seconds") stream = await client.beta.agents.sessions.create( agent_id=agent.id, environment={"type": "none"}, input=( f"我将在 {meeting} 与 Pied Piper(example.com)开会,讨论我们的客户支持软件。" f"请帮我快速了解他们的业务、近期产品公告," f"以及我应该提出的问题。请在 {delivery} 前发给我一份简明的简报," f"以便我在会议前有时间阅读。" ), stream=True, timeout=12 * 60, ) async with stream: async for event in stream: if event.type == "agent.session.created": session_id = event.session.id # 已取消的轮次可能需要一点时间才能释放其会话。 cleanup_sessions = client.with_options( max_retries=4, timeout=10 ).beta.agents.sessions cleanup.push_async_callback( cleanup_sessions.delete, session_id ) active = True elif event.type == "agent.session.turn.item.done": item = event.item if ( isinstance(item, AgentFunctionCallItem) and item.name == "request_user_input_async" and item.call_id not in questions ): questions[item.call_id] = asyncio.create_task( respond( client, event.session_id, item, replies, prompt_lock, reply_cutoff, ) ) saved_answer = final_text(item) or saved_answer elif event.type == "agent.session.turn.completed": active = False for question in questions.values(): question.cancel() # 允许创建流完成连接释放。with suppress(TimeoutError): async with asyncio.timeout(1): async for _ in stream: continue answer = await retrieve_answer( client, event.session_id, event.turn.id ) print( answer or saved_answer or "没有可用的已核实发现。", flush=True, ) return elif event.type in ( "agent.session.failed", "agent.session.environment.failed", "agent.session.turn.failed", "agent.session.turn.cancelled", "error", ): active = False raise RuntimeError( f"会议简报已停止:{event.type}" ) for question in questions.values(): if question.done(): error = question.exception() # 已结束的轮次可能会拒绝仍在传输中的回复。 if error is not None and not isinstance( error, ConflictError ): raise error raise RuntimeError("轮次完成前,事件流已关闭。") except TimeoutError: print( saved_answer or "截止时间前没有可用的已核实发现。", flush=True, ) finally: for question in questions.values(): question.cancel() await asyncio.gather(*questions.values(), return_exceptions=True) if active and session_id: await sessions.events.create( session_id, events=[{"type": "agent.session.input.cancel"}] )
if __name__ == "__main__": asyncio.run(main())
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282package main
import ( "bufio" "context" "encoding/json" "errors" "fmt" "os" "strings" "time"
"github.com/openai/openai-go/v3" "github.com/openai/openai-go/v3/option" )
func createAgent(ctx context.Context, client *openai.Client) (*openai.Agent, error) { return client.Beta.Agents.New(ctx, openai.BetaAgentNewParams{ Name: openai.String("个人助理"), Model: "gpt-6.2-astra", Instructions: openai.String("你是一名个人助理。请在用户的时间限制内提供有用且来源可靠的答案。" + "如果澄清能实质性改善结果,请提出问题,并在等待回复期间继续推进工作。" + "说明重要的不确定性。"), Experimental: openai.BetaAgentNewParamsExperimental{TimeAwareness: openai.Bool(true)}, Tools: []openai.PersistedAgentToolParamUnion{ {OfParamWebSearch: &openai.PersistedAgentToolParamWebSearch{Mode: "live"}}, {OfParamFunction: &openai.PersistedAgentToolParamFunction{ Name: "request_user_input_async", Description: "在继续独立工作的同时询问缺失的背景信息。", Async: openai.Bool(true), Parameters: map[string]any{ "type": "object", "properties": map[string]any{ "question": map[string]any{"type": "string"}, "choices": map[string]any{"type": "array", "items": map[string]any{"type": "string"}}, }, "required": []string{"question"}, "additionalProperties": false, }, }}, }, }) }
func returnResult(ctx context.Context, client *openai.Client, sessionID string, call openai.AgentFunctionCallItem, answer string) error { result := map[string]string{"status": "no_answer"} if answer != "" { result = map[string]string{"status": "answered", "answer": answer} } output, err := json.Marshal(result) if err != nil { return err } return client.Beta.Agents.Sessions.Events.New(ctx, sessionID, openai.BetaAgentSessionEventNewParams{ Events: []openai.AgentSessionInputParamUnion{{ OfParamAgentSessionInputToolResult: &openai.AgentSessionInputParamAgentSessionInputToolResult{ TurnID: call.TurnID, CallID: call.CallID, Success: true, Output: openai.AgentFunctionCallOutputParamUnion{OfString: openai.String(string(output))}, }, }}, }) }
func retrieveAnswer(ctx context.Context, client *openai.Client, sessionID, turnID string) (string, error) { items := client.Beta.Agents.Sessions.Turns.Items.ListAutoPaging(ctx, sessionID, turnID, openai.BetaAgentSessionTurnItemListParams{Order: "desc"}) for items.Next() { item := items.Current() if item.Type == "message" && item.Role == "assistant" && item.Status == "completed" && item.Phase == "final_answer" { var parts []string for _, part := range item.AsMessage().Content { if part.Type == "output_text" { parts = append(parts, part.Text) } } return strings.Join(parts, "\n"), nil } } return "", items.Err() }
func consoleReplies(ctx context.Context) choices) {}
private final OpenAIClient client = OpenAIOkHttpClient.builder().fromEnv().timeout(Duration.ofSeconds(30)).maxRetries(0).build(); private final Instant startedAt = Instant.now(); private final Instant meetingAt = startedAt.plus(Duration.ofMinutes(15)); private final Instant briefingBy = startedAt.plus(Duration.ofMinutes(12)); private final Instant replyCutoff = startedAt.plus(Duration.ofMinutes(10)); private final BlockingQueue replies = new LinkedBlockingQueue<>(); private final Set seenCalls = new HashSet<>(); private final AtomicBoolean active = new AtomicBoolean(); private final AtomicBoolean expired = new AtomicBoolean(); private final AtomicBoolean delivered = new AtomicBoolean(); private final AtomicReference > streamRef = new AtomicReference<>(); private final AtomicReference replyError = new AtomicReference<>(); private final java.util.concurrent.ExecutorService replyWorker = Executors.newSingleThreadExecutor(); private final java.util.concurrent.ScheduledExecutorService timers = Executors.newSingleThreadScheduledExecutor(); private String agentId; private String sessionId; private volatile String finalAnswer = "";
public static void main(String[] args) throws Exception { new MeetingPreparation().run(); }
private void run() throws Exception { startInputReader(); var deliveryTimer = timers.schedule( () -> { expired.set(true); deliver( finalAnswer.isBlank() ? "在简报截止时间前没有核实任何结果。" : finalAnswer); active.set(false); closeStream(); }, Math.max(0, Duration.between(Instant.now(), briefingBy).toMillis()), TimeUnit.MILLISECONDS); try { var agent = client .beta() .agents() .create( AgentCreateParams.builder() .name("个人助理") .model("gpt-6.2-astra") .instructions( "你是一名个人助理。请在用户的时间限制内提供有用且来源可靠的答案。" + "如果澄清问题能显著改善结果,请提出问题;在等待回复期间," + "继续推进工作。说明重要的不确定性。") .experimental( AgentCreateParams.Experimental.builder().timeAwareness(true).build()) .addTool( PersistedAgentToolParam.WebSearch.builder() .mode(PersistedAgentToolParam.WebSearch.Mode.LIVE) .build()) .addTool( PersistedAgentToolParam.Function.builder() .name("request_user_input_async") .description( "向用户询问背景信息。 结果包含用户的回复,或者" + "报告问题已过期。") .async(true) .parameters( PersistedAgentToolParam.Function.Parameters.builder() .putAdditionalProperty("type", JsonValue.from("object")) .putAdditionalProperty( "properties", JsonValue.from( Map.of( "question", Map.of("type", "string"), "choices", Map.of( "type", "array", "items", Map.of("type", "string"), "description", "随问题一同显示的可选建议回复。")))) .putAdditionalProperty( "required", JsonValue.from(List.of("question"))) .putAdditionalProperty( "additionalProperties", JsonValue.from(false)) .build()) .build()) .build()); agentId = agent.id(); if (expired.get()) return; try (var events = client .beta() .agents() .sessions() .createStreaming( SessionCreateParams.builder() .agentId(agent.id()) .environmentNone() .input( "我将在 " + meetingAt + " 与 Pied Piper(example.com)会面,讨论我们的客户支持软件。请帮助我了解他们的业务、近期产品" + "公告,以及我应该提出的问题。请在 " + briefingBy + " 前向我发送一份简明" + "简报,以便我在会面前有时间阅读。") .build(), RequestOptions.builder() .timeout( Duration.ofMillis( Math.max( 1, Duration.between(Instant.now(), briefingBy).toMillis() + 1000))) .build())) { streamRef.set(events); if (expired.get()) return; active.set(true); var iterator = events.stream().iterator(); while (!expired.get() && iterator.hasNext()) { var event = iterator.next(); event.created().ifPresent(created -> sessionId = created.session().id()); if (event.turnItemDone().isPresent()) { var item = event.turnItemDone().get().item(); if (item.functionCall().isPresent()) { var call = item.functionCall().get(); String key = call.turnId() + ":" + call.callId(); if (call.name().equals("request_user_input_async") && seenCalls.add(key)) { // 在使用会话事件的同时,在另一个线程上收集回复。 replyWorker.submit( () -> collectReply(event.turnItemDone().get().sessionId(), call)); } } } event .turnItemDone() .flatMap(done -> done.item().message()) .filter(message -> message.status().toString().equals("completed")) .filter( message -> message .phase() .map(phase -> phase.toString().equals("final_answer")) .orElse(false)) .ifPresent( message -> finalAnswer = message.content().stream() .map(part -> part.text()) .collect(Collectors.joining("\n"))); if (event.turnCompleted().isPresent()) { active.set(false); replyWorker.shutdownNow(); var completed = event.turnCompleted().get(); finalAnswer = retrieveAnswer(completed.sessionId(), completed.turn().id()); deliver(finalAnswer); break; } if (event.turnFailed().isPresent() || event.turnCancelled().isPresent() || event.failed().isPresent() || event.environmentFailed().isPresent() || event.error().isPresent()) { throw new IllegalStateException("智能体生命周期失败:" + event); } } } if (replyError.get() != null) throw replyError.get(); if (!delivered.get()) throw new IllegalStateException("流在轮次完成前已关闭"); } catch (RuntimeException error) { if (!expired.get()) throw error; } finally { deliveryTimer.cancel(false); boolean mustCancel = active.getAndSet(false) || expired.get(); replyWorker.shutdownNow(); timers.shutdownNow(); closeStream(); try { if (sessionId != null) { try { if (mustCancel) { client .beta() .agents() .sessions() .events() .create( EventCreateParams.builder() .sessionId(sessionId) .addEventAgentSessionInputCancel() .build()); } } finally { // 取消操作会异步完成;遇到临时删除冲突时重试。 client .withOptions(options -> options.maxRetries(5).timeout(Duration.ofSeconds(10))) .beta() .agents() .sessions() .delete(sessionId); } } } finally { if (agentId != null) client.beta().agents().delete(agentId); } } }
private void collectReply(String sessionId, AgentFunctionCallItem call) { try { var question = call._arguments().convert(Question.class); System.err.println(question.question()); if (question.choices() != null) System.err.println(String.join(" / ", question.choices())); long remaining = Math.max(0, Duration.between(Instant.now(), replyCutoff).toMillis()); String answer = remaining == 0 ? null : replies.poll(remaining, TimeUnit.MILLISECONDS); if (Instant.now().isAfter(replyCutoff)) answer = null; if (active.get() && !expired.get()) returnResult(sessionId, call, answer); } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); } catch (Exception error) { if (active.get() && !expired.get()) { replyError.set(new IllegalStateException("无法返回回复", error)); closeStream(); } } }
private void returnResult(String sessionId, AgentFunctionCallItem call, String answer) throws Exception { var result = answer == null || answer.isBlank() ? Map.of("status", "no_answer") : Map.of("status", "answered", "answer", answer); client .beta() .agents() .sessions() .events() .create( EventCreateParams.builder() .sessionId(sessionId) .addEvent( AgentSessionInputParam.AgentSessionInputToolResult.builder() .turnId(call.turnId()) .callId(call.callId()) .success(true) .output(new JsonMapper().writeValueAsString(result)) .build()) .build()); }
private String retrieveAnswer(String sessionId, String turnId) { var items = client .beta() .agents() .sessions() .turns() .items() .list( ItemListParams.builder() .sessionId(sessionId) .turnId(turnId) .order(ItemListParams.Order.DESC) .build()); for (var item : items.autoPager()) { if (item.message().isEmpty()) continue; var message = item.message().get(); if (message.role().toString().equals("assistant") && message.status().toString().equals("completed") && message.phase().map(phase -> phase.toString().equals("final_answer")).orElse(false)) { return message.content().stream() .flatMap(part -> part.outputText().stream()) .map(part -> part.text()) .collect(Collectors.joining("\n")); } } throw new IllegalStateException("已完成的轮次没有最终答案"); }
private void deliver(String text) { if (delivered.compareAndSet(false, true)) System.out.println(text); }
private void closeStream() { var stream = streamRef.getAndSet(null); if (stream != null) stream.close(); }
private void startInputReader() { var input = new Thread( () -> { try { var reader = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = reader.readLine()) != null) replies.add(line.trim()); } catch (java.io.IOException error) { System.err.println("输入已关闭:" + error.getMessage()); } }); input.setDaemon(true); input.start(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199require "openai" require "json" require "time" require "timeout"
def create_agent(client) client.beta.agents.create( name: "个人助理", model: "gpt-6.2-astra", experimental: { time_awareness: true }, instructions: "你是一名个人助理。请在用户的时间限制内提供有用且来源可靠的答案。" \ "如果澄清问题能实质性改善结果,请提出问题;等待回复期间也要继续推进工作。" \ "说明重要的不确定性。", tools: [ { type: "web_search", mode: "live" }, { type: "function", name: "request_user_input_async", description: "在继续独立工作的同时询问缺失的背景信息。", async: true, parameters: { type: "object", properties: { question: { type: "string" }, choices: { type: "array", items: { type: "string" } } }, required: ["question"], additionalProperties: false } } ] ) end
def monotonic_time Process.clock_gettime(Process::CLOCK_MONOTONIC) end
def console_reply(arguments, input_lock, cutoff) remaining = cutoff - monotonic_time return if remaining = cutoff
question = arguments.transform_keys(&:to_s) warn question.fetch("question") Array(question["choices"]).each { |choice| warn " #{choice}" } line = $stdin.gets sleep([cutoff - monotonic_time, 0].max) if line.nil? line&.strip if monotonic_time (); let latestResponseId: string | undefined; let finalAnswer = ""; let delivered = false; let deadlineReached = false; let closed = false; let replyError: unknown;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72import asyncio import json import os import sys from contextlib import suppress from dataclasses import dataclass, field from datetime import UTC, datetime, timedelta from threading import Thread
from openai import APITimeoutError, AsyncOpenAI, OpenAIError from openai.types.responses import ( Response, ResponseFunctionToolCall, ResponseInputItemParam, ToolParam, )
instructions = ( "你是一名个人助理。请在用户的时间限制内提供有用且来源可靠的答案。" "如果澄清问题能实质性改善结果,请提出问题;等待回复期间也要继续推进工作。" "说明重要的不确定性。" ) tools: list[ToolParam] = [ {"type": "web_search"}, { "type": "function", "name": "request_user_input_async", "description": "在继续独立工作的同时询问缺失的背景信息。", "async": True, "strict": False, "parameters": { "type": "object", "properties": { "question": {"type": "string"}, "choices": {"type": "array", "items": {"type": "string"}}, }, "required": ["question"], "additionalProperties": False, }, }, ]
@dataclass class State: response_ids: list[str] = field(default_factory=list) questions: dict[str, asyncio.Task[ResponseInputItemParam]] = field( default_factory=dict ) seen_calls: set[str] = field(default_factory=set) latest_response_id: str | None = None saved_answer: str = ""
def configure_task(): now = datetime.now(UTC) started = asyncio.get_running_loop().time() meeting = (now + timedelta(minutes=15)).isoformat(timespec="seconds") delivery = (now + timedelta(minutes=12)).isoformat(timespec="seconds") reply_cutoff = started + 10 * 60 delivery_cutoff = started + 12 * 60 input_items = [ { "role": "user", "content": ( f"我将在 {meeting} 与 Pied Piper(example.com)会面,讨论我们的客户支持软件。" f"请帮我快速了解他们的业务、近期产品公告," f"以及我应该提出的问题。请在 {delivery} 前向我发送一份简明简报," f"以便我在会前有时间阅读。" ), } ] return input_items, reply_cutoff, delivery_cutoff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50package main
import ( "bufio" "context" "encoding/json" "errors" "fmt" "os" "strings" "sync" "time"
"github.com/openai/openai-go/v3" "github.com/openai/openai-go/v3/option" "github.com/openai/openai-go/v3/packages/ssestream" "github.com/openai/openai-go/v3/responses" )
const instructions = "你是一名个人助理。请在用户的时间限制内提供实用且来源可靠的答案。" + "如果澄清信息能显著改善结果,请提出询问,并在等待回复期间" + "继续推进工作。说明重要的不确定性。"
func meetingTools() []responses.ToolUnionParam { question := responses.ToolParamOfFunction("request_user_input_async", map[string]any{ "type": "object", "properties": map[string]any{ "question": map[string]string{"type": "string"}, "choices": map[string]any{"type": "array", "items": map[string]string{"type": "string"}}, }, "required": []string{"question"}, "additionalProperties": false, }, false) question.OfFunction.Async = openai.Bool(true) question.OfFunction.Description = openai.String("在继续独立工作的同时询问缺失的背景信息。") return []responses.ToolUnionParam{responses.ToolParamOfWebSearch("web_search"), question} }
func deadlines(now time.Time) (meeting, delivery, replyCutoff time.Time) { return now.Add(15 * time.Minute), now.Add(12 * time.Minute), now.Add(10 * time.Minute) }
func userRequest(meeting, delivery time.Time) responses.ResponseInputParam { text := fmt.Sprintf("我将在 %s 与 Pied Piper(example.com)会面,讨论我们的客户支持软件。"+ "请帮我快速了解他们的业务、近期产品公告,以及我应该提出的问题。"+ "请在 %s 前向我发送一份简明简报,以便我在会前有时间阅读。", meeting.UTC().Format(time.RFC3339), delivery.UTC().Format(time.RFC3339)) return responses.ResponseInputParam{responses.ResponseInputItemParamOfMessage(text, "user")} }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97import com.fasterxml.jackson.databind.json.JsonMapper; import com.openai.client.OpenAIClient; import com.openai.client.okhttp.OpenAIOkHttpClient; import com.openai.core.JsonValue; import com.openai.core.RequestOptions; import com.openai.core.http.AsyncStreamResponse; import com.openai.models.responses.EasyInputMessage; import com.openai.models.responses.FunctionTool; import com.openai.models.responses.ResponseCreateParams; import com.openai.models.responses.ResponseFunctionToolCall; import com.openai.models.responses.ResponseInputItem; import com.openai.models.responses.ResponseStreamEvent; import com.openai.models.responses.Tool; import com.openai.models.responses.WebSearchTool; import java.io.BufferedReader; import java.io.InputStreamReader; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors;
private final String instructions = "你是一名个人助理。在用户的时间限制内提供实用且有可靠来源的答案" + "。如果澄清问题能显著改善结果,请提出问题,并且" + "在等待回复期间继续推进工作。说明重要的不确定因素。"; private final List tools = List.of( Tool.ofWebSearch(WebSearchTool.builder().type(WebSearchTool.Type.WEB_SEARCH).build()), Tool.ofFunction( FunctionTool.builder() .name("request_user_input_async") .description( "向用户询问背景信息。结果包含用户的回复,或报告问题已过期。") .async(true) .strict(false) .parameters( FunctionTool.Parameters.builder() .putAdditionalProperty("type", JsonValue.from("object")) .putAdditionalProperty( "properties", JsonValue.from( Map.of( "question", Map.of("type", "string"), "choices", Map.of( "type", "array", "items", Map.of("type", "string"), "description", "随问题显示的可选建议回复。")))) .putAdditionalProperty("required", JsonValue.from(List.of("question"))) .putAdditionalProperty("additionalProperties", JsonValue.from(false)) .build()) .build())); private final Instant startedAt = Instant.now(); private final long monotonicStart = System.nanoTime(); private final Instant meetingAt = startedAt.plus(Duration.ofMinutes(15)); private final Instant briefingBy = startedAt.plus(Duration.ofMinutes(12)); private final long deliveryLimit = monotonicStart + Duration.ofMinutes(12).toNanos(); private final long replyLimit = monotonicStart + Duration.ofMinutes(10).toNanos(); private final List initialInput = List.of( ResponseInputItem.ofEasyInputMessage( EasyInputMessage.builder() .role(EasyInputMessage.Role.USER) .content( "我将在 " + meetingAt + " 与 Pied Piper(example.com)会面,讨论我们的客户支持软件。请帮助我快速了解他们的业务、近期产品公告," + "以及我应该提出的问题。请在 " + briefingBy + " 前向我发送一份简明的简报,以便我在会前有时间阅读。") .build())); private final Set responseIds = ConcurrentHashMap.newKeySet(); private final AtomicReference > streamRef = new AtomicReference<>(); private final AtomicBoolean expired = new AtomicBoolean(); private final AtomicBoolean delivered = new AtomicBoolean(); private final AtomicBoolean closed = new AtomicBoolean(); private final AtomicReference replyError = new AtomicReference<>(); private volatile String latestResponseId; private volatile String finalAnswer = "";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49require "json" require "openai" require "time" require "timeout"
INSTRUCTIONS = "你是一名个人助理。请在用户的时间限制内提供实用且有可靠来源的答案。" \ "如果澄清问题能显著改善结果,请提出问题,并在等待回复期间继续" \ "推进工作。说明重要的不确定因素。" TOOLS = [ { type: "web_search" }, { type: "function", name: "request_user_input_async", description: "在继续独立工作的同时,询问缺失的背景信息。", async: true, strict: false, parameters: { type: "object", properties: { question: { type: "string" }, choices: { type: "array", items: { type: "string" } } }, required: ["question"], additionalProperties: false } } ].freeze
def deadlines(now) { meeting: now + 15 * 60, delivery: now + 12 * 60, reply: now + 10 * 60 } end
def user_request(meeting, delivery) { role: "user", content: "我将在 " \ "#{meeting.utc.iso8601} 与 Pied Piper(example.com)会面,讨论我们的客户支持软件。请帮助我快速了解他们的业务、近期产品" \ "公告,以及我应该提出的问题。请在 " \ "#{delivery.utc.iso8601} 前向我发送一份简明的简报,以便我在会前有时间阅读。" } end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50work=$(mktemp -d) stream_pid="" timer_pid="" reader_pid="" owned='[]' latest_response_id="" pending='{}' seen='{}' ready='[]' exec 4 request(List input) { var params = ResponseCreateParams.builder() .model("gpt-6.2-astra") .instructions(instructions) .tools(tools) .store(true) .previousResponseId(Optional.ofNullable(latestResponseId)); var options = RequestOptions.builder() .timeout( Duration.ofNanos(Math.max(1, deliveryLimit - System.nanoTime())).plusSeconds(1)) .build(); var requestInput = new ArrayList<>(input); requestInput.add( ResponseInputItem.ofEasyInputMessage( EasyInputMessage.builder() .role(EasyInputMessage.Role.DEVELOPER) .content("当前时间:" + Instant.now() + "。") .build())); return client .async() .responses() .createStreaming(params.inputOfResponse(requestInput).build(), options); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15def start_response(client, input, previous_id) reminder = { role: "developer", content: "当前时间:#{Time.now.utc.iso8601}。" } params = { model: "gpt-6.2-astra", tools: TOOLS, instructions: INSTRUCTIONS, store: true, input: [*input, reminder] } params[:previous_response_id] = previous_id if previous_id client.responses.stream_raw(params) end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20start_response() { local remaining current_time request remaining=$((delivery_cutoff - SECONDS)) if [ "$remaining" -le 0 ]; then deliver; exit 0; fi rm -f "$work/events" mkfifo "$work/events" current_time=$(date -u +%Y-%m-%dT%H:%M:%SZ) request=$(jq -nc --argjson config "$configuration" --argjson input "$input" \ --arg previous "$latest_response_id" --arg now "$current_time" ' $config + { input: ($input + [{role: "developer", content: ("当前时间:" + $now + "。")}]) } + (if $previous == "" then {} else {previous_response_id: $previous} end)') curl --fail-with-body --silent --show-error --no-buffer --max-time "$remaining" \ https://api.openai.com/v1/responses \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ --data-binary "$request" >"$work/events" & stream_pid=$! exec 3 void>(); const seenCalls = new Set (); const replyTasks = new Set >(); const readyResults: OpenAI.Responses.ResponseInput = [];
terminal.on("line", (line) => { const waiting = pendingReplies.values().next().value; if (waiting) { waiting( performance.now() { return new Promise((resolve) => { let timer: ReturnType | undefined; const finish = (answer?: string) => { clearTimeout(timer); pendingReplies.delete(callId); resolve(answer); }; pendingReplies.set(callId, finish); timer = setTimeout(finish, Math.max(0, replyLimit - performance.now())); if (performance.now() >= replyLimit) finish(); else if (bufferedReplies.length) finish(bufferedReplies.shift() || undefined); }); }
function closeQuestions() { closed = true; for (const finish of pendingReplies.values()) finish(); terminal.close(); process.stdin.pause(); }
async function collectReply(call: OpenAI.Responses.ResponseFunctionToolCall) { const { question, choices } = JSON.parse(call.arguments); console.error(question); if (choices?.length) console.error(choices.join(" / ")); const answer = await readReply(call.call_id); if (closed) return; readyResults.push({ type: "function_call_output", call_id: call.call_id, output: JSON.stringify( answer ? { status: "answered", answer } : { status: "no_answer" } ), }); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39def console_replies() -> asyncio.Queue[str]: loop = asyncio.get_running_loop() replies: asyncio.Queue[str] = asyncio.Queue()
def read_lines(): with os.fdopen( os.dup(sys.stdin.fileno()), encoding=sys.stdin.encoding ) as terminal: for line in terminal: try: loop.call_soon_threadsafe(replies.put_nowait, line.strip()) except RuntimeError: return
Thread(target=read_lines, daemon=True).start() return replies
async def collect_reply(call, replies, prompt_lock, reply_cutoff): arguments = json.loads(call.arguments) answer = "" # 已过期的问题会保留空答案并返回 no_answer。 with suppress(TimeoutError): async with asyncio.timeout_at(reply_cutoff), prompt_lock: if asyncio.get_running_loop().time() >= reply_cutoff: raise TimeoutError print(arguments["question"], file=sys.stderr, flush=True) for choice in arguments.get("choices", []): print(f" {choice}", file=sys.stderr, flush=True) reply = await replies.get() if asyncio.get_running_loop().time() choices) {}
private final BlockingQueue replies = new LinkedBlockingQueue<>(); private final BlockingQueue readyResults = new LinkedBlockingQueue<>(); private final Set seenCalls = ConcurrentHashMap.newKeySet(); private final ExecutorService replyWorker = Executors.newSingleThreadExecutor();
private void collectReply(ResponseFunctionToolCall call) { try { var question = call.arguments(Question.class); System.err.println(question.question()); if (question.choices() != null) System.err.println(String.join(" / ", question.choices())); long remaining = Math.max(0, replyLimit - System.nanoTime()); String answer = remaining == 0 ? null : replies.poll(remaining, TimeUnit.NANOSECONDS); if (System.nanoTime() >= replyLimit) answer = null; if (closed.get()) return; var result = answer == null || answer.isBlank() ? Map.of("status", "no_answer") : Map.of("status", "answered", "answer", answer); readyResults.add( ResponseInputItem.ofFunctionCallOutput( ResponseInputItem.FunctionCallOutput.builder() .callId(call.callId()) .output(new JsonMapper().writeValueAsString(result)) .build())); } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); } catch (Exception error) { if (!closed.get()) { replyError.set(new IllegalStateException("无法收集回复", error)); closeStream(); } } }
private void startInputReader() { var input = new Thread( () -> { try { var reader = new BufferedReader(new InputStreamReader(System.in)); String line; while (!closed.get() && (line = reader.readLine()) != null) replies.add(line.trim()); } catch (java.io.IOException error) { System.err.println("输入已关闭:" + error.getMessage()); } }); input.setDaemon(true); input.start(); }
private void closeQuestions() { closed.set(true); replyWorker.shutdownNow(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36def read_answer(arguments, input_lock, cutoff) remaining = cutoff - Time.now return if remaining = cutoff
warn arguments.fetch("question") Array(arguments["choices"]).each { |choice| warn " #{choice}" } line = $stdin.gets sleep([cutoff - Time.now, 0].max) if line.nil? line&.strip if Time.now "$work/incoming" mv "$work/incoming" "$work/reply-$number" number=$((number + 1)) done ) /dev/null; then return; fi seen=$(jq -c --arg id "$call_id" '.[$id] = true' &2 fi }
collect_replies() { local call_id answer result while :; do call_id=$(jq -r 'keys_unsorted[0] // empty' ) { for await (const event of stream) { if (event.type === "response.created") responseIds.add(event.response.id); if ( event.type === "response.output_item.done" && event.item.type === "function_call" && event.item.name === "request_user_input_async" && !seenCalls.has(event.item.call_id) ) { seenCalls.add(event.item.call_id); // 在终端等待回复时继续接收事件。 const task = collectReply(event.item).catch((error: unknown) => { replyError = error; requests.abort(); }); replyTasks.add(task); } if (event.type === "response.completed") { latestResponseId = event.response.id; const answer = event.response.output .filter( (item) => item.type === "message" && item.status === "completed" && item.phase !== "commentary" ) .flatMap((item) => (item.type === "message" ? ","item.content : [])) .filter((part) => part.type === "output_text") .map((part) => part.text) .join("\n"); if (answer) finalAnswer = answer; return answer; } if ( event.type === "response.failed" || event.type === "response.incomplete" ) { throw new Error(`响应未完成:${event.type}`); } if (event.type === "error") throw new Error(event.message); } throw new Error("流在响应完成前已关闭"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40def final_text(response: Response) -> str: return "\n".join( part.text for item in response.output if item.type == "message" and item.status == "completed" and item.phase in (None, "final_answer") for part in item.content if part.type == "output_text" ) item.content : [])) .filter((part) => part.type === "output_text") .map((part) => part.text) .join("\n"); if (answer) finalAnswer = answer; return answer; } if ( event.type === "response.failed" || event.type === "response.incomplete" ) { throw new Error(`响应未完成:${event.type}`); } if (event.type === "error") throw new Error(event.message); } throw new Error("流在响应完成前已关闭"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40def final_text(response: Response) -> str: return "\n".join( part.text for item in response.output if item.type == "message" and item.status == "completed" and item.phase in (None, "final_answer") for part in item.content if part.type == "output_text" )
async def read_response(stream, state, replies, prompt_lock, reply_cutoff): async with stream: async for event in stream: if event.type == "response.created": state.response_ids.append(event.response.id) elif event.type == "response.output_item.done": call = event.item if ( isinstance(call, ResponseFunctionToolCall) and call.name == "request_user_input_async" and call.call_id not in state.seen_calls ): state.seen_calls.add(call.call_id) state.questions[call.call_id] = asyncio.create_task( collect_reply(call, replies, prompt_lock, reply_cutoff) ) elif event.type == "response.completed": state.latest_response_id = event.response.id answer = final_text(event.response) state.saved_answer = answer or state.saved_answer # 在终止事件后,给传输层一点时间完成收尾。 with suppress(TimeoutError): async with asyncio.timeout(1): async for _ in stream: continue return answer elif event.type in ("response.failed", "response.incomplete", "error"): raise RuntimeError(f"会议简报已停止:{event.type}") raise RuntimeError("响应流在完成前已关闭。")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57func finalText(response responses.Response) string { var parts []string for _, item := range response.Output { if item.Type != "message" { continue } message := item.AsMessage() if message.Role != "assistant" || message.Status != "completed" || (message.Phase != "" && message.Phase != "final_answer") { continue } for _, part := range message.Content { if part.Type == "output_text" { parts = append(parts, part.Text) } } } return strings.Join(parts, "\n") }
type progress struct { latestID, savedAnswer string responseIDs map[string]bool }
func (s *progress) consume(ctx context.Context, stream *ssestream.Stream[responses.ResponseStreamEventUnion], box *questionBox) (string, error) { defer stream.Close() for stream.Next() { event := stream.Current() switch event.Type { case "response.created": s.responseIDs[event.Response.ID] = true case "response.output_item.done": if event.Item.Type == "function_call" { call := event.Item.AsFunctionCall() if call.Name == "request_user_input_async" { if err := box.ask(ctx, call); err != nil { return "", err } } } case "response.completed": s.latestID = event.Response.ID s.responseIDs[s.latestID] = true answer := finalText(event.Response) if answer != "" { s.savedAnswer = answer } return answer, nil case "response.failed", "response.incomplete", "error": return "", fmt.Errorf("response stopped: %s", event.Type) } } if err := stream.Err(); err != nil { return "", err } return "", errors.New("stream closed before response.completed") }
@Override public void onComplete(Optional error) { if (!answerReady.isDone()) { answerReady.completeExceptionally( error.orElseGet( () -> new IllegalStateException( "The stream closed before response completion"))); } } }); return answerReady.join(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35def final_text(item) return unless item.is_a?(OpenAI::Models::Responses::ResponseOutputMessage) return unless item.role.to_s == "assistant" && item.status.to_s == "completed" return unless item.phase.nil? || item.phase.to_s == "final_answer"
text = item.content.filter_map { |part| part.text if part.is_a?(OpenAI::Models::Responses::ResponseOutputText) }.join("\n") text unless text.empty? end
def consume(stream, state, jobs, seen, input_lock, cutoff) stream.each do |event| case event when OpenAI::Models::Responses::ResponseCreatedEvent state[:ids][event.response.id] = true when OpenAI::Models::Responses::ResponseOutputItemDoneEvent item = event.item if item.is_a?(OpenAI::Models::Responses::ResponseFunctionToolCall) && item.name == "request_user_input_async" && !seen.key?(item.call_id) seen[item.call_id] = true jobs[item.call_id] = start_question(item, input_lock, cutoff) end when OpenAI::Models::Responses::ResponseCompletedEvent state[:ids][event.response.id] = true state[:latest_id] = event.response.id answer = event.response.output.filter_map { |item| final_text(item) }.join("\n") state[:saved_answer] = answer unless answer.empty? return answer when OpenAI::Models::Responses::ResponseFailedEvent, OpenAI::Models::Responses::ResponseIncompleteEvent, OpenAI::Models::Responses::ResponseErrorEvent raise "响应已停止:#{event.type}" end end raise "流在 response.completed 之前已关闭。" end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58read_response() { local chunk buffer="" line event event_type id current_answer="" while :; do chunk="" if IFS= read -r -t 1 -u 3 chunk; then line="$buffer$chunk" buffer="" if [[ "$line" == 'data: '* && "$line" != 'data: [DONE]' ]]; then event=${line#data: } event_type=$(jq -r '.type' /dev/null; then register_question "$(jq -c '.item' "$work/candidate.txt" mv "$work/candidate.txt" "$work/final.txt" fi collect_replies if wait "$stream_pid"; then : else status=$? if [ "$status" -eq 28 ]; then deliver; exit 0; fi return "$status" fi stream_pid="" exec 3 &2 return 1 fi fi else buffer="$buffer$chunk" if ! kill -0 "$stream_pid" 2>/dev/null; then if wait "$stream_pid"; then : else status=$? if [ "$status" -eq 28 ]; then deliver; exit 0; fi fi printf '%s\n' 'The response stream closed before completion.' >&2 return 1 fi fi collect_replies done } 5. 使用就绪结果继续循环
研究期间收到的回复应为下一版简报提供信息。每次响应完成后,收集所有已就绪的工具结果,并通过请求辅助函数发送它们,该函数会添加新的时间提醒。使用最近完成的响应 ID 作为 previous_response_id,即使某项结果属于更早响应中的问题也是如此。
保持请求按顺序执行,使每个请求都建立在最新的对话状态之上。如果简报已经完成且没有就绪的结果,就交付简报并关闭待处理的问题。否则,使用更新后的时间继续工作,不要等待回答。继续研究并纳入回复Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15async function runLoop() { let nextInput = initialInput; while (!delivered) { const answer = await consumeEvents(await request(nextInput)); if (replyError) throw replyError; nextInput = readyResults.splice(0); if (nextInput.length > 0) continue; if (answer) { closeQuestions(); deliver(answer); } // 如果没有最终答案或就绪结果,下一个请求将继续工作。 // 它无需等待待处理的回复,就会收到一条新的时钟消息。 } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23async def run_loop(input_items, state, replies, reply_cutoff, delivery_cutoff): prompt_lock = asyncio.Lock() while True: stream = await start_response( input_items, state.latest_response_id, delivery_cutoff ) answer = await read_response(stream, state, replies, prompt_lock, reply_cutoff) await asyncio.sleep(0) # 让已就绪的输入处理程序完成。 input_items = [] for call_id, task in list(state.questions.items()): if task.done(): input_items.append(task.result()) del state.questions[call_id] if input_items: continue # 纳入生成期间收到的回复。 if answer: return answer input_items = [ { "role": "developer", "content": "在等待回复期间,继续处理简报。", } ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49func (box *questionBox) ready() (responses.ResponseInputParam, error) { var input responses.ResponseInputParam for callID, job := range box.jobs { if job == nil { continue } select { case answer := 0 { input = next continue } if answer != "" { return answer, nil } input = responses.ResponseInputParam{responses.ResponseInputItemParamOfMessage( "利用现有上下文继续准备简报。", "developer")} } return "", ctx.Err() }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24private void runLoop() { List nextInput = initialInput; while (!delivered.get()) { String answer; var stream = request(nextInput); try { streamRef.set(stream); if (expired.get()) return; answer = consumeEvents(stream); } finally { streamRef.set(null); stream.close(); } if (replyError.get() != null) throw replyError.get(); nextInput = new ArrayList<>(); readyResults.drainTo(nextInput); if (!nextInput.isEmpty()) continue; if (!answer.isBlank()) { closeQuestions(); deliver(answer); } // 否则,使用一条新的时钟消息继续工作,无需等待输入。 } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22def prepare(client, state, jobs, times) input = [user_request(times[:meeting], times[:delivery])] seen = {} input_lock = Mutex.new loop do stream = start_response(client, input, state[:latest_id]) begin answer = consume(stream, state, jobs, seen, input_lock, times[:reply]) ensure stream.close end ready = jobs.keys.reject { |call_id| jobs.fetch(call_id).alive? } input.replace(ready.map { |call_id| jobs.delete(call_id).value }) next unless input.empty? return answer unless answer.empty?
input { deadlineReached = true; deliver( finalAnswer || "在简报截止时间前未核实任何结果。" ); closeQuestions(); requests.abort(); }, Math.max(0, deliveryLimit - performance.now()) );
try { await runLoop(); } catch (error) { if (!deadlineReached) throw replyError || error; } finally { clearTimeout(deliveryTimer); closeQuestions(); requests.abort(); await Promise.allSettled(replyTasks); const cleanup = await Promise.allSettled( [...responseIds].map((id) => client.responses.delete(id)) ); const failures = cleanup.filter((result) => result.status === "rejected"); if (failures.length) { console.error("无法删除所有已存储的响应", failures); process.exitCode = 1; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30async def main(): state = State() replies = console_replies() input_items, reply_cutoff, delivery_cutoff = configure_task() async with client: try: async with asyncio.timeout_at(delivery_cutoff): answer = await run_loop( input_items, state, replies, reply_cutoff, delivery_cutoff ) print(answer, flush=True) except (TimeoutError, APITimeoutError): print( state.saved_answer or "截止时间前没有可用的已核实发现。", flush=True, ) finally: for task in state.questions.values(): task.cancel() await asyncio.gather(*state.questions.values(), return_exceptions=True) for response_id in reversed(state.response_ids): try: await client.responses.delete(response_id, timeout=10) except OpenAIError as error: print(f"无法删除 {response_id}:{error}", file=sys.stderr)
if __name__ == "__main__": asyncio.run(main())
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26func run(client *openai.Client) error { meeting, delivery, cutoff := deadlines(time.Now()) ctx, cancel := context.WithDeadline(context.Background(), delivery) defer cancel() state := progress{responseIDs: map[string]bool{}} box := questionBox{lines: consoleReplies(ctx), cutoff: cutoff, jobs: map[string] { expired.set(true); deliver( finalAnswer.isBlank() ? "简报截止时间前没有核实任何结果。" : finalAnswer); closeQuestions(); closeStream(); }, Math.max(0, deliveryLimit - System.nanoTime()), TimeUnit.NANOSECONDS); try { runLoop(); } catch (RuntimeException error) { if (!expired.get()) throw error; } finally { deliveryTimer.cancel(false); closeQuestions(); closeStream(); timers.shutdownNow(); RuntimeException cleanupFailure = null; for (String id : responseIds) { try { client.responses().delete(id); } catch (RuntimeException error) { if (cleanupFailure == null) cleanupFailure = new IllegalStateException("无法删除所有已存储的响应"); cleanupFailure.addSuppressed(error); } } client.close(); if (cleanupFailure != null) throw cleanupFailure; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23times = deadlines(Time.now.utc) state = { ids: {}, latest_id: nil, saved_answer: nil } jobs = {} begin answer = Timeout.timeout(times[:delivery] - Time.now) do prepare(client, state, jobs, times) end puts answer rescue Timeout::Error puts(state[:saved_answer] || "截止时间前没有可用的已核实发现。") ensure $stdout.flush jobs.each_value(&:kill) state[:ids].each_key do |id| client.responses.delete(id) rescue OpenAI::Errors::APIError => error warn "无法删除响应 #{id}:#{error.message}" end end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37cleanup() { local exit_status=$? trap - EXIT TERM for pid in "$stream_pid" "$timer_pid" "$reader_pid"; do if [ -n "$pid" ]; then kill "$pid" 2>/dev/null || true wait "$pid" 2>/dev/null || true fi done while IFS= read -r response_id; do curl --fail-with-body --silent --show-error --max-time 10 -X DELETE \ "https://api.openai.com/v1/responses/$response_id" \ -H "Authorization: Bearer $OPENAI_API_KEY" >/dev/null || exit_status=1 done /dev/null; then if [ -s "$work/final.txt" ]; then cat "$work/final.txt" else printf '%s\n' '截止时间前没有可用的已核实发现。' fi fi }
trap cleanup EXIT trap 'exit 0' TERM ( sleep "$((delivery_cutoff - SECONDS))" & sleep_pid=$! trap 'kill "$sleep_pid" 2>/dev/null || true; wait "$sleep_pid" 2>/dev/null || true; exit 0' TERM wait "$sleep_pid" deliver kill -TERM "$$" ) & timer_pid=$!
结论
个人智能体有时需要在截止时间前完成工作。理想情况下,它们会请求澄清以改善结果,但即使没有获得澄清,也仍然需要完成有用的工作。
我们为智能体设定了截止时间,并启用了时间感知,帮助它决定是进一步调查、核实某项发现,还是完成撰写。异步函数工具让它可以在用户考虑如何回答问题时继续研究。即使问题没有得到回答,智能体仍掌握足够的上下文,可以撰写一份有用的会议简报。
ChatGPT 和其他智能体运行框架使用这种模式,在向用户请求澄清的同时继续工作。我们希望 Agents API 中的时间感知功能能帮助你自行构建这种模式。欢迎告诉我们你构建了什么!
AI 热点