File size: 1,502 Bytes
54db4e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Chat helper classes to replace LangChain components.
"""
import streamlit as st


class ChatMessage:
    """Base class for chat messages."""
    
    def __init__(self, content: str, role: str):
        self.content = content
        self.role = role
        self.type = role  # For compatibility with existing code


class HumanMessage(ChatMessage):
    """Message from human user."""
    
    def __init__(self, content: str):
        super().__init__(content, 'user')


class AIMessage(ChatMessage):
    """Message from AI assistant."""
    
    def __init__(self, content: str):
        super().__init__(content, 'ai')


class StreamlitChatMessageHistory:
    """Chat message history stored in Streamlit session state."""
    
    def __init__(self, key: str):
        self.key = key
        if key not in st.session_state:
            st.session_state[key] = []
    
    @property
    def messages(self):
        return st.session_state[self.key]
    
    def add_user_message(self, content: str):
        st.session_state[self.key].append(HumanMessage(content))
    
    def add_ai_message(self, content: str):
        st.session_state[self.key].append(AIMessage(content))


class ChatPromptTemplate:
    """Template for chat prompts."""
    
    def __init__(self, template: str):
        self.template = template
    
    @classmethod
    def from_template(cls, template: str):
        return cls(template)
    
    def format(self, **kwargs):
        return self.template.format(**kwargs)