Update app.py
Browse files
app.py
CHANGED
@@ -705,6 +705,7 @@ def call_anthropic_api(payload, api_key_override=None):
|
|
705 |
# Try to import Anthropic
|
706 |
try:
|
707 |
import anthropic
|
|
|
708 |
except ImportError:
|
709 |
raise ImportError("Anthropic package not installed. Install it with: pip install anthropic")
|
710 |
|
@@ -712,7 +713,7 @@ def call_anthropic_api(payload, api_key_override=None):
|
|
712 |
if not api_key:
|
713 |
raise ValueError("Anthropic API key is required")
|
714 |
|
715 |
-
client =
|
716 |
|
717 |
# Extract parameters from payload
|
718 |
model = payload.get("model", "claude-3-5-sonnet-20241022")
|
@@ -723,30 +724,32 @@ def call_anthropic_api(payload, api_key_override=None):
|
|
723 |
# Format messages for Anthropic
|
724 |
# Find system message if any
|
725 |
system_prompt = None
|
726 |
-
|
727 |
|
728 |
for msg in messages:
|
729 |
if msg["role"] == "system":
|
730 |
system_prompt = msg["content"]
|
731 |
else:
|
732 |
-
#
|
733 |
if isinstance(msg["content"], list):
|
734 |
-
#
|
735 |
-
|
736 |
for item in msg["content"]:
|
737 |
if item["type"] == "text":
|
738 |
-
|
739 |
-
"type": "text",
|
740 |
"text": item["text"]
|
741 |
})
|
742 |
elif item["type"] == "image_url":
|
743 |
-
#
|
744 |
-
|
745 |
-
if
|
746 |
-
# Extract
|
747 |
-
|
748 |
-
|
749 |
-
|
|
|
|
|
750 |
"type": "image",
|
751 |
"source": {
|
752 |
"type": "base64",
|
@@ -754,14 +757,20 @@ def call_anthropic_api(payload, api_key_override=None):
|
|
754 |
"data": base64_data
|
755 |
}
|
756 |
})
|
757 |
-
|
758 |
-
|
759 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
760 |
})
|
761 |
else:
|
762 |
-
# Simple text
|
763 |
-
|
764 |
-
"role":
|
765 |
"content": msg["content"]
|
766 |
})
|
767 |
|
@@ -771,7 +780,7 @@ def call_anthropic_api(payload, api_key_override=None):
|
|
771 |
max_tokens=max_tokens,
|
772 |
temperature=temperature,
|
773 |
system=system_prompt,
|
774 |
-
messages=
|
775 |
)
|
776 |
|
777 |
return response
|
@@ -2854,7 +2863,7 @@ def create_app():
|
|
2854 |
return None
|
2855 |
|
2856 |
# Set up submission event
|
2857 |
-
def submit_message(message,
|
2858 |
openrouter_model, openai_model, hf_model, groq_model, cohere_model, together_model, anthropic_model, googleai_model,
|
2859 |
temperature, max_tokens, top_p, frequency_penalty, presence_penalty, repetition_penalty,
|
2860 |
top_k, min_p, seed, top_a, stream_output, response_format,
|
@@ -2863,41 +2872,41 @@ def create_app():
|
|
2863 |
|
2864 |
"""Submit message to selected provider and model"""
|
2865 |
# Get the currently selected model
|
2866 |
-
model_choice = get_current_model(
|
2867 |
together_model, anthropic_model, googleai_model)
|
2868 |
|
2869 |
# Check if model is selected
|
2870 |
if not model_choice:
|
2871 |
-
error_message = f"Error: No model selected for provider {
|
2872 |
-
return
|
2873 |
{"role": "user", "content": message},
|
2874 |
{"role": "assistant", "content": error_message}
|
2875 |
]
|
2876 |
|
2877 |
# Select the appropriate API key based on the provider
|
2878 |
api_key_override = None
|
2879 |
-
if
|
2880 |
api_key_override = openrouter_api_key
|
2881 |
-
elif
|
2882 |
api_key_override = openai_api_key
|
2883 |
-
elif
|
2884 |
api_key_override = hf_api_key
|
2885 |
-
elif
|
2886 |
api_key_override = groq_api_key
|
2887 |
-
elif
|
2888 |
api_key_override = cohere_api_key
|
2889 |
-
elif
|
2890 |
api_key_override = together_api_key
|
2891 |
-
elif
|
2892 |
api_key_override = anthropic_api_key
|
2893 |
-
elif
|
2894 |
api_key_override = googleai_api_key
|
2895 |
|
2896 |
# Call the ask_ai function with the appropriate parameters
|
2897 |
return ask_ai(
|
2898 |
message=message,
|
2899 |
-
history=
|
2900 |
-
provider=
|
2901 |
model_choice=model_choice,
|
2902 |
temperature=temperature,
|
2903 |
max_tokens=max_tokens,
|
|
|
705 |
# Try to import Anthropic
|
706 |
try:
|
707 |
import anthropic
|
708 |
+
from anthropic import Anthropic
|
709 |
except ImportError:
|
710 |
raise ImportError("Anthropic package not installed. Install it with: pip install anthropic")
|
711 |
|
|
|
713 |
if not api_key:
|
714 |
raise ValueError("Anthropic API key is required")
|
715 |
|
716 |
+
client = Anthropic(api_key=api_key)
|
717 |
|
718 |
# Extract parameters from payload
|
719 |
model = payload.get("model", "claude-3-5-sonnet-20241022")
|
|
|
724 |
# Format messages for Anthropic
|
725 |
# Find system message if any
|
726 |
system_prompt = None
|
727 |
+
chat_messages = []
|
728 |
|
729 |
for msg in messages:
|
730 |
if msg["role"] == "system":
|
731 |
system_prompt = msg["content"]
|
732 |
else:
|
733 |
+
# Format content
|
734 |
if isinstance(msg["content"], list):
|
735 |
+
# Handle multimodal content (images)
|
736 |
+
anthropic_content = []
|
737 |
for item in msg["content"]:
|
738 |
if item["type"] == "text":
|
739 |
+
anthropic_content.append({
|
740 |
+
"type": "text",
|
741 |
"text": item["text"]
|
742 |
})
|
743 |
elif item["type"] == "image_url":
|
744 |
+
# Extract base64 from data URL if present
|
745 |
+
image_url = item["image_url"]["url"]
|
746 |
+
if image_url.startswith("data:"):
|
747 |
+
# Extract media type and base64 data
|
748 |
+
parts = image_url.split(",", 1)
|
749 |
+
media_type = parts[0].split(":")[1].split(";")[0]
|
750 |
+
base64_data = parts[1]
|
751 |
+
|
752 |
+
anthropic_content.append({
|
753 |
"type": "image",
|
754 |
"source": {
|
755 |
"type": "base64",
|
|
|
757 |
"data": base64_data
|
758 |
}
|
759 |
})
|
760 |
+
else:
|
761 |
+
# URL not supported by Anthropic yet
|
762 |
+
anthropic_content.append({
|
763 |
+
"type": "text",
|
764 |
+
"text": f"[Image URL: {image_url}]"
|
765 |
+
})
|
766 |
+
chat_messages.append({
|
767 |
+
"role": msg["role"],
|
768 |
+
"content": anthropic_content
|
769 |
})
|
770 |
else:
|
771 |
+
# Simple text content
|
772 |
+
chat_messages.append({
|
773 |
+
"role": msg["role"],
|
774 |
"content": msg["content"]
|
775 |
})
|
776 |
|
|
|
780 |
max_tokens=max_tokens,
|
781 |
temperature=temperature,
|
782 |
system=system_prompt,
|
783 |
+
messages=chat_messages
|
784 |
)
|
785 |
|
786 |
return response
|
|
|
2863 |
return None
|
2864 |
|
2865 |
# Set up submission event
|
2866 |
+
def submit_message(message, history, provider,
|
2867 |
openrouter_model, openai_model, hf_model, groq_model, cohere_model, together_model, anthropic_model, googleai_model,
|
2868 |
temperature, max_tokens, top_p, frequency_penalty, presence_penalty, repetition_penalty,
|
2869 |
top_k, min_p, seed, top_a, stream_output, response_format,
|
|
|
2872 |
|
2873 |
"""Submit message to selected provider and model"""
|
2874 |
# Get the currently selected model
|
2875 |
+
model_choice = get_current_model(provider, openrouter_model, openai_model, hf_model, groq_model, cohere_model,
|
2876 |
together_model, anthropic_model, googleai_model)
|
2877 |
|
2878 |
# Check if model is selected
|
2879 |
if not model_choice:
|
2880 |
+
error_message = f"Error: No model selected for provider {provider}"
|
2881 |
+
return history + [
|
2882 |
{"role": "user", "content": message},
|
2883 |
{"role": "assistant", "content": error_message}
|
2884 |
]
|
2885 |
|
2886 |
# Select the appropriate API key based on the provider
|
2887 |
api_key_override = None
|
2888 |
+
if provider == "OpenRouter" and openrouter_api_key:
|
2889 |
api_key_override = openrouter_api_key
|
2890 |
+
elif provider == "OpenAI" and openai_api_key:
|
2891 |
api_key_override = openai_api_key
|
2892 |
+
elif provider == "HuggingFace" and hf_api_key:
|
2893 |
api_key_override = hf_api_key
|
2894 |
+
elif provider == "Groq" and groq_api_key:
|
2895 |
api_key_override = groq_api_key
|
2896 |
+
elif provider == "Cohere" and cohere_api_key:
|
2897 |
api_key_override = cohere_api_key
|
2898 |
+
elif provider == "Together" and together_api_key:
|
2899 |
api_key_override = together_api_key
|
2900 |
+
elif provider == "Anthropic" and anthropic_api_key:
|
2901 |
api_key_override = anthropic_api_key
|
2902 |
+
elif provider == "GoogleAI" and googleai_api_key:
|
2903 |
api_key_override = googleai_api_key
|
2904 |
|
2905 |
# Call the ask_ai function with the appropriate parameters
|
2906 |
return ask_ai(
|
2907 |
message=message,
|
2908 |
+
history=history,
|
2909 |
+
provider=provider,
|
2910 |
model_choice=model_choice,
|
2911 |
temperature=temperature,
|
2912 |
max_tokens=max_tokens,
|