의미 체계 커널
팁 (조언)
이 설명과 관련된 자세한 API 설명서는 다음에서 확인할 수 있습니다.
세맨틱 커널에 의한 채팅 완성
채팅 완성 은 기본적으로 채팅 기록이 유지 관리되고 각 요청과 함께 모델에 표시되는 AI 모델과의 채팅 기반 상호 작용을 위한 프로토콜입니다. 의미 체계 커널 AI 서비스는 다양한 AI 모델의 채팅 완성 기능을 통합하기 위한 통합 프레임워크를 제공합니다.
A ChatCompletionAgent 는 이러한 AI 서비스를 활용하여 사용자 또는 다른 에이전트로 전송되는지 여부에 관계없이 응답을 생성할 수 있습니다.
개발 환경 준비
ChatCompletionAgent개발을 계속하려면 적절한 패키지로 개발 환경을 구성합니다.
프로젝트에 Microsoft.SemanticKernel.Agents.Core 패키지를 추가합니다.
dotnet add package Microsoft.SemanticKernel.Agents.Core --prerelease
semantic-kernel 패키지를 설치합니다.
pip install semantic-kernel
중요합니다
어떤 AI 서비스의 일부로 ChatCompletionAgent사용하는지에 따라 추가 패키지를 설치해야 할 수 있습니다. 다음 페이지에서 필요한 추가 항목을 확인하세요.
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-agents-core</artifactId>
<version>[LATEST]</version>
</dependency>
ChatCompletionAgent을 만들기
ChatCompletionAgent 기본적으로 AI 서비스기반으로 합니다. 따라서 ChatCompletionAgent 만드는 것은 하나 이상의 채팅 완료 서비스를 포함하는 Kernel 인스턴스를 만든 다음 해당 Kernel 인스턴스에 대한 참조로 에이전트를 인스턴스화하는 것으로 시작합니다.
// Initialize a Kernel with a chat-completion service
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(/*<...configuration parameters>*/);
Kernel kernel = builder.Build();
// Create the agent
ChatCompletionAgent agent =
new()
{
Name = "SummarizationAgent",
Instructions = "Summarize user input",
Kernel = kernel
};
ChatCompletionAgent만드는 방법에는 두 가지가 있습니다.
1. 채팅 완료 서비스를 직접 제공하여
from semantic_kernel.agents import ChatCompletionAgent
# Create the agent by directly providing the chat completion service
agent = ChatCompletionAgent(
service=AzureChatCompletion(), # your chat completion service instance
name="<agent name>",
instructions="<agent instructions>",
)
2. 커널을 먼저 만들고 서비스를 추가한 다음 커널을 제공합니다.
# Define the kernel
kernel = Kernel()
# Add the chat completion service to the kernel
kernel.add_service(AzureChatCompletion())
# Create the agent using the kernel
agent = ChatCompletionAgent(
kernel=kernel,
name="<agent name>",
instructions="<agent instructions>",
)
첫 번째 방법은 채팅 완료 서비스가 이미 준비되었을 때 유용합니다. 두 번째 방법은 여러 서비스 또는 추가 기능을 관리하는 커널이 필요한 경우에 유용합니다.
// Initialize a Kernel with a chat-completion service
var chatCompletion = OpenAIChatCompletion.builder()
.withOpenAIAsyncClient(client) // OpenAIAsyncClient with configuration parameters
.withModelId(MODEL_ID)
.build();
var kernel = Kernel.builder()
.withAIService(ChatCompletionService.class, chatCompletion)
.build();
// Create the agent
var agent = ChatCompletionAgent.builder()
.withKernel(kernel)
.build();
AI 서비스 선택
Semantic Kernel AI 서비스를 직접 사용하는 것과 다르지 않게,ChatCompletionAgent 서비스 선택기의 사양을 지원합니다. 서비스 선택기는 둘 이상이 포함된 경우 대상으로 지정할 Kernel AI 서비스를 식별합니다.
비고
여러 AI 서비스가 있고 서비스 선택기가 제공되지 않는 경우 외부의 AI 서비스를 사용할 때 찾을 수 있는 에이전트에 동일한 기본 논리가 적용됩니다. Agent Framework
IKernelBuilder builder = Kernel.CreateBuilder();
// Initialize multiple chat-completion services.
builder.AddAzureOpenAIChatCompletion(/*<...service configuration>*/, serviceId: "service-1");
builder.AddAzureOpenAIChatCompletion(/*<...service configuration>*/, serviceId: "service-2");
Kernel kernel = builder.Build();
ChatCompletionAgent agent =
new()
{
Name = "<agent name>",
Instructions = "<agent instructions>",
Kernel = kernel,
Arguments = // Specify the service-identifier via the KernelArguments
new KernelArguments(
new OpenAIPromptExecutionSettings()
{
ServiceId = "service-2" // The target service-identifier.
})
};
from semantic_kernel.connectors.ai.open_ai import (
AzureChatCompletion,
AzureChatPromptExecutionSettings,
)
# Define the Kernel
kernel = Kernel()
# Add the AzureChatCompletion AI Service to the Kernel
kernel.add_service(AzureChatCompletion(service_id="service1"))
kernel.add_service(AzureChatCompletion(service_id="service2"))
settings = AzureChatPromptExecutionSettings(service_id="service2")
# Create the agent
agent = ChatCompletionAgent(
kernel=kernel,
name="<agent name>",
instructions="<agent instructions>",
arguments=KernelArguments(settings=settings)
)
현재 Java에서 기능을 사용할 수 없습니다.
ChatCompletionAgent와 대화하기
ChatCompletionAgent와 대화하는 것은, 채팅 완료 ChatHistory와 상호 작용하는 인스턴스를 기반으로 한다는 점에서 다르지 않습니다.
사용자 메시지를 사용하여 에이전트를 호출하기만 하면 됩니다.
// Define agent
ChatCompletionAgent agent = ...;
// Generate the agent response(s)
await foreach (ChatMessageContent response in agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, "<user input>")))
{
// Process agent response(s)...
}
에이전트와 대화를 나누기 위해 AgentThread을 사용할 수도 있습니다.
여기서는 .를 사용하고 있습니다 ChatHistoryAgentThread.
ChatHistoryAgentThread 또한 이전 대화를 다시 시작하면 선택적 ChatHistory 개체를 해당 생성자를 통해 입력으로 사용할 수 있습니다. (표시되지 않음)
// Define agent
ChatCompletionAgent agent = ...;
AgentThread thread = new ChatHistoryAgentThread();
// Generate the agent response(s)
await foreach (ChatMessageContent response in agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, "<user input>"), thread))
{
// Process agent response(s)...
}
ChatCompletionAgent와 여러 가지 방법으로 대화할 수 있습니다.
가장 쉬운 방법은 get_response호출하고 기다리는 것입니다.
# Define agent
agent = ChatCompletionAgent(...)
# Generate the agent response
response = await agent.get_response(messages="user input")
# response is an `AgentResponseItem[ChatMessageContent]` object
에이전트가 호출 간에 대화 기록을 유지하도록 하려면 다음과 같이 전달할 ChatHistoryAgentThread 수 있습니다.
# Define agent
agent = ChatCompletionAgent(...)
# Generate the agent response(s)
response = await agent.get_response(messages="user input")
# Generate another response, continuing the conversation thread from the first response.
response2 = await agent.get_response(messages="user input", thread=response.thread)
# process agent response(s)
invoke 메서드를 호출하면 AsyncIterable의 AgentResponseItem[ChatMessageContent]가 반환됩니다.
# Define agent
agent = ChatCompletionAgent(...)
# Define the thread
thread = ChatHistoryAgentThread()
# Generate the agent response(s)
async for response in agent.invoke(messages="user input", thread=thread):
# process agent response(s)
ChatCompletionAgent는 스트리밍을 지원하며, 이때 invoke_stream 메서드는 AsyncIterableStreamingChatMessageContent 반환합니다.
# Define agent
agent = ChatCompletionAgent(...)
# Define the thread
thread = ChatHistoryAgentThread()
# Generate the agent response(s)
async for response in agent.invoke_stream(messages="user input", thread=thread):
# process agent response(s)
ChatCompletionAgent agent = ...;
// Generate the agent response(s)
agent.invokeAsync(new ChatMessageContent<>(AuthorRole.USER, "<user input>")).block();
에이전트와 대화를 나누기 위해 AgentThread을 사용할 수도 있습니다.
여기서는 .를 사용하고 있습니다 ChatHistoryAgentThread.
이전 대화를 다시 시작하는 경우, ChatHistoryAgentThread 생성자를 통해 ChatHistory 개체를 입력으로 사용할 수 있습니다. (표시되지 않음)
// Define agent
ChatCompletionAgent agent = ...;
AgentThread thread = new ChatHistoryAgentThread();
// Generate the agent response(s)
agent.invokeAsync(new ChatMessageContent<>(AuthorRole.USER, "<user input>"), thread).block();
중간 메시지를 ChatCompletionAgent로 처리하기
의미 체계 커널 ChatCompletionAgent 은 사용자 쿼리 또는 질문을 충족하는 에이전트를 호출하도록 설계되었습니다. 호출하는 동안 에이전트는 도구를 실행하여 최종 답변을 파생시킬 수 있습니다. 이 프로세스 중에 생성된 중간 메시지에 액세스하기 위해 호출자는 FunctionCallContent 또는 FunctionResultContent 인스턴스를 처리하는 콜백 함수를 제공할 수 있습니다.
ChatCompletionAgent에 대한 콜백 설명서는 곧 제공될 예정입니다.
on_intermediate_message 또는 agent.invoke(...) 안에서 agent.invoke_stream(...) 콜백을 구성하면, 호출자는 에이전트의 최종 응답을 작성하는 과정에서 생성된 중간 메시지를 수신할 수 있습니다.
import asyncio
from typing import Annotated
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent, ChatHistoryAgentThread
from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import AzureChatCompletion
from semantic_kernel.contents import FunctionCallContent, FunctionResultContent
from semantic_kernel.contents.chat_message_content import ChatMessageContent
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, FunctionCallContent):
print(f"Function Call:> {item.name} with arguments: {item.arguments}")
elif isinstance(item, FunctionResultContent):
print(f"Function Result:> {item.result} for function: {item.name}")
else:
print(f"{message.role}: {message.content}")
async def main() -> None:
agent = ChatCompletionAgent(
service=AzureChatCompletion(),
name="Assistant",
instructions="Answer questions about the menu.",
plugins=[MenuPlugin()],
)
# Create a thread for the agent
# If no thread is provided, a new thread will be
# created and returned with the initial response
thread: ChatHistoryAgentThread = None
user_inputs = [
"Hello",
"What is the special soup?",
"How much does that cost?",
"Thank you",
]
for user_input in user_inputs:
print(f"# 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
if __name__ == "__main__":
asyncio.run(main())
다음은 에이전트 호출 프로세스의 샘플 출력을 보여 줍니다.
User: 'Hello'
AuthorRole.ASSISTANT: Hi there! How can I assist you today?
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 today is Clam Chowder. Would you like to know anything else from the menu?
User: 'How much does that cost?'
Function Call:> MenuPlugin-get_item_price with arguments: {"menu_item":"Clam Chowder"}
Function Result:> $9.99 for function: MenuPlugin-get_item_price
AuthorRole.ASSISTANT: The Clam Chowder costs $9.99. Would you like to know more about the menu or anything else?
User: 'Thank you'
AuthorRole.ASSISTANT: You're welcome! If you have any more questions, feel free to ask. Enjoy your day!
현재 Java에서 기능을 사용할 수 없습니다.
선언적 사양
선언적 사양 사용에 대한 설명서는 곧 제공될 예정입니다.
중요합니다
이 기능은 실험 단계에 있습니다. 이 단계의 기능은 개발 중이며 미리 보기 또는 릴리스 후보 단계로 넘어가기 전에 변경될 수 있습니다.
YAML ChatCompletionAgent 선언적 사양에서 직접 인스턴스화할 수 있습니다. 이 방법을 사용하면 에이전트의 핵심 속성, 지침 및 사용 가능한 함수(플러그 인)를 구조화되고 이식 가능한 방식으로 정의할 수 있습니다. YAML을 사용하면 에이전트의 이름, 설명, 명령 프롬프트, 도구 집합 및 모델 매개 변수를 단일 문서에서 설명할 수 있으므로 에이전트의 구성을 쉽게 감사하고 재현할 수 있습니다.
비고
선언적 YAML에 지정된 모든 도구 또는 함수는 에이전트를 만들 때 커널 인스턴스에 이미 있어야 합니다. 에이전트 로더는 사양에서 새 함수를 만들지 않습니다. 대신 커널의 식별자에 의해 참조된 플러그 인 및 함수를 조회합니다. 커널에 필요한 플러그 인 또는 함수가 없으면 에이전트를 만드는 동안 오류가 발생합니다.
예: YAML 사양을 기반으로 ChatCompletionAgent 만들기
import asyncio
from typing import Annotated
from semantic_kernel import Kernel
from semantic_kernel.agents import AgentRegistry, ChatHistoryAgentThread
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions import kernel_function
# Define a plugin with kernel functions
class MenuPlugin:
@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"
# YAML spec for the agent
AGENT_YAML = """
type: chat_completion_agent
name: Assistant
description: A helpful assistant.
instructions: Answer the user's questions using the menu functions.
tools:
- id: MenuPlugin.get_specials
type: function
- id: MenuPlugin.get_item_price
type: function
model:
options:
temperature: 0.7
"""
USER_INPUTS = [
"Hello",
"What is the special soup?",
"What does that cost?",
"Thank you",
]
async def main():
kernel = Kernel()
kernel.add_plugin(MenuPlugin(), plugin_name="MenuPlugin")
agent: ChatCompletionAgent = await AgentRegistry.create_from_yaml(
AGENT_YAML, kernel=kernel, service=OpenAIChatCompletion()
)
thread: ChatHistoryAgentThread | None = None
for user_input in USER_INPUTS:
print(f"# User: {user_input}")
response = await agent.get_response(user_input, thread=thread)
print(f"# {response.name}: {response}")
thread = response.thread
await thread.delete() if thread else None
if __name__ == "__main__":
asyncio.run(main())
이 기능을 사용할 수 없습니다.
사용 방법
ChatCompletionAgent대한 엔드 투 엔드 예제는 다음을 참조하세요.