Can Assistant Functions POST to an API?

Can functions in assistant environment be used to call on an API and POST via curl, for example?

If so, are there examples of how this can be accomplished?

A:

Sure, and such functions don’t necessarily need to involve assistants. Chat Completions models, where functions (and their alternatives, “tools”) reside, are ultimately the core of this AI method.

Firstly, the API has no internet access. It will only return function calls to you and your code.

Thus, you need to write a function specification in a manner that the AI can understand, where the AI isn’t burdened by minutiae. For example, you wouldn’t want to say “write the JSON in the exact format this API requires”. Instead, the parameters the AI outputs should be simplified down to natural language, with your code doing the heavy lifting of translating to and employing an external API.

If you are coding for AI, aiming to handle functions and create return language also understandable to AI, then command-line curl likely isn’t what you’d be using. Rather, your server backend could use Python or node.js as an engine with libraries to interface with the external API. For Python, consider using requests or httpx.

The forum has links to “documentation” with a few function examples. OpenAI don’t promote any particular API by examples though, and the code someone wrote or hired at $100/hr for their own product and a particular application isn’t likely to be a forum gift to you.

Hope that lets you imagine the possibilities, where an AI can see an external service would be useful for giving a fulfilling answer or taking action on behalf of a user.

1 Like

I think that makes sense to me now. I put this together to return ids of items the assistant finds in the Json table, but it doesn’t seem to produce desired results. Any insight as to what is wrong/missing?

{
“name”: “get_id”,
“description”: “Retrieve the ID based on Description”,
“parameters”: {
“type”: “object”,
“properties”: {
“Description”: {
“type”: “string”,
“description”: “The description of the item”
}
},
“required”: [
“Description”
]
}
}

Your function is extremely generic. It doesn’t tell the AI what is behind that function that would be useful for fulfilling user inputs.

What’s an ID for? What’s an item?


To improve, using an example case…

Don’t use “description” as a property name, it overlaps other JSON specification methods. ourcompany_product_description would be instructively verbose.

Then for the function description, communicate to the AI just as you would to a person that has to write a JSON query to an API to answer a user’s question.

“The get_id function will search our company’s product database using a internal database search engine, and will return an ID number for a product that most closely matches. This ID number can then be provided or used by AI if a customer wants to order the product from us.”

“This product description used for searching should target 10 words in length, using natural language, and should include the exact product name if it is provided.”

That works really well in the playground! Thank you.

Could you explain why I need to return true for every function response to get the assistant to output a response to the original prompt?

I also don’t understand how to integrate that into a web app via curl API calls.

Wouldn’t it be easier to achieve the result I’m looking for by just integrating the “function” logic into the assistant instructions and extract the values from the text response?

You’ll need a server backend that can do the code processing of functions. You would not want to place any interactions that go direct to OpenAI or other authenticated APIs in client web code.

Functions are not for obtaining a response to a user, they are for polling an external service for information or for taking actions. Your case of “AI describes a product, to get back its ID number” is an example of a function described correctly, where the AI makes its request to your code, and you make some database search to find the answer for the AI.

You then send back a tool return value if using assistants, or on chat completions, you append a replay of the assistant request message and a function message with the results. I don’t know why you’d return “true”, when it is the natural language that your function promised that needs to be returned. Then the AI can use that new information to answer.