-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Motivation and Context <!-- Thank you for your contribution to the copilot-chat repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> This PR adds the token usage feature, in which token usage is calculated and shown per prompt and per session. Each token usage calculation will be split into two values: 1. total tokens used in chat completion of the bot response prompt 2. total tokens used in dependencies used to generate prompt ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> webapi - Token usage per prompt persists as part of ChatMessage object - Add initial bot message and token usage tracking to ChatHistoryController and tracking of token usage for bot response in ChatMessage and ChatSession models. - Update ChatSkill to save token usage to context variables and return with bot response. - Added token usage calculation in ChatSkill by calculating total token usage for dependency functions and chat completion and sending updated response to client. Copy token usage into original chat context. - Calculate memory extraction token usage by taking into account cumulative semantic memory token usage. - Update Utilities to include GetTokenUsage method. Webapp - AppState: Added a new TokenUsage field to track total usage across all chats by app session, and appSlice has been updated to cumulate session token usage. - Update SignalRMiddleware to handle token usage when receiving message updates from server. Update message property to tokenUsage if tokenUsage is defined, otherwise update content. - Fix ChatHistoryTextContent to include TypingIndicator when bot response is generating. - Changed PromptDetails -> PromptDialog component to show prompt details and token usage graph. - Removed TypingIndicatorRenderer Token usage shown for ChatMessages of type Message and Plan ![image](https://github.com/microsoft/chat-copilot/assets/125500434/9ae5a262-67ed-400c-8e26-b486f0e307c8) Hardcoded bot responses default to 0 ![image](https://github.com/microsoft/chat-copilot/assets/125500434/0240695a-14ea-4a53-90f7-e2c3f64df0fe) Loading state ![image](https://github.com/microsoft/chat-copilot/assets/125500434/cb3b0ab7-d76e-4404-8a09-7fa8078fbbf1) Info ![image](https://github.com/microsoft/chat-copilot/assets/125500434/9747b239-daa8-4553-ab57-9210c5553211) This is what it will look like in settings dialog once changes go in ![image](https://github.com/microsoft/chat-copilot/assets/125500434/aca7d038-96f9-4d3e-95a7-339bede3ebc7) ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [Contribution Guidelines](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md#development-scripts) raises no violations ~~[ ] All unit tests pass, and I have added new tests where possible~~ - [x] I didn't break anyone 😄 --------- Co-authored-by: GitHub Actions <[email protected]>
- Loading branch information
1 parent
d76ab3e
commit 6aefee8
Showing
38 changed files
with
776 additions
and
211 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -103,4 +103,4 @@ private bool ValidateMemoryName(string memoryName) | |
} | ||
|
||
# endregion | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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,36 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace SemanticKernel.Service.CopilotChat.Models; | ||
|
||
/// <summary> | ||
/// Response to chatSession/create request. | ||
/// </summary> | ||
public class CreateChatResponse | ||
{ | ||
/// <summary> | ||
/// ID that is persistent and unique to new chat session. | ||
/// </summary> | ||
[JsonPropertyName("id")] | ||
public string Id { get; set; } | ||
|
||
/// <summary> | ||
/// Title of the chat. | ||
/// </summary> | ||
[JsonPropertyName("title")] | ||
public string Title { get; set; } | ||
|
||
/// <summary> | ||
/// Initial bot message. | ||
/// </summary> | ||
[JsonPropertyName("initialBotMessage")] | ||
public ChatMessage? InitialBotMessage { get; set; } | ||
|
||
public CreateChatResponse(ChatSession chatSession, ChatMessage initialBotMessage) | ||
{ | ||
this.Id = chatSession.Id; | ||
this.Title = chatSession.Title; | ||
this.InitialBotMessage = initialBotMessage; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.