Skip to content

Commit

Permalink
Revert "add context tokens to api response"
Browse files Browse the repository at this point in the history
  • Loading branch information
smallketchup82 authored Oct 4, 2024
1 parent e22f9ad commit 53dce49
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 29 deletions.
12 changes: 5 additions & 7 deletions galaxygpt-api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,24 @@ public static void Main(string[] args)

var requestStart = Stopwatch.StartNew();

(string context, int contextTokenCount, int questionTokenCount) context = await contextManager.FetchContext(askPayload.Prompt, askPayload.MaxContextLength ?? 5);
(string, int) context = await contextManager.FetchContext(askPayload.Prompt, askPayload.MaxContextLength ?? 5);

// hash the username to prevent any potential privacy issues
// string? username = askPayload.Username != null ? Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(askPayload.Username))) : null;

try
{
(string output, int promptTokenCount, int responseTokenCount) answer = await galaxyGpt.AnswerQuestion(askPayload.Prompt, context.Item1, username: askPayload.Username, maxOutputTokens: askPayload.MaxLength);
(string, int) answer = await galaxyGpt.AnswerQuestion(askPayload.Prompt, context.Item1, username: askPayload.Username, maxOutputTokens: askPayload.MaxLength);
requestStart.Stop();

return Results.Json(new AskResponse
{
Answer = answer.Item1.Trim(),
Context = context.context,
Context = context.Item1,
Duration = requestStart.ElapsedMilliseconds.ToString(),
Version = version,
PromptTokens = answer.promptTokenCount.ToString(),
ContextTokens = context.contextTokenCount.ToString(),
QuestionTokens = context.questionTokenCount.ToString(),
ResponseTokens = answer.responseTokenCount.ToString()
QuestionTokens = context.Item2.ToString(),
ResponseTokens = answer.Item2.ToString()
});
}
catch (BonkedException e)
Expand Down
14 changes: 1 addition & 13 deletions galaxygpt-api/Types/AskQuestion/AskResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,7 @@ public class AskResponse
public required string Version { get; init; }

/// <summary>
/// The amount of tokens in the system prompt
/// </summary>
[JsonPropertyName("context_tokens")]
public required string PromptTokens { get; init; }

/// <summary>
/// The amount of tokens in the context
/// </summary>
[JsonPropertyName("context_tokens")]
public required string ContextTokens { get; init; }

/// <summary>
/// The amount of tokens in the user's question
/// The combined amount of tokens in the system prompt, context, and user's question
/// </summary>
[JsonPropertyName("question_tokens")]
public required string QuestionTokens { get; init; }
Expand Down
7 changes: 3 additions & 4 deletions galaxygpt-tests/AiClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AiClientTests(ITestOutputHelper output)
ChatCompletion chatCompletion = OpenAIChatModelFactory.ChatCompletion(content:
[
ChatMessageContentPart.CreateTextPart("goofy ahh uncle productions")
], role: ChatMessageRole.Assistant, usage: OpenAIChatModelFactory.ChatTokenUsage(100, 100, 100));
], role: ChatMessageRole.Assistant);

Mock<ClientResult<ChatCompletion>> chatClientResultMock = new(null!, Mock.Of<PipelineResponse>());

Expand Down Expand Up @@ -78,14 +78,13 @@ public async Task TestAnswersQuestion()
int? maxOutputTokens = 100;

// Act
(string output, int promptTokenCount, int answerTokenCount) result =
(string output, int tokencount) result =
await _aiClient.AnswerQuestion(question, context, maxInputTokens, username, maxOutputTokens);

// Assert
Assert.NotNull(result.output);
Assert.False(string.IsNullOrWhiteSpace(result.output));
Assert.True(result.promptTokenCount > 0);
Assert.True(result.answerTokenCount > 0);
Assert.True(result.tokencount > 0);
_output.WriteLine(result.Item1);
}

Expand Down
4 changes: 2 additions & 2 deletions galaxygpt/AiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public partial class AiClient(
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="BonkedException">The moderation API flagged the response</exception>
public async Task<(string output, int promptTokenCount, int responseTokenCount)> AnswerQuestion(string question, string context, int? maxInputTokens = null,
public async Task<(string output, int tokencount)> AnswerQuestion(string question, string context, int? maxInputTokens = null,
string? username = null, int? maxOutputTokens = null)
{
question = question.Trim();
Expand Down Expand Up @@ -73,7 +73,7 @@ public partial class AiClient(

string finalMessage = messages[^1].Content[0].Text;
await ModerateText(finalMessage, moderationClient);
return (finalMessage, gptTokenizer.CountTokens(messages.First().Content[0].Text), clientResult.Value.Usage.OutputTokenCount);
return (finalMessage, gptTokenizer.CountTokens(finalMessage));
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions galaxygpt/ContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ContextManager(EmbeddingClient embeddingClient,
qdrantUrlAndPort.Length > 1 ? int.Parse(qdrantUrlAndPort[1]) : 6334);
}

public async Task<(string context, int contextTokenCount, int questionTokenCount)> FetchContext(string question, ulong maxResults = 5)
public async Task<(string, int)> FetchContext(string question, ulong maxResults = 5)
{
if (_qdrantClient == null)
throw new InvalidOperationException("The Qdrant client is not available.");
Expand All @@ -63,7 +63,6 @@ public ContextManager(EmbeddingClient embeddingClient,
context.Append($"Page: {searchResult.Payload["title"].StringValue}\nContent: {searchResult.Payload["content"].StringValue}\n\n###\n\n");
}

string contextString = context.ToString();
return (contextString, _embeddingsTokenizer.CountTokens(contextString), _embeddingsTokenizer.CountTokens(question));
return (context.ToString(), _embeddingsTokenizer.CountTokens(question));
}
}

0 comments on commit 53dce49

Please sign in to comment.