forked from GoogleCloudPlatform/generative-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
34 lines (26 loc) · 1.09 KB
/
Copy pathmain.py
File metadata and controls
34 lines (26 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
import os
import functions_framework
from google.cloud import logging
import vertexai
from vertexai.preview.language_models import CodeGenerationModel
PROJECT_ID = os.environ.get("GCP_PROJECT", "-")
LOCATION = os.environ.get("GCP_REGION", "-")
client = logging.Client(project=PROJECT_ID)
client.setup_logging()
LOG_NAME = "predictCode-cloudfunction-log"
logger = client.logger(LOG_NAME)
@functions_framework.http
def predictCode(request):
request_json = request.get_json(silent=True)
if request_json and "prompt" in request_json:
prompt = request_json["prompt"]
logger.log(f"Received request for prompt: {prompt}")
vertexai.init(project=PROJECT_ID, location=LOCATION)
parameters = {"temperature": 0.2, "max_output_tokens": 1024}
model = CodeGenerationModel.from_pretrained("code-bison@002")
prompt_response = model.predict(prompt, **parameters)
logger.log(f"PaLM Code Bison Model response: {prompt_response.text}")
else:
prompt_response = "No prompt provided."
return json.dumps({"response_text": prompt_response.text})