1
- import os
2
- import uuid
3
- import json
4
1
import base64
2
+ import json
5
3
import mimetypes
4
+ import os
5
+ import re
6
+ import uuid
6
7
from io import BytesIO
7
- from typing import List , Literal , Optional , Union , Dict , Type
8
+ from typing import Dict , List , Literal , Optional , Type , Union
8
9
9
10
import httpx
11
+ from loaders .data_loader import ProviderDataEnum
12
+ from loaders .loaders import load_provider
10
13
from pydantic import BaseModel
11
- from edenai_apis .utils .upload_s3 import upload_file_bytes_to_s3
12
- from edenai_apis .llmengine .types .response_types import (
13
- ResponseModel ,
14
+
15
+ from edenai_apis .features .image import (
16
+ ExplicitContentDataClass ,
17
+ GeneratedImageDataClass ,
18
+ GenerationDataClass ,
19
+ LogoDetectionDataClass ,
20
+ QuestionAnswerDataClass ,
14
21
)
15
- from edenai_apis .llmengine .clients import LLM_COMPLETION_CLIENTS
16
- from edenai_apis .llmengine .clients .completion import CompletionClient
17
- from edenai_apis .llmengine .mapping import Mappings
18
- from edenai_apis .utils .types import ResponseType
19
- from edenai_apis .utils .exception import ProviderException
20
- from edenai_apis .features .translation import (
21
- AutomaticTranslationDataClass ,
22
- LanguageDetectionDataClass ,
22
+ from edenai_apis .features .multimodal .chat import (
23
+ ChatDataClass as ChatMultimodalDataClass ,
24
+ )
25
+ from edenai_apis .features .multimodal .chat import (
26
+ ChatStreamResponse as ChatMultimodalStreamResponse ,
23
27
)
28
+ from edenai_apis .features .multimodal .chat import StreamChat as StreamMultimodalChat
24
29
from edenai_apis .features .text import (
25
- SummarizeDataClass ,
26
- TopicExtractionDataClass ,
27
- SpellCheckDataClass ,
28
- SentimentAnalysisDataClass ,
29
- KeywordExtractionDataClass ,
30
30
AnonymizationDataClass ,
31
- NamedEntityRecognitionDataClass ,
32
31
CodeGenerationDataClass ,
32
+ CustomClassificationDataClass ,
33
+ CustomNamedEntityRecognitionDataClass ,
33
34
EmbeddingDataClass ,
34
35
EmbeddingsDataClass ,
36
+ KeywordExtractionDataClass ,
35
37
ModerationDataClass ,
38
+ NamedEntityRecognitionDataClass ,
39
+ SentimentAnalysisDataClass ,
40
+ SpellCheckDataClass ,
41
+ SummarizeDataClass ,
36
42
TextModerationItem ,
37
- CustomClassificationDataClass ,
38
- CustomNamedEntityRecognitionDataClass ,
39
- )
40
- from edenai_apis .features .text .moderation .category import (
41
- CategoryType as CategoryTypeModeration ,
42
- )
43
- from edenai_apis .utils .conversion import standardized_confidence_score
44
- from edenai_apis .features .image import (
45
- LogoDetectionDataClass ,
46
- QuestionAnswerDataClass ,
47
- ExplicitContentDataClass ,
48
- GeneratedImageDataClass ,
49
- GenerationDataClass ,
43
+ TopicExtractionDataClass ,
50
44
)
51
45
from edenai_apis .features .text .chat import ChatDataClass , ChatMessageDataClass
52
46
from edenai_apis .features .text .chat .chat_dataclass import (
53
- StreamChat ,
54
47
ChatStreamResponse ,
48
+ StreamChat ,
55
49
ToolCall ,
56
50
)
57
- from edenai_apis .features .multimodal .chat import (
58
- ChatDataClass as ChatMultimodalDataClass ,
59
- StreamChat as StreamMultimodalChat ,
60
- ChatStreamResponse as ChatMultimodalStreamResponse ,
51
+ from edenai_apis .features .text .moderation .category import (
52
+ CategoryType as CategoryTypeModeration ,
53
+ )
54
+ from edenai_apis .features .translation import (
55
+ AutomaticTranslationDataClass ,
56
+ LanguageDetectionDataClass ,
61
57
)
58
+ from edenai_apis .llmengine .clients import LLM_COMPLETION_CLIENTS
59
+ from edenai_apis .llmengine .clients .completion import CompletionClient
60
+ from edenai_apis .llmengine .mapping import Mappings
62
61
from edenai_apis .llmengine .prompts import BasePrompt
62
+ from edenai_apis .llmengine .types .response_types import ResponseModel
63
63
from edenai_apis .llmengine .utils .moderation import moderate
64
- from loaders .data_loader import ProviderDataEnum
65
- from loaders .loaders import load_provider
66
- import re
64
+ from edenai_apis .utils .conversion import standardized_confidence_score
65
+ from edenai_apis .utils .exception import ProviderException
66
+ from edenai_apis .utils .types import ResponseType
67
+ from edenai_apis .utils .upload_s3 import upload_file_bytes_to_s3
67
68
68
69
69
70
class LLMEngine :
@@ -184,7 +185,7 @@ def chat(
184
185
usage = response .usage ,
185
186
)
186
187
else :
187
- stream = (
188
+ stream_response = (
188
189
ChatStreamResponse (
189
190
text = chunk .choices [0 ].delta .content or "" ,
190
191
blocked = False ,
@@ -195,7 +196,8 @@ def chat(
195
196
)
196
197
197
198
return ResponseType [StreamChat ](
198
- original_response = None , standardized_response = StreamChat (stream = stream )
199
+ original_response = None ,
200
+ standardized_response = StreamChat (stream = stream_response ),
199
201
)
200
202
201
203
@moderate
@@ -238,8 +240,8 @@ def multimodal_chat(
238
240
args ["response_format" ] = response_format
239
241
args ["drop_invalid_params" ] = True
240
242
response = self .completion_client .completion (** args , ** kwargs )
241
- response = ResponseModel .model_validate (response )
242
243
if stream is False :
244
+ response = ResponseModel .model_validate (response )
243
245
generated_text = (
244
246
response .choices [0 ].message .content or "" if response .choices else ""
245
247
)
@@ -256,9 +258,9 @@ def multimodal_chat(
256
258
)
257
259
258
260
else :
259
- stream = (
261
+ stream_response = (
260
262
ChatMultimodalStreamResponse (
261
- text = chunk ["choices" ][0 ]["delta" ].get ("content" , "" ) ,
263
+ text = chunk ["choices" ][0 ]["delta" ].get ("content" ) or "" ,
262
264
blocked = not chunk ["choices" ][0 ].get ("finish_reason" )
263
265
in (None , "stop" ),
264
266
provider = self .provider_name ,
@@ -269,7 +271,7 @@ def multimodal_chat(
269
271
270
272
return ResponseType [StreamMultimodalChat ](
271
273
original_response = None ,
272
- standardized_response = StreamMultimodalChat (stream = stream ),
274
+ standardized_response = StreamMultimodalChat (stream = stream_response ),
273
275
)
274
276
275
277
def summarize (
@@ -820,12 +822,12 @@ def completion(
820
822
os .environ ["GOOGLE_APPLICATION_CREDENTIALS" ] = location
821
823
elif is_gemini :
822
824
api_settings = load_provider (
823
- ProviderDataEnum .KEY , provider_name , api_keys = api_key
825
+ ProviderDataEnum .KEY , provider_name = provider_name , api_keys = api_key
824
826
)
825
827
api_key = api_settings ["genai_api_key" ]
826
828
else :
827
829
api_settings = load_provider (
828
- ProviderDataEnum .KEY , provider_name , api_keys = api_key
830
+ ProviderDataEnum .KEY , provider_name = provider_name , api_keys = api_key
829
831
)
830
832
api_key = api_settings ["api_key" ]
831
833
try :
0 commit comments