-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_api.py
More file actions
67 lines (52 loc) · 2.03 KB
/
model_api.py
File metadata and controls
67 lines (52 loc) · 2.03 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
import json
from openai import OpenAI
def extract_entities(api_key, input_file, output_file, base_url="https://api.xiaoai.plus/v1"):
client = OpenAI(api_key=api_key, base_url=base_url)
prompt_template = '''
{your_prompt}
'''
# 读取 JSON 文件
with open(input_file, 'r', encoding='utf-8') as file:
data = json.load(file)
# 创建一个空列表,用于存储结果
output_data = []
# 遍历每个数据项
for item in data:
text = item['sentText']
prompt = prompt_template.format(text=text)
# 调用 OpenAI API 生成结果,使用 GPT-4 模型
completion = client.chat.completions.create(
model="gpt-4",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': prompt}
],
max_tokens=2048,
temperature=0.8,
stream=False,
)
# 解析并获取生成内容
extracted_entities = completion.choices[0].message.content.strip()
try:
print(f"Extracted entities: {extracted_entities}")
if extracted_entities:
entities_list = json.loads(extracted_entities)
else:
entities_list = None
except json.JSONDecodeError as e:
print(f"JSONDecodeError: {e.msg} at line {e.lineno} column {e.colno}")
entities_list = extracted_entities
new_item = {
"sentText": text,
"preREMentions": entities_list
}
output_data.append(new_item)
with open(output_file, 'w', encoding='utf-8') as outfile:
json.dump(output_data, outfile, ensure_ascii=False, indent=4)
print(f" {output_file} done.")
if __name__ == "__main__":
# 隐藏 API 密钥
api_key = "your_api_key_here"
input_file = 'your_file_here'
output_file = 'output_file'
extract_entities(api_key, input_file, output_file)