-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_toc.py
50 lines (38 loc) · 1.55 KB
/
edit_toc.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
import openai
import yaml
import requests
# Set your OpenAI API key here
api_key =
def load_toc(file_path):
with open(file_path, 'r') as file:
toc = yaml.safe_load(file)
return toc
def save_toc(file_path, toc):
with open(file_path, 'w') as file:
yaml.dump(toc, file)
def gpt3_edit_toc(toc_file_path):
toc = load_toc(toc_file_path)
print("Current TOC:")
print(yaml.dump(toc))
# Define the conversation messages
messages = [
{"role": "system", "content": "You are a helpful assistant that can edit the table of contents (TOC) of a book."},
{"role": "user", "content": "Here is the current TOC:\n" + yaml.dump(toc) + "\nWhat changes would you like to make to the TOC?"}
]
# Set the API endpoint and headers
url = 'https://api.openai.com/v1/chat/completions'
headers = {"Authorization": f"Bearer {api_key}"}
data = {'model': 'gpt-3.5-turbo', 'messages': messages}
# Make the API call using the requests library
response = requests.post(url, headers=headers, json=data).json()
# Extract the assistant's response
assistant_response = response['choices'][0]['message']['content'].strip()
# TODO: Parse the assistant's response and apply the changes to the TOC
# This may involve adding, removing, reordering, or updating chapters and sections
# Save the updated TOC
save_toc(toc_file_path, toc)
print("Updated TOC:")
print(yaml.dump(toc))
if __name__ == "__main__":
toc_file_path = "_toc.yaml" # Path to the TOC file
gpt3_edit_toc(toc_file_path)