Python : CQ MyPy Fix for MistralAI Connector (#7182)

### Motivation and Context
1. I saw that you are having spikes regarding enabling MyPy
2. On ##7049  i had the Liskov Principle commented
3. In #7144 is saw how you plan to solve this so i decided to adjust the
mistral connector to this

### Description

The PR contains changes to be compliant to MyPy in Mistral AI Connector.
Changes input Parameter for MistralAIChatCompletion from
MistralAIPromptExecutionSettings to PromptExecutionSettings.
Adding Test Cases for MistralAIPromptExecutionSettings and
PromptExecutionSettings.


![image](https://github.com/microsoft/semantic-kernel/assets/23048106/5822896e-17d1-4d68-9348-da6765ac3527)


### Tasks

- [x] Changes input Parameter for Liskov
- [x] Settings conversion
- [x] Adjust TestCases to 100%

### 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: Nico Möller <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
This commit is contained in:
Nico Möller
2024-07-11 22:27:28 +00:00
committed by GitHub
co-authored by Nico Möller Evan Mattson
parent 6a0077c5d3
commit 6944914017
2 changed files with 109 additions and 36 deletions
@@ -20,6 +20,7 @@ from semantic_kernel.connectors.ai.mistral_ai.prompt_execution_settings.mistral_
MistralAIChatPromptExecutionSettings,
)
from semantic_kernel.connectors.ai.mistral_ai.settings.mistral_ai_settings import MistralAISettings
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.function_call_content import FunctionCallContent
@@ -95,20 +96,24 @@ class MistralAIChatCompletion(ChatCompletionClientBase):
async def get_chat_message_contents(
self,
chat_history: "ChatHistory",
settings: "MistralAIChatPromptExecutionSettings", # type: ignore[override]
settings: "PromptExecutionSettings",
**kwargs: Any,
) -> list["ChatMessageContent"]:
"""Executes a chat completion request and returns the result.
Args:
chat_history (ChatHistory): The chat history to use for the chat completion.
settings (MistralAIChatPromptExecutionSettings): The settings to use
settings (PromptExecutionSettings): The settings to use
for the chat completion request.
kwargs (Dict[str, Any]): The optional arguments.
Returns:
List[ChatMessageContent]: The completion result(s).
"""
if not isinstance(settings, MistralAIChatPromptExecutionSettings):
settings = self.get_prompt_execution_settings_from_settings(settings)
assert isinstance(settings, MistralAIChatPromptExecutionSettings) # nosec
if not settings.ai_model_id:
settings.ai_model_id = self.ai_model_id
@@ -128,14 +133,14 @@ class MistralAIChatCompletion(ChatCompletionClientBase):
async def get_streaming_chat_message_contents(
self,
chat_history: ChatHistory,
settings: MistralAIChatPromptExecutionSettings, # type: ignore[override]
settings: PromptExecutionSettings,
**kwargs: Any,
) -> AsyncGenerator[list[StreamingChatMessageContent], Any]:
"""Executes a streaming chat completion request and returns the result.
Args:
chat_history (ChatHistory): The chat history to use for the chat completion.
settings (MistralAIChatPromptExecutionSettings): The settings to use
settings (PromptExecutionSettings): The settings to use
for the chat completion request.
kwargs (Dict[str, Any]): The optional arguments.
@@ -143,6 +148,10 @@ class MistralAIChatCompletion(ChatCompletionClientBase):
List[StreamingChatMessageContent]: A stream of
StreamingChatMessageContent when using Azure.
"""
if not isinstance(settings, MistralAIChatPromptExecutionSettings):
settings = self.get_prompt_execution_settings_from_settings(settings)
assert isinstance(settings, MistralAIChatPromptExecutionSettings) # nosec
if not settings.ai_model_id:
settings.ai_model_id = self.ai_model_id