### Install Dependencies and Setup Groq API Key Source: https://context7.com/zwelshman/chat_streamlit/llms.txt Installs necessary Python packages and configures the Groq API key using Streamlit's secrets management. ```bash pip install streamlit groq mkdir -p .streamlit echo 'GROQ_API_KEY = "your-groq-api-key-here"' > .streamlit/secrets.toml streamlit run app.py ``` -------------------------------- ### Initialize Groq Client with Streamlit Secrets Source: https://context7.com/zwelshman/chat_streamlit/llms.txt Initializes the Groq client using the API key securely stored in Streamlit's secrets.toml file. ```python import streamlit as st from groq import Groq client = Groq(api_key=st.secrets["GROQ_API_KEY"]) ``` -------------------------------- ### Generate AI Response with DeepSeek-R1 via Groq API Source: https://context7.com/zwelshman/chat_streamlit/llms.txt Generates an AI response using the DeepSeek-R1 model through the Groq chat completions API. ```python from groq import Groq import streamlit as st client = Groq(api_key=st.secrets["GROQ_API_KEY"]) response = client.chat.completions.create( model="deepseek-r1-distill-llama-70b", messages=[ {"role": "user", "content": "Explain quantum computing in simple terms"} ], stream=False ) full_response = response.choices[0].message.content print(full_response) ``` -------------------------------- ### Configure Streamlit Page Title and Icon Source: https://context7.com/zwelshman/chat_streamlit/llms.txt Sets the browser tab title and the main title displayed on the Streamlit application page. ```python import streamlit as st st.set_page_config(page_title="Free DeepSeek Chat", page_icon="🤖") st.title("DeepSeek-R1 on Groq (Free)") ``` -------------------------------- ### Handle User Input with Streamlit Chat Input Source: https://context7.com/zwelshman/chat_streamlit/llms.txt Captures user input using Streamlit's chat input widget and adds it to the conversation history and UI. ```python import streamlit as st if prompt := st.chat_input("Ask DeepSeek anything..."): st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt) ``` -------------------------------- ### Manage Chat History with Streamlit Session State Source: https://context7.com/zwelshman/chat_streamlit/llms.txt Initializes and manages the conversation history using Streamlit's session state to maintain context across user interactions. ```python import streamlit as st if "messages" not in st.session_state: st.session_state.messages = [] st.session_state.messages.append({"role": "user", "content": "Hello, how are you?"}) st.session_state.messages.append({"role": "assistant", "content": "I'm doing well, thank you!"}) for message in st.session_state.messages: print(f"{message['role']}: {message['content']}") ``` -------------------------------- ### Maintain Context in Multi-turn Conversations Source: https://context7.com/zwelshman/chat_streamlit/llms.txt Sends the entire conversation history to the Groq API to enable multi-turn interactions and maintain context. ```python from groq import Groq client = Groq(api_key="your-api-key") messages = [ {"role": "user", "content": "What is Python?"}, {"role": "assistant", "content": "Python is a high-level programming language..."}, {"role": "user", "content": "What are its main use cases?"} ] response = client.chat.completions.create( model="deepseek-r1-distill-llama-70b", messages=[ {"role": m["role"], "content": m["content"]} for m in messages ], stream=False ) print(response.choices[0].message.content) ``` -------------------------------- ### Display Chat History in Streamlit UI Source: https://context7.com/zwelshman/chat_streamlit/llms.txt Renders the conversation history within Streamlit's chat message containers for a clear user interface. ```python import streamlit as st for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.