Finance_Agent / app.py
capi10's picture
Update app.py
97fe8f8 verified
Raw History Blame Contribute Delete
2.5 kB
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.")