-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgpt_gpt.py
52 lines (39 loc) · 1.56 KB
/
gpt_gpt.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
from openai import OpenAI
import re
from .gpt_cst import SYSTEMPROMPTS
def post_process(final_txt):
final_txt = re.findall(
r'```(.*?)```', final_txt, re.DOTALL)[0]
final_txt = re.sub(
r'^python', '', final_txt, flags=re.MULTILINE)
return final_txt
def chatgpt(context, api_key=''):
if not api_key:
raise Exception("Please provide an OpenAI API key")
scene = context.scene
# sysprompt preparation
messages = [{"role": "system", "content": system_prompt}
for system_prompt in SYSTEMPROMPTS]
# add previous messages
for msg in scene.history[-8:]:
if msg.type == "GPT":
messages.append(
{"role": "assistant", "content": "```\n" + msg.content + "\n```"})
else:
messages.append({"role": "user", "content": msg.content})
# add the current user message
if messages[-1]["role"] != "user":
formatted_message = f"Please provide me with Blender (3D software) python code satisfying the following task: {scene.prompt_input}. \n. Do not provide with anything that is not Python code. Do not provide explanations and comments."
messages.append({"role": "user", "content": formatted_message})
# send message to GPT
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(
model=scene.model,
messages=messages,
temperature=scene.creativity,
)
try:
final_txt = response.choices[0].message.content
return post_process(final_txt)
except IndexError:
return ''