Last active
April 24, 2024 08:19
-
-
Save truevis/f31706b8af60e8c73d62b281bddb988f to your computer and use it in GitHub Desktop.
Basic Groq API Response Streaming using Llama3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
from groq import Groq | |
# Initialize Groq client with API key | |
client = Groq(api_key="gsk_123") | |
def generate_response(user_input): | |
stream = client.chat.completions.create( | |
model="llama3-70b-8192", | |
messages=[ | |
{"role": "system", "content": "Wise Assistant"}, | |
{"role": "user", "content": user_input}, | |
], | |
temperature=0.1, | |
max_tokens=8192, | |
top_p=1, | |
stream=True, | |
stop=None, | |
) | |
for chunk in stream: | |
content = chunk.choices[0].delta.content | |
if content: | |
yield content # Yield content for streaming | |
st.title("Groq API Response Streaming") | |
user_input = st.chat_input('Message to Assistant...', key='prompt_input') | |
if user_input: # Get user input | |
with st.spinner("Generating response..."): | |
st.write_stream(generate_response(user_input)) # Use st.write_stream to display streamed content | |
st.markdown("Message: " + user_input) | |
st.markdown("---") # Add a newline after the response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment