Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,22 @@
|
|
1 |
from datetime import datetime
|
2 |
-
|
3 |
import streamlit as st
|
4 |
import os
|
5 |
from mistralai import Mistral
|
6 |
|
7 |
-
|
8 |
-
api_key = os.environ["MISTRAL_API_KEY"]
|
9 |
-
|
10 |
-
class ChatBot:
|
11 |
-
def __init__(self, api_key):
|
12 |
-
self.client = Mistral(api_key=api_key)
|
13 |
-
self.history = []
|
14 |
-
|
15 |
-
def generate_response(self, agent_id, user_message):
|
16 |
-
# Add the user's message to the history
|
17 |
-
self.history.append({
|
18 |
-
"role": "user",
|
19 |
-
"content": user_message
|
20 |
-
})
|
21 |
-
|
22 |
-
# Send the message to the API and get the response
|
23 |
-
chat_response = self.client.agents.complete(
|
24 |
-
agent_id=agent_id,
|
25 |
-
messages=self.history,
|
26 |
-
)
|
27 |
-
|
28 |
-
# Extract the AI's response and add it to the history
|
29 |
-
ai_response = chat_response.choices[0].message.content
|
30 |
-
self.history.append({
|
31 |
-
"role": "assistant",
|
32 |
-
"content": ai_response
|
33 |
-
})
|
34 |
-
|
35 |
-
# Return the AI's response
|
36 |
-
return ai_response
|
37 |
-
|
38 |
-
def get_history(self):
|
39 |
-
return self.history
|
40 |
-
|
41 |
-
|
42 |
-
# Credit: Time
|
43 |
def current_year():
|
44 |
now = datetime.now()
|
45 |
return now.year
|
46 |
|
47 |
-
|
48 |
st.set_page_config(layout="wide")
|
49 |
st.title("Just chat! π€")
|
50 |
|
51 |
-
|
52 |
with st.sidebar:
|
|
|
|
|
|
|
|
|
53 |
with st.expander("Instruction Manual"):
|
54 |
st.markdown("""
|
55 |
## Mistral AI Agent π€ Chatbot
|
@@ -63,27 +30,16 @@ with st.sidebar:
|
|
63 |
Enjoy chatting with Mistral AI agent!
|
64 |
""")
|
65 |
|
66 |
-
# Example:
|
67 |
with st.expander("Examples"):
|
68 |
st.success("Example: Write a simple hello world flask app for me.")
|
69 |
st.success("Example: Write a program to simulate the value of pi.")
|
70 |
st.success("Example: Write a 3-layer neural network for me?")
|
71 |
|
72 |
-
#
|
73 |
if st.button("Clear Session"):
|
74 |
st.session_state.messages = []
|
75 |
st.rerun()
|
76 |
|
77 |
-
# Credit:
|
78 |
-
# current_year = current_year() # This will print the current year
|
79 |
-
# st.markdown(
|
80 |
-
# f"""
|
81 |
-
# <h6 style='text-align: left;'>Copyright Β© 2010-{current_year} Present Yiqiao Yin</h6>
|
82 |
-
# """,
|
83 |
-
# unsafe_allow_html=True,
|
84 |
-
# )
|
85 |
-
|
86 |
-
|
87 |
# Initialize chat history
|
88 |
if "messages" not in st.session_state:
|
89 |
st.session_state.messages = []
|
@@ -106,20 +62,23 @@ if prompt := st.chat_input("π Ask any question or feel free to use the exampl
|
|
106 |
st.chat_message("user").markdown(prompt)
|
107 |
|
108 |
# Add user message to chat history
|
109 |
-
st.session_state.messages.append({"role": "assistant", "content": f"You are a helpful assistant. Year now is {current_year}"})
|
110 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
111 |
|
112 |
# API Call
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
|
124 |
-
|
125 |
-
|
|
|
|
|
|
1 |
from datetime import datetime
|
|
|
2 |
import streamlit as st
|
3 |
import os
|
4 |
from mistralai import Mistral
|
5 |
|
6 |
+
# Function to get the current year
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def current_year():
|
8 |
now = datetime.now()
|
9 |
return now.year
|
10 |
|
|
|
11 |
st.set_page_config(layout="wide")
|
12 |
st.title("Just chat! π€")
|
13 |
|
14 |
+
# Sidebar inputs for MISTRAL_API_ID and MISTRAL_API_KEY
|
15 |
with st.sidebar:
|
16 |
+
st.header("Mistral API Settings")
|
17 |
+
mistral_api_id = st.text_input("Enter your MISTRAL API ID", placeholder="Enter API ID")
|
18 |
+
api_key = st.text_input("Enter your MISTRAL API Key", type="password", placeholder="Enter API Key")
|
19 |
+
|
20 |
with st.expander("Instruction Manual"):
|
21 |
st.markdown("""
|
22 |
## Mistral AI Agent π€ Chatbot
|
|
|
30 |
Enjoy chatting with Mistral AI agent!
|
31 |
""")
|
32 |
|
|
|
33 |
with st.expander("Examples"):
|
34 |
st.success("Example: Write a simple hello world flask app for me.")
|
35 |
st.success("Example: Write a program to simulate the value of pi.")
|
36 |
st.success("Example: Write a 3-layer neural network for me?")
|
37 |
|
38 |
+
# Button to clear session state
|
39 |
if st.button("Clear Session"):
|
40 |
st.session_state.messages = []
|
41 |
st.rerun()
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
# Initialize chat history
|
44 |
if "messages" not in st.session_state:
|
45 |
st.session_state.messages = []
|
|
|
62 |
st.chat_message("user").markdown(prompt)
|
63 |
|
64 |
# Add user message to chat history
|
65 |
+
st.session_state.messages.append({"role": "assistant", "content": f"You are a helpful assistant. Year now is {current_year()}"})
|
66 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
67 |
|
68 |
# API Call
|
69 |
+
if mistral_api_id and api_key: # Ensure both API ID and Key are entered
|
70 |
+
bot = ChatBot(api_key)
|
71 |
+
bot.history = st.session_state.messages.copy() # Update history from messages
|
72 |
+
response = bot.generate_response(
|
73 |
+
agent_id=mistral_api_id,
|
74 |
+
user_message=prompt
|
75 |
+
)
|
76 |
|
77 |
+
# Display assistant response in chat message container
|
78 |
+
with st.chat_message("assistant"):
|
79 |
+
st.markdown(response)
|
80 |
|
81 |
+
# Add assistant response to chat history
|
82 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
83 |
+
else:
|
84 |
+
st.error("Please enter both MISTRAL API ID and API Key in the sidebar.")
|