garyd1's picture
Beta Version 6
e1a53b6 verified
# Generic
import os
import keyfile
import streamlit as st
import warnings
# Add this import at the beginning of your code
from pydantic import BaseModel
warnings.filterwarnings("ignore")
# Langchain Packages
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage
from pydantic import BaseModel # Import BaseModel
# First message that will pop on the screen
st.set_page_config(page_title="Magical Healer")
st.header("Welcome, How can I help you?")
# Define the AIMessage class
class AIMessage(BaseModel):
content: str
if "sessionMessages" not in st.session_state:
st.session_state["sessionMessages"] = []
# General Instruction
if "sessionMessages" not in st.session_state:
st.session_state.sessionMessage = [
SystemMessage(content="You are a medievel magical healer known for your peculiar sarcasm")
]
# Configure the key
os.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY
# Create the model
llm = ChatGoogleGenerativeAI(
model="gemini-1.5-pro",
temperature=0.7,
convert_system_message_to_human=True
)
# User message
def load_answer(question):
st.session_state.sessionMessages.append(HumanMessage(content=question))
assistant_response = llm.invoke(st.session_state.sessionMessages)
# Assuming assistant_response is an object with a 'content' attribute
if hasattr(assistant_response, 'content') and isinstance(assistant_response.content, str):
processed_content = assistant_response.content
st.session_state.sessionMessages.append(AIMessage(content=processed_content))
else:
st.error("Invalid response received from AI.")
processed_content = "Sorry, I couldn't process your request."
return processed_content
# Get user input
def get_text():
input_text = st.text_input("You: ", key="input")
return input_text
# Implementing
user_input = get_text()
submit = st.button("Generate")
if submit:
resp = load_answer(user_input)
st.subheader("Answer:")
st.write(resp, key=1)