refine llm prompt log

This commit is contained in:
Jicheng Lu 2023-12-19 22:20:08 -06:00
parent 7316c499be
commit 94f81cb173
3 changed files with 19 additions and 7 deletions

View file

@ -37,8 +37,10 @@ public static class StringExtensions
return input.Replace("\r", " ").Replace("\n", " ").Trim();
}
public static bool IsEqualTo(this string str1, string str2, StringComparison option = StringComparison.OrdinalIgnoreCase)
public static bool IsEqualTo(this string? str1, string? str2, StringComparison option = StringComparison.OrdinalIgnoreCase)
{
if (str1 == null) return str2 == null;
return str1.Equals(str2, option);
}

View file

@ -6,6 +6,8 @@ using BotSharp.Abstraction.Agents.Models;
using MongoDB.Driver;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Utilities;
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Core.Repository;
@ -860,10 +862,15 @@ public class FileRepository : IBotSharpRepository
#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{
if (log == null || string.IsNullOrEmpty(log.ConversationId)) return;
if (log == null) return;
log.ConversationId = log.ConversationId.IfNullOrEmptyAs(Guid.Empty.ToString());
var convDir = FindConversationDirectory(log.ConversationId);
if (string.IsNullOrEmpty(convDir)) return;
if (string.IsNullOrEmpty(convDir))
{
convDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, log.ConversationId);
Directory.CreateDirectory(convDir);
}
var logDir = Path.Combine(convDir, "llm_prompt_log");
if (!Directory.Exists(logDir))
@ -872,6 +879,7 @@ public class FileRepository : IBotSharpRepository
}
log.Id = Guid.NewGuid().ToString();
log.MessageId = log.MessageId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
var index = GetNextLlmCompletionLogIndex(logDir, log.MessageId);
var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log");
File.WriteAllText(file, JsonSerializer.Serialize(log, _options));

View file

@ -4,8 +4,10 @@ using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Abstraction.Utilities;
using BotSharp.Plugin.MongoStorage.Collections;
using BotSharp.Plugin.MongoStorage.Models;
using System.Reflection.Emit;
namespace BotSharp.Plugin.MongoStorage.Repository;
@ -892,13 +894,13 @@ public class MongoRepository : IBotSharpRepository
#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{
if (log == null || string.IsNullOrEmpty(log.ConversationId)) return;
if (log == null) return;
var completiongLog = new LlmCompletionLogDocument
{
Id = string.IsNullOrEmpty(log.Id) ? Guid.NewGuid().ToString() : log.Id,
ConversationId = log.ConversationId,
MessageId = log.MessageId,
Id = log.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString()),
ConversationId = log.ConversationId.IfNullOrEmptyAs(Guid.Empty.ToString()),
MessageId = log.MessageId.IfNullOrEmptyAs(Guid.NewGuid().ToString()),
AgentId = log.AgentId,
Prompt = log.Prompt,
Response = log.Response,