File size: 1,636 Bytes
55c4810
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
from dotenv import load_dotenv
from typing import Tuple
from groq import Groq

def get_api_key() -> str:
    """
    Get the api key to the LLM
    
    Returns:
        str: The api key to be used to reach the LLM
    """
    load_dotenv()
    api_key = os.getenv('key')
        
    return api_key


def get_response(prompt: str, api_key) -> Tuple[str, str]:
    """
    Gets the response of the LLM on the provided prompt.
    
    Args:
        prompt (str): The prompt to be plugged in
        
    Returns:
        tuple:
            str: Includes the thinking part of the LLM, showing its thought process.
            str: The actual answer to your prompt
    
    """
    client = Groq(api_key=api_key)
    completion = client.chat.completions.create(
        model="deepseek-r1-distill-llama-70b",
        messages=[
            {
                'role': 'user',
                'content': prompt
            }
            ],
        temperature=0.6,
        max_completion_tokens=4096,
        top_p=0.95,
        stream=True,
        stop=None,
    )
    
    chunks = []
    for chunk in completion:
        current_chunk = chunk.choices[0].delta.content or ""
        chunks.append(current_chunk)
        
    full_response = "".join(chunks)
    
    
    # Splitting the text
    thought_process_match = re.search(r"<think>\s*(.*?)\s*</think>", full_response, re.DOTALL)
    
    thought_process = thought_process_match.group(1) if thought_process_match else ""

    actual_response = re.sub(r"<think>.*?</think>\s*", "", full_response, flags=re.DOTALL)
    
    return thought_process, actual_response