|
import gradio as gr |
|
import time |
|
import re |
|
import os |
|
|
|
|
|
MODEL = "models/mistralai/Mixtral-8x7B-Instruct-v0.1" |
|
|
|
|
|
API_BASE = "https://api.sambanova.ai/v1" |
|
|
|
def create_client(): |
|
"""Creates an client instance.""" |
|
return |
|
|
|
def chat_with_ai(message, chat_history, system_prompt): |
|
"""Formats the chat history for the API call.""" |
|
messages = [{"role": "system", "content": system_prompt}] |
|
for tup in chat_history: |
|
first_key = list(tup.keys())[0] |
|
last_key = list(tup.keys())[-1] |
|
messages.append({"role": "user", "content": tup[first_key]}) |
|
messages.append({"role": "assistant", "content": tup[last_key]}) |
|
messages.append({"role": "user", "content": message}) |
|
return messages |
|
|
|
def respond(message, chat_history, system_prompt, thinking_budget): |
|
"""Sends the message to the API and gets the response.""" |
|
messages = chat_with_ai(message, chat_history, system_prompt.format(budget=thinking_budget)) |
|
start_time = time.time() |
|
|
|
try: |
|
response = |
|
thinking_time = time.time() - start_time |
|
return response, thinking_time |
|
except Exception as e: |
|
error_message = f"Error: {str(e)}" |
|
return error_message, time.time() - start_time |
|
|
|
def parse_response(response): |
|
"""Parses the response from the API.""" |
|
answer_match = re.search(r'<answer>(.*?) |