File size: 3,595 Bytes
9700f95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# src/utils/conversation_manager.py
from typing import List, Dict, Optional
import tiktoken
from datetime import datetime

class ConversationManager:
    def __init__(
        self,
        max_tokens: int = 4000,
        max_messages: int = 10,
        model: str = "gpt-3.5-turbo"
    ):
        """
        Initialize conversation manager
        
        Args:
            max_tokens (int): Maximum tokens to keep in context
            max_messages (int): Maximum number of messages to keep
            model (str): Model name for token counting
        """
        self.max_tokens = max_tokens
        self.max_messages = max_messages
        self.encoding = tiktoken.encoding_for_model(model)
        
    def format_messages(self, messages: List[Dict]) -> str:
        """Format messages into a conversation string"""
        formatted = []
        for msg in messages:
            role = msg.get('role', 'unknown')
            content = msg.get('content', '')
            formatted.append(f"{role.capitalize()}: {content}")
        return "\n".join(formatted)
    
    def count_tokens(self, text: str) -> int:
        """Count tokens in text"""
        return len(self.encoding.encode(text))
    
    def get_relevant_history(
        self,
        messages: List[Dict],
        current_query: str,
        max_tokens: Optional[int] = None
    ) -> List[Dict]:
        """
        Get relevant conversation history within token limit
        
        Args:
            messages (List[Dict]): Full message history
            current_query (str): Current user query
            max_tokens (Optional[int]): Override default max tokens
            
        Returns:
            List[Dict]: Relevant message history
        """
        max_tokens = max_tokens or self.max_tokens
        current_tokens = self.count_tokens(current_query)
        
        # Keep track of tokens and messages
        history = []
        total_tokens = current_tokens
        
        # Process messages from most recent to oldest
        for msg in reversed(messages[-self.max_messages:]):
            msg_text = f"{msg['role']}: {msg['content']}\n"
            msg_tokens = self.count_tokens(msg_text)
            
            # Check if adding this message would exceed token limit
            if total_tokens + msg_tokens > max_tokens:
                break
                
            total_tokens += msg_tokens
            history.append(msg)
        
        # Reverse back to chronological order
        return list(reversed(history))
    
    def generate_prompt_with_history(
        self,
        current_query: str,
        history: List[Dict],
        context_docs: List[str]
    ) -> str:
        """
        Generate a prompt that includes conversation history and context
        
        Args:
            current_query (str): Current user query
            history (List[Dict]): Relevant conversation history
            context_docs (List[str]): Retrieved context documents
            
        Returns:
            str: Formatted prompt
        """
        # Format conversation history
        conversation_context = self.format_messages(history)
        
        # Format context documents
        context_str = "\n\n".join(context_docs)
        
        prompt = f"""
Previous Conversation:
{conversation_context}

Relevant Context:
{context_str}

Current Query: {current_query}

Based on the previous conversation and the provided context, please provide a comprehensive and accurate response that maintains continuity with the conversation history."""
        
        return prompt