Skip to content

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

Merged
moonbox3 merged 4 commits intomicrosoft:mainfrom
yuichiromukaiyama:main
Feb 8, 2024
Merged

Python: Support for cases where "logprobs" does not exist#4924
moonbox3 merged 4 commits intomicrosoft:mainfrom
yuichiromukaiyama:main

Conversation

@yuichiromukaiyama
Copy link
Contributor

Motivation and Context

  1. Why is this change required?
    An error has occurred.
    Python: AttributeError: 'Choice' object has no attribute 'logprobs' #4923

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

Swagger 2023-12-01-preview
Swagger 2023-05-15

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

  2. If it fixes an open issue, please link to the issue here.
    Python: AttributeError: 'Choice' object has no attribute 'logprobs' #4923

Description

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

    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

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

@yuichiromukaiyama yuichiromukaiyama requested a review from a team as a code owner February 8, 2024 11:33
@shawncal shawncal added the python Pull requests for the Python Semantic Kernel label Feb 8, 2024
Copy link
Collaborator

@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.

Hi @yuichiromukaiyama, thank you for your work on this. I agree that it seems like useful to have this type of error handling as it appears the logprobs attribute may not always be present (depending upon what underlying model you're using). I see on the following page that it mentions that logprobs is not available with gpt-35-turbo. Otherwise, in the swagger spec links you provided, for both 2023-05-15-previous and 2023-12-01-preview, they both contain the logprobs properties.

@moonbox3
Copy link
Collaborator

moonbox3 commented Feb 8, 2024

@yuichiromukaiyama note that our GitHub actions markdown-link-check step is currently failing -- it isn't related to your changes. We're trying to investigate as it is blocking our pipelines.

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.

Thanks for adding this @yuichiromukaiyama

@moonbox3 moonbox3 added this pull request to the merge queue Feb 8, 2024
Merged via the queue into microsoft:main with commit fcea354 Feb 8, 2024
@moonbox3 moonbox3 linked an issue Feb 8, 2024 that may be closed by this pull request
@yuichiromukaiyama
Copy link
Contributor Author

Thank you so much!

@moonbox3
Copy link
Collaborator

moonbox3 commented Feb 9, 2024

@yuichiromukaiyama your change went in just in time for the latest release. Thank you for your contribution!

@veeruhai
Copy link

I am still getting error..I am using "0.5.1.dev0", I see latest code ,but not sure issues is resolved completely

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),
}

image

@yuichiromukaiyama
Copy link
Contributor Author

yuichiromukaiyama commented Feb 19, 2024

@veeruhai
Do you fall under this problem? I think there are cases where logprobs do not exist for APIs other than ChatCompletion.
The problem in the following case is that even though APIKEY uses ChatCompletion, the processing flow is TextCompletion. I thought I would raise this as a separate issue. However, since there are multiple countermeasures, I wrote it in the discussion.

#5011

@veeruhai
Copy link

@yuichiromukaiyama ,
for me none of python notebooks working, I am using AzureOpenAI service, followed your workaround and changed PromptTemplate to ChatPromptTemplate in kernel .now I am able to run 00-getting-started notebook and basic planner notebook. Still facing same error for other planner type.

image

@yuichiromukaiyama
Copy link
Contributor Author

@veeruhai

I investigated the issue and I believe it is the same as the one I described in the "Discussion".

Both use PromptTemplate, and the error seems to be occurring in a class that I have not modified.

The problem can be solved by changing the PromptTemplate used in this class to ChatPromptTemplate.

https://github.com/yuichiromukaiyama/semantic-kernel/blob/9cedc95b4ce9bf7aff6ae16892df757076ca1bbb/python/semantic_kernel/planning/sequential_planner/sequential_planner.py#L62

I believe that the sequential planner also performs the following process:

https://github.com/yuichiromukaiyama/semantic-kernel/blob/9cedc95b4ce9bf7aff6ae16892df757076ca1bbb/python/semantic_kernel/kernel.py#L171-L172

To solve the root cause, I plan to issue a PR to make the following changes:

In the following section, kernel.register_semantic_function is called in _init_flow_function of SequentialPlanner.

https://github.com/yuichiromukaiyama/semantic-kernel/blob/9cedc95b4ce9bf7aff6ae16892df757076ca1bbb/python/semantic_kernel/planning/sequential_planner/sequential_planner.py#L74-L75

Since the use of PromptTemplate is enforced in Planner, TextCompletionClientBase is used in the following section:

https://github.com/yuichiromukaiyama/semantic-kernel/blob/9cedc95b4ce9bf7aff6ae16892df757076ca1bbb/python/semantic_kernel/kernel.py#L754

There is TextCompletionClientBase, which is inherited by OpenAITextCompletionBase.

https://github.com/yuichiromukaiyama/semantic-kernel/blob/9cedc95b4ce9bf7aff6ae16892df757076ca1bbb/python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_completion_base.py#L29-L30

OpenAITextCompletionBase also has the corresponding logprobs code.

https://github.com/yuichiromukaiyama/semantic-kernel/blob/9cedc95b4ce9bf7aff6ae16892df757076ca1bbb/python/semantic_kernel/connectors/ai/open_ai/services/open_ai_text_completion_base.py#L144

This will be rewritten as optional in the same way.

@ymuichiro
Copy link
Contributor

@veeruhai I have issued a pull request.
#5071

Bryan-Roe pushed a commit to Bryan-Roe-ai/semantic-kernel that referenced this pull request Oct 6, 2024
…4924)

### Motivation and Context

1. Why is this change required?
An error has occurred.
microsoft#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.
microsoft#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 <35585003+moonbox3@users.noreply.github.com>
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.

Python: AttributeError: 'Choice' object has no attribute 'logprobs'

6 participants