File size: 2,414 Bytes
3fcec00
 
 
 
 
 
 
 
 
 
e3684d1
3fcec00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
from langchain.llms.base import LLM
from typing import Optional, List
import os
from execution_programs import *

# API key for llm model
GROQ_API_KEY = os.environ.get("GROQ_TOKENS")
class GroqLLM(LLM):
    model: str = "meta-llama/llama-4-maverick-17b-128e-instruct"
    temperature: float = 0.3
    api_key: str = GROQ_API_KEY

    @property
    def _llm_type(self) -> str:
        return "groq-llm"

    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        headers = {
            "Authorization": f"Bearer {self.api_key}",
            "Content-Type": "application/json",
        }

        payload = {
            "messages": [{"role": "user", "content": prompt}],
            "model": self.model,
            "temperature": self.temperature,
        }

        try:
            response = requests.post(
                "https://api.groq.com/openai/v1/chat/completions",
                headers=headers,
                json=payload
            )
            response.raise_for_status()
            return response.json()["choices"][0]["message"]["content"]
        except Exception as e:
            st.error(f"Error calling Groq API: {str(e)}")
            return "Sorry, there was an error processing your request."

class GroqTextLLM(LLM):
    model: str = "deepseek-r1-distill-llama-70b"
    temperature: float = 0.7
    api_key: str = GROQ_API_KEY

    @property
    def _llm_type(self) -> str:
        return "groq-text-llm"

    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        headers = {
            "Authorization": f"Bearer {self.api_key}",
            "Content-Type": "application/json",
        }

        payload = {
            "messages": [{"role": "user", "content": prompt}],
            "model": self.model,
            "temperature": self.temperature,
        }

        try:
            response = requests.post(
                "https://api.groq.com/openai/v1/chat/completions",
                headers=headers,
                json=payload
            )
            response.raise_for_status()
            return response.json()["choices"][0]["message"]["content"]
        except Exception as e:
            st.error(f"Error calling Groq API: {str(e)}")
            return "Sorry, there was an error processing your request."

# Initialize LLMs
code_llm = GroqLLM()
text_llm = GroqTextLLM()