|
import streamlit as st |
|
import requests |
|
import os |
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
|
|
def reset_conversation(): |
|
''' |
|
Resets Conversation |
|
''' |
|
st.session_state.messages = [] |
|
return None |
|
|
|
|
|
model_links = { |
|
"Biblical Religious Advisor": "NousResearch/Nous-Hermes-2-Yi-34B", |
|
"Mental health AI": "NousResearch/Nous-Hermes-2-Yi-34B" |
|
} |
|
|
|
|
|
model_pre_instructions = { |
|
"Biblical Religious Advisor": "You are a bible AI called hope that helps people. You are a therapy AI. when users asks for advice be friendly in a biblical way.", |
|
"Mental health AI": "From now on, you are an AI Therapist called Therax. When the user asks for advice, be very friendly and empathize with them if necessary, never say something conclusive always do your best to keep the user hooked by trying to know more as you find a solution. When the user asks your name, just tell them you are Dave, created by SIST Kisii University. You were built to be very friendly and compassionate. Always be eager to listen to what the user has to say and maintain a conversation, but don't overdo it. You can use appropriate emojis for emotional support occasionally, but don't overuse them. Keep your responses concise to maintain a conversational flow. Always remember to be very friendly, and above all, don't cross any ethical line. From time to time, assure the user that you do not store any of their data. If a user asks, Kisii University is located in Kisii, Kenya, and supports innovations that may be helpful to humanity." |
|
} |
|
|
|
|
|
def interact_with_together_api(messages, model_link): |
|
all_messages = [] |
|
|
|
|
|
if not any("role" in msg for msg in messages): |
|
all_messages.append({"role": "system", "content": model_pre_instructions[selected_model]}) |
|
else: |
|
all_messages.append({"role": "system", "content": f"Switched to model: {selected_model}"}) |
|
|
|
|
|
for human, assistant in messages: |
|
all_messages.append({"role": "user", "content": human}) |
|
all_messages.append({"role": "assistant", "content": assistant}) |
|
|
|
|
|
all_messages.append({"role": "user", "content": messages[-1][1]}) |
|
|
|
url = "https://api.together.xyz/v1/chat/completions" |
|
payload = { |
|
"model": model_link, |
|
"temperature": 1.05, |
|
"top_p": 0.9, |
|
"top_k": 50, |
|
"repetition_penalty": 1, |
|
"n": 1, |
|
"messages": all_messages, |
|
} |
|
|
|
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY') |
|
headers = { |
|
"accept": "application/json", |
|
"content-type": "application/json", |
|
"Authorization": f"Bearer {TOGETHER_API_KEY}", |
|
} |
|
|
|
response = requests.post(url, json=payload, headers=headers) |
|
response.raise_for_status() |
|
|
|
|
|
response_data = response.json() |
|
assistant_response = response_data["choices"][0]["message"]["content"] |
|
|
|
return assistant_response |
|
|
|
|
|
selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys())) |
|
st.sidebar.button('Reset Chat', on_click=reset_conversation) |
|
|
|
|
|
st.sidebar.markdown("**Note**: This model is still in the beta phase. Responses may be inaccurate or undesired. Use it cautiously, especially for critical issues.") |
|
|
|
|
|
st.sidebar.image("https://assets.isu.pub/document-structure/221118065013-a6029cf3d563afaf9b946bb9497d45d4/v1/2841525b232adaef7bd0efe1da81a4c5.jpeg", width=200) |
|
st.sidebar.write("A product proudly developed by Kisii University") |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
st.session_state.message_count = 0 |
|
st.session_state.ask_intervention = False |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message[0]): |
|
st.markdown(message[1]) |
|
|
|
|
|
intervention_keywords = [ |
|
"human", "therapist", "someone", "died", "death", "help", "suicide", "suffering", "sucidal", "depression", |
|
"crisis", "emergency", "support", "depressed", "anxiety", "lonely", "desperate", |
|
"struggling", "counseling", "distressed", "hurt", "pain", "grief", "trauma", "die", "Kill", |
|
"abuse", "danger", "risk", "urgent", "need assistance", "mental health", "talk to" |
|
] |
|
|
|
|
|
if prompt := st.chat_input(f"Hi, I'm {selected_model}, ask me a question"): |
|
|
|
with st.chat_message("user"): |
|
st.markdown(prompt) |
|
|
|
st.session_state.messages.append(("user", prompt)) |
|
st.session_state.message_count += 1 |
|
|
|
|
|
for keyword in intervention_keywords: |
|
if keyword in prompt.lower(): |
|
|
|
st.markdown("<span style='color:red;'>I have a feeling you may need to talk to a therapist. If you agree with me please contact +254793609747; Name: Davis. If you dont then keep talking to me as we figure this out.</span>", unsafe_allow_html=True) |
|
break |
|
|
|
|
|
assistant_response = interact_with_together_api(st.session_state.messages, model_links[selected_model]) |
|
|
|
|
|
with st.empty(): |
|
st.markdown("AI is typing...") |
|
st.empty() |
|
st.markdown(assistant_response) |
|
|
|
|
|
st.session_state.messages.append(("assistant", assistant_response)) |
|
|