Update app.py
Browse files
app.py
CHANGED
|
@@ -314,11 +314,15 @@ COHERE_MODELS = {
|
|
| 314 |
|
| 315 |
# TOGETHER MODELS in the free tier
|
| 316 |
TOGETHER_MODELS = {
|
|
|
|
| 317 |
"deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free": 8192,
|
| 318 |
"meta-llama/Llama-Vision-Free": 8192,
|
| 319 |
"meta-llama/Llama-3.3-70B-Instruct-Turbo-Free": 8192,
|
| 320 |
}
|
| 321 |
|
|
|
|
|
|
|
|
|
|
| 322 |
# OVH MODELS - OVH AI Endpoints (free beta)
|
| 323 |
OVH_MODELS = {
|
| 324 |
"ovh/codestral-mamba-7b-v0.1": 131072,
|
|
@@ -501,7 +505,7 @@ def prepare_message_with_media(text, images=None, documents=None):
|
|
| 501 |
return text
|
| 502 |
|
| 503 |
# If we have images, create a multimodal content array
|
| 504 |
-
content = [{"type": "text", "text": text}]
|
| 505 |
|
| 506 |
# Add images if any
|
| 507 |
if images:
|
|
@@ -911,8 +915,14 @@ def extract_ai_response(result, provider):
|
|
| 911 |
return "No response content from Cohere"
|
| 912 |
|
| 913 |
elif provider == "Together":
|
|
|
|
| 914 |
if hasattr(result, "choices") and len(result.choices) > 0:
|
| 915 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 916 |
|
| 917 |
elif provider == "OVH":
|
| 918 |
if isinstance(result, dict) and "choices" in result and len(result["choices"]) > 0:
|
|
@@ -933,27 +943,32 @@ def extract_ai_response(result, provider):
|
|
| 933 |
return f"Error: {str(e)}"
|
| 934 |
|
| 935 |
def call_together_api(payload, api_key_override=None):
|
| 936 |
-
"""Make a call to Together API with error handling"""
|
| 937 |
try:
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 941 |
api_key = api_key_override if api_key_override else TOGETHER_API_KEY
|
| 942 |
if not api_key:
|
| 943 |
raise ValueError("Together API key is required")
|
| 944 |
|
| 945 |
-
# Create
|
| 946 |
-
client =
|
| 947 |
-
api_key=api_key,
|
| 948 |
-
base_url="https://api.together.xyz/v1"
|
| 949 |
-
)
|
| 950 |
|
| 951 |
# Extract parameters from payload
|
| 952 |
requested_model = payload.get("model", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 953 |
|
| 954 |
-
# Use
|
| 955 |
-
# these models are available without dedicated endpoints
|
| 956 |
free_models = [
|
|
|
|
| 957 |
"deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free",
|
| 958 |
"meta-llama/Llama-Vision-Free",
|
| 959 |
"meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
|
|
@@ -965,31 +980,41 @@ def call_together_api(payload, api_key_override=None):
|
|
| 965 |
# Try to match a requested model with a free model if possible
|
| 966 |
if requested_model:
|
| 967 |
for free_model in free_models:
|
| 968 |
-
if requested_model.lower() in free_model.lower():
|
| 969 |
model = free_model
|
| 970 |
break
|
| 971 |
|
| 972 |
-
#
|
| 973 |
-
|
| 974 |
-
for msg in
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
"role": msg["role"],
|
| 978 |
-
"content": msg["content"]
|
| 979 |
-
}
|
| 980 |
-
messages.append(clean_msg)
|
| 981 |
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 990 |
|
| 991 |
-
# Create completion
|
| 992 |
-
response = client.chat.completions.create(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 993 |
|
| 994 |
return response
|
| 995 |
except Exception as e:
|
|
|
|
| 314 |
|
| 315 |
# TOGETHER MODELS in the free tier
|
| 316 |
TOGETHER_MODELS = {
|
| 317 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo": 131072,
|
| 318 |
"deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free": 8192,
|
| 319 |
"meta-llama/Llama-Vision-Free": 8192,
|
| 320 |
"meta-llama/Llama-3.3-70B-Instruct-Turbo-Free": 8192,
|
| 321 |
}
|
| 322 |
|
| 323 |
+
# Add to the vision models list
|
| 324 |
+
VISION_MODELS["Together"] = ["meta-llama/Llama-Vision-Free"]
|
| 325 |
+
|
| 326 |
# OVH MODELS - OVH AI Endpoints (free beta)
|
| 327 |
OVH_MODELS = {
|
| 328 |
"ovh/codestral-mamba-7b-v0.1": 131072,
|
|
|
|
| 505 |
return text
|
| 506 |
|
| 507 |
# If we have images, create a multimodal content array
|
| 508 |
+
content = [{"type": "text", "text": text or "Analyze this image:"}]
|
| 509 |
|
| 510 |
# Add images if any
|
| 511 |
if images:
|
|
|
|
| 915 |
return "No response content from Cohere"
|
| 916 |
|
| 917 |
elif provider == "Together":
|
| 918 |
+
# Handle response from Together's native client
|
| 919 |
if hasattr(result, "choices") and len(result.choices) > 0:
|
| 920 |
+
if hasattr(result.choices[0], "message") and hasattr(result.choices[0].message, "content"):
|
| 921 |
+
return result.choices[0].message.content
|
| 922 |
+
elif hasattr(result.choices[0], "delta") and hasattr(result.choices[0].delta, "content"):
|
| 923 |
+
return result.choices[0].delta.content
|
| 924 |
+
# Fallback
|
| 925 |
+
return str(result)
|
| 926 |
|
| 927 |
elif provider == "OVH":
|
| 928 |
if isinstance(result, dict) and "choices" in result and len(result["choices"]) > 0:
|
|
|
|
| 943 |
return f"Error: {str(e)}"
|
| 944 |
|
| 945 |
def call_together_api(payload, api_key_override=None):
|
| 946 |
+
"""Make a call to Together API with error handling using their native client"""
|
| 947 |
try:
|
| 948 |
+
# Import Together's native client
|
| 949 |
+
# Note: This might need to be installed with: pip install together
|
| 950 |
+
try:
|
| 951 |
+
from together import Together
|
| 952 |
+
except ImportError:
|
| 953 |
+
raise ImportError("The Together Python package is not installed. Please install it with: pip install together")
|
| 954 |
+
|
| 955 |
api_key = api_key_override if api_key_override else TOGETHER_API_KEY
|
| 956 |
if not api_key:
|
| 957 |
raise ValueError("Together API key is required")
|
| 958 |
|
| 959 |
+
# Create the Together client
|
| 960 |
+
client = Together(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
| 961 |
|
| 962 |
# Extract parameters from payload
|
| 963 |
requested_model = payload.get("model", "")
|
| 964 |
+
messages = payload.get("messages", [])
|
| 965 |
+
temperature = payload.get("temperature", 0.7)
|
| 966 |
+
max_tokens = payload.get("max_tokens", 1000)
|
| 967 |
+
stream = payload.get("stream", False)
|
| 968 |
|
| 969 |
+
# Use one of the free, serverless models
|
|
|
|
| 970 |
free_models = [
|
| 971 |
+
"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
| 972 |
"deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free",
|
| 973 |
"meta-llama/Llama-Vision-Free",
|
| 974 |
"meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
|
|
|
|
| 980 |
# Try to match a requested model with a free model if possible
|
| 981 |
if requested_model:
|
| 982 |
for free_model in free_models:
|
| 983 |
+
if requested_model.lower() in free_model.lower() or free_model.lower() in requested_model.lower():
|
| 984 |
model = free_model
|
| 985 |
break
|
| 986 |
|
| 987 |
+
# Process messages for possible image content
|
| 988 |
+
processed_messages = []
|
| 989 |
+
for msg in messages:
|
| 990 |
+
role = msg["role"]
|
| 991 |
+
content = msg["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 992 |
|
| 993 |
+
# Handle multimodal content for vision models
|
| 994 |
+
if isinstance(content, list) and "vision" in model.lower():
|
| 995 |
+
# Format according to Together's expected multimodal format
|
| 996 |
+
parts = []
|
| 997 |
+
for item in content:
|
| 998 |
+
if item["type"] == "text":
|
| 999 |
+
parts.append({"type": "text", "text": item["text"]})
|
| 1000 |
+
elif item["type"] == "image_url":
|
| 1001 |
+
parts.append({
|
| 1002 |
+
"type": "image_url",
|
| 1003 |
+
"image_url": item["image_url"]
|
| 1004 |
+
})
|
| 1005 |
+
processed_messages.append({"role": role, "content": parts})
|
| 1006 |
+
else:
|
| 1007 |
+
# Regular text messages
|
| 1008 |
+
processed_messages.append({"role": role, "content": content})
|
| 1009 |
|
| 1010 |
+
# Create completion with Together's client
|
| 1011 |
+
response = client.chat.completions.create(
|
| 1012 |
+
model=model,
|
| 1013 |
+
messages=processed_messages,
|
| 1014 |
+
temperature=temperature,
|
| 1015 |
+
max_tokens=max_tokens,
|
| 1016 |
+
stream=stream
|
| 1017 |
+
)
|
| 1018 |
|
| 1019 |
return response
|
| 1020 |
except Exception as e:
|