Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions ProjectVG.Application/Models/Chat/ChatProcessContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public record ChatProcessContext
public Guid UserId { get; private set; }
public Guid CharacterId { get; private set; }
public string UserMessage { get; private set; } = string.Empty;
public string MemoryStore { get; private set; } = string.Empty;
public DateTime UserRequestAt { get; private set; } = DateTime.Now;
public bool UseTTS { get; private set; } = true;

Expand All @@ -27,7 +26,6 @@ public ChatProcessContext(ChatRequestCommand command)
UserId = command.UserId;
CharacterId = command.CharacterId;
UserMessage = command.UserPrompt;
MemoryStore = command.UserId.ToString();
UseTTS = command.UseTTS;
UserRequestAt = command.UserRequestAt;
}
Expand All @@ -42,7 +40,6 @@ public ChatProcessContext(
UserId = command.UserId;
CharacterId = command.CharacterId;
UserMessage = command.UserPrompt;
MemoryStore = command.UserId.ToString();
UseTTS = command.UseTTS;
UserRequestAt = command.UserRequestAt;

Expand All @@ -63,11 +60,15 @@ public void AddCost(double additionalCost)
Cost += additionalCost;
}

public IEnumerable<string> ParseConversationHistory(int count = 5)
public IEnumerable<ConversationHistory> ParseConversationHistory(int count = 10)
{
if (ConversationHistory == null) return Enumerable.Empty<string>();
if (ConversationHistory == null)
{
return Enumerable.Empty<ConversationHistory>();
}

return ConversationHistory.Take(count).Select(h => $"{h.Role}: {h.Content}");
var parsed = ConversationHistory.Take(count);
return parsed.ToList();
}

public string ToDebugString()
Expand Down
6 changes: 3 additions & 3 deletions ProjectVG.Application/Services/Chat/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ private async Task<ChatProcessContext> PrepareChatRequestAsync(ChatRequestComman
await _actionProcessor.ProcessAsync(command);

var characterInfo = await _characterService.GetCharacterByIdAsync(command.CharacterId);
var conversationHistoryContext = await _conversationService.GetConversationHistoryAsync(command.UserId, command.CharacterId, 10);

var conversationHistoryContext = await _conversationService.GetConversationHistoryAsync(command.UserId, command.CharacterId, 1, 10);

var memoryContext = await _memoryPreprocessor.CollectMemoryContextAsync(command);



return new ChatProcessContext(
command,
characterInfo,
Expand Down
13 changes: 10 additions & 3 deletions ProjectVG.Application/Services/Chat/Processors/ChatLLMProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ public async Task ProcessAsync(ChatProcessContext context)
{
var format = LLMFormatFactory.CreateChatFormat();

var conversationHistory = context.ParseConversationHistory()
.Select(h => new ProjectVG.Infrastructure.Integrations.LLMClient.Models.History
{
Role = h.Role,
Content = h.Content
})
.ToList();


var llmResponse = await _llmClient.CreateTextResponseAsync(
format.GetSystemMessage(context),
context.UserMessage,
format.GetInstructions(context),
context.ParseConversationHistory().ToList(),
conversationHistory,
model: format.Model,
maxTokens: format.MaxTokens,
temperature: format.Temperature
Expand All @@ -37,8 +46,6 @@ public async Task ProcessAsync(ChatProcessContext context)
var cost = format.CalculateCost(llmResponse.InputTokens, llmResponse.OutputTokens);
context.SetResponse(llmResponse.OutputText, segments, cost);

_logger.LogInformation("채팅 처리 결과: {Response}\n 세그먼트 생성 개수: {SementCount}\n 입력 토큰: {InputTokens}\n 출력 토큰: {OutputTokens}\n 비용: {Cost}",
llmResponse.OutputText, segments.Count, llmResponse.InputTokens, llmResponse.OutputTokens, cost);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ public ChatResultProcessor(

public async Task PersistResultsAsync(ChatProcessContext context)
{

await _conversationService.AddMessageAsync(context.UserId, context.CharacterId, ChatRole.User, context.UserMessage, context.UserRequestAt, context.RequestId.ToString());


await _conversationService.AddMessageAsync(context.UserId, context.CharacterId, ChatRole.Assistant, context.Response, DateTime.UtcNow, context.RequestId.ToString());

await PersistMemoryAsync(context);

_logger.LogDebug("채팅 결과 저장 완료: 세션 {UserId}, 사용자 {UserId}", context.RequestId, context.UserId);
}

private async Task PersistMemoryAsync(ChatProcessContext context)
Expand Down Expand Up @@ -89,7 +91,6 @@ private async Task PersistMemoryAsync(ChatProcessContext context)
await _memoryClient.InsertEpisodicAsync(userMemoryRequest);
await _memoryClient.InsertEpisodicAsync(episodicRequest);

_logger.LogDebug("메모리 삽입 성공: 사용자={UserId}, 캐릭터={CharacterId}", context.UserId, context.CharacterId);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public ConversationService(IConversationRepository conversationRepository, ILogg

public async Task<ConversationHistory> AddMessageAsync(Guid userId, Guid characterId, string role, string content, DateTime timestamp, string? conversationId = null)
{

if (string.IsNullOrWhiteSpace(content))
{
throw new ValidationException(ErrorCode.MESSAGE_EMPTY, content);
Expand All @@ -45,11 +46,13 @@ public async Task<ConversationHistory> AddMessageAsync(Guid userId, Guid charact
};

var addedMessage = await _conversationRepository.AddAsync(message);

return addedMessage;
}

public async Task<IEnumerable<ConversationHistory>> GetConversationHistoryAsync(Guid userId, Guid characterId, int page = 1, int pageSize = 10)
{

if (page <= 0)
{
throw new ValidationException(ErrorCode.VALIDATION_FAILED, $"Page must be greater than 0, but was: {page}");
Expand All @@ -61,6 +64,7 @@ public async Task<IEnumerable<ConversationHistory>> GetConversationHistoryAsync(
}

var history = await _conversationRepository.GetConversationHistoryAsync(userId, characterId, page, pageSize);

return history;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Task<LLMResponse> CreateTextResponseAsync(
string systemMessage,
string userMessage,
string? instructions = "",
List<string>? conversationHistory = default,
List<History>? conversationHistory = default,
string? model = "gpt-4o-mini",
int? maxTokens = 1000,
float? temperature = 0.7f);
Expand Down
4 changes: 2 additions & 2 deletions ProjectVG.Infrastructure/Integrations/LLMClient/LLMClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<LLMResponse> CreateTextResponseAsync(
string systemMessage,
string userMessage,
string? instructions = "",
List<string>? conversationHistory = default,
List<History>? conversationHistory = default,
string? model = "gpt-4o-mini",
int? maxTokens = 1000,
float? temperature = 0.7f)
Expand All @@ -76,7 +76,7 @@ public async Task<LLMResponse> CreateTextResponseAsync(
SystemPrompt = systemMessage,
UserPrompt = userMessage,
Instructions = instructions ?? "",
ConversationHistory = conversationHistory?.Select(msg => new History { Role = "user", Content = msg }).ToList() ?? new List<History>(),
ConversationHistory = conversationHistory ?? new List<History>(),
Model = model ?? "gpt-4o-mini",
MaxTokens = maxTokens ?? 1000,
Temperature = temperature ?? 0.7f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class VoiceSettings
/// 피치 레벨을 조절합니다. 0은 원래 음성 피치이며, ±12단계가 가능합니다. 1단계는 반음입니다.
/// </summary>
[JsonPropertyName("pitch_shift")]
public int PitchShift { get; set; } = 0;
public int PitchShift { get; set; } = -3;

/// <summary>
/// 음성 중 음조 변화의 정도를 조절합니다. 값이 작을수록 음조가 평탄해지고, 값이 클수록 음조가 풍부해집니다.
Expand All @@ -57,6 +57,6 @@ public class VoiceSettings
/// 음성 속도를 조절합니다. 값이 1보다 작으면 음성 속도가 느려지고, 값이 1보다 크면 음성 속도가 빨라집니다.
/// </summary>
[JsonPropertyName("speed")]
public float Speed { get; set; } = 1.2f;
public float Speed { get; set; } = 1.1f;
}
}
Loading