trustgraph-bedrock 0.11.11__tar.gz

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.
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: trustgraph-bedrock
3
+ Version: 0.11.11
4
+ Summary: TrustGraph provides a means to run a pipeline of flexible AI processing components in a flexible means to achieve a processing pipeline.
5
+ Home-page: https://github.com/trustgraph-ai/trustgraph
6
+ Download-URL: https://github.com/trustgraph-ai/trustgraph/archive/refs/tags/v0.11.11.tar.gz
7
+ Author: trustgraph.ai
8
+ Author-email: security@trustgraph.ai
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ Requires-Dist: trustgraph-base
15
+ Requires-Dist: pulsar-client
16
+ Requires-Dist: prometheus-client
17
+ Requires-Dist: boto3
18
+
19
+ See https://trustgraph.ai/
@@ -0,0 +1 @@
1
+ See https://trustgraph.ai/
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env python3
2
+
3
+ from trustgraph.model.text_completion.bedrock import run
4
+
5
+ run()
6
+
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,45 @@
1
+ import setuptools
2
+ import os
3
+ import importlib
4
+
5
+ with open("README.md", "r") as fh:
6
+ long_description = fh.read()
7
+
8
+ # Load a version number module
9
+ spec = importlib.util.spec_from_file_location(
10
+ 'version', 'trustgraph/bedrock_version.py'
11
+ )
12
+ version_module = importlib.util.module_from_spec(spec)
13
+ spec.loader.exec_module(version_module)
14
+
15
+ version = version_module.__version__
16
+
17
+ setuptools.setup(
18
+ name="trustgraph-bedrock",
19
+ version=version,
20
+ author="trustgraph.ai",
21
+ author_email="security@trustgraph.ai",
22
+ description="TrustGraph provides a means to run a pipeline of flexible AI processing components in a flexible means to achieve a processing pipeline.",
23
+ long_description=long_description,
24
+ long_description_content_type="text/markdown",
25
+ url="https://github.com/trustgraph-ai/trustgraph",
26
+ packages=setuptools.find_namespace_packages(
27
+ where='./',
28
+ ),
29
+ classifiers=[
30
+ "Programming Language :: Python :: 3",
31
+ "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
32
+ "Operating System :: OS Independent",
33
+ ],
34
+ python_requires='>=3.8',
35
+ download_url = "https://github.com/trustgraph-ai/trustgraph/archive/refs/tags/v" + version + ".tar.gz",
36
+ install_requires=[
37
+ "trustgraph-base",
38
+ "pulsar-client",
39
+ "prometheus-client",
40
+ "boto3",
41
+ ],
42
+ scripts=[
43
+ "scripts/text-completion-bedrock",
44
+ ]
45
+ )
@@ -0,0 +1 @@
1
+ __version__ = "0.11.11"
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env python3
2
+
3
+ from . llm import run
4
+
5
+ if __name__ == '__main__':
6
+ run()
7
+
@@ -0,0 +1,323 @@
1
+
2
+ """
3
+ Simple LLM service, performs text prompt completion using AWS Bedrock.
4
+ Input is prompt, output is response. Mistral is default.
5
+ """
6
+
7
+ import boto3
8
+ import json
9
+ from prometheus_client import Histogram
10
+
11
+ from .... schema import TextCompletionRequest, TextCompletionResponse, Error
12
+ from .... schema import text_completion_request_queue
13
+ from .... schema import text_completion_response_queue
14
+ from .... log_level import LogLevel
15
+ from .... base import ConsumerProducer
16
+ from .... exceptions import TooManyRequests
17
+
18
+ module = ".".join(__name__.split(".")[1:-1])
19
+
20
+ default_input_queue = text_completion_request_queue
21
+ default_output_queue = text_completion_response_queue
22
+ default_subscriber = module
23
+ default_model = 'mistral.mistral-large-2407-v1:0'
24
+ default_region = 'us-west-2'
25
+ default_temperature = 0.0
26
+ default_max_output = 2048
27
+
28
+
29
+ class Processor(ConsumerProducer):
30
+
31
+ def __init__(self, **params):
32
+
33
+ input_queue = params.get("input_queue", default_input_queue)
34
+ output_queue = params.get("output_queue", default_output_queue)
35
+ subscriber = params.get("subscriber", default_subscriber)
36
+ model = params.get("model", default_model)
37
+ aws_id = params.get("aws_id_key")
38
+ aws_secret = params.get("aws_secret")
39
+ aws_region = params.get("aws_region", default_region)
40
+ temperature = params.get("temperature", default_temperature)
41
+ max_output = params.get("max_output", default_max_output)
42
+
43
+ super(Processor, self).__init__(
44
+ **params | {
45
+ "input_queue": input_queue,
46
+ "output_queue": output_queue,
47
+ "subscriber": subscriber,
48
+ "input_schema": TextCompletionRequest,
49
+ "output_schema": TextCompletionResponse,
50
+ "model": model,
51
+ "temperature": temperature,
52
+ "max_output": max_output,
53
+ }
54
+ )
55
+
56
+ if not hasattr(__class__, "text_completion_metric"):
57
+ __class__.text_completion_metric = Histogram(
58
+ 'text_completion_duration',
59
+ 'Text completion duration (seconds)',
60
+ buckets=[
61
+ 0.25, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
62
+ 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
63
+ 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0,
64
+ 30.0, 35.0, 40.0, 45.0, 50.0, 60.0, 80.0, 100.0,
65
+ 120.0
66
+ ]
67
+ )
68
+
69
+ self.model = model
70
+ self.temperature = temperature
71
+ self.max_output = max_output
72
+
73
+ self.session = boto3.Session(
74
+ aws_access_key_id=aws_id,
75
+ aws_secret_access_key=aws_secret,
76
+ region_name=aws_region
77
+ )
78
+
79
+ self.bedrock = self.session.client(service_name='bedrock-runtime')
80
+
81
+ print("Initialised", flush=True)
82
+
83
+ def handle(self, msg):
84
+
85
+ v = msg.value()
86
+
87
+ # Sender-produced ID
88
+
89
+ id = msg.properties()["id"]
90
+
91
+ print(f"Handling prompt {id}...", flush=True)
92
+
93
+ prompt = v.prompt
94
+
95
+ try:
96
+
97
+ # Mistral Input Format
98
+ if self.model.startswith("mistral"):
99
+ promptbody = json.dumps({
100
+ "prompt": prompt,
101
+ "max_tokens": self.max_output,
102
+ "temperature": self.temperature,
103
+ "top_p": 0.99,
104
+ "top_k": 40
105
+ })
106
+
107
+ # Llama 3.1 Input Format
108
+ elif self.model.startswith("meta"):
109
+ promptbody = json.dumps({
110
+ "prompt": prompt,
111
+ "max_gen_len": self.max_output,
112
+ "temperature": self.temperature,
113
+ "top_p": 0.95,
114
+ })
115
+
116
+ # Anthropic Input Format
117
+ elif self.model.startswith("anthropic"):
118
+ promptbody = json.dumps({
119
+ "anthropic_version": "bedrock-2023-05-31",
120
+ "max_tokens": self.max_output,
121
+ "temperature": self.temperature,
122
+ "top_p": 0.999,
123
+ "messages": [
124
+ {
125
+ "role": "user",
126
+ "content": [
127
+ {
128
+ "type": "text",
129
+ "text": prompt
130
+ }
131
+ ]
132
+ }
133
+ ]
134
+ })
135
+
136
+ # Jamba Input Format
137
+ elif self.model.startswith("ai21"):
138
+ promptbody = json.dumps({
139
+ "max_tokens": self.max_output,
140
+ "temperature": self.temperature,
141
+ "top_p": 0.9,
142
+ "messages": [
143
+ {
144
+ "role": "user",
145
+ "content": prompt
146
+ }
147
+ ]
148
+ })
149
+
150
+ # Cohere Input Format
151
+ elif self.model.startswith("cohere"):
152
+ promptbody = json.dumps({
153
+ "max_tokens": self.max_output,
154
+ "temperature": self.temperature,
155
+ "message": prompt
156
+ })
157
+
158
+ # Use Mistral format as defualt
159
+ else:
160
+ promptbody = json.dumps({
161
+ "prompt": prompt,
162
+ "max_tokens": self.max_output,
163
+ "temperature": self.temperature,
164
+ "top_p": 0.99,
165
+ "top_k": 40
166
+ })
167
+
168
+ accept = 'application/json'
169
+ contentType = 'application/json'
170
+
171
+ # FIXME: Consider catching request limits and raise TooManyRequests
172
+ # See https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html
173
+
174
+ with __class__.text_completion_metric.time():
175
+ response = self.bedrock.invoke_model(
176
+ body=promptbody, modelId=self.model, accept=accept,
177
+ contentType=contentType
178
+ )
179
+
180
+ # Mistral Response Structure
181
+ if self.model.startswith("mistral"):
182
+ response_body = json.loads(response.get("body").read())
183
+ outputtext = response_body['outputs'][0]['text']
184
+
185
+ # Claude Response Structure
186
+ elif self.model.startswith("anthropic"):
187
+ model_response = json.loads(response["body"].read())
188
+ outputtext = model_response['content'][0]['text']
189
+
190
+ # Llama 3.1 Response Structure
191
+ elif self.model.startswith("meta"):
192
+ model_response = json.loads(response["body"].read())
193
+ outputtext = model_response["generation"]
194
+
195
+ # Jamba Response Structure
196
+ elif self.model.startswith("ai21"):
197
+ content = response['body'].read()
198
+ content_str = content.decode('utf-8')
199
+ content_json = json.loads(content_str)
200
+ outputtext = content_json['choices'][0]['message']['content']
201
+
202
+ # Cohere Input Format
203
+ elif self.model.startswith("cohere"):
204
+ content = response['body'].read()
205
+ content_str = content.decode('utf-8')
206
+ content_json = json.loads(content_str)
207
+ outputtext = content_json['text']
208
+
209
+ # Use Mistral as default
210
+ else:
211
+ response_body = json.loads(response.get("body").read())
212
+ outputtext = response_body['outputs'][0]['text']
213
+
214
+ metadata = response['ResponseMetadata']['HTTPHeaders']
215
+ inputtokens = int(metadata['x-amzn-bedrock-input-token-count'])
216
+ outputtokens = int(metadata['x-amzn-bedrock-output-token-count'])
217
+
218
+ print(outputtext, flush=True)
219
+ print(f"Input Tokens: {inputtokens}", flush=True)
220
+ print(f"Output Tokens: {outputtokens}", flush=True)
221
+
222
+ print("Send response...", flush=True)
223
+ r = TextCompletionResponse(
224
+ error=None,
225
+ response=outputtext,
226
+ in_token=inputtokens,
227
+ out_token=outputtokens,
228
+ model=str(self.model),
229
+ )
230
+
231
+ self.send(r, properties={"id": id})
232
+
233
+ print("Done.", flush=True)
234
+
235
+
236
+ # FIXME: Wrong exception, don't know what Bedrock throws
237
+ # for a rate limit
238
+ except TooManyRequests:
239
+
240
+ print("Send rate limit response...", flush=True)
241
+
242
+ r = TextCompletionResponse(
243
+ error=Error(
244
+ type = "rate-limit",
245
+ message = str(e),
246
+ ),
247
+ response=None,
248
+ in_token=None,
249
+ out_token=None,
250
+ model=None,
251
+ )
252
+
253
+ self.producer.send(r, properties={"id": id})
254
+
255
+ self.consumer.acknowledge(msg)
256
+
257
+ except Exception as e:
258
+
259
+ print(f"Exception: {e}")
260
+
261
+ print("Send error response...", flush=True)
262
+
263
+ r = TextCompletionResponse(
264
+ error=Error(
265
+ type = "llm-error",
266
+ message = str(e),
267
+ ),
268
+ response=None,
269
+ in_token=None,
270
+ out_token=None,
271
+ model=None,
272
+ )
273
+
274
+ self.consumer.acknowledge(msg)
275
+
276
+ @staticmethod
277
+ def add_args(parser):
278
+
279
+ ConsumerProducer.add_args(
280
+ parser, default_input_queue, default_subscriber,
281
+ default_output_queue,
282
+ )
283
+
284
+ parser.add_argument(
285
+ '-m', '--model',
286
+ default="mistral.mistral-large-2407-v1:0",
287
+ help=f'Bedrock model (default: Mistral-Large-2407)'
288
+ )
289
+
290
+ parser.add_argument(
291
+ '-z', '--aws-id-key',
292
+ help=f'AWS ID Key'
293
+ )
294
+
295
+ parser.add_argument(
296
+ '-k', '--aws-secret',
297
+ help=f'AWS Secret Key'
298
+ )
299
+
300
+ parser.add_argument(
301
+ '-r', '--aws-region',
302
+ help=f'AWS Region (default: us-west-2)'
303
+ )
304
+
305
+ parser.add_argument(
306
+ '-t', '--temperature',
307
+ type=float,
308
+ default=default_temperature,
309
+ help=f'LLM temperature parameter (default: {default_temperature})'
310
+ )
311
+
312
+ parser.add_argument(
313
+ '-x', '--max-output',
314
+ type=int,
315
+ default=default_max_output,
316
+ help=f'LLM max output tokens (default: {default_max_output})'
317
+ )
318
+
319
+ def run():
320
+
321
+ Processor.start(module, __doc__)
322
+
323
+
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: trustgraph-bedrock
3
+ Version: 0.11.11
4
+ Summary: TrustGraph provides a means to run a pipeline of flexible AI processing components in a flexible means to achieve a processing pipeline.
5
+ Home-page: https://github.com/trustgraph-ai/trustgraph
6
+ Download-URL: https://github.com/trustgraph-ai/trustgraph/archive/refs/tags/v0.11.11.tar.gz
7
+ Author: trustgraph.ai
8
+ Author-email: security@trustgraph.ai
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ Requires-Dist: trustgraph-base
15
+ Requires-Dist: pulsar-client
16
+ Requires-Dist: prometheus-client
17
+ Requires-Dist: boto3
18
+
19
+ See https://trustgraph.ai/
@@ -0,0 +1,12 @@
1
+ README.md
2
+ setup.py
3
+ scripts/text-completion-bedrock
4
+ trustgraph/bedrock_version.py
5
+ trustgraph/model/text_completion/bedrock/__init__.py
6
+ trustgraph/model/text_completion/bedrock/__main__.py
7
+ trustgraph/model/text_completion/bedrock/llm.py
8
+ trustgraph_bedrock.egg-info/PKG-INFO
9
+ trustgraph_bedrock.egg-info/SOURCES.txt
10
+ trustgraph_bedrock.egg-info/dependency_links.txt
11
+ trustgraph_bedrock.egg-info/requires.txt
12
+ trustgraph_bedrock.egg-info/top_level.txt
@@ -0,0 +1,4 @@
1
+ trustgraph-base
2
+ pulsar-client
3
+ prometheus-client
4
+ boto3
@@ -0,0 +1,2 @@
1
+ scripts
2
+ trustgraph