-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
92 lines (71 loc) · 2.76 KB
/
backend.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from streamlit import session_state as state
import openai
MODEL = 'gpt-3.5-turbo' # gpt-4 is restricted yet
# Quick Start: https://platform.openai.com/docs/api-reference/authentication
def login() -> None:
openai.organization = state.org_id
openai.api_key = state.api_key
openai.Model.list() # won't work if API key or organization id is incorrect
# Input parameters documentation: https://platform.openai.com/docs/api-reference/chat/create
# Output description: https://platform.openai.com/docs/guides/gpt/chat-completions-response-format
def call(**kwargs) -> str:
completion = openai.ChatCompletion.create(
model=MODEL,
**kwargs
)
return completion.choices[0].message.content
def pick_random_topic() -> None:
'''
Generates a random song topic with ChatGPT.
'''
# tell ChatGPT which role it should imitate
role = 'You are a famous and influencial song writer. The lyrics of the songs that you\'ve created are known by millions.'
# user request
request = 'Give me a random lyrics topic. Return only the topic with no additional info.'
temperature = 2 # 0 to 2 from less to more creative
max_tokens = 30 # a short topic
messages = [
{
'role': 'system',
'content': role
}, # first assign the role
{
'role': 'user',
'content': request
} # now send the request
]
# Input parameters documentation: https://platform.openai.com/docs/api-reference/chat/create
# Output description: https://platform.openai.com/docs/guides/gpt/chat-completions-response-format
topic = call(
temperature=temperature,
max_tokens=max_tokens,
messages=messages
) # will send the request and process the output, returns a string
state.topic = topic
# return topic
def generate_lyrics() -> None:
'''
Generates lyrics with ChatGPT.
'''
# tell ChatGPT which role it should imitate
role = 'You are a famous and influencial song writer. The lyrics of the songs that you\'ve created are known by millions.'
# user request
decade = '' if state.decade is None else f'Let the lyrics be as if it would\'ve been written in {state.decade}-s.'
request = f'Write lyrics for a song about {state.topic} in the {state.genre} genre. {decade} Return only the lyrics.'
temperature = 0.5 # make it more strict to the request
messages = [
{
'role': 'system',
'content': role
}, # first assign the role
{
'role': 'user',
'content': request
} # now send the request
]
lyrics = call(
temperature=temperature,
messages=messages
)
state.lyrics = lyrics
# return lyrics