File size: 2,500 Bytes
e0bd987
081b4ef
e0bd987
 
97fe8f8
e0bd987
 
 
 
 
 
081b4ef
e0bd987
 
 
 
 
 
 
081b4ef
e0bd987
 
081b4ef
e0bd987
 
 
081b4ef
e0bd987
 
 
081b4ef
e0bd987
 
 
 
 
 
081b4ef
e0bd987
 
 
 
 
 
 
 
081b4ef
e0bd987
 
 
 
 
 
081b4ef
97fe8f8
 
 
 
e0bd987
081b4ef
 
e0bd987
081b4ef
 
e0bd987
081b4ef
 
 
 
 
 
 
 
 
 
e0bd987
 
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
import os
import time
import streamlit as st
from dotenv import load_dotenv
import datetime

from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
from langchain.memory import ConversationBufferMemory
from langchain.prompts import (
    SystemMessagePromptTemplate,
    MessagesPlaceholder,
    HumanMessagePromptTemplate,
)
from langchain.chains import LLMChain

# Load environment variables
load_dotenv()

# Language Model Processor class
class LanguageModelProcessor:
    def __init__(self):
        # Configure the model
        self.llm = ChatGroq(temperature=0, model_name="mixtral-8x7b-32768", groq_api_key=os.getenv("GROQ_API_KEY"))
        self.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

        # Load system prompt from file
        with open('system_prompt_en.txt', 'r') as file:
            system_prompt = file.read().strip()
        
        # Set up the chat prompt
        self.prompt = ChatPromptTemplate.from_messages([
            SystemMessagePromptTemplate.from_template(system_prompt),
            MessagesPlaceholder(variable_name="chat_history"),
            HumanMessagePromptTemplate.from_template("{text}")
        ])

        # Initialize the conversation chain
        self.conversation = LLMChain(
            llm=self.llm,
            prompt=self.prompt,
            memory=self.memory
        )

    def process(self, text):
        start_time = time.time()
        # Invoke the language model
        response = self.conversation.invoke({"text": text})
        end_time = time.time()

        elapsed_time = int((end_time - start_time) * 1000)
        return response['text']

# Streamlit UI Configuration
today = datetime.date.today()

##Put the title of the app Finace agent of todays market and put the variable of today
st.title(f'{today} Finance Agent of Today Market')

# Initialize the language model processor
llm_processor = LanguageModelProcessor()

# User input area
user_input = st.text_area("Enter your message:", placeholder="Type your prompt here...", height=150)

# Button to send the message
if st.button("Send"):
    if user_input.strip() != "":
        # Process the response from the model
        response = llm_processor.process(user_input)
        
        # Display the model's response in a larger text area
        st.text_area("Model Response:", value=response, height=300, disabled=True)
    else:
        st.warning("Please enter a message before sending.")