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

PromptNER based Chain-of-Thought prompting for span tasks #180

Merged
merged 108 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 95 commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
a175d36
initial POC for Chain of Thought NER task
Jun 15, 2023
820337b
ruff fix
Jun 15, 2023
ea261aa
Merge branch 'main' of ssh://github.com/explosion/spacy-llm into kab/…
Jun 20, 2023
6fa0b6a
Merge branch 'develop' of ssh://github.com/explosion/spacy-llm into k…
Jul 4, 2023
4c562d9
update template
vinbo8 Jul 5, 2023
54a9eae
consilidate approach to work with main SpanTask
Jul 5, 2023
8737f4a
Merge branch 'develop' of ssh://github.com/explosion/spacy-llm into k…
Jul 5, 2023
7a1fbdb
fix tests around label consistency checks
Jul 5, 2023
2921ca9
Merge branch 'main' into kab/cot-ner
vinbo8 Jul 6, 2023
2a0b1c8
merge kab/cot-ner-integrate
vinbo8 Jul 6, 2023
4772cb2
fix edge cases
vinbo8 Jul 6, 2023
d95eaac
merge develop
vinbo8 Jul 6, 2023
57a71b3
update label consistency checks
vinbo8 Jul 6, 2023
6335b3b
move label consistency checks
vinbo8 Jul 6, 2023
c565256
handle labels in span.py
vinbo8 Jul 6, 2023
54f28d2
cleanup older NER
vinbo8 Jul 17, 2023
aa25bba
fixes
vinbo8 Jul 17, 2023
f52feb5
cleanup
vinbo8 Jul 17, 2023
19d55cc
update NER template with label_definitions + initial description, fix…
Jul 21, 2023
6edea90
properly parametrize response parsing for SpanReason test
Jul 21, 2023
9f08287
start to parametrize NER tests properly with new v3 template
Jul 21, 2023
e4f8195
fix docstring
Jul 21, 2023
cb64f6a
rm single_match since it's always true now
Jul 21, 2023
a8c88bf
rm single_match since it's always true now
Jul 21, 2023
3e34add
fix typing of description and default properly for SpanCatTask
Jul 21, 2023
c40b897
fix test
Jul 21, 2023
fc3455c
fix NER tests
Jul 24, 2023
36579f4
fir more ner tests + add initial test for SpanReason.from_str
Jul 24, 2023
e0f2ef0
fix ner to_disk test
Jul 25, 2023
5a4b681
enable adding ner prompt examples from initialize and fix ner_init test
Jul 25, 2023
18e5e10
test fixes
Jul 25, 2023
eddac8c
add yaml/jsonl version of ner examples. Fix inconsistent labels tests
Jul 25, 2023
1299787
use yaml/jsonl versions of ner examples
Jul 25, 2023
b0a076d
actually check scoring with real LLM call
Jul 25, 2023
b432a50
rename format_response to extract_span_reasons
Jul 25, 2023
ee64727
move Self to compat types
Jul 25, 2023
01e1de9
fix test for serde
Jul 26, 2023
8c31b05
Self only in 3.10+
Jul 27, 2023
6235091
Self only in 3.11+ actually
Jul 27, 2023
66c7377
ner test fixes
Jul 27, 2023
8e5952c
convert spancat to new span task format
Jul 27, 2023
3fa3fbe
add better doc for SpanReason.to_str
Jul 27, 2023
7beaf93
fixing tests for spancat
Jul 27, 2023
0e162d6
adjust span matching by adding an setting
Jul 31, 2023
6894a80
support conditional allow_overlap like standard spancat
Jul 31, 2023
bb261bd
remove dict | operator that only works in python3.9 +
Jul 31, 2023
dfa96c0
disable test for now so CI passes
Jul 31, 2023
1700849
revert spanreason start_char
Jul 31, 2023
ae3a545
fix spancat template rendering for allow_overlap
Jul 31, 2023
850e8ee
clean up tests for init with spacy examples
Jul 31, 2023
7242d6e
fix spancat test?
Jul 31, 2023
150d21e
make spancat scoring use external model, not weird dummy data
Jul 31, 2023
abd52b8
run case sensitive matching then fallback to case insensitive if the …
Jul 31, 2023
bde5c8d
rm prev_span reference in parsing
Jul 31, 2023
c86b0e9
separate span parsing for a single doc into its own function
Jul 31, 2023
91bffaf
fix typing on the regression test
Jul 31, 2023
d02b425
add description field to cfg_keys so it gets serialized
Jul 31, 2023
17a2a9a
add old spancat/ner versions to tasks.legacy module
Aug 1, 2023
16f5d04
add deprecation warnings + test deprecation warnings
Aug 1, 2023
7cab77f
update usage examples
Aug 1, 2023
b1a4adf
update examples and readme
Aug 1, 2023
8b40100
Merge branch 'main' of ssh://github.com/explosion/spacy-llm into kab/…
Aug 1, 2023
574c9fe
Merge branch 'kab/cot-ner' of ssh://github.com/explosion/spacy-llm in…
Aug 1, 2023
3ab56e5
fix usage_examples
Aug 1, 2023
46802bb
Merge branch 'kab/cot-ner' of ssh://github.com/explosion/spacy-llm in…
Aug 1, 2023
629dc3d
Merge pull request #239 from explosion/kab/cot-ner-legacy
Aug 2, 2023
12093bf
Merge branch 'develop' of ssh://github.com/explosion/spacy-llm into k…
Aug 2, 2023
f824a8e
Merge branch 'kab/cot-ner' of ssh://github.com/explosion/spacy-llm in…
Aug 2, 2023
09533e0
rename warning to LLMW001, fix usage_example + readme tests
Aug 2, 2023
a6888d4
remove separate case sensitive match step before doing case insensiti…
Aug 2, 2023
d43c840
fix regression test to have 3 ents
Aug 2, 2023
b42e849
fix incremental parsing
Aug 2, 2023
47c8a75
rm extra docstring stuff
Aug 2, 2023
edbc17d
rm extra test
Aug 3, 2023
990edf3
consolidate new spans template and ensure valid labels appear in the …
Aug 4, 2023
f77b0cd
make prompt_examples required since it's required in confection facto…
Aug 4, 2023
e87f1a4
fix template rendering tests with new optional description and defaul…
Aug 4, 2023
03ab495
Merge branch 'develop' of ssh://github.com/explosion/spacy-llm into k…
Aug 8, 2023
d1b1410
Remove extra deprecation warning
Aug 8, 2023
208dbd6
Update usage_examples/ner_v3_openai/README.md
Aug 8, 2023
20be25a
Sync with new task structure.
rmitsch Aug 21, 2023
e6f63f5
Fix 3.6 Protocol import.
rmitsch Aug 21, 2023
af04a26
Update ignored warnings.
rmitsch Aug 21, 2023
8a95115
Fix filterwarnings.
rmitsch Aug 21, 2023
80fc6c8
Update filterwarnings.
rmitsch Aug 21, 2023
74a8486
Renamed examples to prompt_examples.
rmitsch Aug 21, 2023
61a6ab7
Add default example if none are provided to COT NER/SpanCat tasks (#270)
Aug 24, 2023
fa4e879
Test Pydantic Mac OS Py 3.8 issue.
rmitsch Aug 24, 2023
5903a03
Incorporate feedback. Readd Pydantic REL example workaround.
rmitsch Aug 24, 2023
69f5111
Readd NER Dolly usage example, removed TextCat Dolly one.
rmitsch Aug 24, 2023
4706aa7
Update NER Dolly usage example to use NER.v3.
rmitsch Aug 24, 2023
134da76
Readd NER Dolly test. Revert to NER.v2. Refactor span extraction for …
rmitsch Aug 24, 2023
7d27d0e
Fix span reason extraction.
rmitsch Aug 24, 2023
2979ffc
Add working Paris-Paris-Paris example.
rmitsch Aug 24, 2023
398652a
Uncomment example for NER prediction test.
rmitsch Aug 24, 2023
b4ad4d8
Fix NER prediction test.
rmitsch Aug 24, 2023
66c4fae
remove errors class entirely
svlandeg Aug 24, 2023
88a2482
Merge branch 'kab/cot-ner' of github.com:explosion/spacy-llm into kab…
rmitsch Aug 25, 2023
2664a47
Update .github/workflows/test.yml
rmitsch Aug 25, 2023
ee7fcdd
Update spacy_llm/tests/tasks/test_ner.py
rmitsch Aug 25, 2023
b64bf90
Remove overlap part in NER template.
rmitsch Aug 25, 2023
f895343
Merge branch 'kab/cot-ner' of github.com:explosion/spacy-llm into kab…
rmitsch Aug 25, 2023
c763689
Remove overlap path in NER and SpanCat templates.
rmitsch Aug 25, 2023
d2f2a07
Update spacy_llm/tasks/spancat/registry.py
rmitsch Aug 25, 2023
48bbd3c
Update spacy_llm/tasks/ner/registry.py
rmitsch Aug 25, 2023
68c0a55
Merge branch 'develop' into kab/cot-ner
rmitsch Aug 25, 2023
1c269e0
Changed SpanCat prompt intro.
rmitsch Aug 25, 2023
3a31530
Add docstring info for description.
rmitsch Aug 25, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ env:
jobs:
run:
strategy:
fail-fast: true
fail-fast: false
rmitsch marked this conversation as resolved.
Show resolved Hide resolved
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python_version: ["3.11"]
Expand All @@ -39,7 +39,7 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
Expand Down
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ This package integrates Large Language Models (LLMs) into [spaCy](https://spacy.

- Serializable `llm` **component** to integrate prompts into your spaCy pipeline
- **Modular functions** to define the [**task**](https://spacy.io/api/large-language-models#tasks) (prompting and parsing) and [**model**](https://spacy.io/api/large-language-models#models)
- Interfaces with the APIs of
- Interfaces with the APIs of
- **[OpenAI](https://platform.openai.com/docs/api-reference/)**
- **[Cohere](https://docs.cohere.com/reference/generate)**
- **[Anthropic](https://docs.anthropic.com/claude/reference/)**
- Supports open-source LLMs hosted on Hugging Face 🤗:
- **[Falcon](https://huggingface.co/tiiuae)**
- **[Dolly](https://huggingface.co/databricks)**
- **[Llama 2](https://huggingface.co/meta-llama)**
- **[Llama 2](https://huggingface.co/meta-llama)**
- **[OpenLLaMA](https://huggingface.co/openlm-research)**
- **[StableLM](https://huggingface.co/stabilityai)**
- Integration with [LangChain](https://github.com/hwchase17/langchain) 🦜️🔗 - all `langchain` models and features can be used in `spacy-llm`
- Integration with [LangChain](https://github.com/hwchase17/langchain) 🦜️🔗 - all `langchain` models and features can be used in `spacy-llm`
- Tasks available out of the box:
- Named Entity Recognition
- Text classification
Expand Down Expand Up @@ -58,13 +58,18 @@ python -m pip install spacy-llm
## 🐍 Quickstart

The task and the model have to be supplied to the `llm` pipeline component using [spaCy's config
system](https://spacy.io/api/data-formats#config).
system](https://spacy.io/api/data-formats#config).

Let's run some text classification using a GPT-4 model from OpenAI. If you're using hosted APIs (as opposed to local
Let's run some text classification using a GPT-4 model from OpenAI. If you're using hosted APIs (as opposed to local
models like Falcon, Dolly or LLaMA), ensure to that your API keys are set as environmental variables.

Create a config file `config.cfg` containing at least the following
(or see the full example [here](usage_examples/textcat_openai)):
Create a new API key from openai.com or fetch an existing one, and ensure the
keys are set as environmental variables. For more background information, see
the [OpenAI](/api/large-language-models#gpt-3-5) section.

Create a config file `config.cfg` containing at least the following (or see the
full example
[here](https://github.com/explosion/spacy-llm/tree/main/usage_examples/textcat_openai)):

```ini
[nlp]
Expand Down Expand Up @@ -93,11 +98,13 @@ from spacy_llm.util import assemble
nlp = assemble("config.cfg")
doc = nlp("You look gorgeous!")
print(doc.cats)
# {"COMPLIMENT": 1.0, "INSULT": 0.0}
```

That's it! There's a lot of other features - prompt templating, more tasks, logging etc. For more information on how to
That's it! There's a lot of other features - prompt templating, more tasks, logging etc. For more information on how to
use those, check out https://spacy.io/api/large-language-models.


## 🚀 Ongoing work

In the near future, we will
Expand All @@ -110,7 +117,7 @@ PRs are always welcome!

## 📝️ Reporting issues

If you have questions regarding the usage of `spacy-llm`, or want to give us feedback after giving it a spin, please use
If you have questions regarding the usage of `spacy-llm`, or want to give us feedback after giving it a spin, please use
the [discussion board](https://github.com/explosion/spacy-llm/discussions).
Bug reports can be filed on the [spaCy issue tracker](https://github.com/explosion/spacy-llm/issues). Thank you!

Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ filterwarnings = [
"ignore:^.*You are trying to use a chat model. This way of initializing it is no longer supported. Instead, please use.*:UserWarning",
"ignore:^.*Xformers is not installed correctly.*:",
"ignore:^.*The 'warn' method is deprecated, use 'warning' instead.*:DeprecationWarning",
"ignore:^.*Support for class-based `config` is deprecated.*:"
"ignore:^.*Support for class-based `config` is deprecated.*:",
"ignore:^.*The `dict` method is deprecated; use `model_dump` instead.*",
"ignore:^.*The `parse_obj` method is deprecated; use `model_validate` instead.*",
"ignore:^.*`__get_validators__` is deprecated.*",
"ignore:^.*The `construct` method is deprecated.*"
]
markers = [
"external: interacts with a (potentially cost-incurring) third-party API",
Expand Down
3 changes: 2 additions & 1 deletion spacy_llm/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
if sys.version_info[:2] >= (3, 8): # Python 3.8+
from typing import Literal, Protocol, runtime_checkable
else:
from typing_extensions import Literal, Protocol, runtime_checkable # noqa: F401
from typing_extensions import runtime_checkable # noqa: F401
from typing_extensions import Literal, Protocol # noqa: F401

if sys.version_info[:2] >= (3, 9): # Python 3.9+
from typing import TypedDict # noqa: F401
Expand Down
3 changes: 2 additions & 1 deletion spacy_llm/registry/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def _fewshot_reader(eg_path: Path) -> Iterable[Dict[str, Any]]:
else:
if not eg_path.exists():
raise ValueError(
"Specified file path doesn't exist. Please ensure to provide a valid file path."
f"Specified file path: {str(eg_path)} doesn't exist. "
"Please ensure to provide a valid file path."
)

suffix = eg_path.suffix.replace("yaml", "yml")
Expand Down
10 changes: 4 additions & 6 deletions spacy_llm/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
from .builtin_task import BuiltinTask
from .lemma import LemmaTask, make_lemma_task
from .ner import NERTask, make_ner_task, make_ner_task_v2
from .ner import NERTask, make_ner_task_v3
from .noop import NoopTask, make_noop_task
from .rel import RELTask, make_rel_task
from .sentiment import SentimentTask, make_sentiment_task
from .spancat import SpanCatTask, make_spancat_task, make_spancat_task_v2
from .spancat import SpanCatTask, make_spancat_task_v3
from .summarization import SummarizationTask, make_summarization_task
from .textcat import TextCatTask, make_textcat_task

__all__ = [
"make_lemma_task",
"make_ner_task",
"make_ner_task_v2",
"make_ner_task_v3",
"make_noop_task",
"make_rel_task",
"make_sentiment_task",
"make_spancat_task",
"make_spancat_task_v2",
"make_spancat_task_v3",
"make_summarization_task",
"make_textcat_task",
"BuiltinTask",
Expand Down
10 changes: 8 additions & 2 deletions spacy_llm/tasks/ner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from .registry import make_ner_task, make_ner_task_v2
from .registry import make_ner_task, make_ner_task_v2, make_ner_task_v3
from .task import NERTask
from .util import NERExample

__all__ = ["make_ner_task", "make_ner_task_v2", "NERExample", "NERTask"]
__all__ = [
"make_ner_task",
"make_ner_task_v2",
"make_ner_task_v3",
"NERExample",
"NERTask",
]
71 changes: 69 additions & 2 deletions spacy_llm/tasks/ner/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
from ...ty import ExamplesConfigType, FewshotExample, Scorer, TaskResponseParser
from ...util import split_labels
from ..span import parse_responses as parse_span_responses
from .task import DEFAULT_NER_TEMPLATE_V1, DEFAULT_NER_TEMPLATE_V2, NERTask, SpanTask
from .util import NERExample, score
from ..span import parse_responses_cot as parse_span_responses_cot
from ..span.util import check_label_consistency, check_label_consistency_cot
from .task import DEFAULT_NER_TEMPLATE_V1, DEFAULT_NER_TEMPLATE_V2
from .task import DEFAULT_NER_TEMPLATE_V3, NERTask, SpanTask
from .util import NERCoTExample, NERExample, score


@registry.llm_tasks("spacy.NER.v1")
Expand Down Expand Up @@ -54,6 +57,8 @@ def make_ner_task(
single_match=single_match,
label_definitions=None,
scorer=scorer or score,
description=None,
check_label_consistency=check_label_consistency,
)


Expand Down Expand Up @@ -111,4 +116,66 @@ def make_ner_task_v2(
case_sensitive_matching=case_sensitive_matching,
single_match=single_match,
scorer=scorer or score,
description=None,
check_label_consistency=check_label_consistency,
)


@registry.llm_tasks("spacy.NER.v3")
def make_ner_task_v3(
parse_responses: Optional[TaskResponseParser[SpanTask]] = None,
prompt_example_type: Optional[Type[FewshotExample]] = None,
labels: Union[List[str], str] = [],
template: str = DEFAULT_NER_TEMPLATE_V3,
label_definitions: Optional[Dict[str, str]] = None,
examples: ExamplesConfigType = None,
normalizer: Optional[Callable[[str], str]] = None,
alignment_mode: Literal["strict", "contract", "expand"] = "contract",
case_sensitive_matching: bool = False,
scorer: Optional[Scorer] = None,
description: Optional[str] = None,
):
"""NER.v3 task factory, with chain-of-thought prompting.

parse_responses (Optional[TaskResponseParser[SpanTask]]): Callable for parsing LLM responses for this task.
prompt_example_type (Optional[Type[FewshotExample]]): Type to use for fewshot examples.
labels (Union[str, List[str]]): List of labels to pass to the template,
either an actual list or a comma-separated string.
Leave empty to populate it at initialization time (only if examples are provided).
template (str): Prompt template passed to the model.
description (str): todo
rmitsch marked this conversation as resolved.
Show resolved Hide resolved
label_definitions (Optional[Dict[str, str]]): Map of label -> description
of the label to help the language model output the entities wanted.
It is usually easier to provide these definitions rather than
full examples, although both can be provided.
examples (Optional[Callable[[], Iterable[Any]]]): Optional callable that reads a file containing task examples for
few-shot learning. If None is passed, then zero-shot learning will be used.
normalizer (Optional[Callable[[str], str]]): optional normalizer function.
alignment_mode (str): "strict", "contract" or "expand".
case_sensitive_matching (bool): Whether to search without case sensitivity.
single_match (bool): If False, allow one substring to match multiple times in
the text. If True, returns the first hit.
rmitsch marked this conversation as resolved.
Show resolved Hide resolved
scorer (Optional[Scorer]): Scorer function.
"""
labels_list = split_labels(labels)
raw_examples = examples() if callable(examples) else examples
example_type = prompt_example_type or NERCoTExample
span_examples = (
[example_type(**eg) for eg in raw_examples] if raw_examples else None
)

return NERTask(
parse_responses=parse_responses or parse_span_responses_cot,
prompt_example_type=example_type,
labels=labels_list,
template=template,
label_definitions=label_definitions,
prompt_examples=span_examples,
normalizer=normalizer,
alignment_mode=alignment_mode,
case_sensitive_matching=case_sensitive_matching,
single_match=False,
scorer=scorer or score,
description=description,
check_label_consistency=check_label_consistency_cot,
)
17 changes: 13 additions & 4 deletions spacy_llm/tasks/ner/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from spacy.util import filter_spans

from ...compat import Literal, Self
from ...ty import Scorer, TaskResponseParser
from ..span import SpanExample, SpanTask
from ...ty import FewshotExample, Scorer, TaskResponseParser
from ..span import SpanTask
from ..span.task import SpanTaskLabelCheck
from ..templates import read_template

DEFAULT_NER_TEMPLATE_V1 = read_template("ner.v1")
DEFAULT_NER_TEMPLATE_V2 = read_template("ner.v2")
DEFAULT_NER_TEMPLATE_V3 = read_template("ner.v3")


class NERTask(SpanTask):
Expand All @@ -20,14 +22,16 @@ def __init__(
labels: List[str],
template: str,
parse_responses: TaskResponseParser[Self],
prompt_example_type: Type[SpanExample],
prompt_example_type: Type[FewshotExample],
label_definitions: Optional[Dict[str, str]],
prompt_examples: Optional[List[SpanExample]],
prompt_examples: Optional[List[FewshotExample]],
normalizer: Optional[Callable[[str], str]],
alignment_mode: Literal["strict", "contract", "expand"],
case_sensitive_matching: bool,
single_match: bool,
scorer: Scorer,
description: Optional[str],
check_label_consistency: SpanTaskLabelCheck[Self],
):
"""Default NER task.

Expand All @@ -47,6 +51,8 @@ def __init__(
single_match (bool): If False, allow one substring to match multiple times in
the text. If True, returns the first hit.
scorer (Scorer): Scorer function.
description (str): todo
rmitsch marked this conversation as resolved.
Show resolved Hide resolved
check_label_consistency (SpanTaskLabelCheck): Callable to check label consistency.
"""
super().__init__(
labels=labels,
Expand All @@ -59,6 +65,9 @@ def __init__(
alignment_mode=alignment_mode,
case_sensitive_matching=case_sensitive_matching,
single_match=single_match,
description=description,
allow_overlap=False,
check_label_consistency=check_label_consistency,
)
self._scorer = scorer

Expand Down
10 changes: 10 additions & 0 deletions spacy_llm/tasks/ner/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ...compat import Self
from ..span import SpanExample
from ..span.examples import SpanCoTExample


class NERExample(SpanExample):
Expand All @@ -18,6 +19,15 @@ def generate(cls, example: Example, **kwargs) -> Self:
return cls(text=example.reference.text, entities=entities)


class NERCoTExample(SpanCoTExample):
@classmethod
def generate(cls, example: Example, **kwargs) -> Self:
return cls(
text=example.reference.text,
spans=SpanCoTExample._extract_span_reasons(example.reference.ents),
)


def score(examples: Iterable[Example], **kwargs) -> Dict[str, Any]:
"""Score NER accuracy in examples.
examples (Iterable[Example]): Examples to score.
Expand Down
10 changes: 4 additions & 6 deletions spacy_llm/tasks/rel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class EntityItem(BaseModel):


class RELExample(FewshotExample):
class Config:
arbitrary_types_allowed = True

text: str
ents: List[EntityItem]
relations: List[RelationItem]

class Config:
arbitrary_types_allowed = True

@classmethod
def generate(cls, example: Example, **kwargs) -> Self:
entities = [
Expand All @@ -48,10 +48,8 @@ def generate(cls, example: Example, **kwargs) -> Self:
for ent in example.reference.ents
]

rel_example = RELExample(
return cls.construct(
text=example.reference.text,
ents=entities,
relations=example.reference._.rel,
)

return rel_example
15 changes: 12 additions & 3 deletions spacy_llm/tasks/span/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from .parser import parse_responses
from .examples import SpanExample, SpanReason
from .parser import parse_responses, parse_responses_cot
from .registry import make_label_check, make_label_check_cot
from .task import SpanTask
from .util import SpanExample

__all__ = ["parse_responses", "SpanExample", "SpanTask"]
__all__ = [
"make_label_check",
"make_label_check_cot",
"parse_responses",
"parse_responses_cot",
"SpanExample",
"SpanReason",
"SpanTask",
]
Loading