Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions langextract/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ def _create_model_with_schema(
A model instance with fence_output configured appropriately.
"""

providers.load_builtins_once()
providers.load_plugins_once()

if config.provider:
provider_class = router.resolve_provider(config.provider)
else:
providers.load_builtins_once()
providers.load_plugins_once()
provider_class = router.resolve(config.model_id)

schema_instance = None
Expand Down
61 changes: 61 additions & 0 deletions tests/test_ollama_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,67 @@ def _ollama_available():
return result == 0


@pytest.mark.skipif(not _ollama_available(), reason="Ollama not running")
def test_ollama_provider_via_model_config_must_be_first_test():
"""
Test Ollama provider using ModelConfig.

This test ensures that the Ollama provider can be used via ModelConfig
and that the provider_kwargs are correctly passed to the provider.

Previously, if the first attempt to extract passed the provider name for
a built-in provider rather than allowing it to be inferred by the model_id,
the extract call would fail with:
langextract.core.exceptions.InferenceConfigError:
No provider found matching: 'ollama'. Available providers can be listed
with list_providers()
"""
input_text = "Isaac Asimov was a prolific science fiction writer."
prompt = "Extract the author's full name and their primary literary genre."

model_id = "gemma2:2b"
config = lx.factory.ModelConfig(
model_id=model_id,
provider="ollama",
provider_kwargs={"model_url": "http://localhost:11434"},
)

examples = [
lx.data.ExampleData(
text=(
"J.R.R. Tolkien was an English writer, best known for"
" high-fantasy."
),
extractions=[
lx.data.Extraction(
extraction_class="author_details",
extraction_text="J.R.R. Tolkien was an English writer...",
attributes={
"name": "J.R.R. Tolkien",
"genre": "high-fantasy",
},
)
],
)
]

result = lx.extract(
text_or_documents=input_text,
prompt_description=prompt,
examples=examples,
config=config,
temperature=0.3,
fence_output=False,
use_schema_constraints=False,
)

assert len(result.extractions) > 0
extraction = result.extractions[0]
assert extraction.extraction_class == "author_details"
if extraction.attributes:
assert "asimov" in extraction.attributes.get("name", "").lower()


@pytest.mark.skipif(not _ollama_available(), reason="Ollama not running")
def test_ollama_extraction():
input_text = "Isaac Asimov was a prolific science fiction writer."
Expand Down