Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbosity to extraction chain #137

Merged
merged 1 commit into from
Apr 24, 2023
Merged
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
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