forked from zentrum-lexikographie/eval-de-lemma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_api_queries.py
58 lines (46 loc) · 1.59 KB
/
run_api_queries.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
51
52
53
54
55
56
57
58
import logging
import os
import sys
import time
import openai
sys.path.append("../..")
from src.loader import load_data
# logging settings
logger = logging.getLogger(__name__)
logging.basicConfig(
filename="../../../logs.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s: %(message)s",
datefmt="%y-%m-%d %H:%M:%S"
)
DATASETSPATH = "../../datasets"
import warnings
warnings.filterwarnings("ignore")
# bash command: export OPEN_AI_KEY=INSERT_KEY_HERE
openai.api_key = os.environ["OPEN_AI_KEY"]
def lemmatize_query(x_test, dname):
"""Query the OpenAI API to predict lemmata of a list of sentences."""
lemmata = []
tokens = 0
with open(f'../../nbs/gpt3_outputs/gpt3-{dname}.txt', 'w',
encoding='utf-8') as f:
for sent in x_test:
try:
prompt = f"Lemmatisiere die Tokenliste: {sent}"
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=len(prompt)
)
answer = response["choices"][0]["text"]
tokens += response['usage']['total_tokens']
f.write(answer+'\n')
time.sleep(3.) # prevent rate limit errors
except Exception as err:
logger.error(err)
logger.info(f"{tokens} tokens used.")
print(f"{tokens} tokens used for {dname}.")
return lemmata
# lemmatize all datasets
for x_test, y_test, z_test, z_test_xpos, dname in load_data(DATASETSPATH):
lemmatize_query(x_test, dname)