Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from groq import Groq
|
4 |
+
import random
|
5 |
+
|
6 |
+
from langchain.chains import ConversationChain
|
7 |
+
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
8 |
+
from langchain_groq import ChatGroq
|
9 |
+
from langchain.prompts import PromptTemplate
|
10 |
+
|
11 |
+
|
12 |
+
def main():
|
13 |
+
"""
|
14 |
+
This function is the main entry point of the application. It sets up the Groq client, the Streamlit interface, and handles the chat interaction.
|
15 |
+
"""
|
16 |
+
|
17 |
+
# Get Groq API key
|
18 |
+
groq_api_key = os.environ['GROQ_API_KEY']
|
19 |
+
|
20 |
+
# Display the Groq logo
|
21 |
+
spacer, col = st.columns([5, 1])
|
22 |
+
with col:
|
23 |
+
st.image('groqcloud_darkmode.png')
|
24 |
+
|
25 |
+
# The title and greeting message of the Streamlit application
|
26 |
+
st.title("Chat with Groq!")
|
27 |
+
st.write("Hello! I'm your friendly Groq chatbot. I can help answer your questions, provide information, or just chat. I'm also super fast! Let's start our conversation!")
|
28 |
+
|
29 |
+
# Add customization options to the sidebar
|
30 |
+
st.sidebar.title('Customization')
|
31 |
+
model = st.sidebar.selectbox(
|
32 |
+
'Choose a model',
|
33 |
+
['mixtral-8x7b-32768', 'llama2-70b-4096']
|
34 |
+
)
|
35 |
+
conversational_memory_length = st.sidebar.slider('Conversational memory length:', 1, 10, value = 5)
|
36 |
+
|
37 |
+
memory=ConversationBufferWindowMemory(k=conversational_memory_length)
|
38 |
+
|
39 |
+
user_question = st.text_input("Ask a question:")
|
40 |
+
|
41 |
+
# session state variable
|
42 |
+
if 'chat_history' not in st.session_state:
|
43 |
+
st.session_state.chat_history=[]
|
44 |
+
else:
|
45 |
+
for message in st.session_state.chat_history:
|
46 |
+
memory.save_context({'input':message['human']},{'output':message['AI']})
|
47 |
+
|
48 |
+
|
49 |
+
# Initialize Groq Langchain chat object and conversation
|
50 |
+
groq_chat = ChatGroq(
|
51 |
+
groq_api_key=groq_api_key,
|
52 |
+
model_name=model
|
53 |
+
)
|
54 |
+
|
55 |
+
conversation = ConversationChain(
|
56 |
+
llm=groq_chat,
|
57 |
+
memory=memory
|
58 |
+
)
|
59 |
+
|
60 |
+
# If the user has asked a question,
|
61 |
+
if user_question:
|
62 |
+
|
63 |
+
# The chatbot's answer is generated by sending the full prompt to the Groq API.
|
64 |
+
response = conversation(user_question)
|
65 |
+
message = {'human':user_question,'AI':response['response']}
|
66 |
+
st.session_state.chat_history.append(message)
|
67 |
+
st.write("Chatbot:", response['response'])
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
main()
|