Created
May 20, 2023 13:41
-
-
Save anna-geller/0743175e17eeffae902eb6170091cc46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform { | |
required_providers { | |
aws = { | |
source = "hashicorp/aws" | |
version = "~> 4.0" | |
} | |
kestra = { | |
source = "kestra-io/kestra" # namespace of Kestra provider | |
version = "~> 0.7.0" # don't worry about 0.7.0 being displayed here - the provider works across the latest version as well | |
} | |
} | |
} | |
variable "region" { | |
default = "eu-central-1" | |
} | |
variable "namespace" { | |
default = "prod" | |
} | |
variable "phone_number" { | |
type = string | |
} | |
provider "aws" { | |
region = var.region | |
profile = "default" | |
} | |
provider "kestra" { | |
url = "http://localhost:8080" | |
} | |
resource "aws_sns_topic" "topic" { | |
name = "kestra" | |
tags = { | |
project = "kestra" | |
} | |
} | |
resource "aws_sns_topic_subscription" "sms" { | |
endpoint = var.phone_number | |
protocol = "sms" | |
topic_arn = aws_sns_topic.topic.arn | |
} | |
resource "kestra_flow" "smsMum" { | |
flow_id = "smsMum" | |
namespace = var.namespace | |
content = <<EOF | |
id: smsMum | |
namespace: ${var.namespace} | |
tasks: | |
- id: openai | |
type: io.kestra.core.tasks.scripts.Python | |
requirements: | |
- openai | |
inputFiles: | |
main.py: | | |
import openai | |
import os | |
from kestra import Kestra | |
openai.api_key = "{{envs.openai_api_key}}" | |
def get_completion(prompt, model="gpt-4"): | |
messages = [{"role": "user", "content": prompt}] | |
response = openai.ChatCompletion.create( | |
model=model, | |
messages=messages, | |
temperature=0.9, | |
) | |
return response.choices[0].message["content"] | |
instructions = f""" | |
Write an SMS to my mum, which lives far away from me. | |
Tell her I'm fine and ask her how she is doing. | |
Say something nice to her in a concise way. | |
The greeting should be very short like "Hi mum!". | |
Don't say that you're checking in - get to the point. | |
The message should be between 80 and 160 characters long. | |
Sign the message at the end with "Anna". | |
""" | |
message = get_completion(instructions) | |
print(message) | |
Kestra.outputs({'message': message}) | |
- id: sendSMS | |
type: io.kestra.plugin.aws.sns.Publish | |
accessKeyId: "{{envs.aws_access_key_id}}" | |
secretKeyId: "{{envs.aws_secret_access_key}}" | |
region: "${var.region}" | |
topicArn: "${aws_sns_topic.topic.arn}" | |
from: | |
data: | | |
{{outputs.openai.vars.message}} | |
triggers: | |
- id: schedule | |
type: io.kestra.core.models.triggers.types.Schedule | |
cron: "0 11 * * 7" # every Sunday in the UTC timezone | |
EOF | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment