Skip to content

Multiple Roundtrips for functions calls are failing in Azure OpenAI Chat Chatcompletion #1042

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

Conversation

timostark
Copy link

@timostark timostark commented Jul 12, 2024

If the user question (or the available functions) force multiple roundtrips of the user flow, the existing Azure OpenAI implementation only calls functions in the first roundtrip.

Example Question: What's the weather like in San Francisco? If the weather is above 25 degrees, please check the weather in Tokyo and Paris

==> Will return:
grafik

The reason for this issue is, that the second call to Azure OpenAI (including the tool_id response) does not include the tools of the original request anymore. This of course makes the model unable to call another tool if required.

Unfortunatly there are additional refactorings required to enable streaming support - see first comment.

Should/Might also fix:

@timostark
Copy link
Author

timostark commented Jul 12, 2024

The first simple fix did not work in the streaming scenario. Also Azure OpenAI was not ready for multiple roundtrips. Reason seems to be some "hacks" required for the azureApi in the ChatModel.stream method. These hacks weren't called again on the second roundtrip as the AbstractFunctionCallSupport was directly calling the Azure Library instead of passing back to the stream method (like e.g. the OpenAI implementation is doing it..).

The suggested fix extracts the azure part of the streaming (the "hacks" :-) ). In case of tool calls - identical to openai - the azure model itself is handling the message adding and recursive calling of the streaming api.

PS: I think that my solution is not really good, but imo a "bigger" refactoring would be required at the streaming side.. as far as i see the flux mapping prevents the consumer to retrieve the tokens as soon as they are sent.. (making streaming "senseless" at the moment). I am not 100% sure where to start here, so i just tried to make it usable for now.

@timostark
Copy link
Author

One last remark.. The upgrade to azure-ai-openai 1.0.0.-beta.10 (from 8) causes a ton of JsonEOFException during streaming && tool-calls (reproducible also before my change).

  <artifactId>azure-ai-openai</artifactId>
  <version>1.0.0-beta.10</version>

These issues do not occur with beta 8.

grafik

@tzolov tzolov self-assigned this Jul 15, 2024
@tzolov tzolov added this to the 1.0.0-M2 milestone Jul 15, 2024
@tzolov tzolov added bug Something isn't working tool/function calling labels Jul 16, 2024
@tzolov
Copy link
Contributor

tzolov commented Jul 16, 2024

Hi @timostark , I'm so grateful for your efforts in identifying and solving this issue. Great job!

Currently, I'm refactoring, the Azure OpenAI function calling support to raise the API to the Spring AI API level. Similar to what I did recently fro OpenAI, Anthropic and VertexAI Gemini.
Have the feeling that this might solve the issue for both call and streaming. Will reuse your ITs to verify it.

Btw, what name should I use for the javadoc @author? Your timostark handle or something else?

@timostark
Copy link
Author

timostark commented Jul 16, 2024

@tzolov i've quickly raised #1054 to avoid double efforts.. I was refactoring Azure AI to be compliant to OpenAI API and also switch to the Async API. By that the streaming is actually working...

I've also reverted to beta 8 and reported an azure bug ( Azure/azure-sdk-for-java#41164 )

If you are fine with the direction that new PR is going (it includes the changes of this PR) you can close this one..

@timostark
Copy link
Author

ah and @autor timostark is fine :-)

@tzolov
Copy link
Contributor

tzolov commented Jul 16, 2024

@timostark , I'm not sure what causes the json reading errors, but it seems to be some Azure API bug.

Even if I iterate the direct openAIClient response without and processing:

IterableStream<ChatCompletions> chatCompletionsStream = this.openAIClient.getChatCompletionsStream(options.getModel(), options);
chatCompletionsStream.stream().forEach(cc -> System.out.println(cc));

I still see errors like this, showing that the underlining json have been cut in the middle

ERROR 82446 --- [oundedElastic-1] c.a.c.i.s.DefaultJsonSerializer          : com.azure.json.implementation.jackson.core.io.JsonEOFException: Unexpected end-of-input within/between Object entries at [Source: (byte[])"{"choices":[{"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"delta":{"content":" Story"},"finish_reason":null,"index""; line: 1, column: 285]

@tzolov
Copy link
Contributor

tzolov commented Jul 16, 2024

@timostark,
please feel free to review this PR before merged: #1055

the #1054 come bit late for me. But if there is something in 1054 missing in 1055 feel free to rise another PR.

Again , thank you very much for the effort.

tzolov added a commit that referenced this pull request Jul 16, 2024
 - follow the timostark advise to replace skip(1) by filter empty choices

 Resolves #613, #1042
@tzolov
Copy link
Contributor

tzolov commented Jul 16, 2024

Most of it is addressed by the feb036d

@tzolov tzolov closed this Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tool/function calling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants