-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathapp.py
More file actions
76 lines (61 loc) · 2.28 KB
/
app.py
File metadata and controls
76 lines (61 loc) · 2.28 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
import io
import os
import sys
import tomllib
from pathlib import Path
from openai import OpenAI
__all__ = ["get_chat_completion"]
# Force UTF-8 output to avoid encoding issues
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
# Authenticate
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Load settings file
settings_path = Path("settings.toml")
with settings_path.open("rb") as settings_file:
SETTINGS = tomllib.load(settings_file)
def parse_args() -> argparse.Namespace:
"""Parse command-line input."""
parser = argparse.ArgumentParser()
parser.add_argument("file_path", type=Path, help="Path to the input file")
return parser.parse_args()
def main(args: argparse.Namespace) -> None:
file_content = args.file_path.read_text("utf-8")
print(get_chat_completion(file_content))
def get_chat_completion(content: str) -> str:
"""Send a request to the /chat/completions endpoint."""
response = client.chat.completions.create(
model=SETTINGS["general"]["model"],
messages=_assemble_chat_messages(content),
temperature=SETTINGS["general"]["temperature"],
seed=12345, # Doesn't do anything for older models
)
return response.choices[0].message.content
def _assemble_chat_messages(content: str) -> list[dict]:
"""Combine all messages into a well-formatted list of dicts."""
messages = [
{"role": "system", "content": SETTINGS["prompts"]["role_prompt"]},
{"role": "user", "content": SETTINGS["prompts"]["negative_example"]},
{
"role": "system",
"content": SETTINGS["prompts"]["negative_reasoning"],
},
{
"role": "assistant",
"content": SETTINGS["prompts"]["negative_output"],
},
{"role": "user", "content": SETTINGS["prompts"]["positive_example"]},
{
"role": "system",
"content": SETTINGS["prompts"]["positive_reasoning"],
},
{
"role": "assistant",
"content": SETTINGS["prompts"]["positive_output"],
},
{"role": "user", "content": f">>>>>\n{content}\n<<<<<"},
{"role": "user", "content": SETTINGS["prompts"]["instruction_prompt"]},
]
return messages
if __name__ == "__main__":
main(parse_args())