Skip to content

Commit

Permalink
add usage tokens to debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-b-iodigital committed Jul 30, 2024
1 parent dce466c commit fcacac1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private static ConversationResponse MapConversationToApiResponse(ConversationRef
ReplacedContextVariables = record.ReplacedContextVariables,
FullPrompt = record.FullPrompt
})
.ToList());
.ToList(), debugInformation.UsedInputTokens, debugInformation.UsedOutputTokens);
}

private static List<ConversationReferenceResponse> MapReferencesToApiResponse(IEnumerable<ConversationReference> references) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<MudDivider Class="mt-2 mb-2" DividerType="@DividerType.FullWidth"/>
<MudText Typo="Typo.h5" Align="Align.Left" GutterBottom="true">Debug information</MudText>

<MudText>Input tokens: @DebugInformation.Sum(di => di.UsedInputTokens)</MudText>
<MudText>Output tokens: @DebugInformation.Sum(di => di.UsedOutputTokens)</MudText>

foreach (var record in DebugInformation.SelectMany(di => di.DebugRecords).OrderBy(record => record.ExecutedAt))
{
<MudText>@record.ExecutedAt.ToString("G")</MudText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ public record ConversationReferencedResponse(ConversationResponse Response,

public record DebugInformationResponse
{
public DebugInformationResponse(List<DebugRecordResponse> debugRecords)
public DebugInformationResponse(List<DebugRecordResponse> debugRecords, int usedInputTokens, int usedOutputTokens)
{
DebugRecords = debugRecords;
UsedInputTokens = usedInputTokens;
UsedOutputTokens = usedOutputTokens;
}

public int UsedInputTokens { get; set; } = 0;
public int UsedOutputTokens { get; set; } = 0;

[JsonPropertyName("debug")]
public List<DebugRecordResponse> DebugRecords { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public async Task<ConversationReferencedResult> ConverseAsync(HoldConversation h
chatResult.Result.Usage ?? throw new InvalidOperationException("No usage was passed in after executing an OpenAI call"),
conversationHistory.Model
);
conversationHistory.DebugInformation?.SetUsage(chatResult.Result.Usage);

answer = chatResult.Result.GetFirstAnswer();
if (answer.Function != null)
Expand All @@ -132,12 +133,14 @@ public async Task<ConversationReferencedResult> ConverseAsync(HoldConversation h
holdConversation.UserPrompt = functionMessage;
(chatBuilder, textReferences, imageReferences) = await BuildChatAsync(holdConversation, conversationHistory, cancellationToken);
chatResult = await chatBuilder.ExecuteAndCalculateCostAsync(false, cancellationToken);
_telemetryService.RegisterGPTUsage(
_telemetryService.RegisterGPTUsage(
holdConversation.ConversationId,
holdConversation.TenantId,
chatResult.Result.Usage ?? throw new InvalidOperationException("No usage was passed in after executing an OpenAI call"),
conversationHistory.Model
);
conversationHistory.DebugInformation?.SetUsage(chatResult.Result.Usage);

answer = chatResult.Result.GetFirstAnswer();
conversationHistory.AppendToConversation(functionMessage, answer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
using Rystem.OpenAi;
using System.Text.Json.Serialization;

namespace ConversationalSearchPlatform.BackOffice.Services.Models.ConversationDebug;

public record DebugInformation
public class DebugInformation
{
public DebugInformation(List<DebugRecord> debugRecords)
public DebugInformation(List<DebugRecord> debugRecords, int usedInputTokens, int usedOutputTokens)
{
DebugRecords = debugRecords;
UsedInputTokens = usedInputTokens;
UsedOutputTokens = usedOutputTokens;
}

public int UsedInputTokens { get; set; } = 0;
public int UsedOutputTokens { get; set; } = 0;

public int CurrentDebugRecordIndex { get; set; }
[JsonPropertyName("debug")]
public List<DebugRecord> DebugRecords { get; set; }

public void SetUsage(CompletionUsage usage)
{
UsedInputTokens += usage.PromptTokens ?? 0;
UsedOutputTokens += usage.CompletionTokens ?? 0;
}
}

public record DebugRecord
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void SaveConversationHistory(this ConversationHistory conversation

public static void InitializeDebugInformation(this ConversationHistory conversationHistory)
{
conversationHistory.DebugInformation ??= new DebugInformation(new List<DebugRecord>());
conversationHistory.DebugInformation ??= new DebugInformation(new List<DebugRecord>(), 0, 0);

conversationHistory.DebugInformation?.DebugRecords.Add(new DebugRecord
{
Expand Down

0 comments on commit fcacac1

Please sign in to comment.