-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
83 lines (69 loc) · 2.8 KB
/
main.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import streamlit as st
from pathlib import Path
from dotenv import load_dotenv
from groq import Groq
project_root = Path(__file__).resolve().parent
load_dotenv(project_root / ".env")
class GroqAPI:
"""Handles API operations with Groq to generate chat responses."""
def __init__(self, model_name: str):
# API key from environment variable
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise ValueError("API key is missing. Please set the GROQ_API_KEY environment variable.")
self.client = Groq(api_key=api_key)
self.model_name = model_name
def _response(self, message):
return self.client.chat.completions.create(
model=self.model_name,
messages=message,
temperature=0,
max_tokens=4096,
stream=True,
stop=None,
)
def response_stream(self, message):
for chunk in self._response(message):
if chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
class Message:
"""Manages chat messages within the Streamlit UI."""
system_prompt = "You are a professional AI. Please generate responses in English to all user inputs."
def __init__(self):
if "messages" not in st.session_state:
st.session_state.messages = [{"role": "system", "content": self.system_prompt}]
def add(self, role: str, content: str):
st.session_state.messages.append({"role": role, "content": content})
def display_chat_history(self):
for message in st.session_state.messages:
if message["role"] == "system":
continue
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Streamlit chat message UI
def display_stream(self, generator):
with st.chat_message("assistant"):
return st.write_stream(generator)
class ModelSelector:
"""Allows the user to select a model from a predefined list."""
def __init__(self):
# List of available models to choose from
self.models = ["llama3-70b-8192","llama3-8b-8192","mixtral-8x7b-32768","gemma-7b-it"]
def select(self):
with st.sidebar:
st.sidebar.title("Groq Chat with Llama3 + α")
return st.selectbox("Select a model:", self.models)
def main():
user_input = st.chat_input("Enter message to AI models...")
model = ModelSelector()
selected_model = model.select()
message = Message()
if user_input:
llm = GroqAPI(selected_model)
message.add("user", user_input)
message.display_chat_history()
response = message.display_stream(llm.response_stream(st.session_state.messages))
message.add("assistant", response)
if __name__ == "__main__":
main()