-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.py
49 lines (40 loc) · 1.48 KB
/
main.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import json
import functions_framework
import google.cloud.logging
import vertexai
from vertexai.language_models import TextGenerationModel
PROJECT_ID = os.environ.get('GCP_PROJECT','-')
LOCATION = os.environ.get('GCP_REGION','-')
client = google.cloud.logging.Client(project=PROJECT_ID)
client.setup_logging()
log_name = "slack-summarizeText-cloudfunction-log"
logger = client.logger(log_name)
@functions_framework.http
def summarizeText(request):
token = request.form['token']
logger.log(f"token received = {token}")
#<TODO>Your code to validate token
request_text = request.form['text']
logger.log(f"Received the following request to summarize : {request_text}")
vertexai.init(project=PROJECT_ID, location=LOCATION)
model = TextGenerationModel.from_pretrained("text-bison@001")
prompt = f"Summarize: {request_text}"
parameters = {
"temperature": 0.2,
"max_output_tokens": 256,
"top_p": 0.8,
"top_k": 40
}
prompt_response = model.predict(prompt,**parameters)
logger.log("PaLM Text Bison Model response: {prompt_response.text}")
#Format the Slack message
data = {}
data['blocks'] = []
data['blocks'].append({"type":"section",
"text": {
"type": "mrkdwn",
"text": "Your message: " + request_text + "\n>Summarization:" + prompt_response.text
}
})
return json.dumps(data),200, {'Content-Type': 'application/json'}