import os
from groq import Groq
import streamlit as st
from dotenv import load_dotenv
from typing import List
import functools
import abc
# Load environment variables (API keys)
load_dotenv()
# Define constants
CONVO_TRAIL_CUTOFF = 5
PERSONAL_AI_ASSISTANT_PROMPT_HEAD = "You are a helpful assistant. [[previous_interactions]] [[latest_input]]"
ASSISTANT_TYPE = "GroqPAF"
# Define Interaction class
class Interaction:
def __init__(self, role: str, content: str):
self.role = role
self.content = content
class PersonalAssistantFramework(abc.ABC):
@staticmethod
def timeit_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result
return wrapper
@abc.abstractmethod
def setup(self):
pass
@abc.abstractmethod
def think(self, prompt: str) -> str:
pass
class GroqPAF(PersonalAssistantFramework):
def __init__(self):
self.client = Groq(api_key=os.getenv("GROQ_API_KEY"))
def setup(self):
self.llm_model = None
@PersonalAssistantFramework.timeit_decorator
def think(self, thought: str) -> str:
try:
response = self.client.chat.completions.create(
model="mixtral-8x7b-32768", # You can change this to other Groq models
messages=[
{"role": "system", "content": "You are a helpful assistant named Luna."},
{"role": "user", "content": thought}
],
max_tokens=500
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"An error occurred: {str(e)}"
def build_prompt(latest_input: str, previous_interactions: List[Interaction]) -> str:
previous_interactions_str = "\n".join(
[
f"""