Skip to content

Commit ce6cd1e

Browse files
committed
simplify fastembed index initialization test
fix
1 parent b2ea259 commit ce6cd1e

File tree

2 files changed

+28
-66
lines changed

2 files changed

+28
-66
lines changed

tests/test_actions_llm_embedding_lazy_init.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import pytest
1919

2020
from nemoguardrails import RailsConfig
21-
from nemoguardrails.imports import check_optional_dependency
2221
from tests.utils import TestChat
2322

2423
BASE_CONFIG = textwrap.dedent("""
@@ -297,40 +296,23 @@ def test_passthrough_no_cache_created(self, tmp_path):
297296
del os.environ["FASTEMBED_CACHE_PATH"]
298297

299298

300-
_has_fastembed = check_optional_dependency("fastembed")
301-
302-
303-
@pytest.mark.skipif(not _has_fastembed, reason="fastembed not installed")
304-
class TestFastEmbedDownloadedForDialogRails:
305-
def test_dialog_rails_cache_created_on_generate(self, tmp_path):
306-
import os
307-
308-
cache_dir = tmp_path / "fastembed_cache"
309-
cache_dir.mkdir()
310-
os.environ["FASTEMBED_CACHE_PATH"] = str(cache_dir)
311-
312-
try:
313-
config = RailsConfig.from_content(
314-
yaml_content=BASE_CONFIG,
315-
colang_content=USER_DEFINITIONS + BOT_DEFINITIONS + FLOW_DEFINITIONS,
316-
)
317-
chat = TestChat(
318-
config,
319-
llm_completions=["express greeting", "Hello! How can I help you?"],
320-
)
321-
322-
cache_before = list(cache_dir.iterdir())
323-
assert len(cache_before) == 0, "Cache should be empty before generate"
299+
class TestIndexInitializedAfterGenerate:
300+
def test_user_message_index_initialized_after_generate(self):
301+
config = RailsConfig.from_content(
302+
yaml_content=BASE_CONFIG,
303+
colang_content=USER_DEFINITIONS + BOT_DEFINITIONS + FLOW_DEFINITIONS,
304+
)
305+
chat = TestChat(
306+
config,
307+
llm_completions=["express greeting", "Hello! How can I help you?"],
308+
)
309+
actions = chat.app.llm_generation_actions
324310

325-
response = chat.app.generate(messages=[{"role": "user", "content": "hello"}])
311+
assert actions.user_message_index is None, "Index should be None before generate"
326312

327-
assert response is not None
313+
chat.app.generate(messages=[{"role": "user", "content": "hello"}])
328314

329-
cache_after = list(cache_dir.iterdir())
330-
assert len(cache_after) > 0, "FastEmbed cache should have models after generate with dialog rails"
331-
finally:
332-
if "FASTEMBED_CACHE_PATH" in os.environ:
333-
del os.environ["FASTEMBED_CACHE_PATH"]
315+
assert actions.user_message_index is not None, "Index should be initialized after generate"
334316

335317

336318
class TestConcurrentInitialization:

tests/v2_x/test_llm_embedding_lazy_init.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515

1616
import textwrap
1717

18-
import pytest
19-
2018
from nemoguardrails import RailsConfig
21-
from nemoguardrails.imports import check_optional_dependency
2219
from tests.utils import TestChat
2320

2421
BASE_CONFIG = textwrap.dedent("""
@@ -147,37 +144,20 @@ def test_passthrough_no_cache_created(self, tmp_path):
147144
del os.environ["FASTEMBED_CACHE_PATH"]
148145

149146

150-
_has_fastembed = check_optional_dependency("fastembed")
151-
152-
153-
@pytest.mark.skipif(not _has_fastembed, reason="fastembed not installed")
154-
class TestFastEmbedDownloadedForDialogRails:
155-
def test_dialog_rails_cache_created_on_generate(self, tmp_path):
156-
import os
157-
158-
cache_dir = tmp_path / "fastembed_cache"
159-
cache_dir.mkdir()
160-
os.environ["FASTEMBED_CACHE_PATH"] = str(cache_dir)
161-
162-
try:
163-
config = RailsConfig.from_content(
164-
yaml_content=BASE_CONFIG,
165-
colang_content=DIALOG_COLANG,
166-
)
167-
chat = TestChat(
168-
config,
169-
llm_completions=["user expressed greeting"],
170-
)
147+
class TestIndexInitializedAfterGenerate:
148+
def test_user_message_index_initialized_after_generate(self):
149+
config = RailsConfig.from_content(
150+
yaml_content=BASE_CONFIG,
151+
colang_content=DIALOG_COLANG,
152+
)
153+
chat = TestChat(
154+
config,
155+
llm_completions=["user expressed greeting"],
156+
)
157+
actions = chat.app.llm_generation_actions
171158

172-
cache_before = list(cache_dir.iterdir())
173-
assert len(cache_before) == 0, "Cache should be empty before generate"
159+
assert actions.user_message_index is None, "Index should be None before generate"
174160

175-
response = chat.app.generate(messages=[{"role": "user", "content": "hello"}])
161+
chat.app.generate(messages=[{"role": "user", "content": "hello"}])
176162

177-
assert response is not None
178-
179-
cache_after = list(cache_dir.iterdir())
180-
assert len(cache_after) > 0, "FastEmbed cache should have models after generate with dialog rails"
181-
finally:
182-
if "FASTEMBED_CACHE_PATH" in os.environ:
183-
del os.environ["FASTEMBED_CACHE_PATH"]
163+
assert actions.user_message_index is not None, "Index should be initialized after generate"

0 commit comments

Comments
 (0)