Skip to content

Commit

Permalink
Remove unused code path in HandleBotResponseAsync() (#825)
Browse files Browse the repository at this point in the history
### Motivation and Context
Some dead wood is present in our code due to churn.

### Description
Remove dead paths.


### Contribution Checklist
- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [Contribution
Guidelines](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
glahaye authored Mar 1, 2024
1 parent e018f90 commit e3853b2
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions webapi/Plugins/Chat/ChatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
Expand Down Expand Up @@ -103,11 +102,7 @@ public ChatPlugin(
// Clone the prompt options to avoid modifying the original prompt options.
this._promptOptions = promptOptions.Value.Copy();

this._semanticMemoryRetriever = new SemanticMemoryRetriever(
promptOptions,
chatSessionRepository,
memoryClient,
logger);
this._semanticMemoryRetriever = new SemanticMemoryRetriever(promptOptions, chatSessionRepository, memoryClient, logger);

this._contentSafety = contentSafety;
}
Expand Down Expand Up @@ -374,7 +369,8 @@ private async Task<CopilotChatMessage> GetChatResponseAsync(string chatId, strin

// Stream the response to the client
var promptView = new BotResponsePrompt(systemInstructions, audience, userIntent, memoryText, allowedChatHistory, chatHistory);
return await this.HandleBotResponseAsync(chatId, userId, chatContext, promptView, cancellationToken, citationMap.Values.AsEnumerable());

return await this.HandleBotResponseAsync(chatId, userId, chatContext, promptView, citationMap.Values.AsEnumerable(), cancellationToken);
}

/// <summary>
Expand All @@ -401,39 +397,24 @@ private async Task<string> RenderSystemInstructions(string chatId, KernelArgumen
/// <param name="userId">The user ID</param>
/// <param name="chatContext">Chat context.</param>
/// <param name="promptView">The prompt view.</param>
/// <param name="citations">Citation sources.</param>
/// <param name="cancellationToken">The cancellation token.</param>
private async Task<CopilotChatMessage> HandleBotResponseAsync(
string chatId,
string userId,
KernelArguments chatContext,
BotResponsePrompt promptView,
CancellationToken cancellationToken,
IEnumerable<CitationSource>? citations = null,
string? responseContent = null)
IEnumerable<CitationSource>? citations,
CancellationToken cancellationToken)
{
CopilotChatMessage chatMessage;
if (responseContent.IsNullOrEmpty())
{
// Get bot response and stream to client
await this.UpdateBotResponseStatusOnClientAsync(chatId, "Generating bot response", cancellationToken);
chatMessage = await AsyncUtils.SafeInvokeAsync(
() => this.StreamResponseToClientAsync(chatId, userId, promptView, cancellationToken, citations), nameof(StreamResponseToClientAsync));
}
else
{
chatMessage = await this.CreateBotMessageOnClient(
chatId,
userId,
JsonSerializer.Serialize(promptView),
responseContent!,
cancellationToken,
citations
);
}
// Get bot response and stream to client
await this.UpdateBotResponseStatusOnClientAsync(chatId, "Generating bot response", cancellationToken);
CopilotChatMessage chatMessage = await AsyncUtils.SafeInvokeAsync(
() => this.StreamResponseToClientAsync(chatId, userId, promptView, cancellationToken, citations), nameof(StreamResponseToClientAsync));

// Save the message into chat history
await this.UpdateBotResponseStatusOnClientAsync(chatId, "Saving message to chat history", cancellationToken);
await this._chatMessageRepository.UpsertAsync(chatMessage!);
await this._chatMessageRepository.UpsertAsync(chatMessage);

// Extract semantic chat memory
await this.UpdateBotResponseStatusOnClientAsync(chatId, "Generating semantic chat memory", cancellationToken);
Expand All @@ -449,7 +430,7 @@ await AsyncUtils.SafeInvokeAsync(

// Calculate total token usage for dependency functions and prompt template
await this.UpdateBotResponseStatusOnClientAsync(chatId, "Calculating token usage", cancellationToken);
chatMessage!.TokenUsage = this.GetTokenUsages(chatContext, chatMessage.Content);
chatMessage.TokenUsage = this.GetTokenUsages(chatContext, chatMessage.Content);

// Update the message on client and in chat history with final completion token usage
await this.UpdateMessageOnClient(chatMessage, cancellationToken);
Expand Down

0 comments on commit e3853b2

Please sign in to comment.