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

Conversation

kabirkhan
Copy link
Contributor

@kabirkhan kabirkhan commented Jun 20, 2023

Description

Implements Chain-of-Thought prompting for Span tasks

Based on https://arxiv.org/pdf/2305.15444.pdf

Example Usage:

import spacy

nlp = spacy.blank("en")
nlp.add_pipe(
    "llm",
    name="llm_ner",
    config={
        "task" : {
            "@llm_tasks": "spacy.NER.v3",
            "description": "Entities are the names of people and basketball teams. Names of sports are not entities. Names of soccer, baseball, rubgy, etc teams are not basketball team entities. adjectives are not entities. Pronouns are not persons. Man city is the name of a soccer team so not an entity in this case.",
            "examples": [
                {
                    "text": "The Golden State Warriors beat the LA Lakers by almost 20 points on the back of an amazing performance from Steph Curry.",
                    "entities": [
                         # In the final code these should be able to be provided in the prompt str format
                         # e.g. | Golden State Warriors | BASKETBALL_TEAM | True | is a basketball team in the NBA |
                        {
                            "text": "Golden State Warriors",
                            "label": "BASKETBALL_TEAM",
                            "is_entity": True,
                            "reason": "is a basketball team in the NBA"
                        },
                        {
                            "text": "LA Lakers",
                            "label": "BASKETBALL_TEAM",
                            "is_entity": True,
                            "reason": "is a basketball team in the NBA"
                        },
                        {
                            "text": "20",
                            "label": "==NONE==",
                            "is_entity": False,
                            "reason": "is a number"
                        },
                        {
                            "text": "amazing performance",
                            "label": "==NONE==",
                            "is_entity": False,
                            "reason": "is a description of an event"
                        },
                        {
                            "text": "Steph Curry",
                            "label": "PERSON",
                            "is_entity": True,
                            "reason": "is the name of a person"
                        },
                    ]
                }
            ],
            "labels": "PERSON,BASKETBALL_TEAM",
        },
        "model": {
            "@llm_models": "spacy.GPT-3-5.v1",
            "config": {"max_tokens": 200},
        },
        "save_io": True
    }
)

doc = nlp("The Denver Nuggets beat the Miami Heat 4 games to 1")
print([(ent.text, ent.label_) for ent in doc.ents])

# The names of these sports teams are specifically changed to view the model's reasoning behind labeling them or not.
doc2 = nlp("In other sports news Atletico Madrid was able to tie the Gonzaga bulldogs away from home with a stunning strike from Kevin De Bruyne.")
print([(ent.text, ent.label_) for ent in doc2.ents])

Types of change

Feature

Checklist

  • I confirm that I have the right to submit this contribution under the project's MIT license.
  • I ran all tests in tests and usage_examples/tests, and all new and existing tests passed. This includes
    • all external tests (i. e. pytest ran with --external)
    • all tests requiring a GPU
  • My changes don't require a change to the documentation, or if they do, I've added all required information.

@svlandeg svlandeg added the feat/new New feature label Jun 21, 2023
@kabirkhan kabirkhan changed the base branch from main to develop July 5, 2023 23:07
@vinbo8 vinbo8 marked this pull request as ready for review July 6, 2023 11:09
Copy link
Collaborator

@rmitsch rmitsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're getting there 🙂 The templates for the other span-based tasks need to be updated still.

spacy_llm/tasks/span.py Outdated Show resolved Hide resolved
spacy_llm/tasks/util/examples.py Outdated Show resolved Hide resolved
spacy_llm/tasks/templates/ner.v3.jinja Outdated Show resolved Hide resolved
spacy_llm/tasks/util/examples.py Outdated Show resolved Hide resolved
spacy_llm/tasks/ner.py Outdated Show resolved Hide resolved
spacy_llm/ty.py Outdated Show resolved Hide resolved
spacy_llm/tasks/ner.py Outdated Show resolved Hide resolved
spacy_llm/tasks/span.py Outdated Show resolved Hide resolved
spacy_llm/tasks/span.py Outdated Show resolved Hide resolved
spacy_llm/tasks/templates/ner.v3.jinja Show resolved Hide resolved
@kabirkhan kabirkhan changed the title [wip] idea for Chain of Thought prompting for span tasks PromptNER based Chain-of-Thought prompting for span tasks Jul 21, 2023
rmitsch and others added 6 commits August 21, 2023 14:32
* use a default example for zero-shot NER/spancat COT tasks if none are provided

* add test for no examples spancat COT

* Fix tests.

* Update filterwarnings.

* Update RELExample factory.

* Comment validator.

* Attempt to fix pydantic macOS error.

* Attempt to fix pydantic macOS error.

* Update filterwarnings.

---------

Co-authored-by: Raphael Mitsch <r.mitsch@outlook.com>
@rmitsch
Copy link
Collaborator

rmitsch commented Aug 24, 2023

I think this is ready for a final once-over and running the external tests in the CI.

Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still two unresolved conversations, can you have a look at those?

I'd also like us to run a few more tests on how v3 can be used as a substitute for v2, and which changes (if any) would be needed in e.g. the Dolly NER example.

Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really good, nice team effort all! 🙏

Had a few nits + one more important question about a phrasing in the NER template.

.github/workflows/test.yml Outdated Show resolved Hide resolved
spacy_llm/tasks/ner/registry.py Outdated Show resolved Hide resolved
spacy_llm/tasks/ner/registry.py Outdated Show resolved Hide resolved
spacy_llm/tasks/ner/task.py Outdated Show resolved Hide resolved
spacy_llm/tasks/spancat/registry.py Outdated Show resolved Hide resolved
spacy_llm/tasks/spancat/registry.py Outdated Show resolved Hide resolved
spacy_llm/tasks/templates/ner.v3.jinja Outdated Show resolved Hide resolved
spacy_llm/tasks/templates/spancat.v3.jinja Outdated Show resolved Hide resolved
spacy_llm/tests/tasks/test_ner.py Outdated Show resolved Hide resolved
spacy_llm/tests/tasks/test_ner.py Outdated Show resolved Hide resolved
rmitsch and others added 11 commits August 25, 2023 08:33
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
# Conflicts:
#	usage_examples/tests/test_usage_examples.py
@rmitsch rmitsch merged commit 8e2fd0f into develop Aug 25, 2023
11 checks passed
@svlandeg svlandeg deleted the kab/cot-ner branch August 25, 2023 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat/new New feature feat/task Feature: tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants