Skip to content

[Azure OpenAI] Function calling - Can't execute multiple function calls #613

Closed
@tha2015

Description

@tha2015

Bug description
When there should be multiple function calls for a single question, only one call is made because AzureOpenAiChatClient doesn't copy tools value from first request to second request, as a result, the next function call will not happen (loosing context in the function call response).

Environment
spring-ai-azure-openai-1.0.0-20240420.063719-112.jar

Steps to reproduce

  • AzureOpenAiChatClient makes a call to gpt-4 with question "What's the weather like in San Francisco, Tokyo, and Paris?" and function "weatherFunction"
  • AzureOpenAiChatClient receives a function call in response for "San Francisco"
  • AzureOpenAiChatClient calls the function and builds a second request including the result, sends new request
  • AzureOpenAiChatClient receives the result for "San Francisco" and stops

Expected behavior
Multiple function calls should happen (for SanFrancisco, Tokyo, Paris).

Analysis
This issue doesn't happen if using OpenAIChatClient. And
comparing OpenAiChatClient.doCreateToolResponseRequest and AzureOpenAiChatClient.doCreateToolResponseRequest shows that OpenAiChatClient uses ModelOptionsUtils.merge(newRequest, previousRequest, ChatCompletionRequest.class) to copy attributes (including tools) from first request to second request, while AzureOpenAiChatClient uses AzureOpenAiChatClient.merge to copy attributes (missing tools).
So it looks like missing tools property is causing the issue.

Minimal Complete Reproducible example

   // Kotlin code snippet
    val response: ChatResponse = chatClient.call(
        Prompt(
            listOf(UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?")),
            AzureOpenAiChatOptions.builder().withFunction("weatherFunction").build()
        )
    )

    println(response.result.output.content)
Getting weather for San Francisco in C
The current temperature in San Francisco is 30 degrees Celsius. 

Now let's check the weather in Tokyo and Paris. 

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions