While trying to run the code for creating basic agents in Python using Semantic Kernel, it is giving ModuleNotFoundError: No module named ‘semantic_kernel.skill’. Facing the same error for semantic_kernel.skill_definition as well.
Referred to 4777 but the hyperlink to the “Example” provided in that issue is giving 404 error.
Can you please help me. This is a blocker for me. As per my knowledge, there is no executable code for Agents using Semantic Kernel in Python. Please share the resources if you have any.
To Reproduce: Execute the below code:
import os
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion
from semantic_kernel.skill import Skill
from semantic_kernel.skill import sk_function
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
from azure.search.documents.models import QueryType
# Initialize the Semantic Kernel
kernel = sk.Kernel()
# Register Azure OpenAI text completion service
azure_text_completion = AzureTextCompletion(
endpoint=AZURE_OPENAI_ENDPOINT,
api_key=AZURE_OPENAI_API_KEY,
deployment_name=DEPLOYMENT_NAME
)
kernel.register_service("text-completion", azure_text_completion)
# Initialize Azure Cognitive Search client
search_client = SearchClient(
endpoint=AZURE_SEARCH_ENDPOINT,
index_name=AZURE_SEARCH_INDEX_NAME,
credential=AzureKeyCredential(AZURE_SEARCH_API_KEY)
)
# Define a simple skill
class GreetingSkill(Skill):
@sk_function
def say_hello(self, name: str) -> str:
return f"Hello, {name}!"
# Define a search skill
class SearchSkill(Skill):
@sk_function
def search_documents(self, query: str) -> str:
results = search_client.search(search_text=query, query_type=QueryType.SIMPLE)
response = [doc['content'] for doc in results]
return 'n'.join(response)
# Register the skills with the kernel
greeting_skill = GreetingSkill()
search_skill = SearchSkill()
kernel.register_skill("GreetingSkill", greeting_skill)
kernel.register_skill("SearchSkill", search_skill)
# Define a function to interact with the AI agent
def interact_with_agent(kernel, skill_name, function_name, **kwargs):
result = kernel.execute(skill_name, function_name, **kwargs)
return result
# Example interaction with the AI agent
result = interact_with_agent(kernel, "GreetingSkill", "say_hello", name="Alice")
print(result) # Output: Hello, Alice!
# Example search interaction with the AI agent
search_result = interact_with_agent(kernel, "SearchSkill", "search_documents", query="Azure Cognitive Services")
print(search_result) # Output: Search results related to "Azure Cognitive Services"
OS: [e.g. Windows, Mac] – Windows
IDE: [e.g. Visual Studio, VS Code] – VS Code
I have raised a GitHub issue as well
I tried executing the above code and I’m expecting an error free code execution