Repository navigation
Expand file tree
/
Copy pathembed_openai.py
More file actions
57 lines (46 loc) · 1.7 KB
/
Copy pathembed_openai.py
File metadata and controls
57 lines (46 loc) · 1.7 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
import requests
import json
import numpy as np
import uc
from conf2year import conf2year
import sys
conference_name = sys.argv[1] if len(sys.argv) > 1 else 'cvpr'
# Your OpenAI API key
api_key = uc.NOT_OAI_API
# The URL of the OpenAI embeddings API
url = 'https://api.openai.com/v1/embeddings'
# The headers for the API request
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
for year in conf2year[conference_name]:
print(f'Processing papers for {conference_name} {year}')
# Load the data from the JSON file
with open(f'data/{conference_name}_{year}_papers.json', 'r') as f:
data = json.load(f)
# Extract the abstracts
abstracts = [paper['abstract'] for paper in data]
# Generate embeddings for each abstract
embeddings = []
for (idx,abstract) in enumerate(abstracts):
print(f'Processing abstract {idx+1} of {len(abstracts)}')
# The data for the API request
data = {
'input': abstract,
'model': 'text-embedding-3-large'
}
# Make the API request
response = requests.post(url, headers=headers, data=json.dumps(data))
# Check if the request was successful
if response.status_code == 200:
# Extract the embedding from the response
embedding = response.json()['data'][0]['embedding']
embeddings.append(embedding)
else:
print(f'Error: {response.status_code}')
print(response.text)
# Convert the list of embeddings to a NumPy array
embeddings = np.array(embeddings)
# Save the embeddings to a .npy file
np.save(f'data/{conference_name}_{year}_embeddings_openai.npy', embeddings)