-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-file
More file actions
850 lines (724 loc) · 30.5 KB
/
Copy pathfunction-file
File metadata and controls
850 lines (724 loc) · 30.5 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
"""
title: Multi Provider External
author: codemonkeying
version: 1.2.1
license: MIT
requirements: pydantic>=2.0.0, requests>=2.0.0, google-generativeai>=0.3.0
environment_variables:
- OPENAI_API_KEY (optional)
- ANTHROPIC_API_KEY (optional)
- GOOGLE_API_KEY (optional)
Auto-discovers models from:
- OpenAI: /v1/models endpoint
- Anthropic: /v1/models endpoint
- Google: genai.list_models()
"""
import os
import json
import time
import logging
import requests
from datetime import datetime
from typing import List, Union, Generator, Iterator, Dict, Optional
from pydantic import BaseModel, Field
from open_webui.utils.misc import pop_system_message
try:
import google.generativeai as genai
from google.generativeai.types import GenerationConfig
GOOGLE_AVAILABLE = True
except ImportError:
GOOGLE_AVAILABLE = False
genai = None
class Pipe:
def __init__(self):
logging.basicConfig(level=logging.INFO)
self.type = "manifold"
self.id = "multi_provider_external"
self.name = ""
self.valves = self.Valves()
self.request_id = None
self._cached_models = {}
self._cache_timestamp = {}
class Valves(BaseModel):
# API Keys
OPENAI_API_KEY: str = Field(
default=os.getenv("OPENAI_API_KEY", ""), description="OpenAI API key"
)
ANTHROPIC_API_KEY: str = Field(
default=os.getenv("ANTHROPIC_API_KEY", ""), description="Anthropic API key"
)
GOOGLE_API_KEY: str = Field(
default=os.getenv("GOOGLE_API_KEY", ""), description="Google API key"
)
# API Base URLs
OPENAI_API_BASE_URL: str = Field(
default="https://api.openai.com/v1", description="OpenAI API base URL"
)
ANTHROPIC_API_BASE_URL: str = Field(
default="https://api.anthropic.com/v1", description="Anthropic API base URL"
)
# Provider Settings
ENABLE_OPENAI: bool = Field(default=True, description="Enable OpenAI models")
ENABLE_ANTHROPIC: bool = Field(
default=True, description="Enable Anthropic models"
)
ENABLE_GOOGLE: bool = Field(default=True, description="Enable Google models")
# Display Settings
MODEL_PREFIX: str = Field(
default="MPE:",
description="Prefix for model names (e.g., 'MPE:', 'External:', 'Multi:')",
)
# Auto-discovery Settings
AUTO_DISCOVER_MODELS: bool = Field(
default=True, description="Auto-discover models from APIs"
)
CACHE_DURATION_MINUTES: int = Field(
default=60, description="Cache model list for N minutes"
)
# Safety Settings
USE_PERMISSIVE_SAFETY: bool = Field(
default=True, description="Use permissive safety settings for Google"
)
def _is_cache_valid(self, provider: str) -> bool:
"""Check if cached models are still valid"""
if provider not in self._cache_timestamp:
return False
cache_age = time.time() - self._cache_timestamp[provider]
return cache_age < (self.valves.CACHE_DURATION_MINUTES * 60)
def _discover_openai_models(self) -> List[dict]:
"""Auto-discover OpenAI models from API"""
if not self.valves.OPENAI_API_KEY:
return []
# Check cache first
if self._is_cache_valid("openai") and "openai" in self._cached_models:
return self._cached_models["openai"]
try:
headers = {
"Authorization": f"Bearer {self.valves.OPENAI_API_KEY}",
"Content-Type": "application/json",
}
response = requests.get(
f"{self.valves.OPENAI_API_BASE_URL}/models", headers=headers, timeout=10
)
if response.status_code == 200:
data = response.json()
models = []
for model in data.get("data", []):
model_id = model.get("id", "")
# Filter for chat models and estimate context/vision support
if any(prefix in model_id for prefix in ["gpt-", "o1-", "o3-"]):
# Estimate context window based on model name
context = self._estimate_openai_context(model_id)
vision = self._estimate_openai_vision(model_id)
models.append(
{
"id": model_id,
"name": self._format_openai_name(model_id),
"context": context,
"vision": vision,
}
)
# Cache the results
self._cached_models["openai"] = models
self._cache_timestamp["openai"] = time.time()
print(f"DEBUG: Discovered {len(models)} OpenAI models")
return models
except Exception as e:
print(f"DEBUG: Failed to discover OpenAI models: {e}")
# Fallback to hardcoded models
return self._get_fallback_openai_models()
def _discover_anthropic_models(self) -> List[dict]:
"""Auto-discover Anthropic models from API"""
if not self.valves.ANTHROPIC_API_KEY:
return []
# Check cache first
if self._is_cache_valid("anthropic") and "anthropic" in self._cached_models:
return self._cached_models["anthropic"]
try:
headers = {
"x-api-key": self.valves.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
}
response = requests.get(
f"{self.valves.ANTHROPIC_API_BASE_URL}/models",
headers=headers,
timeout=10,
)
if response.status_code == 200:
data = response.json()
models = []
for model in data.get("data", []):
model_id = model.get("id", "")
display_name = model.get("display_name", model_id)
# Only include Claude models
if model_id.startswith("claude"):
context = self._estimate_anthropic_context(model_id)
vision = self._estimate_anthropic_vision(model_id)
models.append(
{
"id": model_id,
"name": display_name,
"context": context,
"vision": vision,
}
)
# Cache the results
self._cached_models["anthropic"] = models
self._cache_timestamp["anthropic"] = time.time()
print(f"DEBUG: Discovered {len(models)} Anthropic models")
return models
except Exception as e:
print(f"DEBUG: Failed to discover Anthropic models: {e}")
# Fallback to hardcoded models
return self._get_fallback_anthropic_models()
def _discover_google_models(self) -> List[dict]:
"""Auto-discover Google models from API"""
if not self.valves.GOOGLE_API_KEY or not GOOGLE_AVAILABLE:
return []
# Check cache first
if self._is_cache_valid("google") and "google" in self._cached_models:
return self._cached_models["google"]
try:
genai.configure(api_key=self.valves.GOOGLE_API_KEY)
models = []
for model in genai.list_models():
if "generateContent" in model.supported_generation_methods:
model_id = model.name.replace("models/", "")
# Only include Gemini models
if model_id.startswith("gemini"):
context = self._estimate_google_context(model_id)
vision = self._estimate_google_vision(model_id)
models.append(
{
"id": model_id,
"name": self._format_google_name(model_id),
"context": context,
"vision": vision,
}
)
# Cache the results
self._cached_models["google"] = models
self._cache_timestamp["google"] = time.time()
print(f"DEBUG: Discovered {len(models)} Google models")
return models
except Exception as e:
print(f"DEBUG: Failed to discover Google models: {e}")
# Fallback to hardcoded models
return self._get_fallback_google_models()
def _estimate_openai_context(self, model_id: str) -> int:
"""Estimate context window for OpenAI models"""
if "gpt-5" in model_id:
return 200000
elif "gpt-4o" in model_id:
return 128000
elif "gpt-4-turbo" in model_id:
return 128000
elif "gpt-4" in model_id:
return 8192
elif "gpt-3.5-turbo" in model_id:
return 16385
elif model_id.startswith(("o1-", "o3-")):
return 200000
else:
return 4096 # Default
def _estimate_openai_vision(self, model_id: str) -> bool:
"""Estimate vision support for OpenAI models"""
vision_models = ["gpt-4o", "gpt-4-turbo", "gpt-4-vision"]
return any(vm in model_id for vm in vision_models) or "gpt-5" in model_id
def _estimate_anthropic_context(self, model_id: str) -> int:
"""Estimate context window for Anthropic models"""
if "claude-3" in model_id or "claude-4" in model_id:
return 200000
else:
return 100000 # Default
def _estimate_anthropic_vision(self, model_id: str) -> bool:
"""Estimate vision support for Anthropic models"""
# Most Claude 3+ models support vision except Haiku
return (
"claude-3" in model_id or "claude-4" in model_id and "haiku" not in model_id
)
def _estimate_google_context(self, model_id: str) -> int:
"""Estimate context window for Google models"""
if "gemini-1.5-pro" in model_id or "gemini-2" in model_id:
return 2000000
elif "gemini-1.5" in model_id:
return 1000000
else:
return 32000 # Default
def _estimate_google_vision(self, model_id: str) -> bool:
"""Estimate vision support for Google models"""
return "gemini" in model_id # Most Gemini models support vision
def _format_openai_name(self, model_id: str) -> str:
"""Format OpenAI model name for display"""
name_map = {
"gpt-4o": "GPT-4o",
"gpt-4o-mini": "GPT-4o Mini",
"gpt-4-turbo": "GPT-4 Turbo",
"gpt-3.5-turbo": "GPT-3.5 Turbo",
"o1-preview": "o1-preview",
"o1-mini": "o1-mini",
}
return name_map.get(model_id, model_id.replace("-", " ").title())
def _format_google_name(self, model_id: str) -> str:
"""Format Google model name for display"""
return model_id.replace("gemini-", "Gemini ").replace("-", " ").title()
def _get_fallback_openai_models(self) -> List[dict]:
"""Fallback OpenAI models if discovery fails"""
return [
{"id": "gpt-4o", "name": "GPT-4o", "context": 128000, "vision": True},
{
"id": "gpt-4o-mini",
"name": "GPT-4o Mini",
"context": 128000,
"vision": True,
},
{
"id": "gpt-4-turbo",
"name": "GPT-4 Turbo",
"context": 128000,
"vision": True,
},
{
"id": "gpt-3.5-turbo",
"name": "GPT-3.5 Turbo",
"context": 16000,
"vision": False,
},
{
"id": "o1-preview",
"name": "o1-preview",
"context": 128000,
"vision": False,
},
{"id": "o1-mini", "name": "o1-mini", "context": 128000, "vision": False},
]
def _get_fallback_anthropic_models(self) -> List[dict]:
"""Fallback Anthropic models if discovery fails"""
return [
{
"id": "claude-3-5-sonnet-20241022",
"name": "Claude 3.5 Sonnet",
"context": 200000,
"vision": True,
},
{
"id": "claude-3-5-haiku-20241022",
"name": "Claude 3.5 Haiku",
"context": 200000,
"vision": False,
},
{
"id": "claude-3-opus-20240229",
"name": "Claude 3 Opus",
"context": 200000,
"vision": True,
},
{
"id": "claude-3-sonnet-20240229",
"name": "Claude 3 Sonnet",
"context": 200000,
"vision": True,
},
{
"id": "claude-3-haiku-20240307",
"name": "Claude 3 Haiku",
"context": 200000,
"vision": True,
},
]
def _get_fallback_google_models(self) -> List[dict]:
"""Fallback Google models if discovery fails"""
return [
{
"id": "gemini-1.5-pro",
"name": "Gemini 1.5 Pro",
"context": 2000000,
"vision": True,
},
{
"id": "gemini-1.5-flash",
"name": "Gemini 1.5 Flash",
"context": 1000000,
"vision": True,
},
{
"id": "gemini-pro",
"name": "Gemini Pro",
"context": 32000,
"vision": True,
},
]
def _format_model_name(self, model_name: str) -> str:
"""Format model name with configurable prefix"""
prefix = self.valves.MODEL_PREFIX.strip()
if prefix and not prefix.endswith(":"):
prefix += ":"
if prefix:
return f"{prefix} {model_name}"
else:
return model_name
def pipes(self) -> List[dict]:
"""Return all available models from enabled providers"""
models = []
# OpenAI Models
if self.valves.ENABLE_OPENAI and self.valves.OPENAI_API_KEY:
if self.valves.AUTO_DISCOVER_MODELS:
openai_models = self._discover_openai_models()
else:
openai_models = self._get_fallback_openai_models()
for model in openai_models:
models.append(
{
"id": f"openai.{model['id']}",
"name": self._format_model_name(model["name"]),
"context_length": model["context"],
"supports_vision": model["vision"],
"provider": "openai",
}
)
# Anthropic Models
if self.valves.ENABLE_ANTHROPIC and self.valves.ANTHROPIC_API_KEY:
if self.valves.AUTO_DISCOVER_MODELS:
anthropic_models = self._discover_anthropic_models()
else:
anthropic_models = self._get_fallback_anthropic_models()
for model in anthropic_models:
models.append(
{
"id": f"anthropic.{model['id']}",
"name": self._format_model_name(model["name"]),
"context_length": model["context"],
"supports_vision": model["vision"],
"provider": "anthropic",
}
)
# Google Models
if (
self.valves.ENABLE_GOOGLE
and self.valves.GOOGLE_API_KEY
and GOOGLE_AVAILABLE
):
if self.valves.AUTO_DISCOVER_MODELS:
google_models = self._discover_google_models()
else:
google_models = self._get_fallback_google_models()
for model in google_models:
models.append(
{
"id": f"google.{model['id']}",
"name": self._format_model_name(model["name"]),
"context_length": model["context"],
"supports_vision": model["vision"],
"provider": "google",
}
)
if not models:
return [
{
"id": "error",
"name": "No API keys configured or providers enabled",
}
]
return models
def pipe(
self, body: Dict, __event_emitter__=None, __user__=None
) -> Union[str, Generator, Iterator]:
"""Route request to appropriate provider"""
model_id = body["model"]
print(f"DEBUG: Received model_id: {model_id}")
# Extract provider and model from various formats
provider = None
actual_model = None
if ".openai." in model_id:
provider = "openai"
actual_model = model_id.split(".openai.")[-1]
elif ".anthropic." in model_id:
provider = "anthropic"
actual_model = model_id.split(".anthropic.")[-1]
elif ".google." in model_id:
provider = "google"
actual_model = model_id.split(".google.")[-1]
elif model_id.startswith("openai/"):
provider = "openai"
actual_model = model_id.replace("openai/", "")
elif model_id.startswith("anthropic/"):
provider = "anthropic"
actual_model = model_id.replace("anthropic/", "")
elif model_id.startswith("google/"):
provider = "google"
actual_model = model_id.replace("google/", "")
if provider and actual_model:
print(f"DEBUG: Provider: {provider}, Model: {actual_model}")
if provider == "openai":
return self._handle_openai(body, __event_emitter__, actual_model)
elif provider == "anthropic":
return self._handle_anthropic(body, __event_emitter__, actual_model)
elif provider == "google":
return self._handle_google(body, __event_emitter__, actual_model)
return f"Error: Could not parse model format: {model_id}"
def _handle_openai(self, body: Dict, __event_emitter__=None, model_id: str = None):
"""Handle OpenAI requests"""
if not self.valves.OPENAI_API_KEY:
return "Error: OpenAI API key not configured"
if not model_id:
model_id = body["model"].replace("openai.", "").replace("openai/", "")
print(f"DEBUG: OpenAI model_id: {model_id}")
headers = {
"Authorization": f"Bearer {self.valves.OPENAI_API_KEY}",
"Content-Type": "application/json",
}
payload = {**body, "model": model_id}
# Handle o1 and o3 models (no system messages, no streaming)
if model_id.startswith(("o1-", "o3-")):
if "messages" in payload:
payload["messages"] = [
msg for msg in payload["messages"] if msg.get("role") != "system"
]
payload["stream"] = False
try:
if __event_emitter__:
__event_emitter__(
{
"type": "status",
"data": {
"description": "Processing OpenAI request...",
"done": False,
},
}
)
response = requests.post(
f"{self.valves.OPENAI_API_BASE_URL}/chat/completions",
json=payload,
headers=headers,
stream=payload.get("stream", False),
timeout=60,
)
if response.status_code != 200:
return f"OpenAI Error: HTTP {response.status_code}: {response.text}"
if payload.get("stream", False):
return self._stream_openai_response(response)
else:
result = response.json()
return (
result.get("choices", [{}])[0].get("message", {}).get("content", "")
)
except Exception as e:
return f"OpenAI Error: {str(e)}"
def _handle_anthropic(
self, body: Dict, __event_emitter__=None, model_id: str = None
):
"""Handle Anthropic requests"""
if not self.valves.ANTHROPIC_API_KEY:
return "Error: Anthropic API key not configured"
if not model_id:
model_id = body["model"].replace("anthropic.", "").replace("anthropic/", "")
print(f"DEBUG: Anthropic model_id: {model_id}")
system_message, messages = pop_system_message(body["messages"])
headers = {
"x-api-key": self.valves.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
}
payload = {
"model": model_id,
"messages": self._process_anthropic_messages(messages),
"max_tokens": body.get("max_tokens", 4096),
"temperature": body.get("temperature"),
"stream": body.get("stream", False),
}
if system_message:
payload["system"] = str(system_message)
payload = {k: v for k, v in payload.items() if v is not None}
try:
if __event_emitter__:
__event_emitter__(
{
"type": "status",
"data": {
"description": "Processing Anthropic request...",
"done": False,
},
}
)
response = requests.post(
f"{self.valves.ANTHROPIC_API_BASE_URL}/messages",
json=payload,
headers=headers,
stream=payload.get("stream", False),
timeout=60,
)
if response.status_code != 200:
return f"Anthropic Error: HTTP {response.status_code}: {response.text}"
if payload.get("stream", False):
return self._stream_anthropic_response(response)
else:
result = response.json()
return result.get("content", [{}])[0].get("text", "")
except Exception as e:
return f"Anthropic Error: {str(e)}"
def _handle_google(self, body: Dict, __event_emitter__=None, model_id: str = None):
"""Handle Google requests"""
if not self.valves.GOOGLE_API_KEY or not GOOGLE_AVAILABLE:
return "Error: Google API key not configured or google-generativeai not installed"
if not model_id:
model_id = body["model"].replace("google.", "").replace("google/", "")
print(f"DEBUG: Google model_id: {model_id}")
try:
if __event_emitter__:
__event_emitter__(
{
"type": "status",
"data": {
"description": "Processing Google request...",
"done": False,
},
}
)
genai.configure(api_key=self.valves.GOOGLE_API_KEY)
messages = body["messages"]
system_message = next(
(msg["content"] for msg in messages if msg["role"] == "system"), None
)
# Process messages for Google format
contents = []
for message in messages:
if message["role"] != "system":
if isinstance(message.get("content"), list):
parts = []
for content in message["content"]:
if content["type"] == "text":
parts.append({"text": content["text"]})
elif content["type"] == "image_url":
image_url = content["image_url"]["url"]
if image_url.startswith("data:image"):
image_data = image_url.split(",")[1]
parts.append(
{
"inline_data": {
"mime_type": "image/jpeg",
"data": image_data,
}
}
)
contents.append({"role": message["role"], "parts": parts})
else:
contents.append(
{
"role": (
"user" if message["role"] == "user" else "model"
),
"parts": [{"text": message["content"]}],
}
)
# Create model
if system_message and "gemini-1.5" in model_id:
model = genai.GenerativeModel(
model_name=model_id, system_instruction=system_message
)
else:
model = genai.GenerativeModel(model_name=model_id)
generation_config = GenerationConfig(
temperature=body.get("temperature", 0.7),
top_p=body.get("top_p", 0.9),
top_k=body.get("top_k", 40),
max_output_tokens=body.get("max_tokens", 8192),
)
# Safety settings - Default to permissive (no filtering)
safety_settings = None
if self.valves.USE_PERMISSIVE_SAFETY:
safety_settings = {
genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
}
if body.get("stream", False):
def stream_generator():
response = model.generate_content(
contents,
generation_config=generation_config,
safety_settings=safety_settings,
stream=True,
)
for chunk in response:
if chunk.text:
yield chunk.text
return stream_generator()
else:
response = model.generate_content(
contents,
generation_config=generation_config,
safety_settings=safety_settings,
stream=False,
)
return response.text
except Exception as e:
return f"Google Error: {str(e)}"
def _process_anthropic_messages(self, messages: List[dict]) -> List[dict]:
"""Process messages for Anthropic format"""
processed_messages = []
for message in messages:
if isinstance(message["content"], str):
content = [{"type": "text", "text": message["content"]}]
else:
content = []
for item in message["content"]:
if item["type"] == "text":
content.append({"type": "text", "text": item["text"]})
elif item["type"] == "image_url":
if item["image_url"]["url"].startswith("data:image"):
mime_type, base64_data = item["image_url"]["url"].split(
",", 1
)
media_type = mime_type.split(":")[1].split(";")[0]
content.append(
{
"type": "image",
"source": {
"type": "base64",
"media_type": media_type,
"data": base64_data,
},
}
)
processed_messages.append({"role": message["role"], "content": content})
return processed_messages
def _stream_openai_response(self, response):
"""Stream OpenAI response"""
for line in response.iter_lines():
if line:
line = line.decode("utf-8")
if line.startswith("data: "):
data = line[6:]
if data.strip() == "[DONE]":
break
try:
json_data = json.loads(data)
if "choices" in json_data and json_data["choices"]:
delta = json_data["choices"][0].get("delta", {})
if "content" in delta:
yield delta["content"]
except json.JSONDecodeError:
continue
def _stream_anthropic_response(self, response):
"""Stream Anthropic response"""
for line in response.iter_lines():
if line and line.startswith(b"data: "):
try:
data = json.loads(line[6:])
if (
data["type"] == "content_block_delta"
and "text" in data["delta"]
):
yield data["delta"]["text"]
elif data["type"] == "message_stop":
break
except json.JSONDecodeError:
continue
## 🌟 Repository
#Find the latest version and contribute at: https://github.com/codemonkeying/multi-provider-external
## 📝 License
#MIT License - Feel free to modify and distribute!
#---
#**Built by codemonkeying** | Version 1.2.1