semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Python: #6761 Onnx Connector

Open nmoeller opened this issue 1 year ago • 4 comments

Motivation and Context

  1. Why is this changed required ? To enable Onnx Models with Semantic Kernel, there was the issue #6761 in the Backlog to add a Onnx Connector
  2. What problem does it solve ? It solves the problem, that semantic kernel is not yet integrated with Onnx Gen Ai Runtime
  3. What scenario does it contribute to? The scenario is to use different connector than HF,OpenAI or AzureOpenAI. When User's want to use Onnx they can easliy integrate it now
  4. If it fixes an open issue, please link to the issue here. #6761

Description

The changes made are designed by my own based on other connectors, i tried to stay as close as possible to the structure. For the integration i installed the mistral python package in the repository.

I added the following Classes :

  • OnnxCompletionBase --> Responsible to control the inference
  • OnnxTextCompletion --> Inherits from OnnxCompletionBase
    • Support for Text Completion with and without Images
    • Ready for Multimodal Inference
    • Ready for Text Only Inference
    • Supports all Models on onnxruntime-genai
  • OnnxChatCompletion -->Inherits from OnnxCompletionBase
    • Support for Text Completion with and without Images
    • The user needs to provide the corresponding chat template to use this class
    • Ready for Multimodal Inference
    • Ready for Text Only Inference
    • Supports all Models on onnxruntime-genai

What is integrated yet :

  • [X] OnnxCompletionBase Class
  • [x] OnnxChatCompletionBase Class with Dynamic Template Support
  • [x] OnnxTextCompletionBase Class
  • [x] Sample Multimodal Inference with Phi3-Vision
  • [x] Sample of OnnxChatCompletions with Phi3
  • [x] Sample of OnnxTextCompletions with Phi3
  • [x] Integration Tests
  • [x] Unit Tests

Some Notes

Contribution Checklist

nmoeller avatar Aug 14 '24 07:08 nmoeller

Regarding our offline conversation on the prompt template, is using a prompt template to parse the chat history to some format an overkill? Prompt template can do much more that substituting arguments. Is it possible to override the _prepare_chat_history_for_request method to get what Onnx wants?

TaoChenOSU avatar Sep 18 '24 23:09 TaoChenOSU

As disscussed with @eavanvalkenburg i introduced hardcoded Templates in the onnx_utils.py file and integrated applying the Template into the _prepare_chat_history_for_request.

If people want to introduce custom templates they can overwrite the _prepare_chat_history_for_request. Also the output from string to the SK Objects can now be overwritten, since they are seperated into _create_chat_message_content and _create_streaming_chat_message_content.

nmoeller avatar Sep 20 '24 14:09 nmoeller

A couple of typos we'll need to fix:

Warning: "interogate" should be "interrogate". Warning: "choosen" should be "chosen".

moonbox3 avatar Oct 01 '24 20:10 moonbox3

Also, this is failing on the MacOS unit tests:

Using CPython 3.10.11 interpreter at: /Library/Frameworks/Python.framework/Versions/3.10/bin/python
Creating virtual environment at: .venv
Resolved 288 packages in 4.62s
error: distribution onnxruntime-genai==0.4.0 @ registry+https://pypi.org/simple can't be 
installed because it doesn't have a source distribution or wheel for the current platform

moonbox3 avatar Oct 01 '24 20:10 moonbox3

Thanks for the contribution! Will approve once the unit tests pass.

TaoChenOSU avatar Oct 09 '24 18:10 TaoChenOSU

Python Test Coverage

Python Test Coverage Report
FileStmtsMissCoverMissing
semantic_kernel
   kernel.py1994776%148, 159, 163, 313–316, 423, 437–480
semantic_kernel/agents/group_chat
   agent_chat.py124298%78, 171
   agent_group_chat.py100298%151, 201
   broadcast_queue.py72199%35
semantic_kernel/agents/open_ai
   assistant_content_generation.py133993%96–97, 281, 291–294, 335, 337
   open_ai_assistant_base.py449898%259, 337–338, 746, 867, 870, 932, 990
semantic_kernel/connectors/ai
   chat_completion_client_base.py116298%382, 392
   completion_usage.py8188%17
semantic_kernel/connectors/ai/anthropic/services
   anthropic_chat_completion.py176597%147, 165, 169, 223, 419
semantic_kernel/connectors/ai/azure_ai_inference/services
   azure_ai_inference_chat_completion.py119794%120, 146–149, 159, 180, 202
   azure_ai_inference_text_embedding.py41198%87
semantic_kernel/connectors/ai/embeddings
   embedding_generator_base.py8188%50
semantic_kernel/connectors/ai/google
   shared_utils.py26196%56
semantic_kernel/connectors/ai/google/google_ai/services
   google_ai_chat_completion.py119497%127, 153, 176, 178
   google_ai_text_completion.py63297%98, 121
   utils.py65395%140, 160–165
semantic_kernel/connectors/ai/google/vertex_ai/services
   utils.py66395%141, 161–166
   vertex_ai_chat_completion.py119497%121, 147, 170, 172
   vertex_ai_text_completion.py62297%95, 116
semantic_kernel/connectors/ai/hugging_face/services
   hf_text_completion.py60395%103, 112, 127
   hf_text_embedding.py32584%79–83
semantic_kernel/connectors/ai/mistral_ai/services
   mistral_ai_chat_completion.py118794%118–121, 307–310
semantic_kernel/connectors/ai/ollama/services
   ollama_chat_completion.py60592%95–98, 108, 143
   ollama_text_completion.py57689%72, 90–93, 103, 131
semantic_kernel/connectors/ai/onnx
   utils.py53394%50–51, 112
semantic_kernel/connectors/ai/onnx/services
   onnx_gen_ai_chat_completion.py72790%67–68, 98, 122, 167, 173, 179
   onnx_gen_ai_completion_base.py582164%59–71, 79–90
   onnx_gen_ai_text_completion.py46589%54–55, 87, 117, 133
semantic_kernel/connectors/ai/open_ai/prompt_execution_settings
   open_ai_prompt_execution_settings.py94199%112
semantic_kernel/connectors/ai/open_ai/services
   azure_chat_completion.py107595%118, 123, 157, 166, 169
   azure_text_completion.py28293%82, 87
   azure_text_embedding.py30293%84, 89
   open_ai_chat_completion_base.py127596%71, 121, 141, 177, 287
   open_ai_handler.py63395%86, 95–96
   open_ai_text_completion_base.py80298%56, 161
semantic_kernel/connectors/ai/open_ai/settings
   azure_open_ai_settings.py22195%99
semantic_kernel/connectors/memory/azure_ai_search
   azure_ai_search_collection.py87298%150, 152
semantic_kernel/connectors/memory/redis
   redis_collection.py160299%146, 316
   utils.py451176%145–146, 164, 166, 173–188
semantic_kernel/connectors/openapi_plugin
   openapi_manager.py58297%110–111
   openapi_parser.py88298%71, 128
   openapi_runner.py105298%181–182
semantic_kernel/connectors/openapi_plugin/models
   rest_api_operation.py129199%242
semantic_kernel/contents
   function_call_content.py97199%201
   streaming_chat_message_content.py68199%210
   streaming_content_mixin.py39295%37, 64
semantic_kernel/core_plugins/sessions_python_tool
   sessions_python_plugin.py134894%69, 82–91, 99
   sessions_python_settings.py39490%84–87
semantic_kernel/data
   vector_store_record_collection.py2491992%410, 470–474, 482–486, 526–529, 536–539
   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_function_log_messages.py36683%37–43
   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.py129993%53, 90, 186, 194, 205, 213, 228, 232–233
semantic_kernel/services
   ai_service_client_base.py22195%64
semantic_kernel/template_engine/blocks
   code_block.py77199%119
   named_arg_block.py43198%98
semantic_kernel/utils/authentication
   entra_id_authentication.py15287%26, 38
semantic_kernel/utils/telemetry
   user_agent.py16288%18–19
semantic_kernel/utils/telemetry/model_diagnostics
   decorators.py171498%372–375
TOTAL1169236097% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2560 4 :zzz: 0 :x: 0 :fire: 1m 5s :stopwatch:

markwallace-microsoft avatar Oct 10 '24 17:10 markwallace-microsoft