Partilhar via


Explorando o Kernel Semântico OpenAIAssistantAgent

Importante

Recursos de agente único, como OpenAIAssistantAgent, estão no estágio de candidato a lançamento. Esses recursos são quase completos e geralmente estáveis, embora possam passar por pequenos refinamentos ou otimizações antes de atingir a disponibilidade geral total.

Sugestão

A documentação detalhada da API relacionada a esta discussão está disponível em:

Sugestão

A documentação detalhada da API relacionada a esta discussão está disponível em:

Recurso atualmente indisponível em Java.

O que é um Assistente?

A API OpenAI Assistants é uma interface especializada projetada para recursos de IA mais avançados e interativos, permitindo que os desenvolvedores criem agentes personalizados e orientados a tarefas em várias etapas. Ao contrário da API de conclusão de bate-papo, que se concentra em trocas conversacionais simples, a API do Assistente permite interações dinâmicas e orientadas por objetivos com recursos adicionais, como interpretador de código e pesquisa de arquivos.

Preparando seu ambiente de desenvolvimento

Para prosseguir com o desenvolvimento de um OpenAIAssistantAgent, configure seu ambiente de desenvolvimento com os pacotes apropriados.

Adicione o pacote Microsoft.SemanticKernel.Agents.OpenAI ao seu projeto:

dotnet add package Microsoft.SemanticKernel.Agents.OpenAI --prerelease

Você também pode incluir o pacote Azure.Identity:

dotnet add package Azure.Identity

Instale o pacote semantic-kernel:

pip install semantic-kernel

Recurso atualmente indisponível em Java.

Criando uma OpenAIAssistantAgent

Criar um OpenAIAssistant requer primeiro a criação de um cliente para poder interagir com um serviço remoto.

AssistantClient client = OpenAIAssistantAgent.CreateAzureOpenAIClient(...).GetAssistantClient();
Assistant assistant =
    await client.CreateAssistantAsync(
        "<model name>",
        "<agent name>",
        instructions: "<agent instructions>");
OpenAIAssistantAgent agent = new(assistant, client);
from semantic_kernel.agents import AssistantAgentThread, AzureAssistantAgent, OpenAIAssistantAgent
from semantic_kernel.connectors.ai.open_ai import AzureOpenAISettings, OpenAISettings

# Set up the client and model using Azure OpenAI Resources
client = AzureAssistantAgent.create_client()

# Define the assistant definition
definition = await client.beta.assistants.create(
    model=AzureOpenAISettings().chat_deployment_name,
    instructions="<instructions>",
    name="<agent name>",
)

# Create the AzureAssistantAgent instance using the client and the assistant definition
agent = AzureAssistantAgent(
    client=client,
    definition=definition,
)

# or

# Set up the client and model using OpenAI Resources
client = OpenAIAssistantAgent.create_client()

# Define the assistant definition
definition = await client.beta.assistants.create(
    model=OpenAISettings().chat_model_id,
    instructions="<instructions>",
    name="<agent name>",
)

# Create the OpenAIAssistantAgent instance using the client and the assistant definition
agent = OpenAIAssistantAgent(
    client=client,
    definition=definition,
)

Recurso atualmente indisponível em Java.

Recuperando um OpenAIAssistantAgent

Uma vez criado, o identificador do assistente pode ser acessado através do seu identificador. Esse identificador pode ser usado para criar um OpenAIAssistantAgent a partir de uma definição de assistente existente.

Para .NET, o identificador do agente é exposto como um string através da propriedade definida por qualquer agente.

AssistantClient client = OpenAIAssistantAgent.CreateAzureOpenAIClient(...).GetAssistantClient();
Assistant assistant = await client.GetAssistantAsync("<assistant id>");
OpenAIAssistantAgent agent = new(assistant, client);
# Using Azure OpenAI Resources

# Create the client using Azure OpenAI resources and configuration
client = AzureAssistantAgent.create_client()

# Create the assistant definition
definition = await client.beta.assistants.create(
    model=AzureOpenAISettings().chat_deployment_name,
    name="<agent name>",
    instructions="<instructions>",
)

# Store the assistant ID
assistant_id = definition.id

# Retrieve the assistant definition from the server based on the assistant ID
new_asst_definition = await client.beta.assistants.retrieve(assistant_id)

# Create the AzureAssistantAgent instance using the client and the assistant definition
agent = AzureAssistantAgent(
    client=client,
    definition=new_asst_definition,
)

Recurso atualmente indisponível em Java.

Usando um OpenAIAssistantAgent

Como em todos os aspetos da API do Assistente, as conversas são armazenadas remotamente. Cada conversa é referida como um segmento e identificada por um identificador exclusivo string . As interações com o seu OpenAIAssistantAgent estão vinculadas a este identificador de thread específico. As especificidades da linha da API do Assistente são abstraídas por meio da OpenAIAssistantAgentThread classe, que é uma implementação de AgentThread.

Atualmente, o OpenAIAssistantAgent suporta apenas tópicos do tipo OpenAIAssistantAgentThread.

Você pode invocar o OpenAIAssistantAgent sem especificar um AgentThread, para iniciar um novo thread e um novo AgentThread será retornado como parte da resposta.


// Define agent
OpenAIAssistantAgent agent = ...;
AgentThread? agentThread = null;

// Generate the agent response(s)
await foreach (AgentResponseItem<ChatMessageContent> response in agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, "<user input>")))
{
  // Process agent response(s)...
  agentThread = response.Thread;
}

// Delete the thread if no longer needed
if (agentThread is not null)
{
    await agentThread.DeleteAsync();
}

Você também pode invocar o OpenAIAssistantAgent com um AgentThread que você criou.

// Define agent
OpenAIAssistantAgent agent = ...;

// Create a thread with some custom metadata.
AgentThread agentThread = new OpenAIAssistantAgentThread(client, metadata: myMetadata);

// Generate the agent response(s)
await foreach (ChatMessageContent response in agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, "<user input>"), agentThread))
{
  // Process agent response(s)...
}

// Delete the thread when it is no longer needed
await agentThread.DeleteAsync();

Você também pode criar um OpenAIAssistantAgentThread que retoma uma conversa anterior por id.

// Create a thread with an existing thread id.
AgentThread agentThread = new OpenAIAssistantAgentThread(client, "existing-thread-id");
from semantic_kernel.agents import AssistantAgentThread, AzureAssistantAgent

# Define agent
openai_agent = await ...

# Create a thread for the agent conversation
thread: AssistantAgentThread = None

# Generate the agent response(s)
async for response in agent.invoke(messages="user input", thread=thread):
  # process agent response(s)...
  thread = response.thread

# Delete the thread when it is no longer needed
await thread.delete() if thread else None

Recurso atualmente indisponível em Java.

Eliminar OpenAIAssistantAgent

Como a definição do assistente é armazenada remotamente, ela persistirá se não for excluída.
A exclusão de uma definição de assistente pode ser realizada diretamente com o cliente.

Nota: Tentar usar uma instância de agente depois de ser excluído resultará em uma exceção de serviço.

Para .NET, o identificador do agente é exposto como um string através da Agent.Id propriedade definida por qualquer agente.

AssistantClient client = OpenAIAssistantAgent.CreateAzureOpenAIClient(...).GetAssistantClient();
await client.DeleteAssistantAsync("<assistant id>");
await client.beta.assistants.delete(agent.id)

Recurso atualmente indisponível em Java.

Gerir mensagens intermediárias com um OpenAIAssistantAgent

O Kernel Semântico OpenAIAssistantAgent foi projetado para invocar um agente que atenda às consultas ou perguntas do usuário. Durante a invocação, o agente pode executar ferramentas para derivar a resposta final. Para acessar mensagens intermediárias produzidas durante esse processo, os chamadores podem fornecer uma função de retorno de chamada que lida com instâncias de FunctionCallContent ou FunctionResultContent.

A documentação de retorno de chamada para o OpenAIAssistantAgent será disponibilizada em breve.

Configurar o on_intermediate_message callback dentro de agent.invoke(...) ou agent.invoke_stream(...) permite ao chamador receber mensagens intermediárias geradas durante o processo de formulação da resposta final do agente.

import asyncio
from typing import Annotated

from semantic_kernel.agents import AssistantAgentThread, AzureAssistantAgent
from semantic_kernel.connectors.ai.open_ai import AzureOpenAISettings
from semantic_kernel.contents import AuthorRole, ChatMessageContent, FunctionCallContent, FunctionResultContent
from semantic_kernel.functions import kernel_function


# Define a sample plugin for the sample
class MenuPlugin:
    """A sample Menu Plugin used for the concept sample."""

    @kernel_function(description="Provides a list of specials from the menu.")
    def get_specials(self) -> Annotated[str, "Returns the specials from the menu."]:
        return """
        Special Soup: Clam Chowder
        Special Salad: Cobb Salad
        Special Drink: Chai Tea
        """

    @kernel_function(description="Provides the price of the requested menu item.")
    def get_item_price(
        self, menu_item: Annotated[str, "The name of the menu item."]
    ) -> Annotated[str, "Returns the price of the menu item."]:
        return "$9.99"


# This callback function will be called for each intermediate message,
# which will allow one to handle FunctionCallContent and FunctionResultContent.
# If the callback is not provided, the agent will return the final response
# with no intermediate tool call steps.
async def handle_intermediate_steps(message: ChatMessageContent) -> None:
    for item in message.items or []:
        if isinstance(item, FunctionResultContent):
            print(f"Function Result:> {item.result} for function: {item.name}")
        elif isinstance(item, FunctionCallContent):
            print(f"Function Call:> {item.name} with arguments: {item.arguments}")
        else:
            print(f"{item}")


async def main():
    # Create the client using Azure OpenAI resources and configuration
    client = AzureAssistantAgent.create_client()

    # Define the assistant definition
    definition = await client.beta.assistants.create(
        model=AzureOpenAISettings().chat_deployment_name,
        name="Host",
        instructions="Answer questions about the menu.",
    )

    # Create the AzureAssistantAgent instance using the client and the assistant definition and the defined plugin
    agent = AzureAssistantAgent(
        client=client,
        definition=definition,
        plugins=[MenuPlugin()],
    )

    # Create a new thread for use with the assistant
    # If no thread is provided, a new thread will be
    # created and returned with the initial response
    thread: AssistantAgentThread = None

    user_inputs = [
        "Hello",
        "What is the special soup?",
        "What is the special drink?",
        "How much is that?",
        "Thank you",
    ]

    try:
        for user_input in user_inputs:
            print(f"# {AuthorRole.USER}: '{user_input}'")
            async for response in agent.invoke(
                messages=user_input,
                thread=thread,
                on_intermediate_message=handle_intermediate_steps,
            ):
                print(f"# {response.role}: {response}")
                thread = response.thread
    finally:
        await thread.delete() if thread else None
        await client.beta.assistants.delete(assistant_id=agent.id)


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

A seguir demonstra a saída de exemplo do processo de invocação do agente:

AuthorRole.USER: 'Hello'
AuthorRole.ASSISTANT: Hello! How can I assist you today?
AuthorRole.USER: 'What is the special soup?'
Function Call:> MenuPlugin-get_specials with arguments: {}
Function Result:> 
        Special Soup: Clam Chowder
        Special Salad: Cobb Salad
        Special Drink: Chai Tea
        for function: MenuPlugin-get_specials
AuthorRole.ASSISTANT: The special soup is Clam Chowder. Would you like to know more about the specials or 
    anything else?
AuthorRole.USER: 'What is the special drink?'
AuthorRole.ASSISTANT: The special drink is Chai Tea. If you have any more questions, feel free to ask!
AuthorRole.USER: 'How much is that?'
Function Call:> MenuPlugin-get_item_price with arguments: {"menu_item":"Chai Tea"}
Function Result:> $9.99 for function: MenuPlugin-get_item_price
AuthorRole.ASSISTANT: The Chai Tea is priced at $9.99. If there's anything else you'd like to know, 
    just let me know!
AuthorRole.USER: 'Thank you'
AuthorRole.ASSISTANT: You're welcome! If you have any more questions or need further assistance, feel free to 
    ask. Enjoy your day!

Recurso atualmente indisponível em Java.

Especificação declarativa

A documentação sobre o uso de especificações declarativas será disponibilizada em breve.

Importante

Esta característica encontra-se em fase experimental. Os recursos nesta fase estão em desenvolvimento e sujeitos a alterações antes de avançar para a versão de pré-visualização ou versão candidata.

O OpenAIAssistantAgent suporta instanciação de uma especificação declarativa YAML. A abordagem declarativa permite definir as propriedades, instruções, configuração do modelo, ferramentas e outras opções do agente em um único documento auditável. Isso torna a composição do agente portátil e facilmente gerenciada em todos os ambientes.

Observação

Quaisquer ferramentas, funções ou plugins listados no YAML declarativo devem estar disponíveis para o agente no momento da construção. Para plugins baseados em kernel, isso significa que eles devem ser registrados no Kernel. Para ferramentas internas, como Interpretador de Código ou Pesquisa de Arquivos, a configuração e as credenciais corretas devem ser fornecidas. O carregador do agente não criará funções do zero. Se um componente necessário estiver faltando, a criação do agente falhará.

Como usar a especificação declarativa

Em vez de enumerar todas as configurações possíveis do YAML, esta seção descreve os princípios-chave e fornece links para exemplos de conceito que mostram o código completo para cada tipo de ferramenta. Consulte estes exemplos de conceito para implementações de ponta a ponta de um OpenAIAssistantAgent com especificações declarativas:

AzureAssistantAgent amostras:

OpenAIAssistantAgent amostras:

Exemplo: Criando um AzureAIAgent a partir do YAML

Uma especificação declarativa YAML mínima pode ter a seguinte aparência:

type: openai_assistant
name: Host
instructions: Respond politely to the user's questions.
model:
  id: ${OpenAI:ChatModelId}
tools:
  - id: MenuPlugin.get_specials
    type: function
  - id: MenuPlugin.get_item_price
    type: function

Para obter detalhes sobre como conectar o agente, consulte os exemplos de código completos acima.

Pontos Principais

  • As especificações declarativas permitem definir a estrutura, as ferramentas e o comportamento do agente no YAML.
  • Todas as ferramentas e plugins referenciados devem ser registrados ou acessíveis em tempo de execução.
  • Ferramentas internas como Bing, Pesquisa de Arquivos e Interpretador de Código exigem configuração e credenciais adequadas (geralmente por meio de variáveis de ambiente ou argumentos explícitos).
  • Para obter exemplos abrangentes, consulte os links de exemplo fornecidos que demonstram cenários práticos, incluindo registro de plug-in, configuração de identidade do Azure e uso avançado de ferramentas.

Este recurso não está disponível.

Instruções

Para obter um exemplo completo de um OpenAIAssistantAgent, consulte:

Próximas Etapas