microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.74k stars 3.23k forks source link

Python: Bug: Agent name with space causes error #9340

Open TaoChenOSU opened 1 day ago

TaoChenOSU commented 1 day ago

Describe the bug When invoking a ChatCompletionAgent with a custom name that contains spaces and an OpenAI or Azure OAI service, the following exception is raised:

raise ServiceResponseException(
semantic_kernel.exceptions.service_exceptions.ServiceResponseException: ("<class 'semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion.AzureChatCompletion'> service failed to complete the prompt", BadRequestError('Error code: 400 - {\'error\': {\'message\': "Invalid \'messages[0].name\': string does not match pattern. Expected a string that matches the pattern \'^[a-zA-Z0-9_-]+$\'.", \'type\': \'invalid_request_error\', \'param\': \'messages[0].name\', \'code\': \'invalid_value\'}}'))

To Reproduce Steps to reproduce the behavior:

import asyncio

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent
from semantic_kernel.contents.chat_history import ChatHistory

# A simple debate simulation with a moderator agent and two participants
kernel = Kernel()

kernel.add_service(AzureChatCompletion(service_id="azure"))

# Debate moderator agent
debate_moderator_instructions = """
You are a debate moderator agent that helps facilitate debates.
"""

debate_moderator = ChatCompletionAgent(
    service_id="azure",
    kernel=kernel,
    name="Debate Moderator",
    description="A debate moderator agent that helps facilitate debates.",
    instructions=debate_moderator_instructions,
)

chat_history = ChatHistory()
chat_history.add_user_message("Welcome to the debate! It's now officially started.")

async def debate_simulation():
    async for response in debate_moderator.invoke(chat_history):
        print(response)

if __name__ == "__main__":
    asyncio.run(debate_simulation())

Expected behavior Agent returns a response

Platform

Additional context The OpenAI docs doesn't mention the name of the message cannot contain spaces or what format to follow.

We should probably validate the model to catch the error earlier and give a more helpful error message.

moonbox3 commented 11 hours ago

Thanks @TaoChenOSU, I will add the regex validation to the agent's name field.