trustgraph-vertexai 0.0.0__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.

Potentially problematic release.


This version of trustgraph-vertexai might be problematic. Click here for more details.

@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: trustgraph-vertexai
3
+ Version: 0.0.0
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
+ Author: trustgraph.ai
7
+ Author-email: security@trustgraph.ai
8
+ License: UNKNOWN
9
+ Download-URL: https://github.com/trustgraph-ai/trustgraph/archive/refs/tags/v0.0.0.tar.gz
10
+ Platform: UNKNOWN
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
13
+ Classifier: Operating System :: OS Independent
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+
17
+ See https://trustgraph.ai/
18
+
19
+
@@ -0,0 +1 @@
1
+ See https://trustgraph.ai/
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env python3
2
+
3
+ from trustgraph.model.text_completion.vertexai 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/vertexai_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-vertexai",
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>=0.19,<0.20",
38
+ "pulsar-client",
39
+ "google-cloud-aiplatform",
40
+ "prometheus-client",
41
+ ],
42
+ scripts=[
43
+ "scripts/text-completion-vertexai",
44
+ ]
45
+ )
@@ -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,262 @@
1
+
2
+ """
3
+ Simple LLM service, performs text prompt completion using VertexAI on
4
+ Google Cloud. Input is prompt, output is response.
5
+ """
6
+
7
+ import vertexai
8
+ import time
9
+ from prometheus_client import Histogram
10
+ import os
11
+
12
+ from google.oauth2 import service_account
13
+ import google
14
+
15
+ from vertexai.preview.generative_models import (
16
+ Content,
17
+ FunctionDeclaration,
18
+ GenerativeModel,
19
+ GenerationConfig,
20
+ HarmCategory,
21
+ HarmBlockThreshold,
22
+ Part,
23
+ Tool,
24
+ )
25
+
26
+ from .... schema import TextCompletionRequest, TextCompletionResponse, Error
27
+ from .... schema import text_completion_request_queue
28
+ from .... schema import text_completion_response_queue
29
+ from .... log_level import LogLevel
30
+ from .... base import ConsumerProducer
31
+ from .... exceptions import TooManyRequests
32
+
33
+ module = ".".join(__name__.split(".")[1:-1])
34
+
35
+ default_input_queue = text_completion_request_queue
36
+ default_output_queue = text_completion_response_queue
37
+ default_subscriber = module
38
+ default_model = 'gemini-1.0-pro-001'
39
+ default_region = 'us-central1'
40
+ default_temperature = 0.0
41
+ default_max_output = 8192
42
+ default_private_key = "private.json"
43
+
44
+ class Processor(ConsumerProducer):
45
+
46
+ def __init__(self, **params):
47
+
48
+ input_queue = params.get("input_queue", default_input_queue)
49
+ output_queue = params.get("output_queue", default_output_queue)
50
+ subscriber = params.get("subscriber", default_subscriber)
51
+ region = params.get("region", default_region)
52
+ model = params.get("model", default_model)
53
+ private_key = params.get("private_key", default_private_key)
54
+ temperature = params.get("temperature", default_temperature)
55
+ max_output = params.get("max_output", default_max_output)
56
+
57
+ if private_key is None:
58
+ raise RuntimeError("Private key file not specified")
59
+
60
+ super(Processor, self).__init__(
61
+ **params | {
62
+ "input_queue": input_queue,
63
+ "output_queue": output_queue,
64
+ "subscriber": subscriber,
65
+ "input_schema": TextCompletionRequest,
66
+ "output_schema": TextCompletionResponse,
67
+ }
68
+ )
69
+
70
+ if not hasattr(__class__, "text_completion_metric"):
71
+ __class__.text_completion_metric = Histogram(
72
+ 'text_completion_duration',
73
+ 'Text completion duration (seconds)',
74
+ buckets=[
75
+ 0.25, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
76
+ 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
77
+ 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0,
78
+ 30.0, 35.0, 40.0, 45.0, 50.0, 60.0, 80.0, 100.0,
79
+ 120.0
80
+ ]
81
+ )
82
+
83
+ self.parameters = {
84
+ "temperature": temperature,
85
+ "top_p": 1.0,
86
+ "top_k": 32,
87
+ "candidate_count": 1,
88
+ "max_output_tokens": max_output,
89
+ }
90
+
91
+ self.generation_config = GenerationConfig(
92
+ temperature=temperature,
93
+ top_p=1.0,
94
+ top_k=10,
95
+ candidate_count=1,
96
+ max_output_tokens=max_output,
97
+ )
98
+
99
+ # Block none doesn't seem to work
100
+ block_level = HarmBlockThreshold.BLOCK_ONLY_HIGH
101
+ # block_level = HarmBlockThreshold.BLOCK_NONE
102
+
103
+ self.safety_settings = {
104
+ HarmCategory.HARM_CATEGORY_HARASSMENT: block_level,
105
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: block_level,
106
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: block_level,
107
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: block_level,
108
+ }
109
+
110
+ print("Initialise VertexAI...", flush=True)
111
+
112
+ if private_key:
113
+ credentials = service_account.Credentials.from_service_account_file(private_key)
114
+ else:
115
+ credentials = None
116
+
117
+ if credentials:
118
+ vertexai.init(
119
+ location=region,
120
+ credentials=credentials,
121
+ project=credentials.project_id,
122
+ )
123
+ else:
124
+ vertexai.init(
125
+ location=region
126
+ )
127
+
128
+ print(f"Initialise model {model}", flush=True)
129
+ self.llm = GenerativeModel(model)
130
+ self.model = model
131
+
132
+ print("Initialisation complete", flush=True)
133
+
134
+ def handle(self, msg):
135
+
136
+ try:
137
+
138
+ v = msg.value()
139
+
140
+ # Sender-produced ID
141
+
142
+ id = msg.properties()["id"]
143
+
144
+ print(f"Handling prompt {id}...", flush=True)
145
+
146
+ prompt = v.system + "\n\n" + v.prompt
147
+
148
+ with __class__.text_completion_metric.time():
149
+
150
+ response = self.llm.generate_content(
151
+ prompt, generation_config=self.generation_config,
152
+ safety_settings=self.safety_settings
153
+ )
154
+
155
+ resp = response.text
156
+ inputtokens = int(response.usage_metadata.prompt_token_count)
157
+ outputtokens = int(response.usage_metadata.candidates_token_count)
158
+ print(resp, flush=True)
159
+ print(f"Input Tokens: {inputtokens}", flush=True)
160
+ print(f"Output Tokens: {outputtokens}", flush=True)
161
+
162
+ print("Send response...", flush=True)
163
+
164
+ r = TextCompletionResponse(
165
+ error=None,
166
+ response=resp,
167
+ in_token=inputtokens,
168
+ out_token=outputtokens,
169
+ model=self.model
170
+ )
171
+
172
+ self.producer.send(r, properties={"id": id})
173
+
174
+ print("Done.", flush=True)
175
+
176
+ # Acknowledge successful processing of the message
177
+ self.consumer.acknowledge(msg)
178
+
179
+ except google.api_core.exceptions.ResourceExhausted as e:
180
+
181
+ print("Send rate limit response...", flush=True)
182
+
183
+ r = TextCompletionResponse(
184
+ error=Error(
185
+ type = "rate-limit",
186
+ message = str(e),
187
+ ),
188
+ response=None,
189
+ in_token=None,
190
+ out_token=None,
191
+ model=None,
192
+ )
193
+
194
+ self.producer.send(r, properties={"id": id})
195
+
196
+ self.consumer.acknowledge(msg)
197
+
198
+ except Exception as e:
199
+
200
+ print(f"Exception: {e}")
201
+
202
+ print("Send error response...", flush=True)
203
+
204
+ r = TextCompletionResponse(
205
+ error=Error(
206
+ type = "llm-error",
207
+ message = str(e),
208
+ ),
209
+ response=None,
210
+ in_token=None,
211
+ out_token=None,
212
+ model=None,
213
+ )
214
+
215
+ self.producer.send(r, properties={"id": id})
216
+
217
+ self.consumer.acknowledge(msg)
218
+
219
+ @staticmethod
220
+ def add_args(parser):
221
+
222
+ ConsumerProducer.add_args(
223
+ parser, default_input_queue, default_subscriber,
224
+ default_output_queue,
225
+ )
226
+
227
+ parser.add_argument(
228
+ '-m', '--model',
229
+ default=default_model,
230
+ help=f'LLM model (default: {default_model})'
231
+ )
232
+ # Also: text-bison-32k
233
+
234
+ parser.add_argument(
235
+ '-k', '--private-key',
236
+ help=f'Google Cloud private JSON file'
237
+ )
238
+
239
+ parser.add_argument(
240
+ '-r', '--region',
241
+ default=default_region,
242
+ help=f'Google Cloud region (default: {default_region})',
243
+ )
244
+
245
+ parser.add_argument(
246
+ '-t', '--temperature',
247
+ type=float,
248
+ default=default_temperature,
249
+ help=f'LLM temperature parameter (default: {default_temperature})'
250
+ )
251
+
252
+ parser.add_argument(
253
+ '-x', '--max-output',
254
+ type=int,
255
+ default=default_max_output,
256
+ help=f'LLM max output tokens (default: {default_max_output})'
257
+ )
258
+
259
+ def run():
260
+
261
+ Processor.start(module, __doc__)
262
+
@@ -0,0 +1 @@
1
+ __version__ = "0.0.0"
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: trustgraph-vertexai
3
+ Version: 0.0.0
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
+ Author: trustgraph.ai
7
+ Author-email: security@trustgraph.ai
8
+ License: UNKNOWN
9
+ Download-URL: https://github.com/trustgraph-ai/trustgraph/archive/refs/tags/v0.0.0.tar.gz
10
+ Platform: UNKNOWN
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
13
+ Classifier: Operating System :: OS Independent
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+
17
+ See https://trustgraph.ai/
18
+
19
+
@@ -0,0 +1,12 @@
1
+ README.md
2
+ setup.py
3
+ scripts/text-completion-vertexai
4
+ trustgraph/vertexai_version.py
5
+ trustgraph/model/text_completion/vertexai/__init__.py
6
+ trustgraph/model/text_completion/vertexai/__main__.py
7
+ trustgraph/model/text_completion/vertexai/llm.py
8
+ trustgraph_vertexai.egg-info/PKG-INFO
9
+ trustgraph_vertexai.egg-info/SOURCES.txt
10
+ trustgraph_vertexai.egg-info/dependency_links.txt
11
+ trustgraph_vertexai.egg-info/requires.txt
12
+ trustgraph_vertexai.egg-info/top_level.txt
@@ -0,0 +1,4 @@
1
+ google-cloud-aiplatform
2
+ prometheus-client
3
+ pulsar-client
4
+ trustgraph-base<0.20,>=0.19
@@ -0,0 +1,2 @@
1
+ scripts
2
+ trustgraph