remove message id in state history

add isNeedVersion
This commit is contained in:
Jicheng Lu 2024-01-01 13:53:05 -06:00
parent a534a93d35
commit a523cf5047
5 changed files with 12 additions and 23 deletions

View file

@ -12,7 +12,7 @@ public interface IConversationStateService
string GetState(string name, string defaultValue = "");
bool ContainsState(string name);
ConversationState GetStates();
IConversationStateService SetState<T>(string name, T value);
IConversationStateService SetState<T>(string name, T value, bool isNeedVersion = true);
void SaveStateByArgs(JsonDocument args);
void CleanState();
void Save();

View file

@ -19,7 +19,6 @@ public class HistoryStateKeyValue
public class HistoryStateValue
{
public string? MessageId { get; set; }
public string Data { get; set; }
public DateTime UpdateTime { get; set; }

View file

@ -34,9 +34,9 @@ public class ConversationStateService : IConversationStateService, IDisposable
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="isConst">whether the state is related to message or not</param>
/// <param name="isNeedVersion">whether the state is related to message or not</param>
/// <returns></returns>
public IConversationStateService SetState<T>(string name, T value)
public IConversationStateService SetState<T>(string name, T value, bool isNeedVersion = true)
{
if (value == null)
{
@ -58,17 +58,18 @@ public class ConversationStateService : IConversationStateService, IDisposable
var historyStateValue = new HistoryStateValue
{
MessageId = GetCurrentMessageId(),
Data = currentValue,
UpdateTime = DateTime.UtcNow
};
if (!_historyStates.ContainsKey(name))
if (!_historyStates.ContainsKey(name) || !isNeedVersion)
{
_historyStates[name] = new List<HistoryStateValue>();
_historyStates[name] = new List<HistoryStateValue> { historyStateValue };
}
else
{
_historyStates[name].Add(historyStateValue);
}
_historyStates[name].Add(historyStateValue);
}
return this;
@ -164,12 +165,4 @@ public class ConversationStateService : IConversationStateService, IDisposable
}
}
}
private string? GetCurrentMessageId()
{
if (string.IsNullOrEmpty(_conversationId)) return null;
var dialogs = _db.GetConversationDialogs(_conversationId);
return dialogs.LastOrDefault()?.MetaData?.MessageId;
}
}

View file

@ -47,14 +47,14 @@ public class TokenStatistics : ITokenStatistics
// Accumulated Token
var stat = _services.GetRequiredService<IConversationStateService>();
var inputCount = int.Parse(stat.GetState("prompt_total", "0"));
stat.SetState("prompt_total", stats.PromptCount + inputCount);
stat.SetState("prompt_total", stats.PromptCount + inputCount, false);
var outputCount = int.Parse(stat.GetState("completion_total", "0"));
stat.SetState("completion_total", stats.CompletionCount + outputCount);
stat.SetState("completion_total", stats.CompletionCount + outputCount, false);
// Total cost
var total_cost = float.Parse(stat.GetState("llm_total_cost", "0"));
total_cost += Cost;
stat.SetState("llm_total_cost", total_cost);
stat.SetState("llm_total_cost", total_cost, false);
}
public void PrintStatistics()

View file

@ -28,7 +28,6 @@ public class StateMongoElement
public class StateValueMongoElement
{
public string? MessageId { get; set; }
public string Data { get; set; }
public DateTime UpdateTime { get; set; }
@ -36,7 +35,6 @@ public class StateValueMongoElement
{
return new StateValueMongoElement
{
MessageId = element.MessageId,
Data = element.Data,
UpdateTime = element.UpdateTime
};
@ -46,7 +44,6 @@ public class StateValueMongoElement
{
return new HistoryStateValue
{
MessageId = element.MessageId,
Data = element.Data,
UpdateTime = element.UpdateTime
};