-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildjsondialog.py
56 lines (49 loc) · 1.36 KB
/
buildjsondialog.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
import os
import openai
from dotenv import load_dotenv
import shutil
from datetime import datetime
import requests
import argparse
import json
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
articleprompt=""
articletext=""
identifier=""
def getarticle():
global articletext
global articleprompt
try:
response = openai.chat.completions.create(
model="gpt-4-1106-preview",
response_format={ "type": "json_object" },
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": articleprompt},
],
)
except openai.OpenAIError as e:
print("Error")
print(e)
exit(1)
#print(response)
print("Finish reason: ")
print(response.choices[0].finish_reason)
#articletext=response.choices[0].text
articletext = response.choices[0].message.content
with open("./draft/"+ identifier+"/"+identifier+".json", 'w') as f:
f.write(articletext)
print("Building dialog")
inputfile = "./currentarticle/prompt.json" #args.inputfile
if len(inputfile)>1:
# Opening JSON file
f = open(inputfile)
data = json.load(f)
identifier=data['identifier']
articleprompt=data['prompt']
os.mkdir("./draft/"+ identifier)
shutil.copyfile("./currentarticle/prompt.json","./draft/"+identifier+"/prompt.json")
getarticle()
else:
print("No input")