-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowledge_experiments.py
More file actions
72 lines (60 loc) · 3.29 KB
/
knowledge_experiments.py
File metadata and controls
72 lines (60 loc) · 3.29 KB
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
import os, json
from openai import OpenAI
textfile = "data/alice_in_wonderland_small.txt"
with open(textfile, 'r') as f:
text = f.read()
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def ask_gpt(request, json=True, temp=2, seed=10, max_print=300):
info_message = request
user_message = text
print("SYS:\n" + info_message[:max_print])
print("USER:\n" + user_message[:max_print])
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
response_format={ "type": "json_object" if json else "text"},
messages=[
{"role": "system", "content": info_message},
{"role": "user", "content": user_message}
],
temperature=1,
seed=seed
)
for i,c in enumerate(response.choices):
print("GPT ({}): {}".format(i, c.message.content[:max_print]))
print("\n\n")
response = response.choices[0].message.content
return response
# entity_types = ["character", "location", "special item", "event", "key relationship"]
# example = """{"characters": [...], "locations": [...], "special_items": [...], "relationships": [...], ...}"""
# "locations": ["Jack's Diner", "blu on 4th"...],
# example = """{
# }"""
entities_list_request = " ".join([
"You are a helpful assistant designed to process pages from a novel into a JSON file.",
"You are preparing data to be used in a wiki about the novel, including articles on characters, special items, and locations.",
"Use specific proper nouns and succinct unique descriptors rather than general terms.",
"Read the following chapter and generate a list of named entities that would appear as titles of wiki pages in a wiki about the story.",
# "Each entity should be under a a type like one of {}".format(entity_types),
"""The output should be in a format like {"characters": [...], "locations": [...], "special_items": [...], "relationships": [...], ...}""",
])
entities_json = json.loads(ask_gpt(entities_list_request, json=True))
print(entities_json)
wiki = {}
for entity_type in entities_json:
entity_article_request = " ".join([
"You are a helpful assistant designed to process a novel into a comprehensive wiki in JSON format.",
f"Read the following chapter and generate a concise wiki article about each the following {entity_type}, structured as",
str({entity_type: {entity: "..." for entity in entities_json[entity_type]}}),
# "{" + entity_type + ": [...]"
# "You are preparing data to be used in a wiki about the novel, including articles on characters, special items, and locations.",
# "Use specific proper nouns and succinct unique descriptors rather than general terms.",
# "Read the following chapter and generate a list of named entities that would appear as titles of wiki pages in a wiki about the story.",
# # "Each entity should be under a a type like one of {}".format(entity_types),
# """The output should be in a format like {"characters": [...], "locations": [...], "special_items": [...], "relationships": [...], ...}""",
])
wiki[entity_type] = json.loads(ask_gpt(entity_article_request, json=True))[entity_type]
print(wiki)
savefile = 'wiki2.json'
with open(savefile, 'w', encoding='utf-8') as f:
json.dump(wiki, f, ensure_ascii=False, indent=4)
import json