[{"data":1,"prerenderedAt":1758},["ShallowReactive",2],{"navigation_docs":3,"-agents-openai-agent":208,"-agents-openai-agent-surround":1753},[4,33,60,82,109,151],{"title":5,"path":6,"stem":7,"children":8,"icon":32},"OpenSSL","/openssl","1.openssl/1.index",[9,12,17,22,27],{"title":10,"path":6,"stem":7,"icon":11},"Overview","i-lucide-info",{"title":13,"path":14,"stem":15,"icon":16},"Certificates","/openssl/certificates","1.openssl/2.certificates","i-lucide-file-badge",{"title":18,"path":19,"stem":20,"icon":21},"Keys","/openssl/keys","1.openssl/3.keys","i-lucide-key",{"title":23,"path":24,"stem":25,"icon":26},"Encryption","/openssl/encryption","1.openssl/4.encryption","i-lucide-shield",{"title":28,"path":29,"stem":30,"icon":31},"Verification","/openssl/verification","1.openssl/5.verification","i-lucide-check-circle","i-lucide-lock",{"title":34,"path":35,"stem":36,"children":37,"icon":59},"Voice AI","/voice-ai","2.voice-ai/1.index",[38,39,44,49,54],{"title":10,"path":35,"stem":36,"icon":11},{"title":40,"path":41,"stem":42,"icon":43},"Twilio Media Streams","/voice-ai/twilio","2.voice-ai/2.twilio","i-lucide-radio",{"title":45,"path":46,"stem":47,"icon":48},"WebSocket & Streaming","/voice-ai/websocket","2.voice-ai/3.websocket","i-lucide-cable",{"title":50,"path":51,"stem":52,"icon":53},"LLM Integration","/voice-ai/llm","2.voice-ai/4.llm","i-lucide-brain",{"title":55,"path":56,"stem":57,"icon":58},"Speech Services","/voice-ai/speech","2.voice-ai/5.speech","i-lucide-audio-lines","i-lucide-phone",{"title":61,"path":62,"stem":63,"children":64,"icon":81},"Agents","/agents","3.agents/1.index",[65,66,71,76],{"title":10,"path":62,"stem":63,"icon":11},{"title":67,"path":68,"stem":69,"icon":70},"RAG","/agents/rag","3.agents/2.rag","i-lucide-database",{"title":72,"path":73,"stem":74,"icon":75},"ReAct Agents","/agents/react-agents","3.agents/3.react-agents","i-lucide-activity",{"title":77,"path":78,"stem":79,"icon":80},"OpenAI Agent","/agents/openai-agent","3.agents/4.openai-agent","i-lucide-bot","i-lucide-users",{"title":83,"path":84,"stem":85,"children":86,"icon":88},"Context Engineering","/context-engineering","4.context-engineering/1.index",[87,89,94,99,104],{"title":83,"path":84,"stem":85,"icon":88},"i-lucide-brain-circuit",{"title":90,"path":91,"stem":92,"icon":93},"Managing The Context Window","/context-engineering/managing-context-window","4.context-engineering/2.managing-context-window","i-lucide-settings",{"title":95,"path":96,"stem":97,"icon":98},"Sliding Window Strategy","/context-engineering/sliding-window","4.context-engineering/3.sliding-window","i-lucide-arrow-left-right",{"title":100,"path":101,"stem":102,"icon":103},"Token-based Management","/context-engineering/token-based","4.context-engineering/4.token-based","i-lucide-hash",{"title":105,"path":106,"stem":107,"icon":108},"Summarization Techniques","/context-engineering/summarization","4.context-engineering/5.summarization","i-lucide-file-text",{"title":110,"path":111,"stem":112,"children":113,"icon":150},"AI SDK 6","/ai-sdk","5.ai-sdk/1.index",[114,115,120,125,130,135,140,145],{"title":10,"path":111,"stem":112,"icon":11},{"title":116,"path":117,"stem":118,"icon":119},"Installation & Setup","/ai-sdk/installation","5.ai-sdk/2.installation","i-lucide-package",{"title":121,"path":122,"stem":123,"icon":124},"Model Method","/ai-sdk/model","5.ai-sdk/3.model","i-lucide-box",{"title":126,"path":127,"stem":128,"icon":129},"generateText","/ai-sdk/generate-text","5.ai-sdk/4.generate-text","i-lucide-message-square",{"title":131,"path":132,"stem":133,"icon":134},"streamText","/ai-sdk/stream-text","5.ai-sdk/5.stream-text","i-lucide-zap",{"title":136,"path":137,"stem":138,"icon":139},"Structured Output","/ai-sdk/structured-output","5.ai-sdk/6.structured-output","i-lucide-layers",{"title":141,"path":142,"stem":143,"icon":144},"Text Embeddings","/ai-sdk/embeddings","5.ai-sdk/7.embeddings","i-lucide-git-branch",{"title":146,"path":147,"stem":148,"icon":149},"Agent Loop – ToolLoopAgent","/ai-sdk/agent-loop","5.ai-sdk/8.agent-loop","i-lucide-refresh-cw","i-lucide-cpu",{"title":152,"icon":153,"path":154,"stem":155,"children":156,"page":207},"Syntax Reference","i-lucide-book-open","/syntax-reference","999.syntax-reference",[157,162,167,172,177,182,187,192,197,202],{"title":158,"path":159,"stem":160,"icon":161},"Markdown Syntax","/syntax-reference/markdown-syntax","999.syntax-reference/1.markdown-syntax","i-lucide-heading-1",{"title":163,"path":164,"stem":165,"icon":166},"Code Blocks","/syntax-reference/code-blocks","999.syntax-reference/2.code-blocks","i-lucide-code-xml",{"title":168,"path":169,"stem":170,"icon":171},"Introduction","/syntax-reference/introduction","999.syntax-reference/2.introduction","i-lucide-house",{"title":173,"path":174,"stem":175,"icon":176},"Components","/syntax-reference/components","999.syntax-reference/3.components","i-lucide-component",{"title":178,"path":179,"stem":180,"icon":181},"Installation","/syntax-reference/installation","999.syntax-reference/3.installation","i-lucide-download",{"title":183,"path":184,"stem":185,"icon":186},"Images and Embeds","/syntax-reference/images-embeds","999.syntax-reference/4.images-embeds","i-lucide-image",{"title":188,"path":189,"stem":190,"icon":191},"Project Structure","/syntax-reference/project-structure","999.syntax-reference/4.project-structure","i-lucide-folder-tree",{"title":193,"path":194,"stem":195,"icon":196},"Studio module","/syntax-reference/studio","999.syntax-reference/5.studio","i-lucide-mouse-pointer-2",{"title":198,"path":199,"stem":200,"icon":201},"Migration","/syntax-reference/migration","999.syntax-reference/6.migration","i-lucide-replace",{"title":203,"path":204,"stem":205,"icon":206},"Troubleshooting","/syntax-reference/troubleshooting","999.syntax-reference/7.troubleshooting","i-lucide-wrench",false,{"id":209,"title":77,"body":210,"description":1745,"extension":1746,"links":1747,"meta":1748,"navigation":1749,"path":78,"seo":1750,"stem":79,"__hash__":1752},"docs/3.agents/4.openai-agent.md",{"type":211,"value":212,"toc":1716},"minimark",[213,217,221,226,229,257,261,266,666,670,905,909,913,916,1003,1007,1010,1039,1043,1046,1113,1117,1120,1189,1193,1281,1285,1291,1295,1327,1331,1355,1359,1375,1379,1383,1407,1411,1431,1434,1506,1510,1514,1517,1549,1553,1556,1634,1637,1641,1651,1654,1680,1683,1712],[214,215,77],"h1",{"id":216},"openai-agent",[218,219,220],"p",{},"OpenAI agents leverage the platform's native function calling capabilities to create intelligent assistants that can interact with external tools and APIs. This approach provides a more streamlined and robust implementation compared to manual ReAct pattern parsing.",[222,223,225],"h2",{"id":224},"openai-function-calling-vs-react","OpenAI Function Calling vs ReAct",[218,227,228],{},"While ReAct agents manually parse action patterns from text responses, OpenAI's native function calling provides:",[230,231,232,239,245,251],"ul",{},[233,234,235,238],"li",{},[236,237,136],"strong",{},": Direct function calls instead of text parsing",[233,240,241,244],{},[236,242,243],{},"Type Safety",": JSON schema validation for parameters",[233,246,247,250],{},[236,248,249],{},"Better Reliability",": No regex parsing failures",[233,252,253,256],{},[236,254,255],{},"Native Integration",": Built-in tool selection and argument extraction",[222,258,260],{"id":259},"complete-implementation","Complete Implementation",[262,263,265],"h3",{"id":264},"indexjs-main-agent-logic","index.js - Main Agent Logic",[267,268,273],"pre",{"className":269,"code":270,"language":271,"meta":272,"style":272},"language-javascript shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","import OpenAI from \"openai\"\nimport { getCurrentWeather, getLocation, tools } from \"./tools\"\n\nexport const openai = new OpenAI({\n    apiKey: process.env.OPENAI_API_KEY,\n    dangerouslyAllowBrowser: true\n})\n\nconst availableFunctions = {\n    getCurrentWeather,\n    getLocation\n}\n\nasync function agent(query) {\n    const messages = [\n        { \n            role: \"system\", \n            content: \"You are a helpful AI agent. Give highly specific answers based on information you're provided. Prefer to gather information with tools provided to you rather than giving basic, generic answers.\" \n        },\n        { role: \"user\", content: query }\n    ]\n\n    const MAX_ITERATIONS = 5\n\n    for (let i = 0; i \u003C MAX_ITERATIONS; i++) {\n        console.log(`Iteration #${i + 1}`)\n        const response = await openai.chat.completions.create({\n            model: \"gpt-3.5-turbo-1106\",\n            messages,\n            tools\n        })\n\n        const { finish_reason: finishReason, message } = response.choices[0]\n        const { tool_calls: toolCalls } = message\n        console.log(toolCalls)\n        \n        messages.push(message)\n        \n        if (finishReason === \"stop\") {\n            console.log(message.content)\n            console.log(\"AGENT ENDING\")\n            return\n        } else if (finishReason === \"tool_calls\") {\n            for (const toolCall of toolCalls) {\n                const functionName = toolCall.function.name\n                const functionToCall = availableFunctions[functionName]\n                const functionArgs = JSON.parse(toolCall.function.arguments)\n                const functionResponse = await functionToCall(functionArgs)\n                console.log(functionResponse)\n                messages.push({\n                    tool_call_id: toolCall.id,\n                    role: \"tool\",\n                    name: functionName,\n                    content: functionResponse\n                })\n            }\n        }\n    }\n}\n\nawait agent(\"What's the current weather in my current location?\")\n\n/**\nExpected Output:\nThe current weather in New York is sunny with a temperature of 75°F.\n */\n","javascript","",[274,275,276,284,290,297,303,309,315,321,326,332,338,344,350,355,361,367,373,379,385,391,397,403,408,414,419,425,431,437,443,449,455,461,466,472,478,484,490,496,501,507,513,519,525,531,537,543,549,555,561,567,573,579,585,591,597,603,609,615,621,626,631,637,642,648,654,660],"code",{"__ignoreMap":272},[277,278,281],"span",{"class":279,"line":280},"line",1,[277,282,283],{},"import OpenAI from \"openai\"\n",[277,285,287],{"class":279,"line":286},2,[277,288,289],{},"import { getCurrentWeather, getLocation, tools } from \"./tools\"\n",[277,291,293],{"class":279,"line":292},3,[277,294,296],{"emptyLinePlaceholder":295},true,"\n",[277,298,300],{"class":279,"line":299},4,[277,301,302],{},"export const openai = new OpenAI({\n",[277,304,306],{"class":279,"line":305},5,[277,307,308],{},"    apiKey: process.env.OPENAI_API_KEY,\n",[277,310,312],{"class":279,"line":311},6,[277,313,314],{},"    dangerouslyAllowBrowser: true\n",[277,316,318],{"class":279,"line":317},7,[277,319,320],{},"})\n",[277,322,324],{"class":279,"line":323},8,[277,325,296],{"emptyLinePlaceholder":295},[277,327,329],{"class":279,"line":328},9,[277,330,331],{},"const availableFunctions = {\n",[277,333,335],{"class":279,"line":334},10,[277,336,337],{},"    getCurrentWeather,\n",[277,339,341],{"class":279,"line":340},11,[277,342,343],{},"    getLocation\n",[277,345,347],{"class":279,"line":346},12,[277,348,349],{},"}\n",[277,351,353],{"class":279,"line":352},13,[277,354,296],{"emptyLinePlaceholder":295},[277,356,358],{"class":279,"line":357},14,[277,359,360],{},"async function agent(query) {\n",[277,362,364],{"class":279,"line":363},15,[277,365,366],{},"    const messages = [\n",[277,368,370],{"class":279,"line":369},16,[277,371,372],{},"        { \n",[277,374,376],{"class":279,"line":375},17,[277,377,378],{},"            role: \"system\", \n",[277,380,382],{"class":279,"line":381},18,[277,383,384],{},"            content: \"You are a helpful AI agent. Give highly specific answers based on information you're provided. Prefer to gather information with tools provided to you rather than giving basic, generic answers.\" \n",[277,386,388],{"class":279,"line":387},19,[277,389,390],{},"        },\n",[277,392,394],{"class":279,"line":393},20,[277,395,396],{},"        { role: \"user\", content: query }\n",[277,398,400],{"class":279,"line":399},21,[277,401,402],{},"    ]\n",[277,404,406],{"class":279,"line":405},22,[277,407,296],{"emptyLinePlaceholder":295},[277,409,411],{"class":279,"line":410},23,[277,412,413],{},"    const MAX_ITERATIONS = 5\n",[277,415,417],{"class":279,"line":416},24,[277,418,296],{"emptyLinePlaceholder":295},[277,420,422],{"class":279,"line":421},25,[277,423,424],{},"    for (let i = 0; i \u003C MAX_ITERATIONS; i++) {\n",[277,426,428],{"class":279,"line":427},26,[277,429,430],{},"        console.log(`Iteration #${i + 1}`)\n",[277,432,434],{"class":279,"line":433},27,[277,435,436],{},"        const response = await openai.chat.completions.create({\n",[277,438,440],{"class":279,"line":439},28,[277,441,442],{},"            model: \"gpt-3.5-turbo-1106\",\n",[277,444,446],{"class":279,"line":445},29,[277,447,448],{},"            messages,\n",[277,450,452],{"class":279,"line":451},30,[277,453,454],{},"            tools\n",[277,456,458],{"class":279,"line":457},31,[277,459,460],{},"        })\n",[277,462,464],{"class":279,"line":463},32,[277,465,296],{"emptyLinePlaceholder":295},[277,467,469],{"class":279,"line":468},33,[277,470,471],{},"        const { finish_reason: finishReason, message } = response.choices[0]\n",[277,473,475],{"class":279,"line":474},34,[277,476,477],{},"        const { tool_calls: toolCalls } = message\n",[277,479,481],{"class":279,"line":480},35,[277,482,483],{},"        console.log(toolCalls)\n",[277,485,487],{"class":279,"line":486},36,[277,488,489],{},"        \n",[277,491,493],{"class":279,"line":492},37,[277,494,495],{},"        messages.push(message)\n",[277,497,499],{"class":279,"line":498},38,[277,500,489],{},[277,502,504],{"class":279,"line":503},39,[277,505,506],{},"        if (finishReason === \"stop\") {\n",[277,508,510],{"class":279,"line":509},40,[277,511,512],{},"            console.log(message.content)\n",[277,514,516],{"class":279,"line":515},41,[277,517,518],{},"            console.log(\"AGENT ENDING\")\n",[277,520,522],{"class":279,"line":521},42,[277,523,524],{},"            return\n",[277,526,528],{"class":279,"line":527},43,[277,529,530],{},"        } else if (finishReason === \"tool_calls\") {\n",[277,532,534],{"class":279,"line":533},44,[277,535,536],{},"            for (const toolCall of toolCalls) {\n",[277,538,540],{"class":279,"line":539},45,[277,541,542],{},"                const functionName = toolCall.function.name\n",[277,544,546],{"class":279,"line":545},46,[277,547,548],{},"                const functionToCall = availableFunctions[functionName]\n",[277,550,552],{"class":279,"line":551},47,[277,553,554],{},"                const functionArgs = JSON.parse(toolCall.function.arguments)\n",[277,556,558],{"class":279,"line":557},48,[277,559,560],{},"                const functionResponse = await functionToCall(functionArgs)\n",[277,562,564],{"class":279,"line":563},49,[277,565,566],{},"                console.log(functionResponse)\n",[277,568,570],{"class":279,"line":569},50,[277,571,572],{},"                messages.push({\n",[277,574,576],{"class":279,"line":575},51,[277,577,578],{},"                    tool_call_id: toolCall.id,\n",[277,580,582],{"class":279,"line":581},52,[277,583,584],{},"                    role: \"tool\",\n",[277,586,588],{"class":279,"line":587},53,[277,589,590],{},"                    name: functionName,\n",[277,592,594],{"class":279,"line":593},54,[277,595,596],{},"                    content: functionResponse\n",[277,598,600],{"class":279,"line":599},55,[277,601,602],{},"                })\n",[277,604,606],{"class":279,"line":605},56,[277,607,608],{},"            }\n",[277,610,612],{"class":279,"line":611},57,[277,613,614],{},"        }\n",[277,616,618],{"class":279,"line":617},58,[277,619,620],{},"    }\n",[277,622,624],{"class":279,"line":623},59,[277,625,349],{},[277,627,629],{"class":279,"line":628},60,[277,630,296],{"emptyLinePlaceholder":295},[277,632,634],{"class":279,"line":633},61,[277,635,636],{},"await agent(\"What's the current weather in my current location?\")\n",[277,638,640],{"class":279,"line":639},62,[277,641,296],{"emptyLinePlaceholder":295},[277,643,645],{"class":279,"line":644},63,[277,646,647],{},"/**\n",[277,649,651],{"class":279,"line":650},64,[277,652,653],{},"Expected Output:\n",[277,655,657],{"class":279,"line":656},65,[277,658,659],{},"The current weather in New York is sunny with a temperature of 75°F.\n",[277,661,663],{"class":279,"line":662},66,[277,664,665],{}," */\n",[262,667,669],{"id":668},"toolsjs-tool-definitions-and-implementations","tools.js - Tool Definitions and Implementations",[267,671,673],{"className":269,"code":672,"language":271,"meta":272,"style":272},"export async function getCurrentWeather({ location }) {\n    const weather = {\n        location,\n        temperature: \"75\",\n        forecast: \"sunny\"\n    }\n    return JSON.stringify(weather)\n}\n\nexport async function getLocation() {\n  try {\n    const response = await fetch('https://ipapi.co/json/')\n    const text = await response.json()\n    return JSON.stringify(text)\n  } catch (err) {\n    console.log(err)\n  }\n}\n\nexport const tools = [\n    {\n        type: \"function\",\n        function: {\n            name: \"getCurrentWeather\",\n            description: \"Get current weather\",\n            parameters: {\n                type: \"object\",\n                properties: {\n                    location: {\n                        type: \"string\",\n                        description: \"The location from where to get weather\"\n                    }\n                },\n                required: [\"location\"]\n            }\n        }\n    },\n    {\n        type: \"function\",\n        function: {\n            name: \"getLocation\",\n            description: \"Get user's current location\",\n            parameters: {\n                type: \"object\",\n                properties: {}\n            }\n        }\n    },\n]\n",[274,674,675,680,685,690,695,700,704,709,713,717,722,727,732,737,742,747,752,757,761,765,770,775,780,785,790,795,800,805,810,815,820,825,830,835,840,844,848,853,857,861,865,870,875,879,883,888,892,896,900],{"__ignoreMap":272},[277,676,677],{"class":279,"line":280},[277,678,679],{},"export async function getCurrentWeather({ location }) {\n",[277,681,682],{"class":279,"line":286},[277,683,684],{},"    const weather = {\n",[277,686,687],{"class":279,"line":292},[277,688,689],{},"        location,\n",[277,691,692],{"class":279,"line":299},[277,693,694],{},"        temperature: \"75\",\n",[277,696,697],{"class":279,"line":305},[277,698,699],{},"        forecast: \"sunny\"\n",[277,701,702],{"class":279,"line":311},[277,703,620],{},[277,705,706],{"class":279,"line":317},[277,707,708],{},"    return JSON.stringify(weather)\n",[277,710,711],{"class":279,"line":323},[277,712,349],{},[277,714,715],{"class":279,"line":328},[277,716,296],{"emptyLinePlaceholder":295},[277,718,719],{"class":279,"line":334},[277,720,721],{},"export async function getLocation() {\n",[277,723,724],{"class":279,"line":340},[277,725,726],{},"  try {\n",[277,728,729],{"class":279,"line":346},[277,730,731],{},"    const response = await fetch('https://ipapi.co/json/')\n",[277,733,734],{"class":279,"line":352},[277,735,736],{},"    const text = await response.json()\n",[277,738,739],{"class":279,"line":357},[277,740,741],{},"    return JSON.stringify(text)\n",[277,743,744],{"class":279,"line":363},[277,745,746],{},"  } catch (err) {\n",[277,748,749],{"class":279,"line":369},[277,750,751],{},"    console.log(err)\n",[277,753,754],{"class":279,"line":375},[277,755,756],{},"  }\n",[277,758,759],{"class":279,"line":381},[277,760,349],{},[277,762,763],{"class":279,"line":387},[277,764,296],{"emptyLinePlaceholder":295},[277,766,767],{"class":279,"line":393},[277,768,769],{},"export const tools = [\n",[277,771,772],{"class":279,"line":399},[277,773,774],{},"    {\n",[277,776,777],{"class":279,"line":405},[277,778,779],{},"        type: \"function\",\n",[277,781,782],{"class":279,"line":410},[277,783,784],{},"        function: {\n",[277,786,787],{"class":279,"line":416},[277,788,789],{},"            name: \"getCurrentWeather\",\n",[277,791,792],{"class":279,"line":421},[277,793,794],{},"            description: \"Get current weather\",\n",[277,796,797],{"class":279,"line":427},[277,798,799],{},"            parameters: {\n",[277,801,802],{"class":279,"line":433},[277,803,804],{},"                type: \"object\",\n",[277,806,807],{"class":279,"line":439},[277,808,809],{},"                properties: {\n",[277,811,812],{"class":279,"line":445},[277,813,814],{},"                    location: {\n",[277,816,817],{"class":279,"line":451},[277,818,819],{},"                        type: \"string\",\n",[277,821,822],{"class":279,"line":457},[277,823,824],{},"                        description: \"The location from where to get weather\"\n",[277,826,827],{"class":279,"line":463},[277,828,829],{},"                    }\n",[277,831,832],{"class":279,"line":468},[277,833,834],{},"                },\n",[277,836,837],{"class":279,"line":474},[277,838,839],{},"                required: [\"location\"]\n",[277,841,842],{"class":279,"line":480},[277,843,608],{},[277,845,846],{"class":279,"line":486},[277,847,614],{},[277,849,850],{"class":279,"line":492},[277,851,852],{},"    },\n",[277,854,855],{"class":279,"line":498},[277,856,774],{},[277,858,859],{"class":279,"line":503},[277,860,779],{},[277,862,863],{"class":279,"line":509},[277,864,784],{},[277,866,867],{"class":279,"line":515},[277,868,869],{},"            name: \"getLocation\",\n",[277,871,872],{"class":279,"line":521},[277,873,874],{},"            description: \"Get user's current location\",\n",[277,876,877],{"class":279,"line":527},[277,878,799],{},[277,880,881],{"class":279,"line":533},[277,882,804],{},[277,884,885],{"class":279,"line":539},[277,886,887],{},"                properties: {}\n",[277,889,890],{"class":279,"line":545},[277,891,608],{},[277,893,894],{"class":279,"line":551},[277,895,614],{},[277,897,898],{"class":279,"line":557},[277,899,852],{},[277,901,902],{"class":279,"line":563},[277,903,904],{},"]\n",[222,906,908],{"id":907},"how-openai-function-calling-works","How OpenAI Function Calling Works",[262,910,912],{"id":911},"_1-tool-definition","1. Tool Definition",[218,914,915],{},"Tools are defined using JSON Schema format:",[267,917,919],{"className":269,"code":918,"language":271,"meta":272,"style":272},"{\n    type: \"function\",\n    function: {\n        name: \"getCurrentWeather\",\n        description: \"Get current weather\",\n        parameters: {\n            type: \"object\",\n            properties: {\n                location: {\n                    type: \"string\",\n                    description: \"The location from where to get weather\"\n                }\n            },\n            required: [\"location\"]\n        }\n    }\n}\n",[274,920,921,926,931,936,941,946,951,956,961,966,971,976,981,986,991,995,999],{"__ignoreMap":272},[277,922,923],{"class":279,"line":280},[277,924,925],{},"{\n",[277,927,928],{"class":279,"line":286},[277,929,930],{},"    type: \"function\",\n",[277,932,933],{"class":279,"line":292},[277,934,935],{},"    function: {\n",[277,937,938],{"class":279,"line":299},[277,939,940],{},"        name: \"getCurrentWeather\",\n",[277,942,943],{"class":279,"line":305},[277,944,945],{},"        description: \"Get current weather\",\n",[277,947,948],{"class":279,"line":311},[277,949,950],{},"        parameters: {\n",[277,952,953],{"class":279,"line":317},[277,954,955],{},"            type: \"object\",\n",[277,957,958],{"class":279,"line":323},[277,959,960],{},"            properties: {\n",[277,962,963],{"class":279,"line":328},[277,964,965],{},"                location: {\n",[277,967,968],{"class":279,"line":334},[277,969,970],{},"                    type: \"string\",\n",[277,972,973],{"class":279,"line":340},[277,974,975],{},"                    description: \"The location from where to get weather\"\n",[277,977,978],{"class":279,"line":346},[277,979,980],{},"                }\n",[277,982,983],{"class":279,"line":352},[277,984,985],{},"            },\n",[277,987,988],{"class":279,"line":357},[277,989,990],{},"            required: [\"location\"]\n",[277,992,993],{"class":279,"line":363},[277,994,614],{},[277,996,997],{"class":279,"line":369},[277,998,620],{},[277,1000,1001],{"class":279,"line":375},[277,1002,349],{},[262,1004,1006],{"id":1005},"_2-api-request","2. API Request",[218,1008,1009],{},"The tools array is passed to the API:",[267,1011,1013],{"className":269,"code":1012,"language":271,"meta":272,"style":272},"const response = await openai.chat.completions.create({\n    model: \"gpt-3.5-turbo-1106\",\n    messages,\n    tools  // ← Tools array passed here\n})\n",[274,1014,1015,1020,1025,1030,1035],{"__ignoreMap":272},[277,1016,1017],{"class":279,"line":280},[277,1018,1019],{},"const response = await openai.chat.completions.create({\n",[277,1021,1022],{"class":279,"line":286},[277,1023,1024],{},"    model: \"gpt-3.5-turbo-1106\",\n",[277,1026,1027],{"class":279,"line":292},[277,1028,1029],{},"    messages,\n",[277,1031,1032],{"class":279,"line":299},[277,1033,1034],{},"    tools  // ← Tools array passed here\n",[277,1036,1037],{"class":279,"line":305},[277,1038,320],{},[262,1040,1042],{"id":1041},"_3-response-handling","3. Response Handling",[218,1044,1045],{},"OpenAI returns structured tool calls:",[267,1047,1049],{"className":269,"code":1048,"language":271,"meta":272,"style":272},"const { finish_reason: finishReason, message } = response.choices[0]\nconst { tool_calls: toolCalls } = message\n\n// toolCalls structure:\n[\n    {\n        id: \"call_abc123\",\n        type: \"function\",\n        function: {\n            name: \"getCurrentWeather\",\n            arguments: '{\"location\": \"New York\"}'\n        }\n    }\n]\n",[274,1050,1051,1056,1061,1065,1070,1075,1079,1084,1088,1092,1096,1101,1105,1109],{"__ignoreMap":272},[277,1052,1053],{"class":279,"line":280},[277,1054,1055],{},"const { finish_reason: finishReason, message } = response.choices[0]\n",[277,1057,1058],{"class":279,"line":286},[277,1059,1060],{},"const { tool_calls: toolCalls } = message\n",[277,1062,1063],{"class":279,"line":292},[277,1064,296],{"emptyLinePlaceholder":295},[277,1066,1067],{"class":279,"line":299},[277,1068,1069],{},"// toolCalls structure:\n",[277,1071,1072],{"class":279,"line":305},[277,1073,1074],{},"[\n",[277,1076,1077],{"class":279,"line":311},[277,1078,774],{},[277,1080,1081],{"class":279,"line":317},[277,1082,1083],{},"        id: \"call_abc123\",\n",[277,1085,1086],{"class":279,"line":323},[277,1087,779],{},[277,1089,1090],{"class":279,"line":328},[277,1091,784],{},[277,1093,1094],{"class":279,"line":334},[277,1095,789],{},[277,1097,1098],{"class":279,"line":340},[277,1099,1100],{},"            arguments: '{\"location\": \"New York\"}'\n",[277,1102,1103],{"class":279,"line":346},[277,1104,614],{},[277,1106,1107],{"class":279,"line":352},[277,1108,620],{},[277,1110,1111],{"class":279,"line":357},[277,1112,904],{},[262,1114,1116],{"id":1115},"_4-function-execution","4. Function Execution",[218,1118,1119],{},"Execute the function and add result to conversation:",[267,1121,1123],{"className":269,"code":1122,"language":271,"meta":272,"style":272},"for (const toolCall of toolCalls) {\n    const functionName = toolCall.function.name\n    const functionToCall = availableFunctions[functionName]\n    const functionArgs = JSON.parse(toolCall.function.arguments)\n    const functionResponse = await functionToCall(functionArgs)\n    \n    messages.push({\n        tool_call_id: toolCall.id,\n        role: \"tool\",\n        name: functionName,\n        content: functionResponse\n    })\n}\n",[274,1124,1125,1130,1135,1140,1145,1150,1155,1160,1165,1170,1175,1180,1185],{"__ignoreMap":272},[277,1126,1127],{"class":279,"line":280},[277,1128,1129],{},"for (const toolCall of toolCalls) {\n",[277,1131,1132],{"class":279,"line":286},[277,1133,1134],{},"    const functionName = toolCall.function.name\n",[277,1136,1137],{"class":279,"line":292},[277,1138,1139],{},"    const functionToCall = availableFunctions[functionName]\n",[277,1141,1142],{"class":279,"line":299},[277,1143,1144],{},"    const functionArgs = JSON.parse(toolCall.function.arguments)\n",[277,1146,1147],{"class":279,"line":305},[277,1148,1149],{},"    const functionResponse = await functionToCall(functionArgs)\n",[277,1151,1152],{"class":279,"line":311},[277,1153,1154],{},"    \n",[277,1156,1157],{"class":279,"line":317},[277,1158,1159],{},"    messages.push({\n",[277,1161,1162],{"class":279,"line":323},[277,1163,1164],{},"        tool_call_id: toolCall.id,\n",[277,1166,1167],{"class":279,"line":328},[277,1168,1169],{},"        role: \"tool\",\n",[277,1171,1172],{"class":279,"line":334},[277,1173,1174],{},"        name: functionName,\n",[277,1176,1177],{"class":279,"line":340},[277,1178,1179],{},"        content: functionResponse\n",[277,1181,1182],{"class":279,"line":346},[277,1183,1184],{},"    })\n",[277,1186,1187],{"class":279,"line":352},[277,1188,349],{},[222,1190,1192],{"id":1191},"key-differences-from-react","Key Differences from ReAct",[1194,1195,1196,1212],"table",{},[1197,1198,1199],"thead",{},[1200,1201,1202,1206,1209],"tr",{},[1203,1204,1205],"th",{},"Aspect",[1203,1207,1208],{},"ReAct",[1203,1210,1211],{},"OpenAI Function Calling",[1213,1214,1215,1229,1242,1255,1268],"tbody",{},[1200,1216,1217,1223,1226],{},[1218,1219,1220],"td",{},[236,1221,1222],{},"Tool Selection",[1218,1224,1225],{},"Manual text parsing",[1218,1227,1228],{},"Automatic tool selection",[1200,1230,1231,1236,1239],{},[1218,1232,1233],{},[236,1234,1235],{},"Argument Extraction",[1218,1237,1238],{},"Regex patterns",[1218,1240,1241],{},"JSON schema validation",[1200,1243,1244,1249,1252],{},[1218,1245,1246],{},[236,1247,1248],{},"Error Handling",[1218,1250,1251],{},"Manual parsing errors",[1218,1253,1254],{},"Structured error responses",[1200,1256,1257,1262,1265],{},[1218,1258,1259],{},[236,1260,1261],{},"Reliability",[1218,1263,1264],{},"Prone to parsing failures",[1218,1266,1267],{},"More reliable",[1200,1269,1270,1275,1278],{},[1218,1271,1272],{},[236,1273,1274],{},"Complexity",[1218,1276,1277],{},"Higher implementation complexity",[1218,1279,1280],{},"Lower implementation complexity",[222,1282,1284],{"id":1283},"example-execution-flow","Example Execution Flow",[218,1286,1287,1290],{},[236,1288,1289],{},"Query",": \"What's the current weather in my current location?\"",[262,1292,1294],{"id":1293},"iteration-1","Iteration 1:",[1296,1297,1298,1312,1321],"ol",{},[233,1299,1300,1303,1304,1307,1308,1311],{},[236,1301,1302],{},"OpenAI Response",": ",[274,1305,1306],{},"tool_calls"," with ",[274,1309,1310],{},"getLocation"," function",[233,1313,1314,1303,1317,1320],{},[236,1315,1316],{},"Function Execution",[274,1318,1319],{},"getLocation()"," returns user IP location",[233,1322,1323,1326],{},[236,1324,1325],{},"Message Added",": Tool response with location data",[262,1328,1330],{"id":1329},"iteration-2","Iteration 2:",[1296,1332,1333,1342,1350],{},[233,1334,1335,1303,1337,1307,1339,1311],{},[236,1336,1302],{},[274,1338,1306],{},[274,1340,1341],{},"getCurrentWeather",[233,1343,1344,1303,1346,1349],{},[236,1345,1316],{},[274,1347,1348],{},"getCurrentWeather({location: \"New York\"})"," returns weather",[233,1351,1352,1354],{},[236,1353,1325],{},": Tool response with weather data",[262,1356,1358],{"id":1357},"iteration-3","Iteration 3:",[1296,1360,1361,1369],{},[233,1362,1363,1303,1365,1368],{},[236,1364,1302],{},[274,1366,1367],{},"finish_reason: \"stop\""," with final answer",[233,1370,1371,1374],{},[236,1372,1373],{},"Agent Output",": \"The current weather in New York is sunny with a temperature of 75°F.\"",[222,1376,1378],{"id":1377},"best-practices","Best Practices",[262,1380,1382],{"id":1381},"tool-design","Tool Design",[230,1384,1385,1391,1397,1402],{},[233,1386,1387,1390],{},[236,1388,1389],{},"Clear Descriptions",": Help OpenAI understand when to use each tool",[233,1392,1393,1396],{},[236,1394,1395],{},"Required Parameters",": Specify which parameters are mandatory",[233,1398,1399,1401],{},[236,1400,243],{},": Use proper JSON Schema types",[233,1403,1404,1406],{},[236,1405,1248],{},": Implement robust error handling in tool functions",[262,1408,1410],{"id":1409},"message-management","Message Management",[230,1412,1413,1419,1425],{},[233,1414,1415,1418],{},[236,1416,1417],{},"Context Preservation",": Maintain full conversation history",[233,1420,1421,1424],{},[236,1422,1423],{},"Tool Responses",": Add tool results with proper role and IDs",[233,1426,1427,1430],{},[236,1428,1429],{},"Iteration Limits",": Prevent infinite loops with MAX_ITERATIONS",[262,1432,1248],{"id":1433},"error-handling",[267,1435,1437],{"className":269,"code":1436,"language":271,"meta":272,"style":272},"try {\n    const functionResponse = await functionToCall(functionArgs)\n    messages.push({\n        tool_call_id: toolCall.id,\n        role: \"tool\",\n        name: functionName,\n        content: functionResponse\n    })\n} catch (error) {\n    messages.push({\n        tool_call_id: toolCall.id,\n        role: \"tool\",\n        name: functionName,\n        content: JSON.stringify({ error: error.message })\n    })\n}\n",[274,1438,1439,1444,1448,1452,1456,1460,1464,1468,1472,1477,1481,1485,1489,1493,1498,1502],{"__ignoreMap":272},[277,1440,1441],{"class":279,"line":280},[277,1442,1443],{},"try {\n",[277,1445,1446],{"class":279,"line":286},[277,1447,1149],{},[277,1449,1450],{"class":279,"line":292},[277,1451,1159],{},[277,1453,1454],{"class":279,"line":299},[277,1455,1164],{},[277,1457,1458],{"class":279,"line":305},[277,1459,1169],{},[277,1461,1462],{"class":279,"line":311},[277,1463,1174],{},[277,1465,1466],{"class":279,"line":317},[277,1467,1179],{},[277,1469,1470],{"class":279,"line":323},[277,1471,1184],{},[277,1473,1474],{"class":279,"line":328},[277,1475,1476],{},"} catch (error) {\n",[277,1478,1479],{"class":279,"line":334},[277,1480,1159],{},[277,1482,1483],{"class":279,"line":340},[277,1484,1164],{},[277,1486,1487],{"class":279,"line":346},[277,1488,1169],{},[277,1490,1491],{"class":279,"line":352},[277,1492,1174],{},[277,1494,1495],{"class":279,"line":357},[277,1496,1497],{},"        content: JSON.stringify({ error: error.message })\n",[277,1499,1500],{"class":279,"line":363},[277,1501,1184],{},[277,1503,1504],{"class":279,"line":369},[277,1505,349],{},[222,1507,1509],{"id":1508},"advanced-features","Advanced Features",[262,1511,1513],{"id":1512},"parallel-tool-calls","Parallel Tool Calls",[218,1515,1516],{},"OpenAI can call multiple tools simultaneously:",[267,1518,1520],{"className":269,"code":1519,"language":271,"meta":272,"style":272},"// OpenAI might return multiple tool_calls in one response\nfor (const toolCall of toolCalls) {\n    // Execute each tool call\n    const functionResponse = await functionToCall(functionArgs)\n    // Add each response separately\n}\n",[274,1521,1522,1527,1531,1536,1540,1545],{"__ignoreMap":272},[277,1523,1524],{"class":279,"line":280},[277,1525,1526],{},"// OpenAI might return multiple tool_calls in one response\n",[277,1528,1529],{"class":279,"line":286},[277,1530,1129],{},[277,1532,1533],{"class":279,"line":292},[277,1534,1535],{},"    // Execute each tool call\n",[277,1537,1538],{"class":279,"line":299},[277,1539,1149],{},[277,1541,1542],{"class":279,"line":305},[277,1543,1544],{},"    // Add each response separately\n",[277,1546,1547],{"class":279,"line":311},[277,1548,349],{},[262,1550,1552],{"id":1551},"conditional-tool-usage","Conditional Tool Usage",[218,1554,1555],{},"Design tools that can be used conditionally:",[267,1557,1559],{"className":269,"code":1558,"language":271,"meta":272,"style":272},"export const tools = [\n    {\n        type: \"function\",\n        function: {\n            name: \"searchDatabase\",\n            description: \"Search database for information\",\n            parameters: {\n                type: \"object\",\n                properties: {\n                    query: { type: \"string\" },\n                    filters: { type: \"object\" }\n                },\n                required: [\"query\"]\n            }\n        }\n    }\n]\n",[274,1560,1561,1565,1569,1573,1577,1582,1587,1591,1595,1599,1604,1609,1613,1618,1622,1626,1630],{"__ignoreMap":272},[277,1562,1563],{"class":279,"line":280},[277,1564,769],{},[277,1566,1567],{"class":279,"line":286},[277,1568,774],{},[277,1570,1571],{"class":279,"line":292},[277,1572,779],{},[277,1574,1575],{"class":279,"line":299},[277,1576,784],{},[277,1578,1579],{"class":279,"line":305},[277,1580,1581],{},"            name: \"searchDatabase\",\n",[277,1583,1584],{"class":279,"line":311},[277,1585,1586],{},"            description: \"Search database for information\",\n",[277,1588,1589],{"class":279,"line":317},[277,1590,799],{},[277,1592,1593],{"class":279,"line":323},[277,1594,804],{},[277,1596,1597],{"class":279,"line":328},[277,1598,809],{},[277,1600,1601],{"class":279,"line":334},[277,1602,1603],{},"                    query: { type: \"string\" },\n",[277,1605,1606],{"class":279,"line":340},[277,1607,1608],{},"                    filters: { type: \"object\" }\n",[277,1610,1611],{"class":279,"line":346},[277,1612,834],{},[277,1614,1615],{"class":279,"line":352},[277,1616,1617],{},"                required: [\"query\"]\n",[277,1619,1620],{"class":279,"line":357},[277,1621,608],{},[277,1623,1624],{"class":279,"line":363},[277,1625,614],{},[277,1627,1628],{"class":279,"line":369},[277,1629,620],{},[277,1631,1632],{"class":279,"line":375},[277,1633,904],{},[218,1635,1636],{},"OpenAI function calling provides a more robust and reliable way to build AI agents compared to manual ReAct implementations, with better error handling, type safety, and integration with OpenAI ecosystem.",[222,1638,1640],{"id":1639},"future-openai-responses-api","Future: OpenAI Responses API",[218,1642,1643,1646,1647,1650],{},[236,1644,1645],{},"Note",": OpenAI has introduced the new ",[236,1648,1649],{},"Responses API",", which is an evolution of the Chat Completions API. The Responses API brings added simplicity and powerful agentic primitives to your integrations.",[218,1652,1653],{},"Key improvements in the Responses API:",[230,1655,1656,1662,1668,1674],{},[233,1657,1658,1661],{},[236,1659,1660],{},"Enhanced simplicity"," - More streamlined API interface",[233,1663,1664,1667],{},[236,1665,1666],{},"Powerful agentic primitives"," - Built-in capabilities for agent development",[233,1669,1670,1673],{},[236,1671,1672],{},"Better tool integration"," - Improved function calling mechanisms",[233,1675,1676,1679],{},[236,1677,1678],{},"Future-proof"," - Designed with agentic applications in mind",[218,1681,1682],{},"As this API matures, future implementations should migrate to the Responses API for the most current and capable agent development experience.",[267,1684,1686],{"className":269,"code":1685,"language":271,"meta":272,"style":272},"// Future implementation will use Responses API\n// (API subject to change as it evolves)\nconst response = await openai.responses.create({\n    // Enhanced parameters for better agent capabilities\n})\n",[274,1687,1688,1693,1698,1703,1708],{"__ignoreMap":272},[277,1689,1690],{"class":279,"line":280},[277,1691,1692],{},"// Future implementation will use Responses API\n",[277,1694,1695],{"class":279,"line":286},[277,1696,1697],{},"// (API subject to change as it evolves)\n",[277,1699,1700],{"class":279,"line":292},[277,1701,1702],{},"const response = await openai.responses.create({\n",[277,1704,1705],{"class":279,"line":299},[277,1706,1707],{},"    // Enhanced parameters for better agent capabilities\n",[277,1709,1710],{"class":279,"line":305},[277,1711,320],{},[1713,1714,1715],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":272,"searchDepth":286,"depth":286,"links":1717},[1718,1719,1723,1729,1730,1735,1740,1744],{"id":224,"depth":286,"text":225},{"id":259,"depth":286,"text":260,"children":1720},[1721,1722],{"id":264,"depth":292,"text":265},{"id":668,"depth":292,"text":669},{"id":907,"depth":286,"text":908,"children":1724},[1725,1726,1727,1728],{"id":911,"depth":292,"text":912},{"id":1005,"depth":292,"text":1006},{"id":1041,"depth":292,"text":1042},{"id":1115,"depth":292,"text":1116},{"id":1191,"depth":286,"text":1192},{"id":1283,"depth":286,"text":1284,"children":1731},[1732,1733,1734],{"id":1293,"depth":292,"text":1294},{"id":1329,"depth":292,"text":1330},{"id":1357,"depth":292,"text":1358},{"id":1377,"depth":286,"text":1378,"children":1736},[1737,1738,1739],{"id":1381,"depth":292,"text":1382},{"id":1409,"depth":292,"text":1410},{"id":1433,"depth":292,"text":1248},{"id":1508,"depth":286,"text":1509,"children":1741},[1742,1743],{"id":1512,"depth":292,"text":1513},{"id":1551,"depth":292,"text":1552},{"id":1639,"depth":286,"text":1640},"Learn how to build OpenAI agents using native function calling capabilities with tools integration.","md",[],{},{"icon":80},{"title":77,"description":1751},"Building AI agents with OpenAI's native function calling and tools.","HbO8OrrX5CTGzIsGYCt_eZ4aAFRAjrwqS3h3ouKO5oA",[1754,1756],{"title":72,"path":73,"stem":74,"description":1755,"icon":75,"children":-1},"ReAct (Reasoning and Acting) is a powerful agent pattern that combines the reasoning capabilities of Large Language Models with the ability to take actions through tools. This approach allows agents to break down complex problems, gather necessary information, and provide accurate responses.",{"title":83,"path":84,"stem":85,"description":1757,"icon":88,"children":-1},"Understanding and managing context windows in large language models.",1772944811041]