Skip to content

Instantly share code, notes, and snippets.

@lmolkova
Last active October 16, 2024 22:50
Show Gist options
  • Save lmolkova/1edde91055476433b3d69eb4cb224afe to your computer and use it in GitHub Desktop.
Save lmolkova/1edde91055476433b3d69eb4cb224afe to your computer and use it in GitHub Desktop.
.NET events
private string GetFinishReason(ChatFinishReason finishReason) => finishReason switch
{
ChatFinishReason.Stop => "stop",
ChatFinishReason.Length => "length",
ChatFinishReason.ToolCalls => "tool_calls",
// ...
_ => finishReason.ToString(),
};
private void LogChoice(ChatCompletion message)
{
if (_logger.IsEnabled(LogLevel.Information))
{
var eventBody = new
{
id = message.Id,
index = 0,
finish_reason = GetFinishReason(message.FinishReason),
message = new
{
content = new
{
role = "assistant",
content = IsContentLoggingEnabled ?
message.Content.Where(c => c.Kind == ChatMessageContentPartKind.Text).First().Text : null
},
tool_calls = message.ToolCalls.Any() ? message.ToolCalls.Select(t => new
{
id = t.Id,
type = t.Kind,
function = new
{
name = t.FunctionName,
arguments = IsContentLoggingEnabled ? t.FunctionArguments : null,
}
}) : null
}
};
var eventBodyString = JsonSerializer.Serialize(eventBody);
List<KeyValuePair<string, object?>> state =
[
new KeyValuePair<string, object?>("event.name", "gen_ai.choice"),
new KeyValuePair<string, object?>("gen_ai.system", "openai"),
];
_logger.Log(LogLevel.Information, new EventId(1, "gen_ai.choice"), state, null, (s, e) => eventBodyString);
}
}
// a perfect future
private void LogChoiceFuture(ChatCompletion message)
{
List<KeyValuePair<string, object?>> attributes =
[
new KeyValuePair<string, object?>("gen_ai.system", "openai"),
];
_logger.Log(LogLevel.Information, new EventId(1, "gen_ai.choice"), attributes, eventBody);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment