-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabbrGPT.py
More file actions
52 lines (46 loc) · 1.93 KB
/
abbrGPT.py
File metadata and controls
52 lines (46 loc) · 1.93 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
import json
from pathlib import Path
from openai import OpenAI
def load_api_key(
config_path: Path = Path(__file__).parent / "cfg" / "openai_key.json"
) -> str:
return json.loads(config_path.read_text())["api_key"]
client = OpenAI(api_key=load_api_key())
system_prompt = (
"You are an abbreviation expert. When given a timetable phrase, pick two to three most representative words "
"and produce their abbreviations using these rules:\n"
"1. Preserve any existing all-caps acronyms unchanged.\n"
"2. Short words perserve first 3 letters;\n"
"3. Common biomedical terms can apply common abbreviations;\n"
"4. Otherwise, remove all vowels (A, E, I, O, U), drop non-alphanumerics, then take three to four consonants and uppercase them.\n"
"5. Return only two or three final abbreviated words, separated by spaces—no extra text.\n"
"Abbreviation example:\n"
"Writing session DRG paper -> DRG PPR\n"
"Pack materials to sterilize for CT -> STRL CT\n"
"NHP spinal cord RNA-seq -> NHP SC RNA"
)
def abbreviateBatch(
phrases: list[str],
model: str = "gpt-4.1",
temperature: float = 0.3
) -> str:
batched_prompt = "For each of the following phrases, return only the abbreviation as per the rules:\n\n" + \
"\n".join(f"{i+1}. {p}" for i, p in enumerate(phrases))
resp = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": batched_prompt}
],
temperature=temperature,
max_tokens=500,
)
return resp.choices[0].message.content.strip()
if __name__ == "__main__":
examples = [
"Writing session DRG paper",
"Pack materials to sterilize for CT",
"Important Tsinghua Program update",
"NHP spinal cord RNA-seq"
]
print(abbreviateBatch(examples))