aibridgecore 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aibridgecore/__init__.py +40 -0
- aibridgecore/ai_services/__init__.py +0 -0
- aibridgecore/ai_services/ai21labs_text.py +271 -0
- aibridgecore/ai_services/ai_abstraction.py +28 -0
- aibridgecore/ai_services/ai_services_response.py +33 -0
- aibridgecore/ai_services/anthropic_ai.py +362 -0
- aibridgecore/ai_services/cohere_llm.py +338 -0
- aibridgecore/ai_services/geminin_services.py +407 -0
- aibridgecore/ai_services/image_optimisaton.py +92 -0
- aibridgecore/ai_services/ollama_services.py +325 -0
- aibridgecore/ai_services/openai_images.py +184 -0
- aibridgecore/ai_services/openai_services.py +332 -0
- aibridgecore/ai_services/process_mq.py +51 -0
- aibridgecore/ai_services/stable_diffusion_image.py +168 -0
- aibridgecore/constant/__init__.py +0 -0
- aibridgecore/constant/common.py +258 -0
- aibridgecore/constant/constant.py +41 -0
- aibridgecore/database/__init__.py +0 -0
- aibridgecore/database/db_layer.py +33 -0
- aibridgecore/database/models/__init__.py +0 -0
- aibridgecore/database/models/ai_response.py +13 -0
- aibridgecore/database/models/prompts.py +12 -0
- aibridgecore/database/models/variables.py +11 -0
- aibridgecore/database/no_sql_service.py +103 -0
- aibridgecore/database/session.py +29 -0
- aibridgecore/database/sql_service.py +137 -0
- aibridgecore/database/table_oprations.py +18 -0
- aibridgecore/exceptions.py +185 -0
- aibridgecore/output_validation/__init__.py +0 -0
- aibridgecore/output_validation/active_validator.py +13 -0
- aibridgecore/output_validation/convertors.py +65 -0
- aibridgecore/output_validation/validations.py +155 -0
- aibridgecore/prompts/__init__.py +0 -0
- aibridgecore/prompts/prompt_completion.py +74 -0
- aibridgecore/prompts/prompts_save.py +105 -0
- aibridgecore/prompts/prompts_varibales.py +74 -0
- aibridgecore/queue_integration/__init__.py +0 -0
- aibridgecore/queue_integration/assign_queue.py +13 -0
- aibridgecore/queue_integration/message_queue.py +26 -0
- aibridgecore/queue_integration/queue_model.py +110 -0
- aibridgecore/queue_integration/response_class.py +175 -0
- aibridgecore/setconfig.py +93 -0
- aibridgecore-1.0.1.dist-info/LICENSE +202 -0
- aibridgecore-1.0.1.dist-info/METADATA +582 -0
- aibridgecore-1.0.1.dist-info/RECORD +47 -0
- aibridgecore-1.0.1.dist-info/WHEEL +5 -0
- aibridgecore-1.0.1.dist-info/top_level.txt +1 -0
aibridgecore/__init__.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from aibridgecore.ai_services.openai_services import OpenAIService
|
|
2
|
+
from aibridgecore.exceptions import ConfigException, OpenAIException, PromptSaveException
|
|
3
|
+
from aibridgecore.setconfig import SetConfig
|
|
4
|
+
import aibridgecore.exceptions as exceptions
|
|
5
|
+
from aibridgecore.prompts.prompts_save import PromptInsertion
|
|
6
|
+
from aibridgecore.prompts.prompts_varibales import VariableInsertion
|
|
7
|
+
from aibridgecore.ai_services.ai_services_response import FetchAIResponse
|
|
8
|
+
from aibridgecore.queue_integration.message_queue import MessageQ
|
|
9
|
+
from aibridgecore.output_validation.active_validator import ActiveValidator
|
|
10
|
+
from aibridgecore.output_validation.validations import Validation
|
|
11
|
+
from aibridgecore.ai_services.openai_images import OpenAIImage
|
|
12
|
+
from aibridgecore.ai_services.stable_diffusion_image import StableDiffusion
|
|
13
|
+
from aibridgecore.ai_services.cohere_llm import CohereApi
|
|
14
|
+
from aibridgecore.ai_services.ai21labs_text import AI21labsText
|
|
15
|
+
from aibridgecore.ai_services.geminin_services import GeminiAIService
|
|
16
|
+
from aibridgecore.ai_services.anthropic_ai import AnthropicService
|
|
17
|
+
from aibridgecore.ai_services.ollama_services import OllamaService
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"OpenAIService",
|
|
21
|
+
"SetConfig",
|
|
22
|
+
"COnfigException",
|
|
23
|
+
"OpenAIException",
|
|
24
|
+
"PromptSaveException",
|
|
25
|
+
"VariableInsertion",
|
|
26
|
+
"PromptInsertion",
|
|
27
|
+
"FetchAIResponse",
|
|
28
|
+
"MessageQ",
|
|
29
|
+
"ActiveValidator",
|
|
30
|
+
"Validation",
|
|
31
|
+
"OpenAIImage",
|
|
32
|
+
"StableDiffusion",
|
|
33
|
+
"CohereApi",
|
|
34
|
+
"AI21labsText",
|
|
35
|
+
"GeminiAIService",
|
|
36
|
+
"AnthropicService",
|
|
37
|
+
"OllamaService"
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
__version__ = "0.0.0"
|
|
File without changes
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import uuid
|
|
3
|
+
from aibridgecore.exceptions import Ai21Exception, AIBridgeException, ValidationException
|
|
4
|
+
from aibridgecore.prompts.prompt_completion import Completion
|
|
5
|
+
from aibridgecore.ai_services.ai_abstraction import AIInterface
|
|
6
|
+
from aibridgecore.output_validation.active_validator import ActiveValidator
|
|
7
|
+
import json
|
|
8
|
+
from aibridgecore.constant.common import parse_fromat, parse_api_key
|
|
9
|
+
from ai21 import AI21Client
|
|
10
|
+
from ai21.models.chat import ChatMessage,UserMessage
|
|
11
|
+
from aibridgecore.output_validation.convertors import FromJson, IntoJson
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class AI21labsText(AIInterface):
|
|
15
|
+
"""
|
|
16
|
+
Base class for Ai21 labs
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
@classmethod
|
|
20
|
+
def generate(
|
|
21
|
+
self,
|
|
22
|
+
prompts: list[str] = [],
|
|
23
|
+
prompt_ids: list[str] = [],
|
|
24
|
+
prompt_data: list[dict] = [],
|
|
25
|
+
variables: list[dict] = [],
|
|
26
|
+
output_format: list[str] = [],
|
|
27
|
+
format_strcture: list[str] = [],
|
|
28
|
+
model="jamba-instruct-preview",
|
|
29
|
+
variation_count: int = 1,
|
|
30
|
+
max_tokens: int = None,
|
|
31
|
+
temperature: float = 0.5,
|
|
32
|
+
message_queue=False,
|
|
33
|
+
api_key=None,
|
|
34
|
+
output_format_parse=True,
|
|
35
|
+
context=[],
|
|
36
|
+
):
|
|
37
|
+
try:
|
|
38
|
+
if prompts and prompt_ids:
|
|
39
|
+
raise Ai21Exception(
|
|
40
|
+
"please provide either prompts or prompts ids at a time"
|
|
41
|
+
)
|
|
42
|
+
if not prompts and not prompt_ids:
|
|
43
|
+
raise Ai21Exception(
|
|
44
|
+
"Either provide prompts or prompts ids to generate the data"
|
|
45
|
+
)
|
|
46
|
+
if prompt_ids:
|
|
47
|
+
prompts_list = Completion.create_prompt_from_id(
|
|
48
|
+
prompt_ids=prompt_ids,
|
|
49
|
+
prompt_data_list=prompt_data,
|
|
50
|
+
variables_list=variables,
|
|
51
|
+
)
|
|
52
|
+
if prompts:
|
|
53
|
+
if prompt_data or variables:
|
|
54
|
+
prompts_list = Completion.create_prompt(
|
|
55
|
+
prompt_list=prompts,
|
|
56
|
+
prompt_data_list=prompt_data,
|
|
57
|
+
variables_list=variables,
|
|
58
|
+
)
|
|
59
|
+
else:
|
|
60
|
+
prompts_list = prompts
|
|
61
|
+
if output_format:
|
|
62
|
+
if len(output_format) != len(prompts_list):
|
|
63
|
+
raise ValidationException(
|
|
64
|
+
"length of output_format must be equal to length of the prompts"
|
|
65
|
+
)
|
|
66
|
+
if format_strcture:
|
|
67
|
+
if len(format_strcture) != len(prompts_list):
|
|
68
|
+
raise ValidationException(
|
|
69
|
+
"length of format_strcture must be equal to length of the prompts"
|
|
70
|
+
)
|
|
71
|
+
updated_prompts = []
|
|
72
|
+
for _prompt in prompts_list:
|
|
73
|
+
format = None
|
|
74
|
+
format_str = None
|
|
75
|
+
if output_format:
|
|
76
|
+
format = output_format[prompts_list.index(_prompt)]
|
|
77
|
+
if format_strcture:
|
|
78
|
+
format_str = format_strcture[prompts_list.index(_prompt)]
|
|
79
|
+
if output_format_parse:
|
|
80
|
+
u_prompt = parse_fromat(
|
|
81
|
+
_prompt, format=format, format_structure=format_str
|
|
82
|
+
)
|
|
83
|
+
updated_prompts.append(u_prompt)
|
|
84
|
+
if not updated_prompts:
|
|
85
|
+
updated_prompts = prompts_list
|
|
86
|
+
if message_queue:
|
|
87
|
+
id = uuid.uuid4()
|
|
88
|
+
message_data = {
|
|
89
|
+
"id": str(id),
|
|
90
|
+
"prompts": json.dumps(updated_prompts),
|
|
91
|
+
"model": model,
|
|
92
|
+
"variation_count": variation_count,
|
|
93
|
+
"max_tokens": max_tokens,
|
|
94
|
+
"temperature": temperature,
|
|
95
|
+
"ai_service": "ai21_api",
|
|
96
|
+
"output_format": json.dumps(output_format),
|
|
97
|
+
"format_structure": json.dumps(format_strcture),
|
|
98
|
+
"api_key": api_key,
|
|
99
|
+
"context": context,
|
|
100
|
+
}
|
|
101
|
+
message = {"data": json.dumps(message_data)}
|
|
102
|
+
from aibridgecore.queue_integration.message_queue import MessageQ
|
|
103
|
+
|
|
104
|
+
MessageQ.mq_enque(message=message)
|
|
105
|
+
return {"response_id": str(id)}
|
|
106
|
+
return self.get_response(
|
|
107
|
+
updated_prompts,
|
|
108
|
+
model,
|
|
109
|
+
variation_count,
|
|
110
|
+
max_tokens,
|
|
111
|
+
temperature,
|
|
112
|
+
output_format,
|
|
113
|
+
format_strcture,
|
|
114
|
+
api_key=api_key,
|
|
115
|
+
context=context,
|
|
116
|
+
)
|
|
117
|
+
except Exception as e:
|
|
118
|
+
raise Ai21Exception(e)
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def get_prompt_context(self, context):
|
|
122
|
+
context_data = []
|
|
123
|
+
if context:
|
|
124
|
+
for _context in context:
|
|
125
|
+
if _context["role"] not in ["user", "system", "assistant"]:
|
|
126
|
+
raise Ai21Exception(
|
|
127
|
+
"Invalid role provided. Please provide either user or system, assistant"
|
|
128
|
+
)
|
|
129
|
+
context_data.append(
|
|
130
|
+
ChatMessage(
|
|
131
|
+
role = _context["role"],
|
|
132
|
+
content = _context["context"]
|
|
133
|
+
)
|
|
134
|
+
)
|
|
135
|
+
print(context_data,"xxxxxxxxxxxxxxxx")
|
|
136
|
+
return context_data
|
|
137
|
+
|
|
138
|
+
@classmethod
|
|
139
|
+
def get_response(
|
|
140
|
+
self,
|
|
141
|
+
prompts,
|
|
142
|
+
model="jamba-instruct-preview",
|
|
143
|
+
variation_count=1,
|
|
144
|
+
max_tokens=None,
|
|
145
|
+
temperature=0.5,
|
|
146
|
+
output_format=[],
|
|
147
|
+
format_structure=[],
|
|
148
|
+
api_key=None,
|
|
149
|
+
context=[],
|
|
150
|
+
):
|
|
151
|
+
try:
|
|
152
|
+
if output_format:
|
|
153
|
+
if isinstance(output_format, str):
|
|
154
|
+
output_format = json.loads(output_format)
|
|
155
|
+
if format_structure:
|
|
156
|
+
if isinstance(format_structure, str):
|
|
157
|
+
format_structure = json.loads(format_structure)
|
|
158
|
+
if not prompts:
|
|
159
|
+
raise Ai21Exception("No prompts provided")
|
|
160
|
+
api_key = api_key if api_key else parse_api_key("ai21_api")
|
|
161
|
+
client = AI21Client(api_key=api_key)
|
|
162
|
+
model_output = []
|
|
163
|
+
token_used = 0
|
|
164
|
+
input_tokens = 0
|
|
165
|
+
output_tokens = 0
|
|
166
|
+
context_data = self.get_prompt_context(context)
|
|
167
|
+
for index, prompt in enumerate(prompts):
|
|
168
|
+
context_data.append(ChatMessage(
|
|
169
|
+
role = "user",
|
|
170
|
+
content = prompt
|
|
171
|
+
))
|
|
172
|
+
if max_tokens:
|
|
173
|
+
response = client.chat.completions.create(
|
|
174
|
+
messages=context_data,
|
|
175
|
+
model=model,
|
|
176
|
+
max_tokens= max_tokens,
|
|
177
|
+
temperature=temperature,
|
|
178
|
+
n=variation_count,
|
|
179
|
+
top_p=1,
|
|
180
|
+
response_format={"type":"json_object"}
|
|
181
|
+
)
|
|
182
|
+
else:
|
|
183
|
+
response = client.chat.completions.create(
|
|
184
|
+
messages=context_data,
|
|
185
|
+
model=model,
|
|
186
|
+
temperature=temperature,
|
|
187
|
+
n=variation_count,
|
|
188
|
+
top_p=1,
|
|
189
|
+
response_format={"type":"json_object"}
|
|
190
|
+
)
|
|
191
|
+
input_tokens = input_tokens + response.usage.prompt_tokens
|
|
192
|
+
output_tokens = output_tokens +response.usage.completion_tokens
|
|
193
|
+
token_used = token_used + response.usage.total_tokens
|
|
194
|
+
print(response)
|
|
195
|
+
for res in response.choices:
|
|
196
|
+
content = res.message.content
|
|
197
|
+
try:
|
|
198
|
+
content = json.loads(content)
|
|
199
|
+
except json.JSONDecodeError:
|
|
200
|
+
if "```json" in content:
|
|
201
|
+
try:
|
|
202
|
+
json_content = content.split("```json")[1].split("```")[0]
|
|
203
|
+
content = json.loads(json_content.strip())
|
|
204
|
+
except (IndexError, json.JSONDecodeError):
|
|
205
|
+
pass
|
|
206
|
+
elif "```" in content:
|
|
207
|
+
try:
|
|
208
|
+
json_content = content.split("```")[1].split("```")[0]
|
|
209
|
+
content = json.loads(json_content.strip())
|
|
210
|
+
except (IndexError, json.JSONDecodeError):
|
|
211
|
+
pass
|
|
212
|
+
print(content, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
|
|
213
|
+
if output_format:
|
|
214
|
+
_formatter = output_format[index]
|
|
215
|
+
try:
|
|
216
|
+
if _formatter == "csv":
|
|
217
|
+
content=json.loads(content)
|
|
218
|
+
if isinstance(content, list):
|
|
219
|
+
if len(content) == 1:
|
|
220
|
+
content = content[0]
|
|
221
|
+
if isinstance(content, str):
|
|
222
|
+
content = json.loads(content)
|
|
223
|
+
content = FromJson.json_to_csv(content)
|
|
224
|
+
elif _formatter == "xml":
|
|
225
|
+
content=json.loads(content)
|
|
226
|
+
if isinstance(content, list):
|
|
227
|
+
if len(content) == 1:
|
|
228
|
+
content = content[0]
|
|
229
|
+
if isinstance(content, str):
|
|
230
|
+
content = json.loads(content)
|
|
231
|
+
content = FromJson.json_to_xml(content)
|
|
232
|
+
except AIBridgeException as e:
|
|
233
|
+
raise ValidationException(
|
|
234
|
+
f"Ai response is not in valid {_formatter}"
|
|
235
|
+
)
|
|
236
|
+
if _formatter == "json":
|
|
237
|
+
_validate_obj = ActiveValidator.get_active_validator(
|
|
238
|
+
_formatter
|
|
239
|
+
)
|
|
240
|
+
try:
|
|
241
|
+
content = _validate_obj.validate(
|
|
242
|
+
content,
|
|
243
|
+
schema=(
|
|
244
|
+
format_structure[index]
|
|
245
|
+
if format_structure
|
|
246
|
+
else None
|
|
247
|
+
),
|
|
248
|
+
)
|
|
249
|
+
except AIBridgeException as e:
|
|
250
|
+
content_error = {
|
|
251
|
+
"error": f"{e}",
|
|
252
|
+
"ai_response": content,
|
|
253
|
+
}
|
|
254
|
+
content = json.dumps(content_error)
|
|
255
|
+
if index >= len(model_output):
|
|
256
|
+
model_output.append({"data": [content]})
|
|
257
|
+
else:
|
|
258
|
+
model_output[index]["data"].append(content)
|
|
259
|
+
message_value = {
|
|
260
|
+
"items": {
|
|
261
|
+
"response": model_output,
|
|
262
|
+
"input_tokens": input_tokens,
|
|
263
|
+
"output_tokens": output_tokens,
|
|
264
|
+
"token_used": token_used,
|
|
265
|
+
"created_at": time.time(),
|
|
266
|
+
"ai_service": "ai21_api",
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return message_value
|
|
270
|
+
except Exception as e:
|
|
271
|
+
raise Ai21Exception(e)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class AIInterface(ABC):
|
|
5
|
+
@abstractmethod
|
|
6
|
+
def generate(
|
|
7
|
+
self,
|
|
8
|
+
prompts,
|
|
9
|
+
prompt_ids,
|
|
10
|
+
prompt_data,
|
|
11
|
+
variables,
|
|
12
|
+
model,
|
|
13
|
+
variation_count,
|
|
14
|
+
max_tokens,
|
|
15
|
+
temperature,
|
|
16
|
+
):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
@abstractmethod
|
|
20
|
+
def get_response(
|
|
21
|
+
self,
|
|
22
|
+
prompts,
|
|
23
|
+
model,
|
|
24
|
+
variation_count,
|
|
25
|
+
max_tokens,
|
|
26
|
+
temperature,
|
|
27
|
+
):
|
|
28
|
+
pass
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from aibridgecore.database.models.ai_response import AIResponse, Base
|
|
2
|
+
import json
|
|
3
|
+
import uuid
|
|
4
|
+
import calendar
|
|
5
|
+
import time
|
|
6
|
+
from aibridgecore.exceptions import AIResponseException
|
|
7
|
+
from aibridgecore.database.db_layer import DBLayer
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class FetchAIResponse:
|
|
11
|
+
@classmethod
|
|
12
|
+
def get_time(self):
|
|
13
|
+
return calendar.timegm(time.gmtime())
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def save_response(self, response_data, model, id):
|
|
17
|
+
table_exist = DBLayer.check_table(AIResponse, Base, method="save")
|
|
18
|
+
res = {
|
|
19
|
+
"id": id,
|
|
20
|
+
"response": json.dumps(response_data),
|
|
21
|
+
"model": model,
|
|
22
|
+
"updated_at": self.get_time(),
|
|
23
|
+
"created_at": self.get_time(),
|
|
24
|
+
}
|
|
25
|
+
data = DBLayer.save(AIResponse, Base, res)
|
|
26
|
+
|
|
27
|
+
@classmethod
|
|
28
|
+
def get_response(self, id):
|
|
29
|
+
try:
|
|
30
|
+
res = DBLayer.get_by_id(AIResponse, id)
|
|
31
|
+
except:
|
|
32
|
+
return {"messgae": "response in process"}
|
|
33
|
+
return res
|