mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-10 22:07:10 +01:00
384 findings to none across ten scripts. Most of it is annotation and
docstring work, but the lint surfaced two defects that had nothing to do with
style:
- `gpt_chunks.py` stripped its GPT preamble with
`input_string.strip("iSTS chunks:")`. str.strip takes a SET OF CHARACTERS,
not a prefix, so it also ate any leading i/S/T/s/c/h/u/n/k/space/colon:
"iSTS chunks: sentence one" came out as "entence one", silently losing the
first letter of every chunked sentence. Now removeprefix. ruff found it as
B005 + PLE1310.
- `is_numbered_line_format` had `for string in string` in its list branch,
which raises NameError the moment a list is passed (F821).
- `processing.test_XML()` read a module-level `alignments_data` that does not
exist at module scope; it only ever ran when pasted into a live session. It
takes the data as an argument now and is renamed `log_first_alignment` so
pytest stops collecting a non-test as a test.
Also real rather than cosmetic:
- The OpenAI key was a placeholder string literal in three files, one of them
carrying a "Replace me with os.environ['API_KEY']" comment. All three read
OPENAI_API_KEY now and say so when it is missing -- as committed, none of
these scripts could actually run.
- `rating.rate_similarity` was submitted unfinished: it built a client, never
touched its two arguments and returned None despite `-> str`. It now makes
the same call the rest of the package makes.
- The two 2 kB alignment prompts and one 344-character chunking prompt moved
to prompts/*.txt. Wrapping them in source would have put newlines into the
middle of the prompt.
- `format_alignments` parsed GPT's replies with `eval()`; the file holds one
repr()'d string per line, so ast.literal_eval is both sufficient and safe.
- NLP/__init__.py deleted -- it made the course directory a package named
"NLP", which is an invalid module name (N999) and which nothing imports;
the scripts use plain `import processing`.
INP001 is settled the same way throughout: a coursework file run as
`python x.py` gets a shebang and the executable bit, which is what it is,
rather than an __init__.py that would make a course code into a module name.
Verified: every module compiles and `./run.sh run Programming/NLP` is clean.
The scripts themselves need the SemEval corpus and a paid API key, so they
are not executed here.
Repo-wide: 3,159 findings -> 2,775.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172dj34FDpao6wsix8ZuCzE
73 lines
2.4 KiB
Python
Executable File
73 lines
2.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Get chunk alignment from ChatGPT.
|
|
|
|
The two prompts live in ``prompts/`` rather than in this file: they are 2 kB
|
|
of instructions each, on lines far past any line-length limit, and wrapping
|
|
them here would put newlines into the middle of the prompt itself.
|
|
"""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
from openai import OpenAI
|
|
|
|
PROMPTS = Path(__file__).parent / "prompts"
|
|
THE_PROMPT = (PROMPTS / "alignment.txt").read_text(encoding="utf-8")
|
|
THE_PROMPT_WITH_COMMENT = (PROMPTS / "alignment_with_comment.txt").read_text(
|
|
encoding="utf-8"
|
|
)
|
|
|
|
|
|
def create_gpt() -> OpenAI:
|
|
"""Build an OpenAI client from OPENAI_API_KEY.
|
|
|
|
The key used to be a placeholder string literal here, which meant the
|
|
script could not actually run as committed.
|
|
"""
|
|
key = os.environ.get("OPENAI_API_KEY")
|
|
if not key:
|
|
msg = "set OPENAI_API_KEY to run the alignment scripts"
|
|
raise RuntimeError(msg)
|
|
return OpenAI(api_key=key)
|
|
|
|
|
|
def call_api(client: OpenAI, chunks: str) -> str:
|
|
"""Ask the model to align one pair of chunked sentences."""
|
|
response = client.chat.completions.create(
|
|
model="gpt-3.5-turbo-1106",
|
|
messages=[
|
|
{"role": "user", "content": THE_PROMPT},
|
|
{"role": "user", "content": chunks},
|
|
],
|
|
)
|
|
return response.choices[0].message.content.strip()
|
|
|
|
|
|
def call_api_examples(client: OpenAI, examples: pd.DataFrame, alignment: str) -> str:
|
|
"""Ask the model to align, few-shot prompted with worked examples."""
|
|
user_input = []
|
|
assistant_output = []
|
|
for _index, row in examples.iterrows():
|
|
chunks = ""
|
|
for chunk in row["chunked_sentance1"]:
|
|
chunks = chunks + "[ " + chunk + " ] "
|
|
chunks = chunks + "\n"
|
|
for chunk in row["chunked_sentance2"]:
|
|
chunks = chunks + "[ " + chunk + " ] "
|
|
user_input.append(chunks)
|
|
assistant_output.append(row["alignment_text"])
|
|
|
|
messages = []
|
|
messages.append({"role": "user", "content": THE_PROMPT})
|
|
for u, a in zip(user_input, assistant_output, strict=True):
|
|
messages.append({"role": "user", "content": u})
|
|
messages.append({"role": "assistant", "content": a})
|
|
|
|
messages.append({"role": "user", "content": alignment})
|
|
|
|
response = client.chat.completions.create(
|
|
model="gpt-3.5-turbo-1106", messages=messages
|
|
)
|
|
return response.choices[0].message.content.strip()
|