-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeepseek-groq-streamlit.py
130 lines (108 loc) · 4.78 KB
/
deepseek-groq-streamlit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage
import streamlit as st
import os
# Set up Streamlit page
st.title("🚀 Deepseek-R1 Llama-70B Chat")
st.write("⚡ Blazing-fast Thinking Model powered by Groq ❤️")
# Add sidebar for API key input and details
with st.sidebar:
st.header("⚙️ Configuration")
# Store API key directly in session state
st.session_state.groq_api_key = st.text_input("GROQ API Key", type="password")
# Add model details section
st.divider()
st.markdown("**Model Details**")
st.caption("Running: `deepseek-r1-distill-llama-70b`")
st.caption("Groq LPU Inference Engine")
# Add New Chat button
st.divider()
if st.button("🔄 Start New Chat", use_container_width=True):
st.session_state.messages = [
SystemMessage(content="You are a helpful AI assistant.")
]
st.rerun()
# Add branding with hyperlink
st.divider()
st.markdown(
"**Built by** [Build Fast with AI](https://buildfastwithai.com/genai-course)",
unsafe_allow_html=True
)
# Display welcome message in chat format
with st.chat_message("assistant"):
st.write("Ask me anything!")
# Initialize chat history in session state if it doesn't exist
if "messages" not in st.session_state:
st.session_state.messages = [
SystemMessage(content="You are a helpful AI assistant.")
]
# Display chat history
for message in st.session_state.messages[1:]: # Skip the system message
if isinstance(message, HumanMessage):
with st.chat_message("user"):
st.write(message.content)
else:
with st.chat_message("assistant"):
content = message.content
if "<think>" in content and "</think>" in content:
# Extract thinking content
think_start = content.index("<think>")
think_end = content.index("</think>") + len("</think>")
thinking = content[think_start:think_end]
actual_response = content[think_end:].strip()
# Display thinking in expandable section
with st.expander("Show AI thinking process"):
st.write(thinking)
# Display actual response
st.write(actual_response)
else:
st.write(content)
# Chat input
if prompt := st.chat_input("What's on your mind?"):
# Check for API key in the input field directly
if not st.session_state.groq_api_key:
st.error("Please enter your GROQ API key in the sidebar")
st.stop()
# Initialize the ChatOpenAI model with Groq
chat = ChatOpenAI(
model="deepseek-r1-distill-llama-70b",
openai_api_key=st.session_state.groq_api_key,
openai_api_base="https://api.groq.com/openai/v1"
)
# Add user message to chat history
st.session_state.messages.append(HumanMessage(content=prompt))
# Display user message
with st.chat_message("user"):
st.write(prompt)
# Get AI response
with st.chat_message("assistant"):
thinking_placeholder = st.empty() # Place thinking placeholder first
message_placeholder = st.empty() # Then message placeholder
full_response = ""
# Stream the response
for chunk in chat.stream(st.session_state.messages):
if chunk.content:
full_response += chunk.content
# Only update message placeholder during streaming if no thinking tags detected yet
if "<think>" not in full_response:
message_placeholder.write(full_response)
# After streaming is complete, check for thinking tags
if "<think>" in full_response and "</think>" in full_response:
# Extract thinking content
think_start = full_response.index("<think>")
think_end = full_response.index("</think>") + len("</think>")
thinking = full_response[think_start:think_end]
# Extract actual response
actual_response = full_response[think_end:].strip()
# First display thinking in expandable section
with thinking_placeholder:
with st.expander("Show AI thinking process"):
st.write(thinking)
# Then display actual response
message_placeholder.write(actual_response)
else:
# If no thinking tags, clear thinking placeholder and show full response
thinking_placeholder.empty()
message_placeholder.write(full_response)
# Add AI response to chat history
st.session_state.messages.append(AIMessage(content=full_response))