Public
Edited
Jun 21
Importers
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
img_response = responses({
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "what's in this image?"
},
{
type: "input_image",
image_url: getDataUrl(disk, "image/png")
}
]
}
]
})
Insert cell
Insert cell
Insert cell
Insert cell
websearch_response = responses({
model: "gpt-4o",
input: "Whats the weather in Berlin today?",
tools: [{ type: "web_search_preview" }]
})
Insert cell
Insert cell
Insert cell
Insert cell
image_response = responses({
model: "gpt-4o",
input: "Draw a pelican riding a bike",
tools: [{ type: "image_generation" }]
})
Insert cell
Insert cell
resolved_function_response.output.at(-1).content.at(-1).text
Insert cell
function_response = responses({
model: "o4-mini",
input: "You are executing in a browser. What is the current baseURL?",
tools: [evalJavaScriptTool],
reasoning: {
effort: "high",
summary: "detailed"
},
parallel_tool_calls: false
})
Insert cell
Insert cell
Insert cell
Insert cell
async function responses({
url = "https://api.openai.com/v1/responses",
model = "o4-mini",
input,
background,
include,
instructions,
max_output_tokens,
metadata,
parallel_tool_calls,
previous_response_id,
reasoning,
service_tier,
store,
temperature,
text,
tool_choice,
tools,
top_p,
truncation,
user
} = {}) {
debugger;
if (typeof input === "string") {
input = [
{
role: "user",
content: input
}
];
}
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${viewof OPENAI_API_KEY.value}`,
"Content-type": "application/json"
},
method: "POST",
body: JSON.stringify({
model,
background,
input: await deepResolve(input),
include,
instructions: await (typeof instructions == "function"
? instructions()
: instructions),
max_output_tokens,
metadata,
parallel_tool_calls,
previous_response_id,
reasoning,
service_tier,
store,
temperature,
text,
tool_choice,
tools,
top_p,
truncation,
user
})
});
if (response.status == 403 || response.status == 401)
throw "Authentication error: update OPENAI_API_KEY";
const responseJson = {
...arguments[0],
input,
...(await response.json()),
tools
};

console.log(arguments[0], responseJson);

// Auto decode images to a blob
responseJson.output &&
(await Promise.all(
responseJson.output
.filter((o) => o.type == "image_generation_call")
.map(async (call) => {
call.blob = await fetch(
`data:image/${call.format};base64,${call.result}`
).then((r) => r.blob());
})
));

// Auto decode arguments
responseJson.output &&
(await Promise.all(
responseJson.output
.filter((o) => o.type == "function_call")
.map(async (call) => {
call.arguments =
typeof call.arguments == "string"
? JSON.parse(call.arguments)
: call.arguments;
})
));

return responseJson;
}
Insert cell
async function runTools(response) {
// Auto function calls
// https://platform.openai.com/docs/guides/function-calling?api-mode=responses#handling-function-calls
const toolCalls =
response.output &&
(
await Promise.all(
response.output.flatMap(async (call, index) => {
if (call.type !== "function_call") return [];
const tool = response.tools.find((t) => t.name == call.name);
const result = await tool.execute(call.arguments);
return [
...(index > 1
? [
/*response.output[index - 2],*/
/*response.output[index - 1]*/
]
: []),
{
type: "function_call_output",
call_id: call.call_id,
output:
(typeof result == "string" ? result : JSON.stringify(result)) ||
"undefined"
}
];
})
)
).flat();
if (toolCalls?.length > 0) {
// auto-follow up
return await responses({
url: response.url,
output: undefined,
input: toolCalls,
tools: response.tools,
reasoning: response.reasoning,
previous_response_id: response.id,
tool_choice: response.tool_choice
});
}
return undefined; // Nothing to do
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more