-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
46 lines (37 loc) · 1.3 KB
/
lambda_function.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
import tweepy
import openai
import os
from dotenv import load_dotenv
load_dotenv()
ACCESS_TOKEN= os.getenv('ACCESS_TOKEN')
ACCESS_TOKEN_SECRET= os.getenv('ACCESS_TOKEN_SECRET')
API_KEY = os.getenv('API_KEY')
API_KEY_SECRET = os.getenv('API_KEY_SECRET')
OPENAI_API_KEY= os.getenv('OPENAI_API_KEY')
# Set up the tweepy API client
auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitter = tweepy.API(auth)
# Set up the openai API client
openai.api_key = OPENAI_API_KEY
def lambda_handler(event, context):
# Define a function to generate a text using GPT-3
def generate_story(prompt):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=event["max_tokens"],
n=1,
stop=None,
temperature=event["temperature"],
)
generation = response["choices"][0]["text"]
return generation
# Use tweepy to tweet the story generated by the function
def tweet_story(prompt):
story = generate_story(prompt)[:280]
twitter.update_status(story)
return story
res = tweet_story(event["prefix"] + ' ' + event["prompt"] + ' ' + event["suffix"])
print(res)
return {"statusCode": 200, "tweet": res}