File size: 1,411 Bytes
b04a659 b041fc3 b04a659 b041fc3 3538aac b041fc3 3538aac b041fc3 3538aac b041fc3 |
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 |
import gradio as gr
import time
import re
import os
# Available models
MODEL = "models/mistralai/Mixtral-8x7B-Instruct-v0.1"
# Sambanova API base URL
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] # First key
last_key = list(tup.keys())[-1] # Last key
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>(.*?) |