Skip to content

Commit

Permalink
Add verbosity to extraction chain (#137)
Browse files Browse the repository at this point in the history
Add ability to set verbosity to extraction chain.
  • Loading branch information
eyurtsev authored Apr 24, 2023
1 parent b986eda commit 7291946
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 9 additions & 1 deletion kor/extraction/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def create_extraction_chain(
validator: Optional[Validator] = None,
input_formatter: InputFormatter = None,
instruction_template: Optional[PromptTemplate] = None,
verbose: Optional[bool] = None,
**encoder_kwargs: Any,
) -> LLMChain:
"""Create an extraction chain.
Expand All @@ -73,12 +74,13 @@ def create_extraction_chain(
* "type_description": type description of the node (from TypeDescriptor)
* "format_instructions": information on how to format the output
(from Encoder)
verbose: if provided, sets the verbosity on the chain, otherwise default
verbosity of the chain will be used
encoder_kwargs: Keyword arguments to pass to the encoder class
Returns:
A langchain chain
Examples:
.. code-block:: python
Expand All @@ -94,6 +96,11 @@ def create_extraction_chain(
raise ValueError(f"node must be an Object got {type(node)}")
encoder = initialize_encoder(encoder_or_encoder_class, node, **encoder_kwargs)
type_descriptor_to_use = initialize_type_descriptors(type_descriptor)

chain_kwargs = {}
if verbose is not None:
chain_kwargs["verbose"] = verbose

return LLMChain(
llm=llm,
prompt=create_langchain_prompt(
Expand All @@ -104,6 +111,7 @@ def create_extraction_chain(
instruction_template=instruction_template,
input_formatter=input_formatter,
),
**chain_kwargs,
)


Expand Down
21 changes: 20 additions & 1 deletion tests/extraction/test_extraction_with_chain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test that the extraction chain works as expected."""
from typing import Any, Mapping
from typing import Any, Mapping, Optional

import langchain
import pytest
from langchain import PromptTemplate
from langchain.chains import LLMChain
Expand Down Expand Up @@ -102,6 +103,24 @@ def test_not_implemented_assertion_raised_for_csv(options: Mapping[str, Any]) ->
create_extraction_chain(chat_model, **options)


@pytest.mark.parametrize("verbose", [True, False, None])
def test_instantiation_with_verbose_flag(verbose: Optional[bool]) -> None:
"""Create an extraction chain."""
chat_model = ToyChatModel(response="hello")
chain = create_extraction_chain(
chat_model,
SIMPLE_OBJECT_SCHEMA,
encoder_or_encoder_class="json",
verbose=verbose,
)
assert isinstance(chain, LLMChain)
if verbose is None:
expected_verbose = langchain.verbose
else:
expected_verbose = verbose
assert chain.verbose == expected_verbose


def test_using_custom_template() -> None:
"""Create an extraction chain with a custom template."""
template = PromptTemplate(
Expand Down

0 comments on commit 7291946

Please sign in to comment.