|
import streamlit as st
|
|
from openai import OpenAI
|
|
|
|
|
|
client = OpenAI(api_key='up_CpZjdAyzHNZSbSTAQWVqYuJGgQNJW', base_url='https://api.upstage.ai/v1/solar')
|
|
|
|
|
|
st.set_page_config(page_title='Chat with me π', layout='wide')
|
|
st.title("Chat with me π")
|
|
|
|
|
|
if 'messages' not in st.session_state:
|
|
st.session_state.messages = [
|
|
{'role': 'system', 'content': 'You are a helpful friend that someone can depend on and want to chat with, you provides positive, optimistic, uplifting, and motivational suggestions and advices filled with wisdom, encouragement and hope to uplift and inspire people.'}
|
|
]
|
|
|
|
def generate_response(prompt: str) -> str:
|
|
response = client.chat.completions.create(
|
|
model="solar-pro",
|
|
messages=st.session_state.messages + [{'role': 'user', 'content': prompt}],
|
|
temperature=0.7,
|
|
max_tokens=200
|
|
)
|
|
|
|
return response.choices[0].message.content
|
|
|
|
|
|
for message in st.session_state.messages[1:]:
|
|
with st.chat_message(message['role']):
|
|
st.write(message['content'])
|
|
|
|
|
|
user_input = st.chat_input("I am waiting to hear from you π")
|
|
|
|
if user_input:
|
|
if user_input.lower() in ['quit', 'exit', 'escape', 'out', 'ex']:
|
|
st.session_state.messages = [st.session_state.messages[0]]
|
|
st.success("Thanks for chatting with me!")
|
|
else:
|
|
|
|
st.session_state.messages.append({'role': 'user', 'content': user_input})
|
|
|
|
|
|
with st.chat_message("user"):
|
|
st.write(user_input)
|
|
|
|
|
|
response = generate_response(user_input)
|
|
|
|
|
|
st.session_state.messages.append({'role': 'assistant', 'content': response})
|
|
|
|
|
|
with st.chat_message("assistant"):
|
|
st.write(response)
|
|
|
|
|