feat(agent): enhance token management and context handling in AgentEngine
- Added token usage tracking to the AgentEngine, allowing for better estimation of current context token counts. - Implemented a new method to estimate current tokens based on previous usage and newly appended messages, improving context management. - Updated context window management to utilize estimated token counts for more efficient message consolidation. - Enhanced streaming methods to include token usage information, providing better insights into LLM interactions. - Refactored the token estimator to support BPE tokenization, ensuring accurate token counts for messages. - Added unit tests to validate new token estimation and context management functionalities.
This commit is contained in:
@@ -87,7 +87,7 @@ Now generate the final answer:`, query)
|
||||
answerID := generateEventID("answer")
|
||||
logger.Debugf(ctx, "[Agent][FinalAnswer] AnswerID: %s", answerID)
|
||||
|
||||
fullAnswer, _, err := e.streamLLMToEventBus(
|
||||
llmResult, err := e.streamLLMToEventBus(
|
||||
ctx,
|
||||
messages,
|
||||
&chat.ChatOptions{Temperature: e.config.Temperature, Thinking: e.config.Thinking},
|
||||
@@ -95,7 +95,7 @@ Now generate the final answer:`, query)
|
||||
if chunk.Content != "" {
|
||||
logger.Debugf(ctx, "[Agent][FinalAnswer] Emitting answer chunk: %d chars", len(chunk.Content))
|
||||
e.eventBus.Emit(ctx, event.Event{
|
||||
ID: answerID, // Same ID for all chunks in this stream
|
||||
ID: answerID,
|
||||
Type: event.EventAgentFinalAnswer,
|
||||
SessionID: sessionID,
|
||||
Data: event.AgentFinalAnswerData{
|
||||
@@ -115,6 +115,7 @@ Now generate the final answer:`, query)
|
||||
return err
|
||||
}
|
||||
|
||||
fullAnswer := llmResult.Content
|
||||
logger.Infof(ctx, "[Agent][FinalAnswer] Final answer generated: %d characters", len(fullAnswer))
|
||||
common.PipelineInfo(ctx, "Agent", "final_answer_done", map[string]interface{}{
|
||||
"session_id": sessionID,
|
||||
|
||||
Reference in New Issue
Block a user