Spaces:
Sleeping
Sleeping
from datetime import datetime | |
import streamlit as st | |
import os | |
from mistralai import Mistral | |
# Function to get the current year | |
def current_year(): | |
now = datetime.now() | |
return now.year | |
st.set_page_config(layout="wide") | |
st.title("Just chat! π€") | |
# Sidebar inputs for MISTRAL_API_ID and MISTRAL_API_KEY | |
with st.sidebar: | |
st.header("Mistral API Settings") | |
mistral_api_id = st.text_input("Enter your MISTRAL API ID", placeholder="Enter API ID") | |
api_key = st.text_input("Enter your MISTRAL API Key", type="password", placeholder="Enter API Key") | |
with st.expander("Instruction Manual"): | |
st.markdown(""" | |
## Mistral AI Agent π€ Chatbot | |
This Streamlit app allows you to chat with Mistral AI Coder Agent. | |
### How to Use: | |
1. **Input**: Type your prompt into the chat input box labeled "What is up?". | |
2. **Response**: The app will display a response from Mistral AI. | |
3. **Chat History**: Previous conversations will be shown on the app. | |
### Credits: | |
- **Developer**: [Yiqiao Yin](https://www.y-yin.io/) | [App URL](https://huggingface.co/spaces/eagle0504/gpt-4o-demo) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/) | |
Enjoy chatting with Mistral AI agent! | |
""") | |
with st.expander("Examples"): | |
st.success("Example: Write a simple hello world flask app for me.") | |
st.success("Example: Write a program to simulate the value of pi.") | |
st.success("Example: Write a 3-layer neural network for me?") | |
# Button to clear session state | |
if st.button("Clear Session"): | |
st.session_state.messages = [] | |
st.rerun() | |
# Initialize chat history | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
# Ensure messages are a list of dictionaries | |
if not isinstance(st.session_state.messages, list): | |
st.session_state.messages = [] | |
if not all(isinstance(msg, dict) for msg in st.session_state.messages): | |
st.session_state.messages = [] | |
# Display chat messages from history on app rerun | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.markdown(message["content"]) | |
# React to user input | |
if prompt := st.chat_input("π Ask any question or feel free to use the examples provided in the left sidebar."): | |
# Display user message in chat message container | |
st.chat_message("user").markdown(prompt) | |
# Add user message to chat history | |
st.session_state.messages.append({"role": "assistant", "content": f"You are a helpful assistant. Year now is {current_year()}"}) | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
# API Call | |
if mistral_api_id and api_key: # Ensure both API ID and Key are entered | |
bot = ChatBot(api_key) | |
bot.history = st.session_state.messages.copy() # Update history from messages | |
response = bot.generate_response( | |
agent_id=mistral_api_id, | |
user_message=prompt | |
) | |
# Display assistant response in chat message container | |
with st.chat_message("assistant"): | |
st.markdown(response) | |
# Add assistant response to chat history | |
st.session_state.messages.append({"role": "assistant", "content": response}) | |
else: | |
st.error("Please enter both MISTRAL API ID and API Key in the sidebar.") | |