ryanpereira commited on
Commit
c575d92
·
verified ·
1 Parent(s): e9ddfac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GOAL: Building an ai powered chatbot
2
+ # GOAL: Building an AI-powered chatbot using GROQ API
3
+ import gradio as gr
4
+ import requests
5
+ import os
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv() # Load GROQ_API_KEY from .env
9
+
10
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
11
+ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
12
+ MODEL = "llama3-8b-8192" # or "mixtral-8x7b-32768", etc.
13
+
14
+ headers = {
15
+ "Authorization": f"Bearer {GROQ_API_KEY}",
16
+ "Content-Type": "application/json"
17
+ }
18
+
19
+ # backend: Python
20
+ def echo(message, history):
21
+ messages = [
22
+ {"role": "system", "content": "You are a helpful LLM teacher."},
23
+ {"role": "user", "content": message}
24
+ ]
25
+
26
+ response = requests.post(
27
+ GROQ_API_URL,
28
+ headers=headers,
29
+ json={
30
+ "model": MODEL,
31
+ "messages": messages
32
+ }
33
+ )
34
+
35
+ if response.status_code == 200:
36
+ completion = response.json()
37
+ return completion["choices"][0]["message"]["content"]
38
+ else:
39
+ return f"Error: {response.status_code}\n{response.text}"
40
+
41
+ # frontend: Gradio
42
+ demo = gr.ChatInterface(
43
+ fn=echo,
44
+ type="messages",
45
+ examples=[
46
+ "I want to learn about LLMs",
47
+ "What is NLP",
48
+ "What is RAG"
49
+ ],
50
+ title="LLM Mentor"
51
+ )
52
+
53
+ demo.launch()