Skip to content
20 changes: 11 additions & 9 deletions openai/tests/test_file_cli.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import json
import subprocess
import time
from tempfile import NamedTemporaryFile
from pathlib import Path

STILL_PROCESSING = "File is still processing. Check back later."


def test_file_cli() -> None:
def test_file_cli(tmp_path: Path) -> None:
contents = json.dumps({"prompt": "1 + 3 =", "completion": "4"}) + "\n"
with NamedTemporaryFile(suffix=".jsonl", mode="wb") as train_file:
train_file.write(contents.encode("utf-8"))
train_file.flush()
create_output = subprocess.check_output(
["openai", "api", "files.create", "-f", train_file.name, "-p", "fine-tune"]
)
train_file = tmp_path / "data.jsonl"
train_file.write_bytes(contents.encode("utf-8"))
create_output = subprocess.check_output(
["openai", "api", "files.create", "-f", str(train_file), "-p", "fine-tune"]
)

file_obj = json.loads(create_output)
assert file_obj["bytes"] == len(contents)

file_id: str = file_obj["id"]
assert file_id.startswith("file-")

start_time = time.time()
while True:
delete_result = subprocess.run(
["openai", "api", "files.delete", "-i", file_id],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
text=True,
)
if delete_result.returncode == 0:
break
Expand Down
30 changes: 14 additions & 16 deletions openai/tests/test_long_examples_validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import subprocess
from tempfile import NamedTemporaryFile
from pathlib import Path

import pytest

Expand All @@ -10,7 +10,7 @@

@pytest.mark.skipif(not HAS_PANDAS, reason=PANDAS_INSTRUCTIONS)
@pytest.mark.skipif(not HAS_NUMPY, reason=NUMPY_INSTRUCTIONS)
def test_long_examples_validator() -> None:
def test_long_examples_validator(tmp_path: Path) -> None:
"""
Ensures that long_examples_validator() handles previously applied recommendations,
namely dropped duplicates, without resulting in a KeyError.
Expand All @@ -30,21 +30,19 @@ def test_long_examples_validator() -> None:
{"prompt": long_prompt, "completion": long_completion}, # 2 of 2 duplicates
]

with NamedTemporaryFile(suffix=".jsonl", mode="w") as training_data:
print(training_data.name)
train_file = tmp_path / "data.jsonl"
print(train_file) # show the full path to the temporary file
with open(train_file, mode="w", encoding="utf-8") as file:
for prompt_completion_row in unprepared_training_data:
training_data.write(json.dumps(prompt_completion_row) + "\n")
training_data.flush()

prepared_data_cmd_output = subprocess.run(
[f"openai tools fine_tunes.prepare_data -f {training_data.name}"],
stdout=subprocess.PIPE,
text=True,
input="y\ny\ny\ny\ny", # apply all recommendations, one at a time
stderr=subprocess.PIPE,
encoding="utf-8",
shell=True,
)
print(json.dumps(prompt_completion_row), file=file)

prepared_data_cmd_output = subprocess.run(
["openai", "tools", "fine_tunes.prepare_data", "-f", str(train_file)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
input="y\ny\ny\ny\ny", # apply all recommendations, one at a time
text=True,
)

# validate data was prepared successfully
assert prepared_data_cmd_output.stderr == ""
Expand Down
30 changes: 12 additions & 18 deletions openai/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
import json
from tempfile import NamedTemporaryFile
from pathlib import Path

import pytest
from pytest import MonkeyPatch

import openai
from openai import util


@pytest.fixture(scope="function")
def api_key_file():
saved_path = openai.api_key_path
try:
with NamedTemporaryFile(prefix="openai-api-key", mode="wt") as tmp:
openai.api_key_path = tmp.name
yield tmp
finally:
openai.api_key_path = saved_path


def test_openai_api_key_path(api_key_file) -> None:
print("sk-foo", file=api_key_file)
api_key_file.flush()
def api_key_file(tmp_path: Path, monkeypatch: MonkeyPatch) -> Path:
key_file = tmp_path / "openai_api_key"
monkeypatch.setattr("openai.api_key_path", str(key_file))
return key_file


def test_openai_api_key_path(api_key_file: Path) -> None:
api_key_file.write_text("sk-foo\n", encoding="utf-8")
assert util.default_api_key() == "sk-foo"


def test_openai_api_key_path_with_malformed_key(api_key_file) -> None:
print("malformed-api-key", file=api_key_file)
api_key_file.flush()
def test_openai_api_key_path_with_malformed_key(api_key_file: Path) -> None:
api_key_file.write_text("malformed-api-key\n", encoding="utf-8")
with pytest.raises(ValueError, match="Malformed API key"):
util.default_api_key()

Expand Down