-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python: Function Calling Error when Using APIM Managed Azure OpenAI #10182
Comments
A bad request means that something formed in the request body is not accepted. Can you please give us some more details about the request being made to the model? |
I am able to reproduce the issue with a very simple function call script below.
Let me know if there is anything else I need to provide. |
Hi @awonglk, thanks for the added context. Can you please make sure you're following the follow flow when using the APIM auth: |
The only thing I'm not following from that link is I'm not using "OnBehalfOfCredential". Is using OnBehalfOfCredential auth a must? |
Alright, I understand. Apologies to try and lead you down the wrong path. I'll need to figure out how to replicate your issue. For further context, have you followed this step? Pass the API key in API requests - set-backend-service policy. |
@awonglk, based on this previous comment - a user was experiencing a function calling error using APIM in SK .Net -- could it be that your model you're calling doesn't support function calling? |
Or this option as well: #8340 (comment) AzureChatCompletion(
api_key=`Value of Ocp-Apim-SubscriptionKey`
endpoint=AZURE_OPENAI_ENDPOINT, # your endpoint, which doesn't include `deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview`
deployment_name=AZURE_OPENAI_CHAT_DEPLOYMENT_NAME, # gpt-4o
api_version='2024-02-15-preview'
default_headers={'Ocp-Apim-SubscriptionKey': <value>}
} |
For your 3 x comments above:
|
Which Azure OpenAI API version are you using? For example, I know APIM is a part of this, but the model is not failing with a 401/403, so it doesn't look like an auth issue.
In the "it works" case, are you including tools in your request? Or are you removing tools? |
Regarding which Azure Open API version -> I'm using 2024-08-01-preview Regarding "it works" case, I'm still passing the list of tools (as per simple script above). The only difference is the question I ask. "what is the time?" -> invokes Time_plugin which fails with the error |
I have also just updated the model to use version 2024-08-06, with same endpoint URL with same results. |
This is the error log with debug enabled: Tell me a joke Because they make up everything! The above exception was the direct cause of the following exception: Traceback (most recent call last): |
Hi @awonglk. @fbinotto and I have been working together to track down what could be going on. I recently created an APIM resource and hooked it up to my AOAI instance, and am able to perform function calling. @fbinotto was hitting the same error, as you can see above, and we were able to get him up and running with function calling (we're still trying to determine exactly what we changed on his side to fix things). I can try and give a high level overview about what I did, as I didn't do too much to get things working: I created the APIM resource in the portal, and then created a new Azure OpenAI Service API based on the March preview OpenAPI spec. I clicked a box to "improve SDK compatibility" which adds the display name to the base url. There are no secrets here, so I will share this image. Then, in Semantic Kernel, I am doing the following: chat = AzureChatCompletion(
service_id="apim",
endpoint="https://apim-ev3.azure-api.net/evan-apim/",
deployment_name="gpt-4o",
api_version="2024-12-01-preview",
# default_headers={"api-key": "my APIM key"},
api_key="my APIM key",
) One other config that I did was add my APIM managed identity to the AOAI resource's access control. But because I am using the "all access APIM API key" I don't think this step is necessary. When I run a simple example that leverages the
|
The issue was related to APIM policy, more specifically the azure-openai-token-limit. We had different policies applied to different products. Basically, the attribute estimate-prompt-tokens for that policy has to be set to false. If set to true, it throws the 400 error stating it is unable to parse and estimate tokens from incoming request. When that attribute is set to true, the number of tokens is estimated based on the prompt schema in the API. When it is set to false, the tokens are calculated based on the actual token usage from the response of the model. |
Discussed in #10176
Originally posted by awonglk January 14, 2025
I've followed the article pointed by following issue to try to get my Semantic Kernel app working with APIM managed Azure OpenAI:
#7143
If there's no function calls involved, the responses back from LLM seems to work as normal.
But as soon as I ask a question that involves a plugin (even the core plugin time_plugin() as an example)
This is what I get when asking a simple question like "What is the time?"
<class 'semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion.AzureChatCompletion'> service failed to complete the prompt\", BadRequestError('Error code: 400 - {\\'statusCode\\': 400, \\'message\\': \"Unable to parse and estimate tokens from incoming request. Please ensure incoming request does not contain any images and is of one of the following types: \\'Chat Completion\\', \\'Completion\\', \\'Embeddings\\' and works with current prompt estimation mode of \\'Auto\\'.\"}'))
Is there anything obvious that I may have missed?
Using semantic-kernel 1.16.0
The text was updated successfully, but these errors were encountered: