forked from openai/chatgpt-retrieval-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_metadata.py
43 lines (37 loc) · 1.37 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
39
40
41
42
43
from models.models import Source
from services.openai import get_chat_completion
import json
from typing import Dict
import os
from loguru import logger
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},
]
# NOTE: Azure Open AI requires deployment id
# Read environment variable - if not set - not used
completion = get_chat_completion(
messages,
"gpt-4",
os.environ.get("OPENAI_METADATA_EXTRACTIONMODEL_DEPLOYMENTID")
) # TODO: change to your preferred model name
logger.info(f"completion: {completion}")
try:
metadata = json.loads(completion)
except:
metadata = {}
return metadata