forked from openai/chatgpt-retrieval-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
extract_metadata.py
38 lines (31 loc) · 1.15 KB
/
extract_metadata.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
from models.models import Source
from services.openai import get_chat_completion
import json
from typing import Dict
def extract_metadata_from_document(text: str) -> Dict[str, str]:
sources = Source.__members__.keys()
sources_string = ", ".join(sources)
# This prompt is just an example, change it to fit your use case
messages = [
{
"role": "system",
"content": f"""
Given a document from a user, try to extract the following metadata:
- source: string, one of {sources_string}
- url: string or don't specify
- created_at: string or don't specify
- author: string or don't specify
Respond with a JSON containing the extracted metadata in key value pairs. If you don't find a metadata field, don't specify it.
""",
},
{"role": "user", "content": text},
]
completion = get_chat_completion(
messages, "gpt-4"
) # TODO: change to your preferred model name
print(f"completion: {completion}")
try:
metadata = json.loads(completion)
except:
metadata = {}
return metadata