Spaces:
Running
Running
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 |