ll-24-25
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# function calling Assistants Function Calling Beta =================================== Based on your feedback from the Assistants API beta, we've incorporated key improvements into the Responses API. After we achieve full feature parity, we will announce a **deprecation plan** later this year, with a target sunset date in the first half of 2026. [Learn more](/docs/guides/responses-vs-chat-completions). Overview -------- Similar to the Chat Completions API, the Assistants API supports function calling. Function calling allows you to describe functions to the Assistants API and have it intelligently return the functions that need to be called along with their arguments. Quickstart ---------- In this example, we'll create a weather assistant and define two functions, `get_current_temperature` and `get_rain_probability`, as tools that the Assistant can call. Depending on the user query, the model will invoke parallel function calling if using our latest models released on or after Nov 6, 2023. In our example that uses parallel function calling, we will ask the Assistant what the weather in San Francisco is like today and the chances of rain. We also show how to output the Assistant's response with streaming. With the launch of Structured Outputs, you can now use the parameter `strict: true` when using function calling with the Assistants API. For more information, refer to the [Function calling guide](/docs/guides/function-calling#function-calling-with-structured-outputs). Please note that Structured Outputs are not supported in the Assistants API when using vision. ### Step 1: Define functions When creating your assistant, you will first define the functions under the `tools` param of the assistant. ```python from openai import OpenAI client = OpenAI() assistant = client.beta.assistants.create( instructions="You are a weather bot. Use the provided functions to answer questions.", model="gpt-4o", tools=[ { "type": "function", "function": { "name": "get_current_temperature", "description": "Get the current temperature for a specific location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g., San Francisco, CA" }, "unit": { "type": "string", "enum": ["Celsius", "Fahrenheit"], "description": "The temperature unit to use. Infer this from the user's location." } }, "required": ["location", "unit"] } } }, { "type": "function", "function": { "name": "get_rain_probability", "description": "Get the probability of rain for a specific location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g., San Francisco, CA" } }, "required": ["location"] } } } ] ) ``` ```javascript const assistant = await client.beta.assistants.create({ model: "gpt-4o", instructions: "You are a weather bot. Use the provided functions to answer questions.", tools: [ { type: "function", function: { name: "getCurrentTemperature", description: "Get the current temperature for a specific location", parameters: { type: "object", properties: { location: { type: "string", description: "The city and state, e.g., San Francisco, CA", }, unit: { type: "string", enum: ["Celsius", "Fahrenheit"], description: "The temperature unit to use. Infer this from the user's location.", }, }, required: ["location", "unit"], }, }, }, { type: "function", function: { name: "getRainProbability", description: "Get the probability of rain for a specific location", parameters: { type: "object", properties: { location: { type: "string", description: "The city and state, e.g., San Francisco, CA", }, }, required: ["location"], }, }, }, ], }); ``` ### Step 2: Create a Thread and add Messages Create a Thread when a user starts a conversation and add Messages to the Thread as the user asks questions. ```python thread = client.beta.threads.create() message = client.beta.threads.messages.create( thread_id=thread.id, role="user", content="What's the weather in San Francisco today and the likelihood it'll rain?", ) ``` ```javascript const thread = await client.beta.threads.create(); const message = client.beta.threads.messages.create(thread.id, { role: "user", content: "What's the weather in San Francisco today and the likelihood it'll rain?", }); ``` ### Step 3: Initiate a Run When you initiate a Run on a Thread containing a user Message that triggers one or more functions, the Run will enter a `pending` status. After it processes, the run will enter a `requires_action` state which you can verify by checking the Run’s `status`. This indicates that you need to run tools and submit their outputs to the Assistant to continue Run execution. In our case, we will see two `tool_calls`, which indicates that the user query resulted in parallel function calling. Note that a runs expire ten minutes after creation. Be sure to submit your tool outputs before the 10 min mark. You will see two `tool_calls` within `required_action`, which indicates the user query triggered parallel function calling. ```json { "id": "run_qJL1kI9xxWlfE0z1yfL0fGg9", ... "status": "requires_action", "required_action": { "submit_tool_outputs": { "tool_calls": [ { "id": "call_FthC9qRpsL5kBpwwyw6c7j4k", "function": { "arguments": "{"location": "San Francisco, CA"}", "name": "get_rain_probability" }, "type": "function" }, { "id": "call_RpEDoB8O0FTL9JoKTuCVFOyR", "function": { "arguments": "{"location": "San Francisco, CA", "unit": "Fahrenheit"}", "name": "get_current_temperature" }, "type": "function" } ] }, ... "type": "submit_tool_outputs" } } ``` Run object truncated here for readability How you initiate a Run and submit `tool_calls` will differ depending on whether you are using streaming or not, although in both cases all `tool_calls` need to be submitted at the same time. You can then complete the Run by submitting the tool outputs from the functions you called. Pass each `tool_call_id` referenced in the `required_action` object to match outputs to each function call. With streaming For the streaming case, we create an EventHandler class to handle events in the response stream and submit all tool outputs at once with the “submit tool outputs stream” helper in the Python and Node SDKs. ```python from typing_extensions import override from openai import AssistantEventHandler class EventHandler(AssistantEventHandler): @override def on_event(self, event): # Retrieve events that are denoted with 'requires_action' # since these will have our tool_calls if event.event == 'thread.run.requires_action': run_id = event.data.id # Retrieve the run ID from the event data self.handle_requires_action(event.data, run_id) def handle_requires_action(self, data, run_id): tool_outputs = [] for tool in data.required_action.submit_tool_outputs.tool_calls: if tool.function.name == "get_current_temperature": tool_outputs.append({"tool_call_id": tool.id, "output": "57"}) elif tool.function.name == "get_rain_probability": tool_outputs.append({"tool_call_id": tool.id, "output": "0.06"}) # Submit all tool_outputs at the same time self.submit_tool_outputs(tool_outputs, run_id) def submit_tool_outputs(self, tool_outputs, run_id): # Use the submit_tool_outputs_stream helper with client.beta.threads.runs.submit_tool_outputs_stream( thread_id=self.current_run.thread_id, run_id=self.current_run.id, tool_outputs=tool_outputs, event_handler=EventHandler(), ) as stream: for text in stream.text_deltas: print(text, end="", flush=True) print() with client.beta.threads.runs.stream( thread_id=thread.id, assistant_id=assistant.id, event_handler=EventHandler() ) as stream: stream.until_done() ``` ```javascript class EventHandler extends EventEmitter { constructor(client) { super(); this.client = client; } async onEvent(event) { try { console.log(event); // Retrieve events that are denoted with 'requires_action' // since these will have our tool_calls if (event.event === "thread.run.requires_action") { await this.handleRequiresAction( event.data, event.data.id, event.data.thread_id, ); } } catch (error) { console.error("Error handling event:", error); } } async handleRequiresAction(data, runId, threadId) { try { const toolOutputs = data.required_action.submit_tool_outputs.tool_calls.map((toolCall) => { if (toolCall.function.name === "getCurrentTemperature") { return { tool_call_id: toolCall.id, output: "57", }; } else if (toolCall.function.name === "getRainProbability") { return { tool_call_id: toolCall.id, output: "0.06", }; } }); // Submit all the tool outputs at the same time await this.submitToolOutputs(toolOutputs, runId, threadId); } catch (error) { console.error("Error processing required action:", error); } } async submitToolOutputs(toolOutputs, runId, threadId) { try { // Use the submitToolOutputsStream helper const stream = this.client.beta.threads.runs.submitToolOutputsStream( threadId, runId, { tool_outputs: toolOutputs }, ); for await (const event of stream) { this.emit("event", event); } } catch (error) { console.error("Error submitting tool outputs:", error); } } } const eventHandler = new EventHandler(client); eventHandler.on("event", eventHandler.onEvent.bind(eventHandler)); const stream = await client.beta.threads.runs.stream( threadId, { assistant_id: assistantId }, eventHandler, ); for await (const event of stream) { eventHandler.emit("event", event); } ``` Without streaming Runs are asynchronous, which means you'll want to monitor their `status` by polling the Run object until a [terminal status](https://platform.openai.com/docs/assistants/deep-dive#runs-and-run-steps) is reached. For convenience, the 'create and poll' SDK helpers assist both in creating the run and then polling for its completion. Once the Run completes, you can list the Messages added to the Thread by the Assistant. Finally, you would retrieve all the `tool_outputs` from `required_action` and submit them at the same time to the 'submit tool outputs and poll' helper. ```python run = client.beta.threads.runs.create_and_poll( thread_id=thread.id, assistant_id=assistant.id, ) if run.status == 'completed': messages = client.beta.threads.messages.list( thread_id=thread.id ) print(messages) else: print(run.status) # Define the list to store tool outputs tool_outputs = [] # Loop through each tool in the required action section for tool in run.required_action.submit_tool_outputs.tool_calls: if tool.function.name == "get_current_temperature": tool_outputs.append({ "tool_call_id": tool.id, "output": "57" }) elif tool.function.name == "get_rain_probability": tool_outputs.append({ "tool_call_id": tool.id, "output": "0.06" }) # Submit all tool outputs at once after collecting them in a list if tool_outputs: try: run = client.beta.threads.runs.submit_tool_outputs_and_poll( thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs ) print("Tool outputs submitted successfully.") except Exception as e: print("Failed to submit tool outputs:", e) else: print("No tool outputs to submit.") if run.status == 'completed': messages = client.beta.threads.messages.list( thread_id=thread.id ) print(messages) else: print(run.status) ``` ```javascript const handleRequiresAction = async (run) => { // Check if there are tools that require outputs if ( run.required_action && run.required_action.submit_tool_outputs && run.required_action.submit_tool_outputs.tool_calls ) { // Loop through each tool in the required action section const toolOutputs = run.required_action.submit_tool_outputs.tool_calls.map( (tool) => { if (tool.function.name === "getCurrentTemperature") { return { tool_call_id: tool.id, output: "57", }; } else if (tool.function.name === "getRainProbability") { return { tool_call_id: tool.id, output: "0.06", }; } }, ); // Submit all tool outputs at once after collecting them in a list if (toolOutputs.length > 0) { run = await client.beta.threads.runs.submitToolOutputsAndPoll( thread.id, run.id, { tool_outputs: toolOutputs }, ); console.log("Tool outputs submitted successfully."); } else { console.log("No tool outputs to submit."); } // Check status after submitting tool outputs return handleRunStatus(run); } }; const handleRunStatus = async (run) => { // Check if the run is completed if (run.status === "completed") { let messages = await client.beta.threads.messages.list(thread.id); console.log(messages.data); return messages.data; } else if (run.status === "requires_action") { console.log(run.status); return await handleRequiresAction(run); } else { console.error("Run did not complete:", run); } }; // Create and poll run let run = await client.beta.threads.runs.createAndPoll(thread.id, { assistant_id: assistant.id, }); handleRunStatus(run); ``` ### Using Structured Outputs When you enable [Structured Outputs](/docs/guides/structured-outputs) by supplying `strict: true`, the OpenAI API will pre-process your supplied schema on your first request, and then use this artifact to constrain the model to your schema. ```python from openai import OpenAI client = OpenAI() assistant = client.beta.assistants.create( instructions="You are a weather bot. Use the provided functions to answer questions.", model="gpt-4o-2024-08-06", tools=[ { "type": "function", "function": { "name": "get_current_temperature", "description": "Get the current temperature for a specific location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g., San Francisco, CA" }, "unit": { "type": "string", "enum": ["Celsius", "Fahrenheit"], "description": "The temperature unit to use. Infer this from the user's location." } }, "required": ["location", "unit"], "additionalProperties": False }, "strict": True } }, { "type": "function", "function": { "name": "get_rain_probability", "description": "Get the probability of rain for a specific location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g., San Francisco, CA" } }, "required": ["location"], "additionalProperties": False }, "strict": True } } ] ) ``` ```javascript const assistant = await client.beta.assistants.create({ model: "gpt-4o-2024-08-06", instructions: "You are a weather bot. Use the provided functions to answer questions.", tools: [ { type: "function", function: { name: "getCurrentTemperature", description: "Get the current temperature for a specific location", parameters: { type: "object", properties: { location: { type: "string", description: "The city and state, e.g., San Francisco, CA", }, unit: { type: "string", enum: ["Celsius", "Fahrenheit"], description: "The temperature unit to use. Infer this from the user's location.", }, }, required: ["location", "unit"], additionalProperties: false }, strict: true }, }, { type: "function", function: { name: "getRainProbability", description: "Get the probability of rain for a specific location", parameters: { type: "object", properties: { location: { type: "string", description: "The city and state, e.g., San Francisco, CA", }, }, required: ["location"], additionalProperties: false }, strict: true }, }, ], }); ```

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully