-
Notifications
You must be signed in to change notification settings - Fork 54
Add ChatCompletionAgent #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ed651d6
Add draft changes for agents
e242d99
Updates
83daec2
Renaming sample and adjust invocation context
4664b2b
Updates
9cfab02
Remove openai package
afaa50c
Remove openai package
3dcbb89
Add EI_EXPOSE_REP updates
3c0f255
Add EI_EXPOSE_REP updates
fff6b90
Add EI_EXPOSE_REP updates
ae91765
Make NEW_MESSAGES_ONLY the default for kernel agent invocation context
c004ae0
Return Mono.error instead of runtime exception
a19b09f
Remove authorName from ChatMessageContent
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>com.microsoft.semantic-kernel</groupId> | ||
| <artifactId>semantickernel-parent</artifactId> | ||
| <version>1.4.4-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
|
|
||
| <artifactId>semantickernel-agents-core</artifactId> | ||
|
|
||
| <name>Semantic Kernel Chat Completion Agent</name> | ||
| <description>Chat Completion Agent for Semantic Kernel</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.microsoft.semantic-kernel</groupId> | ||
| <artifactId>semantickernel-api</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
308 changes: 308 additions & 0 deletions
308
...src/main/java/com/microsoft/semantickernel/agents/chatcompletion/ChatCompletionAgent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,308 @@ | ||
| package com.microsoft.semantickernel.agents.chatcompletion; | ||
|
|
||
| import com.microsoft.semantickernel.Kernel; | ||
| import com.microsoft.semantickernel.agents.AgentInvokeOptions; | ||
| import com.microsoft.semantickernel.agents.AgentResponseItem; | ||
| import com.microsoft.semantickernel.agents.AgentThread; | ||
| import com.microsoft.semantickernel.agents.KernelAgent; | ||
| import com.microsoft.semantickernel.builders.SemanticKernelBuilder; | ||
| import com.microsoft.semantickernel.orchestration.InvocationContext; | ||
| import com.microsoft.semantickernel.orchestration.InvocationReturnMode; | ||
| import com.microsoft.semantickernel.orchestration.PromptExecutionSettings; | ||
| import com.microsoft.semantickernel.orchestration.ToolCallBehavior; | ||
| import com.microsoft.semantickernel.semanticfunctions.KernelArguments; | ||
| import com.microsoft.semantickernel.semanticfunctions.PromptTemplate; | ||
| import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; | ||
| import com.microsoft.semantickernel.semanticfunctions.PromptTemplateFactory; | ||
| import com.microsoft.semantickernel.services.ServiceNotFoundException; | ||
| import com.microsoft.semantickernel.services.chatcompletion.AuthorRole; | ||
| import com.microsoft.semantickernel.services.chatcompletion.ChatCompletionService; | ||
| import com.microsoft.semantickernel.services.chatcompletion.ChatHistory; | ||
| import com.microsoft.semantickernel.services.chatcompletion.ChatMessageContent; | ||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ChatCompletionAgent extends KernelAgent { | ||
|
|
||
| private ChatCompletionAgent( | ||
| String id, | ||
| String name, | ||
| String description, | ||
| Kernel kernel, | ||
| KernelArguments kernelArguments, | ||
| InvocationContext context, | ||
| String instructions, | ||
| PromptTemplate template | ||
| ) { | ||
| super( | ||
| id, | ||
| name, | ||
| description, | ||
| kernel, | ||
| kernelArguments, | ||
| context, | ||
| instructions, | ||
| template | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Invoke the agent with the given chat history. | ||
| * | ||
| * @param messages The chat history to process | ||
| * @param thread The agent thread to use | ||
| * @param options The options for invoking the agent | ||
| * @return A Mono containing the agent response | ||
| */ | ||
| @Override | ||
| public Mono<List<AgentResponseItem<ChatMessageContent<?>>>> invokeAsync( | ||
| List<ChatMessageContent<?>> messages, | ||
| AgentThread thread, | ||
| @Nullable AgentInvokeOptions options | ||
| ) { | ||
| return ensureThreadExistsWithMessagesAsync(messages, thread, ChatHistoryAgentThread::new) | ||
| .cast(ChatHistoryAgentThread.class) | ||
| .flatMap(agentThread -> { | ||
| // Extract the chat history from the thread | ||
| ChatHistory history = new ChatHistory( | ||
| agentThread.getChatHistory().getMessages() | ||
| ); | ||
|
|
||
| // Invoke the agent with the chat history | ||
| return internalInvokeAsync( | ||
| history, | ||
| options | ||
| ) | ||
| .flatMapMany(Flux::fromIterable) | ||
| // notify on the new thread instance | ||
| .concatMap(agentMessage -> { | ||
| // Set the author name for the message | ||
| agentMessage.setAuthorName(this.name); | ||
|
|
||
| return this.notifyThreadOfNewMessageAsync(agentThread, agentMessage).thenReturn(agentMessage); | ||
| }) | ||
| .collectList() | ||
| .map(chatMessageContents -> | ||
| chatMessageContents.stream() | ||
| .map(message -> new AgentResponseItem<ChatMessageContent<?>>(message, agentThread)) | ||
| .collect(Collectors.toList()) | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| private Mono<List<ChatMessageContent<?>>> internalInvokeAsync( | ||
| ChatHistory history, | ||
| @Nullable AgentInvokeOptions options | ||
| ) { | ||
| if (options == null) { | ||
| options = new AgentInvokeOptions(); | ||
| } | ||
|
|
||
| final Kernel kernel = options.getKernel() != null ? options.getKernel() : this.kernel; | ||
| final KernelArguments arguments = mergeArguments(options.getKernelArguments()); | ||
| final String additionalInstructions = options.getAdditionalInstructions(); | ||
| final InvocationContext invocationContext = options.getInvocationContext() != null ? options.getInvocationContext() : this.invocationContext; | ||
|
|
||
| try { | ||
| ChatCompletionService chatCompletionService = kernel.getService(ChatCompletionService.class, arguments); | ||
|
|
||
| PromptExecutionSettings executionSettings = invocationContext != null && invocationContext.getPromptExecutionSettings() != null | ||
| ? invocationContext.getPromptExecutionSettings() | ||
| : kernelArguments.getExecutionSettings().get(chatCompletionService.getServiceId()); | ||
|
|
||
| ToolCallBehavior toolCallBehavior = invocationContext != null | ||
| ? invocationContext.getToolCallBehavior() | ||
| : ToolCallBehavior.allowAllKernelFunctions(true); | ||
|
|
||
| // Build base invocation context | ||
| InvocationContext.Builder builder = InvocationContext.builder() | ||
| .withPromptExecutionSettings(executionSettings) | ||
| .withToolCallBehavior(toolCallBehavior) | ||
| .withReturnMode(InvocationReturnMode.NEW_MESSAGES_ONLY); | ||
|
|
||
| if (invocationContext != null) { | ||
| builder = builder | ||
| .withTelemetry(invocationContext.getTelemetry()) | ||
| .withContextVariableConverter(invocationContext.getContextVariableTypes()) | ||
| .withKernelHooks(invocationContext.getKernelHooks()); | ||
| } | ||
|
|
||
| InvocationContext agentInvocationContext = builder.build(); | ||
|
|
||
| return renderInstructionsAsync(kernel, arguments, agentInvocationContext).flatMap( | ||
| instructions -> { | ||
| // Create a new chat history with the instructions | ||
| ChatHistory chat = new ChatHistory( | ||
| instructions | ||
| ); | ||
|
|
||
| // Add agent additional instructions | ||
| if (additionalInstructions != null) { | ||
| chat.addMessage(new ChatMessageContent<>( | ||
| AuthorRole.SYSTEM, | ||
| additionalInstructions | ||
| )); | ||
| } | ||
|
|
||
| // Add the chat history to the new chat | ||
| chat.addAll(history); | ||
|
|
||
| return chatCompletionService.getChatMessageContentsAsync(chat, kernel, agentInvocationContext); | ||
| } | ||
| ); | ||
|
|
||
| } catch (ServiceNotFoundException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public Mono<Void> notifyThreadOfNewMessageAsync(AgentThread thread, ChatMessageContent<?> message) { | ||
| return Mono.defer(() -> { | ||
| return thread.onNewMessageAsync(message); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Builder for creating instances of ChatCompletionAgent. | ||
| */ | ||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder implements SemanticKernelBuilder<ChatCompletionAgent> { | ||
| private String id; | ||
| private String name; | ||
| private String description; | ||
| private Kernel kernel; | ||
| private KernelArguments kernelArguments; | ||
| private InvocationContext invocationContext; | ||
| private String instructions; | ||
| private PromptTemplate template; | ||
|
|
||
| /** | ||
| * Set the ID of the agent. | ||
| * | ||
| * @param id The ID of the agent. | ||
| */ | ||
| public Builder withId(String id) { | ||
| this.id = id; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the name of the agent. | ||
| * | ||
| * @param name The name of the agent. | ||
| */ | ||
| public Builder withName(String name) { | ||
| this.name = name; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the description of the agent. | ||
| * | ||
| * @param description The description of the agent. | ||
| */ | ||
| public Builder withDescription(String description) { | ||
| this.description = description; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the kernel to use for the agent. | ||
| * | ||
| * @param kernel The kernel to use. | ||
| */ | ||
| public Builder withKernel(Kernel kernel) { | ||
| this.kernel = kernel; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the kernel arguments to use for the agent. | ||
| * | ||
| * @param KernelArguments The kernel arguments to use. | ||
| */ | ||
| @SuppressFBWarnings("EI_EXPOSE_REP2") | ||
| public Builder withKernelArguments(KernelArguments KernelArguments) { | ||
| this.kernelArguments = KernelArguments; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the instructions for the agent. | ||
| * | ||
| * @param instructions The instructions for the agent. | ||
| */ | ||
| public Builder withInstructions(String instructions) { | ||
| this.instructions = instructions; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the invocation context for the agent. | ||
| * | ||
| * @param invocationContext The invocation context to use. | ||
| */ | ||
| public Builder withInvocationContext(InvocationContext invocationContext) { | ||
| this.invocationContext = invocationContext; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the template for the agent. | ||
| * | ||
| * @param template The template to use. | ||
| */ | ||
| public Builder withTemplate(PromptTemplate template) { | ||
| this.template = template; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Build the ChatCompletionAgent instance. | ||
| * | ||
| * @return The ChatCompletionAgent instance. | ||
| */ | ||
| public ChatCompletionAgent build() { | ||
| return new ChatCompletionAgent( | ||
| id, | ||
| name, | ||
| description, | ||
| kernel, | ||
| kernelArguments, | ||
| invocationContext, | ||
| instructions, | ||
| template | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Build the ChatCompletionAgent instance with the given prompt template config and factory. | ||
| * | ||
| * @param promptTemplateConfig The prompt template config to use. | ||
| * @param promptTemplateFactory The prompt template factory to use. | ||
| * @return The ChatCompletionAgent instance. | ||
| */ | ||
| public ChatCompletionAgent build(PromptTemplateConfig promptTemplateConfig, PromptTemplateFactory promptTemplateFactory) { | ||
| return new ChatCompletionAgent( | ||
| id, | ||
| name, | ||
| description, | ||
| kernel, | ||
| kernelArguments, | ||
| invocationContext, | ||
| promptTemplateConfig.getTemplate(), | ||
| promptTemplateFactory.tryCreate(promptTemplateConfig) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.