Skip to content
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: Anthropic function calling fixes #9938

Merged

Conversation

TaoChenOSU
Copy link
Contributor

@TaoChenOSU TaoChenOSU commented Dec 10, 2024

Motivation and Context

The current implementation of the Anthropic connector relies on the inner_contents in chat messages to prepare the chat history for the Anthropic client. This will only work when the chat history is created by the Anthropic connector. This won't work if the chat history has been processed by other connectors, or if it is hardcoded as in testing.

Description

  1. Prepare the chat history for the Anthropic client by parsing the actual Semantic Kernel item types.
  2. Fix tests for the Anthropic connector.

Contribution Checklist

@TaoChenOSU TaoChenOSU added PR: in progress Under development and/or addressing feedback python Pull requests for the Python Semantic Kernel labels Dec 10, 2024
@TaoChenOSU TaoChenOSU self-assigned this Dec 10, 2024
@TaoChenOSU TaoChenOSU requested a review from a team as a code owner December 10, 2024 18:27
@TaoChenOSU TaoChenOSU linked an issue Dec 10, 2024 that may be closed by this pull request
@github-actions github-actions bot changed the title Anthropic function calling fixes Python: Anthropic function calling fixes Dec 10, 2024
Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small note, but good improvement!

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Dec 16, 2024

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
semantic_kernel/connectors/ai/anthropic/services
   anthropic_chat_completion.py158696%141, 147, 159, 165, 169, 375
   utils.py32391%56, 90–93
TOTAL16759185789% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2966 4 💤 0 ❌ 0 🔥 1m 6s ⏱️

@TaoChenOSU TaoChenOSU removed the PR: in progress Under development and/or addressing feedback label Dec 16, 2024
@TaoChenOSU TaoChenOSU enabled auto-merge December 16, 2024 22:51
@TaoChenOSU TaoChenOSU added this pull request to the merge queue Dec 16, 2024
Merged via the queue into microsoft:main with commit 4a21254 Dec 16, 2024
25 checks passed
@TaoChenOSU TaoChenOSU deleted the taochen/python-anthropic-issue-9853 branch December 16, 2024 23:00
github-merge-queue bot pushed a commit that referenced this pull request Dec 19, 2024
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
When using the Anthropic connector with function calling, it's possible
that the model will request multiple functions in a single request. This
is referred to as [`parallel tool
use`](https://docs.anthropic.com/en/docs/build-with-claude/tool-use#disabling-parallel-tool-use)
by Anthropic.

When the model requests multiple functions, it expects the tool results
to be included in a single user message to be passed back to the model.
Right now, the Anthropic connector parses the tool results into multiple
user messages, which causes the model to throw an error.

This is a possible regression introduced by this PR:
#9938

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Fix the Anthropic connector to handle parallel tool calls.
2. Add unit tests to ensure that future changes don't break this
functionality.
3. Enable integration test on Anthropic since we have a service endpoint
now.
4. Refactoring.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Pull requests for the Python Semantic Kernel
Projects
Status: Sprint: Done
Development

Successfully merging this pull request may close these issues.

Python: Bug: Issues with Anthropic tool calling
4 participants