pythonimportsemantic-kernel

cannot import name 'Service' from 'services' when using semantic-kernel


I am currently working through Microsoft's semantic-kernel exercises found here

But am getting the following error when I try to access the Services within the library.

ImportError                               Traceback (most recent call last)
Cell In[1], line 1
----> 1 from services import Service
      3 # Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)
      4 selectedService = Service.OpenAI

ImportError: cannot import name 'Service' from 'services' (c:\Users\xxx\AppData\Local\Programs\Python\Python312\Lib\site-packages\services\__init__.py)

Has anyone else had a similar issue? It seems the services library on pypi is something much older and not the one being used here by Microsoft.


Solution

  • It looks like it runs some module services which you have installed

    but there is file services.py below 02-running-prompts-from-file.ipynb

    https://github.com/microsoft/semantic-kernel/blob/main/python/notebooks/services.py

    which you need to copy to folder with your code.


    Frankly, services.py has only few lines which you could even put directly in your code

    from enum import Enum
    
    
    class Service(Enum):
        """
        Attributes:
            OpenAI (str): Represents the OpenAI service.
            AzureOpenAI (str): Represents the Azure OpenAI service.
            HuggingFace (str): Represents the HuggingFace service.
        """
    
        OpenAI = "openai"
        AzureOpenAI = "azureopenai"
        HuggingFace = "huggingface"
    

    Or maybe you could even use

    selectedService = "openai"
    

    without using file services.py