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: #6501 Anthropic Chat Completion #7851

Merged
merged 27 commits into from
Aug 19, 2024

Conversation

andrewldesousa
Copy link
Contributor

@andrewldesousa andrewldesousa commented Aug 5, 2024

Motivation and Context

  1. Why is this changed required? Support Anthropic models with Python (Python: Add Anthropic AI Connector #6501)
  2. What problem does it solve? ^^
  3. What scenario does it contribute to? Multi model use cases and applications that would like to use Anthropic models
  4. If it fixes an open issue, please link to the issue here: Python: Add Anthropic AI Connector #6501

Description

The changes made follow the designs followed by the Mistral AI and Open AI connectors.

I added the following Classes :

  • AnthropicChatPromptExcecutionSettings
  • AnthropicChatCompletion
  • AnthropicSettings

To test the changes with the tests please add ANTHROPIC_API_KEY and
ANTHROPIC_CHAT_MODEL_ID as Enviorment Variable

What is implemented:

  • Integrate Anthropic ChatCompletion Models without Streaming
  • Integrate Anthropic Chat Completion Models with Streaming
  • Unit tests and integration tests
  • Extended Testing including Unit Testing & More Integration Tests

Contribution Checklist

@markwallace-microsoft markwallace-microsoft added the python Pull requests for the Python Semantic Kernel label Aug 5, 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.

Couple of small things, but overall this looks great!

@moonbox3
Copy link
Contributor

moonbox3 commented Aug 5, 2024

This is looking great, @andrewldesousa. Thank you for working on it!

@andrewldesousa
Copy link
Contributor Author

Some additional info on streaming with the Anthropic SDK. Here is an example showing the streaming events. I make sure to stream all the content and still stream the metadata about usage, message author role, and stop reason.

async with client.messages.stream(
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": "What is semantic kernel??",
            }
        ],
        model="claude-3-opus-20240229",
    ) as stream:
        async for text in stream:
            print(text)
        print()

Output:

RawMessageStartEvent(message=Message(id='msg_01Mqrmvgr8SKRMAWkAgzaUGe', content=[], model='claude-3-opus-20240229', role='assistant', stop_reason=None, stop_sequence=None, type='message', usage=Usage(input_tokens=11, output_tokens=3)), type='message_start')
RawContentBlockStartEvent(content_block=TextBlock(text='', type='text'), index=0, type='content_block_start')
RawContentBlockDeltaEvent(delta=TextDelta(text='Hello there!', type='text_delta'), index=0, type='content_block_delta')
TextEvent(type='text', text='Hello there!', snapshot='Hello there!')
ContentBlockStopEvent(index=0, type='content_block_stop', content_block=TextBlock(text='Hello there!', type='text'))
RawMessageDeltaEvent(delta=Delta(stop_reason='end_turn', stop_sequence=None), type='message_delta', usage=MessageDeltaUsage(output_tokens=6))
MessageStopEvent(type='message_stop', message=Message(id='msg_01Mqrmvgr8SKRMAWkAgzaUGe', content=[TextBlock(text='Hello there!', type='text')], model='claude-3-opus-20240229', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=Usage(input_tokens=11, output_tokens=6)))

To learn more about the streaming events for Anthropic:
https://docs.anthropic.com/en/api/messages-streaming#event-types

cc: @TaoChenOSU @moonbox3 @eavanvalkenburg

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Aug 6, 2024

Python Test Coverage

Python Test Coverage Report
FileStmtsMissCoverMissing
semantic_kernel
   kernel.py1994776%150, 161, 165, 315–318, 425, 439–482
semantic_kernel/agents/open_ai
   open_ai_assistant_base.py384299%300–301
semantic_kernel/connectors/ai/anthropic/services
   anthropic_chat_completion.py942573%171–181, 196–205, 222–236
semantic_kernel/connectors/ai/azure_ai_inference/services
   azure_ai_inference_chat_completion.py149497%169, 312, 416, 430
semantic_kernel/connectors/ai/embeddings
   embedding_generator_base.py8188%50
semantic_kernel/connectors/ai/google
   shared_utils.py39295%100, 122
semantic_kernel/connectors/ai/google/google_ai/services
   google_ai_chat_completion.py145398%149, 251, 287
   utils.py63395%139, 157–162
semantic_kernel/connectors/ai/google/vertex_ai/services
   utils.py65395%141, 159–164
   vertex_ai_chat_completion.py145398%143, 244, 280
semantic_kernel/connectors/ai/hugging_face/services
   hf_text_completion.py54394%97, 105, 136
   hf_text_embedding.py32584%79–83
semantic_kernel/connectors/memory/redis
   redis_collection.py157299%145, 313
   utils.py451176%145–146, 164, 166, 173–188
semantic_kernel/connectors/openapi_plugin
   openapi_manager.py58297%110–111
   openapi_parser.py88199%127
   openapi_runner.py105298%181–182
semantic_kernel/connectors/openapi_plugin/models
   rest_api_operation.py129199%242
semantic_kernel/contents
   function_call_content.py97199%201
semantic_kernel/data
   vector_store_record_collection.py2491992%400, 460–464, 472–476, 516–519, 526–529
   vector_store_record_utils.py26292%50, 52
semantic_kernel/functions
   kernel_function_decorator.py98199%102
   kernel_function_from_method.py96199%153
   kernel_function_from_prompt.py154795%165–166, 180, 201, 219, 239, 322
   kernel_plugin.py187299%472, 475
semantic_kernel/planners
   plan.py2344581%54, 163–165, 197, 214–227, 264, 269, 277–278, 288–291, 308, 313, 329, 332–337, 355, 360, 363, 365, 372, 386–388, 393–397
semantic_kernel/planners/function_calling_stepwise_planner
   function_calling_stepwise_planner.py116497%145, 189–190, 198
semantic_kernel/planners/sequential_planner
   sequential_planner.py64691%71, 75, 109, 125, 134–135
   sequential_planner_extensions.py50982%31–32, 56, 110–124
   sequential_planner_parser.py771284%66–74, 93, 117–120
semantic_kernel/schema
   kernel_json_schema_builder.py119596%50, 84, 206, 210–211
semantic_kernel/template_engine/blocks
   code_block.py77199%119
   named_arg_block.py43198%98
semantic_kernel/utils/telemetry
   decorators.py113496%232, 240, 243, 252
   user_agent.py16288%18–19
TOTAL1000324298% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2263 1 💤 0 ❌ 0 🔥 50.355s ⏱️

@andrewldesousa andrewldesousa marked this pull request as ready for review August 6, 2024 19:25
@andrewldesousa andrewldesousa requested a review from a team as a code owner August 6, 2024 19:25
Copy link
Contributor

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

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

A small comment otherwise LGTM.

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.

couple of small notes, but looking good overall!

@eavanvalkenburg
Copy link
Member

@andrewldesousa there is a merge conflict, let me know when you resolve that, then we can merge this in! nice work!

auto-merge was automatically disabled August 16, 2024 20:13

Head branch was pushed to by a user without write access

@andrewldesousa
Copy link
Contributor Author

@andrewldesousa there is a merge conflict, let me know when you resolve that, then we can merge this in! nice work!

Thanks! Will look into function calling next

@andrewldesousa
Copy link
Contributor Author

@moonbox3 @eavanvalkenburg can you guys rereview given I have made some changes since your last review?

@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Aug 19, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 19, 2024
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Aug 19, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 19, 2024
@eavanvalkenburg eavanvalkenburg added this pull request to the merge queue Aug 19, 2024
Merged via the queue into microsoft:main with commit 48d8dbc Aug 19, 2024
25 checks passed
LudoCorporateShark pushed a commit to LudoCorporateShark/semantic-kernel that referenced this pull request Aug 25, 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.
-->
1. Why is this changed required? Support Anthropic models with Python
(microsoft#6501)
2. What problem does it solve? ^^
3. What scenario does it contribute to? Multi model use cases and
applications that would like to use Anthropic models
4. If it fixes an open issue, please link to the issue here: microsoft#6501

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

The changes made follow the designs followed by the Mistral AI and Open
AI connectors.

I added the following Classes :
- AnthropicChatPromptExcecutionSettings
- AnthropicChatCompletion
- AnthropicSettings

**To test the changes with the tests please add ANTHROPIC_API_KEY and
ANTHROPIC_CHAT_MODEL_ID as Enviorment Variable**

What is implemented:
- [X] Integrate Anthropic ChatCompletion Models without Streaming
- [X] Integrate Anthropic Chat Completion Models with Streaming
- [X] Unit tests and integration tests
- [X] Extended Testing including Unit Testing & More Integration Tests

### 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 😄

---------

Co-authored-by: Tao Chen <[email protected]>
Co-authored-by: Eduard van Valkenburg <[email protected]>
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
None yet
Development

Successfully merging this pull request may close these issues.

5 participants