2024-07-10 23:20:06 +01:00
|
|
|
|
|
|
|
|
"""
|
2024-07-12 15:12:40 +01:00
|
|
|
Simple LLM service, performs text prompt completion using VertexAI on
|
|
|
|
|
Google Cloud. Input is prompt, output is response.
|
2024-07-10 23:20:06 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import vertexai
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
from google.oauth2 import service_account
|
|
|
|
|
import google
|
|
|
|
|
|
|
|
|
|
from vertexai.preview.generative_models import (
|
|
|
|
|
Content,
|
|
|
|
|
FunctionDeclaration,
|
|
|
|
|
GenerativeModel,
|
|
|
|
|
GenerationConfig,
|
|
|
|
|
HarmCategory,
|
|
|
|
|
HarmBlockThreshold,
|
|
|
|
|
Part,
|
|
|
|
|
Tool,
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-23 21:34:03 +01:00
|
|
|
from .... schema import TextCompletionRequest, TextCompletionResponse
|
|
|
|
|
from .... schema import text_completion_request_queue
|
|
|
|
|
from .... schema import text_completion_response_queue
|
|
|
|
|
from .... log_level import LogLevel
|
|
|
|
|
from .... base import ConsumerProducer
|
2024-08-19 22:15:32 +01:00
|
|
|
from .... exceptions import TooManyRequests
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-23 21:34:03 +01:00
|
|
|
module = ".".join(__name__.split(".")[1:-1])
|
|
|
|
|
|
|
|
|
|
default_input_queue = text_completion_request_queue
|
|
|
|
|
default_output_queue = text_completion_response_queue
|
|
|
|
|
default_subscriber = module
|
2024-07-15 17:17:04 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
class Processor(ConsumerProducer):
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-18 17:20:42 +01:00
|
|
|
def __init__(self, **params):
|
|
|
|
|
|
|
|
|
|
input_queue = params.get("input_queue", default_input_queue)
|
|
|
|
|
output_queue = params.get("output_queue", default_output_queue)
|
|
|
|
|
subscriber = params.get("subscriber", default_subscriber)
|
|
|
|
|
region = params.get("region", "us-west1")
|
|
|
|
|
model = params.get("model", "gemini-1.0-pro-001")
|
|
|
|
|
private_key = params.get("private_key")
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
super(Processor, self).__init__(
|
2024-07-18 17:20:42 +01:00
|
|
|
**params | {
|
|
|
|
|
"input_queue": input_queue,
|
|
|
|
|
"output_queue": output_queue,
|
|
|
|
|
"subscriber": subscriber,
|
|
|
|
|
"input_schema": TextCompletionRequest,
|
|
|
|
|
"output_schema": TextCompletionResponse,
|
|
|
|
|
}
|
2024-07-10 23:20:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.parameters = {
|
|
|
|
|
"temperature": 0.2,
|
|
|
|
|
"top_p": 1.0,
|
|
|
|
|
"top_k": 32,
|
|
|
|
|
"candidate_count": 1,
|
|
|
|
|
"max_output_tokens": 8192,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.generation_config = GenerationConfig(
|
|
|
|
|
temperature=0.2,
|
|
|
|
|
top_p=1.0,
|
|
|
|
|
top_k=10,
|
|
|
|
|
candidate_count=1,
|
|
|
|
|
max_output_tokens=8191,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Block none doesn't seem to work
|
|
|
|
|
block_level = HarmBlockThreshold.BLOCK_ONLY_HIGH
|
|
|
|
|
# block_level = HarmBlockThreshold.BLOCK_NONE
|
|
|
|
|
|
|
|
|
|
self.safety_settings = {
|
|
|
|
|
HarmCategory.HARM_CATEGORY_HARASSMENT: block_level,
|
|
|
|
|
HarmCategory.HARM_CATEGORY_HATE_SPEECH: block_level,
|
|
|
|
|
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: block_level,
|
|
|
|
|
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: block_level,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print("Initialise VertexAI...", flush=True)
|
|
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
if private_key:
|
|
|
|
|
credentials = service_account.Credentials.from_service_account_file(private_key)
|
|
|
|
|
else:
|
|
|
|
|
credentials = None
|
|
|
|
|
|
2024-07-10 23:20:06 +01:00
|
|
|
if credentials:
|
|
|
|
|
vertexai.init(
|
|
|
|
|
location=region,
|
|
|
|
|
credentials=credentials,
|
|
|
|
|
project=credentials.project_id,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
vertexai.init(
|
|
|
|
|
location=region
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print(f"Initialise model {model}", flush=True)
|
|
|
|
|
self.llm = GenerativeModel(model)
|
|
|
|
|
|
|
|
|
|
print("Initialisation complete", flush=True)
|
|
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
def handle(self, msg):
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
try:
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
v = msg.value()
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
# Sender-produced ID
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
id = msg.properties()["id"]
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
print(f"Handling prompt {id}...", flush=True)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
prompt = v.prompt
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
resp = self.llm.generate_content(
|
|
|
|
|
prompt, generation_config=self.generation_config,
|
|
|
|
|
safety_settings=self.safety_settings
|
|
|
|
|
)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
resp = resp.text
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
resp = resp.replace("```json", "")
|
|
|
|
|
resp = resp.replace("```", "")
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
print("Send response...", flush=True)
|
|
|
|
|
r = TextCompletionResponse(response=resp)
|
|
|
|
|
self.producer.send(r, properties={"id": id})
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
print("Done.", flush=True)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
# Acknowledge successful processing of the message
|
|
|
|
|
self.consumer.acknowledge(msg)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
except google.api_core.exceptions.ResourceExhausted:
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-08-19 22:15:32 +01:00
|
|
|
# 429 / rate limits case
|
|
|
|
|
raise TooManyRequests
|
2024-07-15 17:17:04 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
# Let other exceptions fall through
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def add_args(parser):
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
ConsumerProducer.add_args(
|
|
|
|
|
parser, default_input_queue, default_subscriber,
|
|
|
|
|
default_output_queue,
|
2024-07-10 23:20:06 +01:00
|
|
|
)
|
|
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-m', '--model',
|
|
|
|
|
default="gemini-1.0-pro-001",
|
|
|
|
|
help=f'LLM model (default: gemini-1.0-pro-001)'
|
|
|
|
|
)
|
|
|
|
|
# Also: text-bison-32k
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-k', '--private-key',
|
|
|
|
|
help=f'Google Cloud private JSON file'
|
|
|
|
|
)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-r', '--region',
|
|
|
|
|
default='us-west1',
|
|
|
|
|
help=f'Google Cloud region (default: us-west1)',
|
|
|
|
|
)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 17:18:24 +01:00
|
|
|
def run():
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-23 21:34:03 +01:00
|
|
|
Processor.start(module, __doc__)
|
2024-07-10 23:20:06 +01:00
|
|
|
|