File size: 1,130 Bytes
9ee3f6b
 
 
 
26e4452
bc2c65e
26e4452
 
71ce63b
9ee3f6b
26e4452
bc2c65e
9ee3f6b
bc2c65e
 
 
 
 
9ee3f6b
bc2c65e
9ee3f6b
bc2c65e
 
 
9ee3f6b
bc2c65e
 
 
 
 
 
 
302b71d
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
import os
import gradio as gr
from groq import Groq

# Set up the Groq API client
apikey = os.getenv("apikey")  # Fetch the API key from secrets
if not apikey:
    raise ValueError("API Key is not set. Add it in the Secrets tab.")
client = Groq(api_key=apikey)

# Function to interact with the LLM
def chatbot(user_input, history):
    try:
        # Format conversation history
        messages = [{"role": "user", "content": user} for user, _ in history]
        messages.append({"role": "user", "content": user_input})

        # Send to Groq's API
        chat_completion = client.chat.completions.create(
            messages=messages, model="llama3-8b-8192"
        )
        response = chat_completion.choices[0].message.content
        history.append((user_input, response))
        return history, history
    except Exception as e:
        return history, history + [(user_input, f"An error occurred: {str(e)}")]

# Enhanced Gradio interface
with gr.Blocks(title="Groq LLM Chatbot") as demo:
    gr.Markdown(
        """
        # 🧠 **Groq LLM Chatbot**
        Interact with a powerful LLM in real-time using Groq's