File size: 1,525 Bytes
b444893
bb0552d
b444893
bb0552d
b444893
 
 
 
bb0552d
b444893
 
 
 
bb0552d
 
b444893
 
bb0552d
b444893
 
 
bb0552d
b444893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
from groq import Groq

# Set your Groq API key here or load it from an environment variable
api_key = os.getenv("GROQ_API_KEY")  # Preferred for security
# Alternatively, hardcode the API key if it's safe to do so (less secure)
# api_key = "your-groq-api-key"


# Streamlit app setup
st.title("Groq API Chat App")
st.write("Enter your input to get a response from the Groq model.")

if api_key:
    # Initialize the Groq client with the pre-configured API key
    client = Groq(api_key=api_key)

    # User input for chat content
    user_input = st.text_input("Enter your question or text:")
    
    if st.button("Get Response"):
        # Ensure user input is not empty
        if user_input:
            try:
                chat_completion = client.chat.completions.create(
                    messages=[
                        {
                            "role": "user",
                            "content": user_input
                        }
                    ],
                    model="llama3-8b-8192",
                )

                # Display the response
                response = chat_completion.choices[0].message.content
                st.write("### Response")
                st.write(response)
            except Exception as e:
                st.error(f"An error occurred: {e}")
        else:
            st.warning("Please enter some text before clicking 'Get Response'")
else:
    st.error("API key not found. Please set the API key in your environment.")