-
Notifications
You must be signed in to change notification settings - Fork 30
/
gpt3_wordpress.py
74 lines (65 loc) · 2.77 KB
/
gpt3_wordpress.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import openai
import rich
import typer
from rich.progress import Progress, SpinnerColumn, TextColumn
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
class Gpt3Wordpress:
def __init__(self, api_key: str, blog_url: str, username: str, password: str):
openai.api_key = api_key
self.blog_url = blog_url + "/xmlrpc.php"
self.username = username
self.password = password
def _gpt3_query(self, prompt: str) -> str:
"""Query the OpenAI GPT-3 API with the given prompt and return the response."""
try:
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.6,
max_tokens=3000,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.6,
)
return response.choices[0].text
except Exception() as e:
rich.print(f"Error: {e}")
exit(1)
def _generate_loop(self, prompt: str, kind: str) -> str:
"""Generate a title or post until it meets the requirements."""
while True:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
) as progress:
progress.add_task(description=f"Generating {kind}...", total=None)
content = self._gpt3_query(prompt)
rich.print(f"\nGenerated {kind}: {content}")
confirm = typer.confirm(f"\nDo you like the generated {kind}?")
if confirm:
return content
def generate_post_title(self, topic: str) -> str:
"""Generate a post title."""
return self._generate_loop(f"Blog post title."
f"Title must be about {topic}."
f"Length must be maximum 70 characters",
"title")
def generate_post(self, title: str, tone: str, max_words: int) -> str:
"""Generate a post."""
return self._generate_loop(f"Blog post which titles {title}. "
f"Tone must be {tone}. "
f"Length must be maximum {max_words} words.",
"post")
def create_wordpress_post(self, title: str, content: str) -> None:
"""Create a WordPress post."""
client = Client(self.blog_url, self.username, self.password)
post = WordPressPost()
post.title = title
post.content = content
post.post_status = 'draft'
try:
client.call(NewPost(post))
except Exception() as e:
rich.print(f"Error: {e}")
exit(1)