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

Type Error raised in delegate_inference.py #691

Closed
eunjiinkim opened this issue Apr 27, 2023 · 3 comments
Closed

Type Error raised in delegate_inference.py #691

eunjiinkim opened this issue Apr 27, 2023 · 3 comments
Assignees
Labels
python Pull requests for the Python Semantic Kernel

Comments

@eunjiinkim
Copy link

eunjiinkim commented Apr 27, 2023

Describe the bug
During calling my own native function which is not properly designed,
the error should be raised via infer_unknown below.

    @staticmethod
    @_infers(DelegateTypes.Unknown)
    def infer_unknown(signature: Signature) -> NoReturn:
        raise KernelException(
            KernelException.ErrorCodes.FunctionTypeNotSupported,
            "Invalid function type detected, unable to infer DelegateType.",
        )

However, the type error is raised since awaitable parameter is not included in the infer_unknown function.

TypeError: DelegateInference.infer_unknown() takes 1 positional argument but 2 were given

    def infer_delegate_type(function) -> DelegateTypes:
        # Get the function signature
        function_signature = signature(function)
        awaitable = iscoroutinefunction(function)

        for name, value in DelegateInference.__dict__.items():
            wrapped = getattr(value, "__wrapped__", getattr(value, "__func__", None))

            if name.startswith("infer_") and hasattr(wrapped, "_delegate_type"):
                # Get the delegate type
                if wrapped(**function_signature, awaitable**):
                    return wrapped._delegate_type

        return DelegateTypes.Unknown

To Reproduce
What I've added as a native function is:

import os
import aiofiles
from semantic_kernel.orchestration.sk_context import SKContext
from semantic_kernel.orchestration.context_variables import ContextVariables
from semantic_kernel.skill_definition import sk_function

class ChatHistoryProcessingSkill:
    
    @sk_function(description="save chat history")
    async def save_chat(self, context_vars: ContextVariables):
        chat_id = context_vars.get("chat_id")
        chat_history = context_vars.get("chat_history")
        async with aiofiles.open(f"chat_history_{chat_id}.txt", "w") as f: 
            await f.write(chat_history)
        
    @sk_function(description="recall chat history")
    async def recall_chat(self, input: str) -> str:
        if not os.path.exists(f"chat_history_{input}.txt"):
            return ''
        async with aiofiles.open(f"chat_history_{input}.txt", "r") as f:
            content = await f.read()
            return content

Then, I've run code:

import semantic_kernel as sk
from semantic_kernel.skills.ChatHistorySkill.chat_history_skill import ChatHistoryProcessingSkill

kernel = sk.Kernel()
kernel.import_skill(ChatHistoryProcessingSkill())

(I think FunctionTypeNotSupported error is raised since the input type is ContextVariables.
When I modified it to SKContext, it works well.)

Expected behavior
Thus, awaitable should be added like:
def infer_unknown(signature: Signature, awaitable: bool) -> NoReturn:

Or, Exclude the 'infer_unknown' such as :
if name.startswith("infer_") and not name.endswith("unknown") and hasattr(wrapped, "_delegate_type"):

Screenshots
image

Desktop (please complete the following information):

  • OS: Mac M1
  • IDE: VSCode
  • NuGet Package Version [e.g. 0.1.0]

Additional context

@alexchaomander
Copy link
Contributor

Thanks for sending this! I encountered similar errors when writing my own native functions. I raised it here #624

I think these are related and could be resolved with a common update.

@dluc @mkarle @awharrison-28

@evchaki evchaki added the python Pull requests for the Python Semantic Kernel label Apr 27, 2023
@eunjiinkim
Copy link
Author

Great! Could you tell me the upcoming update date? :D

@microsoftShannon
Copy link
Contributor

@dluc Let's put this in the backlog.

lemillermicrosoft pushed a commit that referenced this issue May 19, 2023
### Motivation and Context
#691 
#1053 

### Description
1. Added an argument to prevent TypeError when inferring an unknown
type.
2. Fixed a bug in the process of checking parameters when inferring the
InStringAndContext type.
shawncal pushed a commit to shawncal/semantic-kernel that referenced this issue Jul 6, 2023
### Motivation and Context
microsoft#691 
microsoft#1053 

### Description
1. Added an argument to prevent TypeError when inferring an unknown
type.
2. Fixed a bug in the process of checking parameters when inferring the
InStringAndContext type.
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

No branches or pull requests

7 participants