Python: Support for cases where "logprobs" does not exist (#4924)

### Motivation and Context

1. Why is this change required?
An error has occurred.
https://github.com/microsoft/semantic-kernel/issues/4923

2. What problem does it solve?
Non-existent property lobprobs is referenced when using Azure OpenAI
Chat Completion.

[Swagger
2023-12-01-preview](https://github.com/Azure/azure-rest-api-specs/tree/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/preview/2023-12-01-preview)
[Swagger
2023-05-15](https://github.com/Azure/azure-rest-api-specs/blob/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable/2023-05-15/inference.json)

3. What scenario does it contribute to?
When using Azure OpenAI ChatCompletion

4. If it fixes an open issue, please link to the issue here.
https://github.com/microsoft/semantic-kernel/issues/4923

### Description

Azure OpenAI ChatCompletion does not return logprobs.
Thus, the following code would be "AttributeError: 'Choice' object has
no attribute 'logprobs'".

```python
    def _get_metadata_from_chat_choice(self, choice: Union[Choice, ChunkChoice]) -> Dict[str, Any]:
        """Get metadata from a chat choice."""
        return {
            "logprobs": choice.logprobs,
        }
```

do the following

```python
def _get_metadata_from_chat_choice(self, choice: Union[Choice, ChunkChoice]) -> Dict[str, Any]:
    """Get metadata from a chat choice."""
    return {
        "logprobs": getattr(choice, "logprobs", None),
    }
```

### Contribution Checklist

- [- ] The code builds clean without any errors or warnings
- [- ] 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
- [-] All unit tests pass, and I have added new tests where possible
- [- ] I didn't break anyone 😄

Co-authored-by: Evan Mattson <[email protected]>
This commit is contained in:
Mukaiyama(**094**)
2024-02-08 16:43:29 +00:00
committed by GitHub
co-authored by Evan Mattson
parent a8233dc068
commit fcea3547d4
@@ -198,7 +198,7 @@ class OpenAIChatCompletionBase(OpenAIHandler, ChatCompletionClientBase):
def _get_metadata_from_chat_choice(self, choice: Union[Choice, ChunkChoice]) -> Dict[str, Any]:
"""Get metadata from a chat choice."""
return {
"logprobs": choice.logprobs,
"logprobs": getattr(choice, "logprobs", None),
}
def _get_tool_calls_from_chat_choice(self, choice: Union[Choice, ChunkChoice]) -> Optional[List[ToolCall]]: