|
1 | 1 | import copy |
| 2 | +import os |
2 | 3 | import time |
3 | 4 | from pathlib import Path |
4 | 5 | from typing import Dict |
|
10 | 11 | from spacy.tokens import DocBin |
11 | 12 |
|
12 | 13 | from ..cache import BatchCache |
| 14 | +from .compat import has_openai_key |
13 | 15 |
|
14 | 16 | _DEFAULT_CFG = { |
15 | 17 | "backend": {"api": "NoOp", "config": {"model": "NoOp"}}, |
@@ -153,3 +155,32 @@ def test_path_dir_created(): |
153 | 155 | config["cache"]["path"] = str(tmpdir / "new_dir") |
154 | 156 | spacy.blank("en").add_pipe("llm", config=config) |
155 | 157 | assert (tmpdir / "new_dir").exists() |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.external |
| 161 | +@pytest.mark.skipif(has_openai_key is False, reason="OpenAI API key not available") |
| 162 | +def test_caching_llm_io() -> None: |
| 163 | + """Test availability of LLM IO after caching.""" |
| 164 | + with spacy.util.make_tempdir() as tmpdir: |
| 165 | + config = copy.deepcopy(_DEFAULT_CFG) |
| 166 | + config["backend"]["api"] = "OpenAI" # type: ignore[index] |
| 167 | + config["backend"]["config"] = {"model": "gpt-3.5-turbo"} # type: ignore[index] |
| 168 | + config["cache"]["path"] = str(tmpdir) # type: ignore[index] |
| 169 | + config["cache"]["batch_size"] = 3 # type: ignore[index] |
| 170 | + config["save_io"] = True |
| 171 | + nlp = spacy.blank("en") |
| 172 | + nlp.add_pipe("llm", config=config) |
| 173 | + docs = [nlp(txt) for txt in ("What's 1+1?", "What's 2+2?", "What's 3+3?")] |
| 174 | + |
| 175 | + assert all([doc.user_data["llm_io"]["llm"]] for doc in docs) |
| 176 | + cached_file_names = [ |
| 177 | + f |
| 178 | + for f in os.listdir(tmpdir) |
| 179 | + if os.path.isfile(tmpdir / f) and f.endswith(".spacy") |
| 180 | + ] |
| 181 | + assert len(cached_file_names) == 1 |
| 182 | + cached_docs = list( |
| 183 | + DocBin().from_disk(tmpdir / cached_file_names[0]).get_docs(nlp.vocab) |
| 184 | + ) |
| 185 | + assert len(cached_docs) == 3 |
| 186 | + assert all([doc.user_data["llm_io"]["llm"]] for doc in cached_docs) |
0 commit comments