feat: Fix vector indexing stability, add Gitea linking, enhance admin dashboard
This commit is contained in:
@@ -175,21 +175,59 @@ class ChatLogger:
|
||||
# Get file size
|
||||
file_size = self.log_file.stat().st_size
|
||||
|
||||
# Count by character
|
||||
# Count stats
|
||||
character_counts = {}
|
||||
total_tokens = 0
|
||||
tokens_by_model = {}
|
||||
tokens_by_role = {}
|
||||
context_found_count = 0
|
||||
|
||||
# Simple pricing model (Blended average for OpenRouter)
|
||||
# Input: ~$5/M, Output: ~$15/M -> Avg ~$10/M = $0.00001 per token
|
||||
PRICE_PER_TOKEN = 0.00001
|
||||
|
||||
for line in lines:
|
||||
try:
|
||||
entry = json.loads(line)
|
||||
|
||||
# Character stats
|
||||
char_id = entry.get('character', {}).get('id', 'unknown')
|
||||
character_counts[char_id] = character_counts.get(char_id, 0) + 1
|
||||
|
||||
# Token stats
|
||||
tokens = entry.get('tokens_estimated', 0)
|
||||
total_tokens += tokens
|
||||
|
||||
# Model stats
|
||||
model = entry.get('ai', {}).get('model', 'unknown')
|
||||
tokens_by_model[model] = tokens_by_model.get(model, 0) + tokens
|
||||
|
||||
# Role stats
|
||||
tokens_by_role[char_id] = tokens_by_role.get(char_id, 0) + tokens
|
||||
|
||||
# RAG stats
|
||||
if entry.get('rag', {}).get('context_found'):
|
||||
context_found_count += 1
|
||||
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
total_interactions = len(lines)
|
||||
context_hit_rate = round((context_found_count / total_interactions * 100), 1) if total_interactions > 0 else 0
|
||||
estimated_cost = round(total_tokens * PRICE_PER_TOKEN, 4)
|
||||
|
||||
return {
|
||||
"total_interactions": len(lines),
|
||||
"total_interactions": total_interactions,
|
||||
"total_tokens_estimated": total_tokens,
|
||||
"estimated_cost_usd": estimated_cost,
|
||||
"context_found_count": context_found_count,
|
||||
"context_hit_rate_percent": context_hit_rate,
|
||||
"file_size_bytes": file_size,
|
||||
"file_size_mb": round(file_size / (1024 * 1024), 2),
|
||||
"characters": character_counts
|
||||
"characters": character_counts,
|
||||
"tokens_by_model": tokens_by_model,
|
||||
"tokens_by_role": tokens_by_role,
|
||||
"last_updated": datetime.utcnow().isoformat() + "Z"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
|
||||
@@ -106,7 +106,8 @@ class RAGChatService:
|
||||
character_name: str,
|
||||
character_prompt: str,
|
||||
context_limit: int = 3,
|
||||
lang: str = "de"
|
||||
lang: str = "de",
|
||||
model_override: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Answer a question using RAG with character persona.
|
||||
@@ -117,6 +118,7 @@ class RAGChatService:
|
||||
character_prompt: Character system prompt
|
||||
context_limit: Number of context chunks to retrieve
|
||||
lang: Language code
|
||||
model_override: Optional model name to override default
|
||||
|
||||
Returns:
|
||||
Dictionary with answer and sources
|
||||
@@ -156,7 +158,8 @@ class RAGChatService:
|
||||
# Generate answer with character persona
|
||||
answer = self._get_completion_with_system(
|
||||
system_message=system_message,
|
||||
user_message=question
|
||||
user_message=question,
|
||||
model_override=model_override
|
||||
)
|
||||
|
||||
# Format sources
|
||||
@@ -175,13 +178,14 @@ class RAGChatService:
|
||||
'sources': sources,
|
||||
'context_found': len(search_results) > 0,
|
||||
'provider': self.completion_provider.provider_name,
|
||||
'model': self.completion_provider.model_name
|
||||
'model': model_override or self.completion_provider.model_name
|
||||
}
|
||||
|
||||
def _get_completion_with_system(
|
||||
self,
|
||||
system_message: str,
|
||||
user_message: str
|
||||
user_message: str,
|
||||
model_override: Optional[str] = None
|
||||
) -> str:
|
||||
"""
|
||||
Get completion with explicit system message.
|
||||
@@ -190,6 +194,7 @@ class RAGChatService:
|
||||
Args:
|
||||
system_message: System prompt with character + context
|
||||
user_message: User's question
|
||||
model_override: Optional model name to use
|
||||
|
||||
Returns:
|
||||
Generated response
|
||||
@@ -200,7 +205,11 @@ class RAGChatService:
|
||||
|
||||
# Call completion provider
|
||||
# Note: We pass the system message as context
|
||||
# Assuming provider.get_completion supports model argument?
|
||||
# If not, we might need to check BaseProvider.
|
||||
# But get_completion usually takes keyword args passed to API.
|
||||
return self.completion_provider.get_completion(
|
||||
prompt=user_message,
|
||||
context=system_message
|
||||
context=system_message,
|
||||
model=model_override
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user