-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextract.py
More file actions
31 lines (27 loc) · 1.04 KB
/
extract.py
File metadata and controls
31 lines (27 loc) · 1.04 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
import requests
# OpenAI API configuration
openai_api_key = 'Paste your OpenAI API Key here'
def extract_keywords(text):
url = 'https://api.openai.com/v1/engines/text-davinci-003/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {openai_api_key}'
}
data = {
'prompt': f'Extract the top 5 most relevant keywords from this text. Make the first letter of every word uppercase and separate with commas:\n\n{text}',
'temperature': 0.5,
'max_tokens': 60,
'top_p': 1.0,
'frequency_penalty': 0.8,
'presence_penalty': 0.0
}
response = requests.post(url, headers=headers, json=data)
response_data = response.json()
# Extract the keywords from the response
choices = response_data['choices']
if len(choices) > 0:
keywords = choices[0]['text'].strip().split(',')
keywords = [keyword.strip().capitalize() for keyword in keywords]
return keywords[:5]
else:
return []