Code Monkey home page Code Monkey logo

explosion / spacy-llm Goto Github PK

View Code? Open in Web Editor NEW
935.0 15.0 71.0 1.93 MB

๐Ÿฆ™ Integrating LLMs into structured NLP pipelines

Home Page: https://spacy.io/usage/large-language-models

License: MIT License

Python 96.67% Shell 0.05% Jinja 3.28%
large-language-models llm openai spacy dolly gpt-3 gpt-4 machine-learning named-entity-recognition natural-language-processing nlp text-classification prompt-engineering anthropic claude cohere falcon llama

spacy-llm's Introduction

spacy-llm
Structured NLP with LLMs



GitHub Workflow Status pypi Version Code style: black

This package integrates Large Language Models (LLMs) into spaCy, featuring a modular system for fast prototyping and prompting, and turning unstructured responses into robust outputs for various NLP tasks, no training data required.

Feature Highlight

๐Ÿง  Motivation

Large Language Models (LLMs) feature powerful natural language understanding capabilities. With only a few (and sometimes no) examples, an LLM can be prompted to perform custom NLP tasks such as text categorization, named entity recognition, coreference resolution, information extraction and more.

spaCy is a well-established library for building systems that need to work with language in various ways. spaCy's built-in components are generally powered by supervised learning or rule-based approaches.

Supervised learning is much worse than LLM prompting for prototyping, but for many tasks it's much better for production. A transformer model that runs comfortably on a single GPU is extremely powerful, and it's likely to be a better choice for any task for which you have a well-defined output. You train the model with anything from a few hundred to a few thousand labelled examples, and it will learn to do exactly that. Efficiency, reliability and control are all better with supervised learning, and accuracy will generally be higher than LLM prompting as well.

spacy-llm lets you have the best of both worlds. You can quickly initialize a pipeline with components powered by LLM prompts, and freely mix in components powered by other approaches. As your project progresses, you can look at replacing some or all of the LLM-powered components as you require.

Of course, there can be components in your system for which the power of an LLM is fully justified. If you want a system that can synthesize information from multiple documents in subtle ways and generate a nuanced summary for you, bigger is better. However, even if your production system needs an LLM for some of the task, that doesn't mean you need an LLM for all of it. Maybe you want to use a cheap text classification model to help you find the texts to summarize, or maybe you want to add a rule-based system to sanity check the output of the summary. These before-and-after tasks are much easier with a mature and well-thought-out library, which is exactly what spaCy provides.

โณ Install

spacy-llm will be installed automatically in future spaCy versions. For now, you can run the following in the same virtual environment where you already have spacy installed.

python -m pip install spacy-llm

โš ๏ธ This package is still experimental and it is possible that changes made to the interface will be breaking in minor version updates.

๐Ÿ Quickstart

Let's run some text classification using a GPT model from 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 documentation around setting API keys.

In Python code

To do some quick experiments, from 0.5.0 onwards you can run:

import spacy

nlp = spacy.blank("en")
llm = nlp.add_pipe("llm_textcat")
llm.add_label("INSULT")
llm.add_label("COMPLIMENT")
doc = nlp("You look gorgeous!")
print(doc.cats)
# {"COMPLIMENT": 1.0, "INSULT": 0.0}

By using the llm_textcat factory, the latest version of the built-in textcat task is used, as well as the default GPT-3-5 model from OpenAI.

Using a config file

To control the various parameters of the llm pipeline, we can use spaCy's config system. To start, create a config file config.cfg containing at least the following (or see the full example here):

[nlp]
lang = "en"
pipeline = ["llm"]

[components]

[components.llm]
factory = "llm"

[components.llm.task]
@llm_tasks = "spacy.TextCat.v3"
labels = ["COMPLIMENT", "INSULT"]

[components.llm.model]
@llm_models = "spacy.OpenAI.v1"
name = "gpt-4"

Now run:

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 use those, check out https://spacy.io/api/large-language-models.

๐Ÿš€ Ongoing work

In the near future, we will

  • Add more example tasks
  • Support a broader range of models
  • Provide more example use-cases and tutorials

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 the discussion board. Bug reports can be filed on the spaCy issue tracker. Thank you!

Migration guides

Please refer to our migration guide.

spacy-llm's People

Contributors

adrianeboyd avatar bdura avatar habibhaidari1 avatar honnibal avatar ines avatar kabirkhan avatar kadarakos avatar kennethenevoldsen avatar koaning avatar ljvmiranda921 avatar magdaaniol avatar rmitsch avatar shademe avatar svlandeg avatar victorialslocum avatar vinbo8 avatar viveksilimkhan1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spacy-llm's Issues

ner_v3_openai not run

!python run_pipeline.py "ๅ‰็™ฝ่›‹็™ฝ๏ผˆPA๏ผ‰ 302.65 mg/L 180-400 ๅ‚่€ƒๅ€ผ 2 ๏ผŠๆ€ป่›‹็™ฝ๏ผˆTP๏ผ‰ 69.9 19๏ผŠๅฐฟ้…ธ๏ผˆURIC๏ผ‰ 542.06 umol/L 1 148.8-416.5 g/L 65-85 20 " ./fewshot.cfg ./examples.json

error:Traceback (most recent call last):

File "D:\anaconda\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
six.raise_from(e, None)

File "", line 3, in raise_from

File "D:\anaconda\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
httplib_response = conn.getresponse()

File "D:\anaconda\lib\http\client.py", line 1374, in getresponse
response.begin()

File "D:\anaconda\lib\http\client.py", line 318, in begin
version, status, reason = self._read_status()

File "D:\anaconda\lib\http\client.py", line 279, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")

File "D:\anaconda\lib\socket.py", line 705, in readinto
return self._sock.recv_into(b)

File "D:\anaconda\lib\ssl.py", line 1274, in recv_into
return self.read(nbytes, buffer)

File "D:\anaconda\lib\ssl.py", line 1130, in read
return self._sslobj.read(len, buffer)

TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "D:\anaconda\lib\site-packages\requests\adapters.py", line 489, in send
resp = conn.urlopen(

File "D:\anaconda\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
retries = retries.increment(

File "D:\anaconda\lib\site-packages\urllib3\util\retry.py", line 550, in increment
raise six.reraise(type(error), error, _stacktrace)

File "D:\anaconda\lib\site-packages\urllib3\packages\six.py", line 770, in reraise
raise value

File "D:\anaconda\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(

File "D:\anaconda\lib\site-packages\urllib3\connectionpool.py", line 451, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)

File "D:\anaconda\lib\site-packages\urllib3\connectionpool.py", line 340, in _raise_timeout
raise ReadTimeoutError(

urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.openai.com', port=443): Read timed out. (read timeout=30)

add a NER feature

Can we add a feature if I don't know the tags I extracted beforehand and rely on the model to understand and structure them.

AttributeError: Could not find: llms

I'm running into the following error

     98 if not has_langchain or any(
     99     [
    100         (handle.startswith("langchain"))
    101         for handle in registry.llm_models.get_all()
    102     ]
    103 ):
    104     return
--> 106 for class_id, cls in LangChain.get_type_to_cls_dict().items():
    107     registry.llm_models.register(
    108         f"langchain.{cls.__name__}.v1",
    109         func=LangChain._langchain_model_maker(class_id=class_id),
    110     )

File ~/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy_llm/models/langchain/model.py:38, in LangChain.get_type_to_cls_dict()
     33 @staticmethod
     34 def get_type_to_cls_dict() -> Dict[str, Type["langchain.llms.base.BaseLLM"]]:
     35     """Returns langchain.llms.type_to_cls_dict.
     36     RETURNS (Dict[str, Type[langchain.llms.base.BaseLLM]]): langchain.llms.type_to_cls_dict.
     37     """
---> 38     return langchain.llms.type_to_cls_dict

File ~/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/__init__.py:308, in __getattr__(name)
    306     return SerpAPIWrapper
    307 else:
--> 308     raise AttributeError(f"Could not find: {name}")

AttributeError: Could not find: llms

whenever I try to use the following langchain config

[nlp]
lang = "en"
pipeline = ["llm"]

[components]

[components.llm]
factory = "llm"

[components.llm.task]
@llm_tasks = "spacy.TextCat.v1"
labels = ["COMPLIMENT", "INSULT"]

[components.llm.model]
@llm_models = "langchain.AzureOpenAI.v1"
name = "gpt-4"

Bug: Custom Endpoint Validation Breaks Feature Support

In the v0.7.0 release, the following feature was added

Allow custom endpoints for all models to support usage of fine-tuned models (#390)

I am not sure what the exact specification for this feature was, and I filed an issue related to this a few months back, but I am not sure this feature works as expected. For example, the OpenAI REST model still contains this line which causes an error to be thrown if you pass an endpoint that is not in the Endpoints enum. For example, if your config looks something like

[components.llm_rel.model]
@llm_models = "spacy.GPT-4.v3"
endpoint = YOUR_CUSTOM_ENDPOINT
config = {{"temperature": 0.0}}
"""

Am I missing something about how this feature is intended to be used? Thanks! And thanks for the prompt follow-up from my previous issue!

langchain dependency problem

spacy-llm's langchain interface is incompatible with langchain>=0.0.302 because in 302 they stopped exporting modules, so the .llms property that is used in this code no longer exists. This renders spacy-llm unloadable because the registry model initialization calls the Langchain model and the Langchain model's __init__() calls this function.

Stack trace with 0.0.326 looks like this:

Traceback (most recent call last):
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/my_spacy_llm_extension/tasks.py", line 6, in <module>
    from spacy_llm.registry import registry
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/spacy_llm/__init__.py", line 1, in <module>
    from . import cache  # noqa: F401
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/spacy_llm/cache.py", line 11, in <module>
    from .ty import LLMTask, PromptTemplateProvider
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/spacy_llm/ty.py", line 14, in <module>
    from .models import langchain
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/spacy_llm/models/__init__.py", line 2, in <module>
    from .langchain import query_langchain
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/spacy_llm/models/langchain/__init__.py", line 4, in <module>
    LangChain.register_models()
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/spacy_llm/models/langchain/model.py", line 106, in register_models
    for class_id, cls in LangChain.get_type_to_cls_dict().items():
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/spacy_llm/models/langchain/model.py", line 38, in get_type_to_cls_dict
    return langchain.llms.type_to_cls_dict
  File "/opt/dagster/dagster_home/.venv/lib/python3.10/site-packages/langchain/__init__.py", line 368, in __getattr__
    raise AttributeError(f"Could not find: {name}")
AttributeError: Could not find: llms

I verified that 0.0.331 gives the same error, but somehow that's listed in the dev requirements.txt.

running into issues using the langchain on text-davinci-003 model

I'm trying to use the text-davinci-003 model using the following config.cfg

[nlp]
lang = "en"
pipeline = ["llm"]

[components]

[components.llm]
factory = "llm"

[components.llm.task]
@llm_tasks = "spacy.TextCat.v3"
labels = ["COMPLIMENT", "INSULT"]

[components.llm.model]
@llm_models = "langchain.AzureOpenAI.v1"
name = "text-davinci-003"

but I'm running into

  File "/Users/mariamoya/mlplatform/lib/document_classifier/src/document_classifier/test-spacy-llm.py", line 5, in <module>
    doc = nlp("You look gorgeous!")
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy/language.py", line 1047, in __call__
    error_handler(name, proc, [doc], e)
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy/util.py", line 1724, in raise_error
    raise e
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy/language.py", line 1042, in __call__
    doc = proc(doc, **component_cfg.get(name, {}))  # type: ignore[call-arg]
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy_llm/pipeline/llm.py", line 156, in __call__
    docs = self._process_docs([doc])
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy_llm/pipeline/llm.py", line 210, in _process_docs
    responses_iters = tee(self._model(prompts_iters[0]), n_iters)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy_llm/models/langchain/model.py", line 45, in __call__
    return self.query(self._langchain_model, prompts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy_llm/models/langchain/model.py", line 56, in query_langchain
    return [model(pr) for pr in prompts]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy_llm/models/langchain/model.py", line 56, in <listcomp>
    return [model(pr) for pr in prompts]
            ^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/llms/base.py", line 786, in __call__
    self.generate(
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/llms/base.py", line 582, in generate
    output = self._generate_helper(
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/llms/base.py", line 488, in _generate_helper
    raise e
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/llms/base.py", line 475, in _generate_helper
    self._generate(
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/llms/openai.py", line 399, in _generate
    response = completion_with_retry(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/llms/openai.py", line 115, in completion_with_retry
    return _completion_with_retry(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/tenacity/__init__.py", line 289, in wrapped_f
    return self(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/tenacity/__init__.py", line 379, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/tenacity/__init__.py", line 314, in iter
    return fut.result()
           ^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/lib/python3.11/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/tenacity/__init__.py", line 382, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/llms/openai.py", line 113, in _completion_with_retry
    return llm.client.create(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/openai/api_resources/completion.py", line 25, in create
    return super().create(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 155, in create
    response, _, api_key = requestor.request(
                           ^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/openai/api_requestor.py", line 299, in request
    resp, got_stream = self._interpret_response(result, stream)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/openai/api_requestor.py", line 710, in _interpret_response
    self._interpret_response_line(
  File "/Users/mariamoya/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/openai/api_requestor.py", line 775, in _interpret_response_line
    raise self.handle_error_response(
openai.error.InvalidRequestError: Resource not found

was wondering if anyone would happen to know what I might be missing?

Langchain 0.0.302 breaks langchain integration

See the stacktrace below

File ~/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy_llm/models/langchain/model.py:106, in LangChain.register_models()
     98 if not has_langchain or any(
     99     [
    100         (handle.startswith("langchain"))
    101         for handle in registry.llm_models.get_all()
    102     ]
    103 ):
    104     return
--> 106 for class_id, cls in LangChain.get_type_to_cls_dict().items():
    107     registry.llm_models.register(
    108         f"langchain.{cls.__name__}.v1",
    109         func=LangChain._langchain_model_maker(class_id=class_id),
    110     )

File ~/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/spacy_llm/models/langchain/model.py:38, in LangChain.get_type_to_cls_dict()
     33 @staticmethod
     34 def get_type_to_cls_dict() -> Dict[str, Type["langchain.llms.base.BaseLLM"]]:
     35     """Returns langchain.llms.type_to_cls_dict.
     36     RETURNS (Dict[str, Type[langchain.llms.base.BaseLLM]]): langchain.llms.type_to_cls_dict.
     37     """
---> 38     return langchain.llms.type_to_cls_dict

File ~/.pyenv/versions/3.11.3/envs/document_classifier/lib/python3.11/site-packages/langchain/__init__.py:308, in __getattr__(name)
    306     return SerpAPIWrapper
    307 else:
--> 308     raise AttributeError(f"Could not find: {name}")

AttributeError: Could not find: llms

spacy_llm/models/langchain/model.py:38, imports langchain.llms.type_to_cls_dict but as of yesterday's Langchain release - https://github.com/langchain-ai/langchain/pull/11017/files they make this import raise an Attribute error that isn't present in their init.py`

They warn:

    warnings.warn(
        f"Importing {name} from langchain root module is no longer supported."
    )

How do I use the summary module?

I'm trying to use the summary pipe, but I haven't found any references for how to use it. Does anyone know?

nlp = spacy.blank("pt")
llm = nlp.add_pipe("llm_summarization")
nlp("""The United Nations, referred to informally as the UN, is an
intergovernmental organization whose stated purposes are to maintain
international peace and security, develop friendly relations among nations,
achieve international cooperation, and serve as a centre for harmonizing the
actions of nations. It is the world's largest international organization.
The UN is headquartered on international territory in New York City, and the
organization has other offices in Geneva, Nairobi, Vienna, and The Hague,
where the International Court of Justice is headquartered.\n\n The UN was
established after World War II with the aim of preventing future world wars,
and succeeded the League of Nations, which was characterized as
ineffective.""")

output: same text (without resume)

Few-shot limiations problem (textcat_openai)

Hi,
according to example with https://github.com/explosion/spacy-llm/tree/main/usage_examples/textcat_openai I tried to add more data in case of few-shot training. Unfortunately, according to openai token limitations, My request cannot be accepted.

used model: Ada (of course I know that there are better options, but none of them has a super-large token capacity)

**ERROR:

ValueError: Request to OpenAI API failed: This model's maximum context length is 2049 tokens, however you requested 83753 tokens (83737 in your prompt; 16 for the completion). Please reduce your prompt; or completion length.**

I crossed the whole internet and I still haven't received any solution. Every
similiar problem is described accordingly to the summarization task.
My question is: is there a solution for chunking JSON files which provide the few-shot learning
available for the code from this repository? Ore there exists any different correct idea?

I installed spaCy correctly and there are no problems with
my openai api key.

I will be thankful for any help!

Anthropic error: "anthropic-version header is required"

Whatever I do, I can't use Anthropic's claude with spacy-llm. I tried both spacy.Claude-instant-1.v1 and spacy.Claude-2.v1.

I am getting the following error during nlp = assemble(config_file_path):

...
File ~/miniconda3/envs/_my_env_/lib/python3.9/site-packages/requests/models.py:1021, in Response.raise_for_status(self)
   1020 if http_error_msg:
-> 1021     raise HTTPError(http_error_msg, response=self)

HTTPError: 400 Client Error: Bad Request for url: https://api.anthropic.com/v1/complete
...
...

File ~/miniconda3/envs/_my_env_/lib/python3.9/site-packages/spacy_llm/models/rest/anthropic/model.py:80, in Anthropic.__call__.<locals>._request(json_data)
     78     if error["type"] == "not_found_error":
     79         error_msg += f". Ensure that the selected model ({self._name}) is supported by the API."
---> 80     raise ValueError(error_msg) from ex
     81 response = r.json()
     83 # c.f. https://console.anthropic.com/docs/api/errors

ValueError: Request to Anthropic API failed: {'type': 'invalid_request_error', 'message': 'anthropic-version header is required'}

Here is my config file:

[nlp]
lang = "en"
pipeline = ["llm"]

[components]

[components.llm]
factory = "llm"

[components.llm.model]
@llm_models = "spacy.Claude-instant-1.v1"
name = "claude-instant-1"
max_request_time = 5000
max_tries = 10
interval = 3
# config = {"temperature": 0.0}

[components.llm.task]
@llm_tasks = "spacy.TextCat.v3"
labels = ["MY_LABEL"]
exclusive_classes = True

Supports maximum text length

Regarding spacy_llm, what is the maximum text length it supports? If the text length in a single instance is very long, exceeding the limit of OpenAI, how does it handle this?

LLM assemble does not read .yaml example files

Using spacy-llm, I am attempting to inject a yaml file with examples of an NER parsing task into the PalmAI API model.

How to reproduce the behaviour

from spacy_llm.util import assemble
model = assemble('config.cfg')

with the following configuration in config.cfg:

[components.llm.task.examples]
@misc = "spacy.FewShotReader.v1"
path ="<project_path>/examples.yaml"

(where the path does exist and examples.yaml does contain data)

I get
ValueError: The examples file expects a .yml, .yaml, .json, or .jsonl file type. Ensure that your file corresponds to one of these file formats.

The issue is sourced in reader.py of the package (lines 54-71), which recognizes the file's suffix as a .yaml and loads the yaml reader but breaks in a frame I cannot see.

from reader.py:

       suffix = eg_path.suffix.replace("yaml", "yml")
        readers = {
            ".yml": srsly.read_yaml,
            ".json": srsly.read_json,
            ".jsonl": lambda path: list(srsly.read_jsonl(eg_path)),
        }

        # Try to read in indicated format.
        success = False
        if suffix in readers:
            try:
                data = readers[suffix](eg_path)
                success = True
            except Exception:
                pass
        if not success:
            # Try to read file in all supported formats.
            for file_format, reader in readers.items():
                if file_format == suffix:
                    continue
                try:
                    data = reader(eg_path)
                    success = True
                    break
                except Exception:
                    pass

I attempted using .yml and .yaml and both failed.

Your Environment

  • spaCy version: 3.7.2
  • Platform: macOS-14.1.1-arm64-arm-64bit
  • Python version: 3.11.6
    Environment Information:
  • Anaconda3 distribution for arm64.

How would I extract a value for an entity

Guys I have an enourmous amount of medical reports to perfrom NER on. But I need not only to extract the entities but also values for those entities e.g if I need the Gleason score. The orginal text has "Gleason grade 4+3" but I need to give an attribute to the entity of 4+3. I am also creating knowledge graphs with neo4j and I would like to have the 4+3 as an attribute in the Gleason grade node.
How would you recommend that I do that? I guess I will need to modify the example parser?

langchain integration not compatible with GPT 3.5 or GPT 4

Hello, I'm trying to use Azure's OpenAI service with spacy-llm via langchain given that the Azure integration is not currently supported

explosion/spaCy#12653

However, I noticed that the langchain model is using prompting structure that's consistent with openAI GPT-3 and prior rather than GPT-3.5 or GPT-4 which uses langchain.chat_models
https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/chat_models/base.py#L82

rather than langchain.llms
https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/llms/base.py#L158

In particular, this line seems to be referencing langchain.llms

["langchain.llms.base.BaseLLM", Iterable[Any]],

as a result, I'm getting the following error when I try considering a pinned updated version of langchain and GPT-4 or GPT-3.5

openai.error.InvalidRequestError: Must provide an 'engine' or 'deployment_id' parameter to create a <class 'openai.api_resources.chat_completion.ChatCompletion'>

DollyV2 not utilizing GPU

Hi,
Really excited about using this awesome technology you're all building. I am running some of the example code, specifically the one you put with using DollyV2 for NER described here https://github.com/explosion/spacy-llm/tree/main/usage_examples/ner_dolly .
Everything runs fine and as expected but when I monitor my GPU utilization, it is clear that it is all running on CPU.
I have checked to make sure that torch.cuda.is_available() is true and that my 2 devices are listed but it is still not working. Any idea how to force it to use GPU?
Thanks,
Michael

`RELTask` generates prompts that use ints instead of entity labels

The relation task tells the LLM:

The text below contains pre-extracted entities, denoted in the following format within the text:
<entity text>[ENT<entity id>:<entity label>]

However, it generates prompt text like:

well[ENT0:14862748245026736845] hello[ENT1:14230521632333904559] there[ENT2:149303876845869574]!

Reproduce via:

import spacy_llm
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp("well hello there!")
doc.set_ents([
    spacy.tokens.Span(doc,0,1,"A"),
    spacy.tokens.Span(doc,1,2,"B"),
    spacy.tokens.Span(doc,2,3,"C")]
)
tsk = spacy_llm.tasks.make_rel_task(labels = ["A","B","C"])
for prompt in tsk.generate_prompts([doc]):
    print(prompt)

The problem is a single character difference here:

annotation = f"[ENT{i}:{ent.label}]"

It should be label_ instead of label. I'd open the PR, but it feels weird to have label be the non-string version of the entity so maybe the real problem is upstream.

print(doc.ents[0].label)
print(doc.ents[0].label_)

14862748245026736845
A

ModuleNotFoundError: No module named 'spacy.language'; 'spacy' is not a package

Traceback (most recent call last):
File "/home/ec2-user/spacy/spacy.py", line 8, in
from spacy_llm.util import assemble
File "/home/ec2-user/.conda/envs/naitik2/lib/python3.10/site-packages/spacy_llm/init.py", line 1, in
from . import cache # noqa: F401
File "/home/ec2-user/.conda/envs/naitik2/lib/python3.10/site-packages/spacy_llm/cache.py", line 7, in
from spacy.tokens import Doc, DocBin
File "/home/ec2-user/spacy/spacy.py", line 8, in
from spacy_llm.util import assemble
File "/home/ec2-user/.conda/envs/naitik2/lib/python3.10/site-packages/spacy_llm/util.py", line 5, in
from spacy.language import Language
ModuleNotFoundError: No module named 'spacy.language'; 'spacy' is not a package

What could be the issue? I am able to use it on Google colab

429 Too Many Requests

I am getting a 429 error from the OpenAI API when iterating over a model.pipe result.
I am using @llm_tasks = "spacy.NER.v2" and

[components.llm.model]
@llm_models = "spacy.GPT-3-5.v1"
name = "gpt-3.5-turbo"
config = {"temperature": 0.3}

This is the traceback:

  File "/workspaces/project/llm.py", line 16, in <genexpr>
    model_result_fin = (convert_llm_entities(doc) for doc in model_result_fin)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/spacy/language.py", line 1611, in pipe
    for doc in docs:
  File "/usr/local/lib/python3.11/site-packages/spacy/util.py", line 1705, in _pipe
    yield from proc.pipe(docs, **kwargs)
  File "spacy/pipeline/pipe.pyx", line 55, in pipe
  File "/usr/local/lib/python3.11/site-packages/spacy/util.py", line 1705, in _pipe
    yield from proc.pipe(docs, **kwargs)
  File "/usr/local/lib/python3.11/site-packages/spacy_llm/pipeline/llm.py", line 175, in pipe
    error_handler(self._name, self, doc_batch, e)
  File "/usr/local/lib/python3.11/site-packages/spacy/util.py", line 1724, in raise_error
    raise e
  File "/usr/local/lib/python3.11/site-packages/spacy_llm/pipeline/llm.py", line 173, in pipe
    yield from iter(self._process_docs(doc_batch))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/spacy_llm/pipeline/llm.py", line 199, in _process_docs
    responses_iters = tee(self._model(prompts_iters[0]), n_iters)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/spacy_llm/models/rest/openai/model.py", line 115, in __call__
    responses = _request(
                ^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/spacy_llm/models/rest/openai/model.py", line 86, in _request
    r = self.retry(
        ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/spacy_llm/models/rest/base.py", line 147, in retry
    raise ConnectionError(
ConnectionError: API could not be reached after 32.592 seconds in total and attempting to connect 5 times. Check your network connection and the API's availability.
429     Too Many Requests

Could you please help with this?
Is there some kind of throttling mechanism that could resolve this?

Active learning NER

For example, I want to take a space_ Llm, perform NER auxiliary annotation. With my corrections and updates, how can I let the model know more information and actively learn

Anthropic error : API failed prompt must start with "\n\nHuman:" turn

Hello,
I'm trying to use spacy-llm for ner task using different models.

When I want to use Claude-2-v1 or Claude-1-v1, I get the same error :
Traceback (most recent call last):
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\models\rest\anthropic\model.py", line 73, in _request
r.raise_for_status()
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\requests\models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.anthropic.com/v1/complete

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\alexis\Desktop\git-repo\ner-benchmark\spacy-llm-test.py", line 23, in
nlp = assemble("fewshot.cfg")
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\util.py", line 48, in assemble
return assemble_from_config(config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\util.py", line 28, in assemble_from_config
nlp = load_model_from_config(config, auto_fill=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy\util.py", line 587, in load_model_from_config
nlp = lang_cls.from_config(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy\language.py", line 1864, in from_config
nlp.add_pipe(
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy\language.py", line 821, in add_pipe
pipe_component = self.create_pipe(
^^^^^^^^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy\language.py", line 709, in create_pipe
resolved = registry.resolve(cfg, validate=validate)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\confection_init_.py", line 756, in resolve
resolved, _ = cls.make(
^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\confection_init
.py", line 805, in _make
filled, _, resolved = cls.fill(
^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\confection_init
.py", line 860, in _fill
filled[key], validation[v_key], final[key] = cls.fill(
^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\confection_init
.py", line 877, in _fill
getter_result = getter(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\models\rest\anthropic\registry.py", line 33, in anthropic_claude_2
return Anthropic(
^^^^^^^^^^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\models\rest\base.py", line 64, in init
self._verify_auth()
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\models\rest\anthropic\model.py", line 51, in _verify_auth
raise err
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\models\rest\anthropic\model.py", line 43, in _verify_auth
self(["test"])
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\models\rest\anthropic\model.py", line 97, in call
responses = [
^
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\models\rest\anthropic\model.py", line 98, in
_request({"prompt": f"{SystemPrompt.HUMAN} {prompt}{SystemPrompt.ASST}"})
File "C:\Users\alexis\miniconda3\envs\benchmark\Lib\site-packages\spacy_llm\models\rest\anthropic\model.py", line 81, in _request
raise ValueError(error_msg) from ex
ValueError: Request to Anthropic API failed: {'type': 'invalid_request_error', 'message': 'prompt must start with "\n\nHuman:" turn'}

I am using Spacy-LLM version 0.6.4 and python 3.11.5.

My code is :

from spacy_llm.util import assemble

# load the model settings from the config file
nlp = assemble("fewshot.cfg")
doc = nlp("In    most patients with isolated unilateral retinoblastoma , tumor development is initiated by somatic inactivation of both alleles of the RB1 gene .")

# print the llm output
print([(ent.text, ent.label_) for ent in doc.ents])

My config file is :

[paths]
examples = null

[nlp]
lang = "en"
pipeline = ["llm"]

[components]

[components.llm]
factory = "llm"

[components.llm.task]
@llm_tasks = "spacy.NER.v3"
labels = ["DISEASE", "GENE", "CHEMICAL"]
description = Entities related to biomedical text, 
    specifically focusing on diseases, genes, and chemicals.
    Adjectives, verbs, adverbs are not entities.
    Pronouns are not entities.
    Entities should represent concrete entities within the biomedical domain.

[components.llm.task.label_definitions]
DISEASE = "Known diseases or medical conditions, e.g., diabetes, cancer. Entities should refer to specific diseases rather than general health conditions."
GENE = "Genes or genetic elements mentioned in the text. Entities should represent specific genes or genetic elements, and not general terms related to genetics. For example, 'BRCA1' is an entity, while 'genetic' is not."
CHEMICAL = "Chemicals or substances, e.g., drugs, molecules. Entities should represent specific chemical compounds or substances. General terms related to chemistry, such as 'chemical' or 'substance,' are not entities."

[components.llm.task.examples]
@misc = "spacy.FewShotReader.v1"
path = "examples.json"

[components.llm.model]
@llm_models = "spacy.Claude-2.v1"
config = {"max_tokens_to_sample": 1024}

I tried with the ner v2 with and without examples.
I still get the same error.

My API key is also set up as env variable.

I don't know if I miss something or if it's an error.

Thanks in advence for the help.

ValueError: Request to Azure OpenAI API failed: Resource not found

The error occurs when model_type is chat.

[components.llm.model]
@llm_models = "spacy.Azure.v1"
name = "deployment-name"
base_url = "base-url"
model_type = "chat"
api_version = ""
config = {"temperature": 0.0 }

In the case of model_type = "chat", I think we need to append /completions:

+ f"openai/deployments/{self._name}/{self._model_type.value}"

References

README examples fail to run

Hi,

Both examples in the README file fail to run. I get the following error:

ImportError: cannot import name 'SimpleFrozenDict' from 'confection' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/confection/init.py)

It happens when the following is executed (see the full stack trace below):

from spacy_llm.util import assemble

The code is run from within PyCharm on a MacBook Pro (Intel core i7) under macOS Monterey (Version 12.5.1).

Thanks,
Ron

/Library/Frameworks/Python.framework/Versions/3.10/bin/python /Users/ron.katriel/PycharmProjects/Transformer/spacy-llm-test.py
2023-09-08 10:55:19.576814: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
File "/Users/ron.katriel/PycharmProjects/Transformer/spacy-llm-test.py", line 1, in
from spacy_llm.util import assemble
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/spacy_llm/init.py", line 1, in
from . import cache # noqa: F401
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/spacy_llm/cache.py", line 11, in
from .ty import LLMTask, PromptTemplateProvider
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/spacy_llm/ty.py", line 14, in
from .models import langchain
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/spacy_llm/models/init.py", line 1, in
from .hf import dolly_hf, openllama_hf, stablelm_hf
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/spacy_llm/models/hf/init.py", line 2, in
from .dolly import dolly_hf
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/spacy_llm/models/hf/dolly.py", line 3, in
from confection import SimpleFrozenDict
ImportError: cannot import name 'SimpleFrozenDict' from 'confection' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/confection/init.py)

Feature Request: Support proxy URLs

Problem Summary

There is no easy way to configure a pipeline to hit anything besides the default service provider endpoint for LLM models. For example, the default llm_models in the registry for OpenAI hardcode the endpoints from Endpoints.

Current Solution(s)

You can, in theory, override this by creating a custom model in the registry and replacing endpoint=Endpoints.CHAT.value, when initializing OpenAI. However, since you are not changing the value of the Endpoints enum, you still get errors during validation. To get around this, you can implement a custom model class that extends REST but doesn't do any endpoint validation. However this feels like overkill for what otherwise would be such a simple change.

Although I have not gotten it to work, another approach would be to use the langchain.OpenAI.v1 model and then pass openai_api_base to the config since langchain supports proxies. However, from my testing there isn't stable langchain support for the chat completions API, and this also just feels like overkill.

Proposed Solution

I think the ideal solution would be to essentially support the openai_api_base parameter natively. Something like

[components.llm.model]
@llm_models = "spacy.GPT-4.v1"
name = "gpt-4"
endpoint = "YOUR_PROXY_URL"

The model would still default endpoint=Endpoints.CHAT.value, but would be overridden if the user passed a value. The validation step could stay, but instead of raising an exception it could just log a warning that you are sending requests to a proxy.

Provide NER identification

Indeed, this can solve some NER problems. How to target those parts that cannot be extracted?

  1. Is there a method for fine tuning provided

  2. Is a knowledge base provided

FileNotFoundError: [Errno 2] No such file or directory: 'local-ner-cache/9963044417883968883.spacy'

I changed the spacy.NER.v2 to spacy.NER.v3

ValueError: Prompt template in cache directory (local-ner-cache/prompt_template.txt) is not equal with current prompt template. Reset your cache if you are using a new prompt template.

After deleting the folder local-ner-cache, I encountered the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'local-ner-cache/9963044417883968883.spacy'

What is the right way to "Reset your cache if you are using a new prompt template."?

Because after the deleting the folder local-ner-cache, I'm no longer able to annotate the same dataset:

dotenv run -- prodigy ner.llm.correct
image

There are still around 1k samples to annotate.

I got errors with spacy-llm==0.5.1

Discussed in #303

Originally posted by innocent-charles September 23, 2023
I have upgraded the spacy-llm version to 0.5.1 and used the task of spacy.NER.v3 and model of spacy.GPT-3-5.v2.

But unfortunately I got the following error , what to do ?

File "pydantic/main.py", line 342, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for NERCoTExample
spans
  field required (type=value_error.missing)```</div>

KeyError: 'data' when loading sample config file

I can't seem to load a config file. I have tried some of the examples in the documentation but get the same key error. I assume that I am doing something wrong, but I can't figure out what.

Here is the the full code

import os
import yaml

from spacy_llm.util import assemble

with open("config.yaml", 'r') as f:
    config = yaml.safe_load(f)

os.environ["OPENAI_API_KEY"] = config.get('OPENAI_API_KEY')

nlp = assemble("config.cfg")
doc = nlp("You look gorgeous!")
print(doc.cats)

Here is the config file:

[nlp]
lang = "en"
pipeline = ["llm"]

[components]

[components.llm]
factory = "llm"

[components.llm.task]
@llm_tasks = "spacy.TextCat.v3"
labels = ["COMPLIMENT", "INSULT"]

[components.llm.model]
@llm_models = "spacy.GPT-4.v2"

Full error


KeyError Traceback (most recent call last)
Cell In[4], line 1
----> 1 nlp = assemble("config.cfg")
2 doc = nlp("You look gorgeous!")
3 print(doc.cats)

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy_llm\util.py:48, in assemble(config_path, overrides)
46 config_path = Path(config_path)
47 config = load_config(config_path, overrides=overrides, interpolate=False)
---> 48 return assemble_from_config(config)

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy_llm\util.py:28, in assemble_from_config(config)
22 def assemble_from_config(config: Config) -> Language:
23 """Assemble a spaCy pipeline from a confection Config object.
24
25 config (Config): Config to load spaCy pipeline from.
26 RETURNS (Language): An initialized spaCy pipeline.
27 """
---> 28 nlp = load_model_from_config(config, auto_fill=True)
29 config = config.interpolate()
30 sourced = get_sourced_components(config)

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy\util.py:587, in load_model_from_config(config, meta, vocab, disable, enable, exclude, auto_fill, validate)
584 # This will automatically handle all codes registered via the languages
585 # registry, including custom subclasses provided via entry points
586 lang_cls = get_lang_class(nlp_config["lang"])
--> 587 nlp = lang_cls.from_config(
588 config,
589 vocab=vocab,
590 disable=disable,
591 enable=enable,
592 exclude=exclude,
593 auto_fill=auto_fill,
594 validate=validate,
595 meta=meta,
596 )
597 return nlp

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy\language.py:1847, in Language.from_config(cls, config, vocab, disable, enable, exclude, meta, auto_fill, validate)
1844 factory = pipe_cfg.pop("factory")
1845 # The pipe name (key in the config) here is the unique name
1846 # of the component, not necessarily the factory
-> 1847 nlp.add_pipe(
1848 factory,
1849 name=pipe_name,
1850 config=pipe_cfg,
1851 validate=validate,
1852 raw_config=raw_config,
1853 )
1854 else:
1855 assert "source" in pipe_cfg

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy\language.py:814, in Language.add_pipe(self, factory_name, name, before, after, first, last, source, config, raw_config, validate)
810 pipe_component, factory_name = self.create_pipe_from_source(
811 factory_name, source, name=name
812 )
813 else:
--> 814 pipe_component = self.create_pipe(
815 factory_name,
816 name=name,
817 config=config,
818 raw_config=raw_config,
819 validate=validate,
820 )
821 pipe_index = self._get_pipe_index(before, after, first, last)
822 self._pipe_meta[name] = self.get_factory_meta(factory_name)

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy\language.py:702, in Language.create_pipe(self, factory_name, name, config, raw_config, validate)
699 cfg = {factory_name: config}
700 # We're calling the internal _fill here to avoid constructing the
701 # registered functions twice
--> 702 resolved = registry.resolve(cfg, validate=validate)
703 filled = registry.fill({"cfg": cfg[factory_name]}, validate=validate)["cfg"]
704 filled = Config(filled)

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\confection_init_.py:756, in registry.resolve(cls, config, schema, overrides, validate)
747 @classmethod
748 def resolve(
749 cls,
(...)
754 validate: bool = True,
755 ) -> Dict[str, Any]:
--> 756 resolved, _ = cls._make(
757 config, schema=schema, overrides=overrides, validate=validate, resolve=True
758 )
759 return resolved

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\confection_init_.py:805, in registry._make(cls, config, schema, overrides, resolve, validate)
803 if not is_interpolated:
804 config = Config(orig_config).interpolate()
--> 805 filled, _, resolved = cls._fill(
806 config, schema, validate=validate, overrides=overrides, resolve=resolve
807 )
808 filled = Config(filled, section_order=section_order)
809 # Check that overrides didn't include invalid properties not in config

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\confection_init_.py:860, in registry._fill(cls, config, schema, validate, resolve, parent, overrides)
858 schema.fields[key] = copy_model_field(field, Any)
859 promise_schema = cls.make_promise_schema(value, resolve=resolve)
--> 860 filled[key], validation[v_key], final[key] = cls._fill(
861 value,
862 promise_schema,
863 validate=validate,
864 resolve=resolve,
865 parent=key_parent,
866 overrides=overrides,
867 )
868 reg_name, func_name = cls.get_constructor(final[key])
869 args, kwargs = cls.parse_args(final[key])

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\confection_init_.py:877, in registry._fill(cls, config, schema, validate, resolve, parent, overrides)
874 getter = cls.get(reg_name, func_name)
875 # We don't want to try/except this and raise our own error
876 # here, because we want the traceback if the function fails.
--> 877 getter_result = getter(*args, **kwargs)
878 else:
879 # We're not resolving and calling the function, so replace
880 # the getter_result with a Promise class
881 getter_result = Promise(
882 registry=reg_name, name=func_name, args=args, kwargs=kwargs
883 )

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy_llm\models\rest\openai\registry.py:43, in openai_gpt_4_v2(config, name, strict, max_tries, interval, max_request_time)
24 @registry.llm_models("spacy.GPT-4.v2")
25 def openai_gpt_4_v2(
26 config: Dict[Any, Any] = SimpleFrozenDict(temperature=_DEFAULT_TEMPERATURE),
(...)
33 max_request_time: float = OpenAI.DEFAULT_MAX_REQUEST_TIME,
34 ) -> Callable[[Iterable[str]], Iterable[str]]:
35 """Returns OpenAI instance for 'gpt-4' model using REST to prompt API.
36
37 config (Dict[Any, Any]): LLM config passed on to the model's initialization.
(...)
41 DOCS: https://spacy.io/api/large-language-models#models
42 """
---> 43 return OpenAI(
44 name=name,
45 endpoint=Endpoints.CHAT.value,
46 config=config,
47 strict=strict,
48 max_tries=max_tries,
49 interval=interval,
50 max_request_time=max_request_time,
51 )

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy_llm\models\rest\base.py:65, in REST.init(self, name, endpoint, config, strict, max_tries, interval, max_request_time)
62 assert self._max_request_time > 0
64 self._check_model()
---> 65 self._verify_auth()

File c:\Users\benja\Code\spacy-llm\env\lib\site-packages\spacy_llm\models\rest\openai\model.py:70, in OpenAI._verify_auth(self)
65 else:
66 warnings.warn(
67 f"Error accessing api.openai.com ({r.status_code}): {r.text}"
68 )
---> 70 response = r.json()["data"]
71 models = [response[i]["id"] for i in range(len(response))]
72 if self._name not in models:

KeyError: 'data'

Here is my python env:

Package Version


aiohttp 3.8.5
aiosignal 1.3.1
annotated-types 0.5.0
anyio 4.0.0
argon2-cffi 23.1.0
argon2-cffi-bindings 21.2.0
arrow 1.2.3
asttokens 2.4.0
async-lru 2.0.4
async-timeout 4.0.3
attrs 23.1.0
Babel 2.12.1
backcall 0.2.0
beautifulsoup4 4.12.2
bleach 6.0.0
blis 0.7.10
catalogue 2.0.9
certifi 2023.7.22
cffi 1.15.1
charset-normalizer 3.2.0
click 8.1.7
colorama 0.4.6
comm 0.1.4
confection 0.1.3
cymem 2.0.8
dataclasses-json 0.5.14
debugpy 1.8.0
decorator 5.1.1
defusedxml 0.7.1
en-core-web-lg 3.6.0
exceptiongroup 1.1.3
executing 1.2.0
fastjsonschema 2.18.0
fqdn 1.5.1
frozenlist 1.4.0
greenlet 2.0.2
idna 3.4
ipykernel 6.25.2
ipython 8.15.0
ipython-genutils 0.2.0
ipywidgets 8.1.1
isoduration 20.11.0
jedi 0.19.0
Jinja2 3.1.2
json5 0.9.14
jsonpointer 2.4
jsonschema 4.19.0
jsonschema-specifications 2023.7.1
jupyter 1.0.0
jupyter_client 8.3.1
jupyter-console 6.6.3
jupyter_core 5.3.1
jupyter-events 0.7.0
jupyter-lsp 2.2.0
jupyter_server 2.7.3
jupyter_server_terminals 0.4.4
jupyterlab 4.0.6
jupyterlab-pygments 0.2.2
jupyterlab_server 2.25.0
jupyterlab-widgets 3.0.9
langchain 0.0.292
langcodes 3.3.0
langsmith 0.0.37
MarkupSafe 2.1.3
marshmallow 3.20.1
matplotlib-inline 0.1.6
mistune 3.0.1
multidict 6.0.4
murmurhash 1.0.10
mypy-extensions 1.0.0
nbclient 0.8.0
nbconvert 7.8.0
nbformat 5.9.2
nest-asyncio 1.5.7
notebook 7.0.3
notebook_shim 0.2.3
numexpr 2.8.6
numpy 1.25.2
overrides 7.4.0
packaging 23.1
pandas 2.1.0
pandocfilters 1.5.0
parso 0.8.3
pathy 0.10.2
pickleshare 0.7.5
pip 23.2.1
platformdirs 3.10.0
preshed 3.0.9
prometheus-client 0.17.1
prompt-toolkit 3.0.39
psutil 5.9.5
pure-eval 0.2.2
pycparser 2.21
pydantic 2.3.0
pydantic_core 2.6.3
Pygments 2.16.1
python-dateutil 2.8.2
python-json-logger 2.0.7
pytz 2023.3.post1
pywin32 306
pywinpty 2.0.11
PyYAML 6.0.1
pyzmq 25.1.1
qtconsole 5.4.4
QtPy 2.4.0
referencing 0.30.2
requests 2.31.0
rfc3339-validator 0.1.4
rfc3986-validator 0.1.1
rpds-py 0.10.3
Send2Trash 1.8.2
setuptools 68.2.2
six 1.16.0
smart-open 6.4.0
sniffio 1.3.0
soupsieve 2.5
spacy 3.6.1
spacy-legacy 3.0.12
spacy-llm 0.5.1
spacy-loggers 1.0.5
SQLAlchemy 2.0.20
srsly 2.4.7
stack-data 0.6.2
tenacity 8.2.3
terminado 0.17.1
thinc 8.1.12
tinycss2 1.2.1
tomli 2.0.1
tornado 6.3.3
tqdm 4.66.1
traitlets 5.10.0
typer 0.9.0
typing_extensions 4.7.1
typing-inspect 0.9.0
tzdata 2023.3
uri-template 1.3.0
urllib3 2.0.4
wasabi 1.1.2
wcwidth 0.2.6
webcolors 1.13
webencodings 0.5.1
websocket-client 1.6.3
wheel 0.41.2
widgetsnbextension 4.0.9
yarl 1.9.2

Response generated with Mistral 7B Instruct for NER task contains only one named entity at the most.

I have run spacy-llm for ner task to identify organisation and product entities with the Mistral 7B instruct model.

In the LLM response I can see that only a single named entity is being identified at most. I have checked this with multiple examples and the result is same.

Example:

LLM response for doc: Mike Iandoli is a self-confessed car sicko. He's executive director of the Larz Anderson Auto Museum in Brookline, and the owner of -- count 'em -- six cars: 1969 Jaguar Roadster, 1961 Jaguar Mark II Sedan, 1995 Land Rover Defender, 1950 Willys Jeepster, 2007 Audi S4, and a 2001 Ferrari Maranello.

1. Mike Iandoli | False | ==NONE== | It is a name, not

Also the explanation generated by the LLM is trimmed in every output I have seen.

Here is my config.cfg

[nlp]
lang = "en"
pipeline = ["llm"]

[components]

[components.llm]
factory = "llm"

[components.llm.task]
@llm_tasks = "spacy.NER.v3"
labels = ["ORGANIZATION", "PRODUCT"]
description = Entities are the names of organizations, companies, and their lines of product and service offerings. Adjectives, verbs, and adverbs are not entities. Pronouns are not entities.

[components.llm.task.label_definitions]
ORGANIZATION = "Known organizations, companies and industries, e.g. Apple Inc., Microsoft Corporation, Pfizer Inc."
PRODUCT = "Physical or digital items offered for sale or use, e.g. MacBook Pro, Skype, Celebrex."

[components.llm.task.examples]
@misc = "spacy.FewShotReader.v1"
path = "./examples.json"

[components.llm.model]
@llm_models = "spacy.Mistral.v1"
name = "Mistral-7B-Instruct-v0.1"

[Warning] the current text generation call will exceed the model's predefined maximum length (4096).

When using LLM to do NER task, there is a warning saying "This is a friendly reminder - the current text generation call will exceed the model's predefined maximum length (4096). Depending on the model, you may observe exceptions, performance degradation, or nothing at all."

How to change the maximum length of the LLM output?

[components.llm.task]
@llm_tasks = "spacy.NER.v3"

[components.llm.model]
@llm_models = "spacy.Llama2.v1"
name = "Llama-2-7b-hf"

FewShot Learning-spacy-llm

Hi,

This issue is all about the fewshot learning challenges by using Dolly LLM for NER approach.

Task and model used:
`[components.llm.task]
@llm_tasks = "spacy.NER.v2"
labels = BATTERY_CAPACITY_KWH,BODY_TYPE,NUMBER_OF_DOORS,DRIVEN_WHEELS,ENGINE_SIZE,FUEL_TYPE,MAKE,MAX_POWER_KW,MAX_POWER_HP,MODEL,POWER_TRAIN_TYPE,TRANSMISSION,TRIM_LEVEL,SECONDARY_VERSION

[components.llm.model]
@llm_models = "spacy.Dolly.v1"
name = "dolly-v2-3b"`

With GPU:
Case-1: Few-shot Learning with single example
As per spacy-llm NER concept, to make the LLM model (Dolly) to predict for a custom data, requires sending the example which contains version name and it's respective entities. Prepared an example file in .yaml format. spacy accepts the example file and created the pipeline for prediction.
Consider a text. On first execution, no prediction at all. In the next execution of the same text, two or three entities got predicted. In the third execution, either prediction of 5 entities or no prediction. This is unstable if I need to work on 5k data.
Case-2: Few-shot Learning with more than one example
โ€‚โ€‚โ€‚โ€‚โ€‚โ€‚Created a .yaml and .yml file with more than one example and it's respective entities. While running the script, the error message "File formats are not supported" popping out. Cross checked the files against the file formats available in github where there is no any issues with the files I had created. Also, checked the video of Vincent's spacy-llm explanation. Even tried the same, facing the same "file formats not supported"

  1. format of .yaml file as per video :

    image

  2. format of .yml file as per spacy-llm's github example.yml:

    image

  3. format of JSONL , tried as per examples provided in re_openai folder where considered only text and ents keys. faced below error:

image

with respect to CPU, will the prediction for a text takes more than 10 mins? Because for single piece of text (contains not more than 20 words), tooks more than 10 mins.

Working dummy example for custom LLM endpoint integration

How would a working dummy example work for the scenario of an own LLM like in this README.md https://github.com/explosion/spacy-llm/blob/main/usage_examples/README.md when running it with the proposed config file and adding the dummy RandomClassification.v1 I get this error:

Exception has occurred: AssertionError
exception: no description

from spacy_llm.registry import registry
import random
from typing import Iterable

@registry.llm_models("RandomClassification.v1")
def random_textcat(labels: str):
    labels = labels.split(",")
    def _classify(prompts: Iterable[str]) -> Iterable[str]:
        for _ in prompts:
            yield random.choice(labels)

    return _classify
...
[components.llm.task]
@llm_tasks = "spacy.TextCat.v1"
labels = LABEL1,LABEL2,LABEL3


[components.llm.model]
@llm_models = "RandomClassification.v1"
labels = ${components.llm.task.labels}  # Make sure to use the same label

Unsupported Langchain models

spacy-llm 0.6.3 comes with the support of langchain 0.0.335.
This version of Langchain comes with many more LLMs that we could potentially use.

In practice, this is not the case. In Langchain, signatures of BaseLanguageModel aren't harmonized. But the current code of spacy-llm assumes that every models constructor in Langchain have the parameter model_name, which is not true anymore.

Let's take the example of Ollama. The signature is taking a model parameter instead of a model_name parameter.

The following config:

@llm_models = "langchain.Ollama.v1"
name = "llama2"
query = {"@llm_queries": "spacy.CallLangChain.v1"}
config = {"temperature": 0.0}

will then leads to errors like

pydantic.error_wrappers.ValidationError: 1 validation error for Ollama
model_name
  extra fields not permitted (type=value_error.extra)

I can modify the code to handle those specific cases, but I need some guidance on the decisions:

  • For Langchain models, should we let the user indicate the name of the model with the appropriate parameter name within the config?
@llm_models = "langchain.Ollama.v1"
query = {"@llm_queries": "spacy.CallLangChain.v1"}
config = {"model": "llama2", "temperature": 0.0}
  • Rather than modifying SpaCy's config format, should we maintain a list of known Langchain parameter name for the model's name, and testing them with try/except?

The second one is really dirty, but the first option implies some breaking changes in people's config.

Code not producing expected entities using spacy_llm

Hi,
I was trying out the new NERv2 task from spacy-llm. I built the following code by concatenating the various examples in the different spacy sources.

from spacy_llm.util import assemble
nlp = assemble(
        "config.cfg",
        overrides={}
    )
doc = nlp("Jack and Jill went up the hill")

print(f"Text: {doc.text}")
print(doc.ents)
print(f"Entities: {[(ent.text, ent.label_) for ent in doc.ents]}")

The code should recognize named entities in the input text and display them in the output. But the output is the following:

> Text: Jack and Jill went up the hill
> ()
> Entities: []

This is the config file:

[nlp]
lang = "en"
pipeline = ["llm"]

[components]

[components.llm]
factory = "llm"

[components.llm.task]
@llm_tasks = "spacy.NER.v2"
labels = ["PERSON", "ORGANIZATION", "LOCATION"]

[components.llm.model]
@llm_models = "spacy.Dolly.v1"
name = "dolly-v2-3b"

[components.llm.task.label_definitions]
PERSON = "Extract any named individual in the text. This entity refers to a specific person who can be identified by name or title."
ORGANIZATION = "Extract any named individual in the text. This entity refers to a specific person who can be identified by name or title."
LOCATION = "Extract any named geographical location or place in the text. This entity represents the names of cities, countries, landmarks, or any other identifiable geographic areas."

[components.llm.task.examples]
@misc = "spacy.FewShotReader.v1"
path = "ner_examples.json"

Also, I tried the same code using this pattern:

[components.llm.model]
@llm_models = "spacy.Dolly.v1"
name = "dolly-v2-7b"

But I got the same output (after 7.5 seconds).

Anyone has the same issue?

segmentation fault

Hi all, I installed spacy 3.6 and spacy-llm 0.4.2, however, when tried to import spacy_llm, I got the segmentation fault error:

Screenshot 2023-07-17 at 21 32 01

my system:

Info about spaCy

  • spaCy version: 3.6.0
  • Platform: Linux-5.4.0-148-generic-x86_64-with-glibc2.27
  • Python version: 3.11.3
  • Pipelines: en_core_web_lg (3.6.0), en_core_web_sm (3.6.0), en_core_web_trf (3.6.1)

Any ideas, please?

Inconsistent output on Dolly NER

Here is a block of example.yml:

- text: Jack and Jill went up the hill.
  spans:
    - text: Jack
      is_entity: true
      label: PERSON
      reason: is the name of a person
    - text: Jill
      is_entity: true
      label: PERSON
      reason: is the name of a person
    - text: went up
      is_entity: false
      label: ==NONE==
      reason: is a verb
    - text: hill
      is_entity: true
      label: LOCATION
      reason: is a location

Block of fewshot.cfg

[paths]


[nlp]
lang = "en"
pipeline = ["llm"]
batch_size = 128

[components]

[components.llm]
factory = "llm"

[components.llm.model]
@llm_models = "spacy.Dolly.v1"
name = "dolly-v2-3b"

[components.llm.task]
@llm_tasks = "spacy.NER.v3"
labels = PERSON,ORGANISATION,LOCATION

[components.llm.task.examples]
@misc = "spacy.FewShotReader.v1"
path = "example.yml"

[components.llm.task.normalizer]
@misc = "spacy.LowercaseNormalizer.v1"

Block of pipeline run:

from spacy_llm.util import assemble
nlp = assemble(
        "fewshot.cfg"
    )
doc = nlp("Jack and Jill went up the hill.")

print(f"Text: {doc.text}")
print(doc.ents)
print(f"Entities: {[(ent.text, ent.label_) for ent in doc.ents]}")

There are inconsistencies in output, and how do i resolve it?

python spacyllmtry.py
Text: Jack and Jill went up the hill.
(Jack, Jill, hill)
Entities: [('Jack', 'PERSON'), ('Jill', 'PERSON'), ('hill', 'LOCATION')]
python spacyllmtry.py
Text: Jack and Jill went up the hill.
()
Entities: []

Spacy-llm bedrock issue

I am getting the below error

[2023-11-16 17:39:57,111] ERROR in app: Exception on /watsonnlu [POST]
Traceback (most recent call last):
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nlu_service.py", line 62, in message
    resp = predict(data["input"], data["bot_id"])
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nlu/assistant_nlu.py", line 250, in predict
    intent_result=predict_intent(bot_id,user_query,sql_connection,language,session,Is_FAQ_present)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nlu/predict_intent.py", line 498, in predict_intent
    spacy_llm_output_list=predict_spacy_llm(user_query,file_name)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nlu/predict_intent.py", line 349, in predict_spacy_llm
    Spacy_LLM_model = assemble(config_file_path)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/spacy_llm/util.py", line 48, in assemble
    return assemble_from_config(config)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/spacy_llm/util.py", line 28, in assemble_from_config
    nlp = load_model_from_config(config, auto_fill=True)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/spacy/util.py", line 587, in load_model_from_config
    nlp = lang_cls.from_config(
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/spacy/language.py", line 1847, in from_config
    nlp.add_pipe(
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/spacy/language.py", line 814, in add_pipe
    pipe_component = self.create_pipe(
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/spacy/language.py", line 702, in create_pipe
    resolved = registry.resolve(cfg, validate=validate)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/confection/__init__.py", line 756, in resolve
    resolved, _ = cls._make(
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/confection/__init__.py", line 805, in _make
    filled, _, resolved = cls._fill(
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/confection/__init__.py", line 860, in _fill
    filled[key], validation[v_key], final[key] = cls._fill(
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/confection/__init__.py", line 877, in _fill
    getter_result = getter(*args, **kwargs)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/spacy_llm/models/langchain/model.py", line 90, in langchain_model
    return LangChain(
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/spacy_llm/models/langchain/model.py", line 34, in __init__
    self._langchain_model = LangChain.get_type_to_cls_dict()[api](
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/langchain/load/serializable.py", line 97, in __init__
    super().__init__(**kwargs)
  File "/Users/Naveen/04102023/assistant_nlu_service_proprietary/nav-spacy-test/lib/python3.9/site-packages/pydantic/v1/main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for Bedrock
model_name
  extra fields not permitted (type=value_error.extra)

where my config file is as follows,

[nlp]
lang = "en"
pipeline = ["llm"]
batch_size = 128

[components]

[components.llm]
factory = "llm"

[components.llm.model]
@llm_models = "langchain.Bedrock.v1"
name = "amazon.titan-text-express-v1"
config = {"model_id": "amazon.titan-text-express-v1"}

[components.llm.task]
@llm_tasks = "spacy.TextCat.v2"
labels = What_can_you_do, Unlock_Account, More_questions
exclusive_classes = false

[components.llm.task.examples]
@misc = "spacy.FewShotReader.v1"
path = "models/nav_testing_3fed4208-dad-4635-8419-b4kumar002_selective_examples.jsonl"

[components.llm.task.normalizer]
@misc = "spacy.LowercaseNormalizer.v1"

'<' not supported between instances of 'str' and 'int'

Hello, when nlp = assemble("config.cfg"), I get the following Traceback, I use the dolly config.cfg example from here : https://spacy.io/usage/large-language-models#usage. Thanks in advance !

File [/usr/local/lib/python3.9/dist-packages/spacy_llm/util.py:48](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy_llm/util.py:48), in assemble(config_path, overrides)
     46 config_path = Path(config_path)
     47 config = load_config(config_path, overrides=overrides, interpolate=False)
---> 48 return assemble_from_config(config)

File [/usr/local/lib/python3.9/dist-packages/spacy_llm/util.py:28](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy_llm/util.py:28), in assemble_from_config(config)
     22 def assemble_from_config(config: Config) -> Language:
     23     """Assemble a spaCy pipeline from a confection Config object.
     24 
     25     config (Config): Config to load spaCy pipeline from.
     26     RETURNS (Language): An initialized spaCy pipeline.
     27     """
---> 28     nlp = load_model_from_config(config, auto_fill=True)
     29     config = config.interpolate()
     30     sourced = get_sourced_components(config)

File [/usr/local/lib/python3.9/dist-packages/spacy/util.py:587](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy/util.py:587), in load_model_from_config(config, meta, vocab, disable, enable, exclude, auto_fill, validate)
    584 # This will automatically handle all codes registered via the languages
    585 # registry, including custom subclasses provided via entry points
    586 lang_cls = get_lang_class(nlp_config["lang"])
--> 587 nlp = lang_cls.from_config(
    588     config,
    589     vocab=vocab,
    590     disable=disable,
    591     enable=enable,
    592     exclude=exclude,
    593     auto_fill=auto_fill,
    594     validate=validate,
    595     meta=meta,
    596 )
    597 return nlp

File [/usr/local/lib/python3.9/dist-packages/spacy/language.py:1864](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy/language.py:1864), in Language.from_config(cls, config, vocab, disable, enable, exclude, meta, auto_fill, validate)
   1861     factory = pipe_cfg.pop("factory")
   1862     # The pipe name (key in the config) here is the unique name
   1863     # of the component, not necessarily the factory
-> 1864     nlp.add_pipe(
   1865         factory,
   1866         name=pipe_name,
   1867         config=pipe_cfg,
   1868         validate=validate,
   1869         raw_config=raw_config,
   1870     )
   1871 else:
   1872     assert "source" in pipe_cfg

File [/usr/local/lib/python3.9/dist-packages/spacy/language.py:821](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy/language.py:821), in Language.add_pipe(self, factory_name, name, before, after, first, last, source, config, raw_config, validate)
    817     pipe_component, factory_name = self.create_pipe_from_source(
    818         factory_name, source, name=name
    819     )
    820 else:
--> 821     pipe_component = self.create_pipe(
    822         factory_name,
    823         name=name,
    824         config=config,
    825         raw_config=raw_config,
    826         validate=validate,
    827     )
    828 pipe_index = self._get_pipe_index(before, after, first, last)
    829 self._pipe_meta[name] = self.get_factory_meta(factory_name)

File [/usr/local/lib/python3.9/dist-packages/spacy/language.py:709](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy/language.py:709), in Language.create_pipe(self, factory_name, name, config, raw_config, validate)
    706 cfg = {factory_name: config}
    707 # We're calling the internal _fill here to avoid constructing the
    708 # registered functions twice
--> 709 resolved = registry.resolve(cfg, validate=validate)
    710 filled = registry.fill({"cfg": cfg[factory_name]}, validate=validate)["cfg"]
    711 filled = Config(filled)

File [/usr/local/lib/python3.9/dist-packages/confection/__init__.py:759](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/confection/__init__.py:759), in registry.resolve(cls, config, schema, overrides, validate)
    750 @classmethod
    751 def resolve(
    752     cls,
   (...)
    757     validate: bool = True,
    758 ) -> Dict[str, Any]:
--> 759     resolved, _ = cls._make(
    760         config, schema=schema, overrides=overrides, validate=validate, resolve=True
    761     )
    762     return resolved

File [/usr/local/lib/python3.9/dist-packages/confection/__init__.py:808](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/confection/__init__.py:808), in registry._make(cls, config, schema, overrides, resolve, validate)
    806 if not is_interpolated:
    807     config = Config(orig_config).interpolate()
--> 808 filled, _, resolved = cls._fill(
    809     config, schema, validate=validate, overrides=overrides, resolve=resolve
    810 )
    811 filled = Config(filled, section_order=section_order)
    812 # Check that overrides didn't include invalid properties not in config

File [/usr/local/lib/python3.9/dist-packages/confection/__init__.py:863](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/confection/__init__.py:863), in registry._fill(cls, config, schema, validate, resolve, parent, overrides)
    861     schema.__fields__[key] = copy_model_field(field, Any)
    862 promise_schema = cls.make_promise_schema(value, resolve=resolve)
--> 863 filled[key], validation[v_key], final[key] = cls._fill(
    864     value,
    865     promise_schema,
    866     validate=validate,
    867     resolve=resolve,
    868     parent=key_parent,
    869     overrides=overrides,
    870 )
    871 reg_name, func_name = cls.get_constructor(final[key])
    872 args, kwargs = cls.parse_args(final[key])

File [/usr/local/lib/python3.9/dist-packages/confection/__init__.py:880](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/confection/__init__.py:880), in registry._fill(cls, config, schema, validate, resolve, parent, overrides)
    877     getter = cls.get(reg_name, func_name)
    878     # We don't want to try/except this and raise our own error
    879     # here, because we want the traceback if the function fails.
--> 880     getter_result = getter(*args, **kwargs)
    881 else:
    882     # We're not resolving and calling the function, so replace
    883     # the getter_result with a Promise class
    884     getter_result = Promise(
    885         registry=reg_name, name=func_name, args=args, kwargs=kwargs
    886     )

File [/usr/local/lib/python3.9/dist-packages/spacy_llm/models/hf/dolly.py:63](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy_llm/models/hf/dolly.py:63), in dolly_hf(name, config_init, config_run)
     50 @registry.llm_models("spacy.Dolly.v1")
     51 def dolly_hf(
     52     name: Dolly.MODEL_NAMES,
     53     config_init: Optional[Dict[str, Any]] = SimpleFrozenDict(),
     54     config_run: Optional[Dict[str, Any]] = SimpleFrozenDict(),
     55 ) -> Callable[[Iterable[str]], Iterable[str]]:
     56     """Generates Dolly instance that can execute a set of prompts and return the raw responses.
     57     name (Literal): Name of the Dolly model. Has to be one of Dolly.get_model_names().
     58     config_init (Optional[Dict[str, Any]]): HF config for initializing the model.
   (...)
     61         the raw responses.
     62     """
---> 63     return Dolly(name=name, config_init=config_init, config_run=config_run)

File [/usr/local/lib/python3.9/dist-packages/spacy_llm/models/hf/base.py:39](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy_llm/models/hf/base.py:39), in HuggingFace.__init__(self, name, config_init, config_run)
     37 HuggingFace.check_installation()
     38 self._check_model()
---> 39 self._model = self.init_model()

File [/usr/local/lib/python3.9/dist-packages/spacy_llm/models/hf/dolly.py:17](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/spacy_llm/models/hf/dolly.py:17), in Dolly.init_model(self)
     13 def init_model(self) -> Any:
     14     """Sets up HF model and needed utilities.
     15     RETURNS (Any): HF model.
     16     """
---> 17     return transformers.pipeline(
     18         model=self._name, return_full_text=False, **self._config_init
     19     )

File [/usr/local/lib/python3.9/dist-packages/transformers/pipelines/__init__.py:767](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/transformers/pipelines/__init__.py:767), in pipeline(task, model, config, tokenizer, feature_extractor, framework, revision, use_fast, use_auth_token, device_map, torch_dtype, trust_remote_code, model_kwargs, pipeline_class, **kwargs)
    764 if feature_extractor is not None:
    765     kwargs["feature_extractor"] = feature_extractor
--> 767 return pipeline_class(model=model, framework=framework, task=task, **kwargs)

File [~/.cache/huggingface/modules/transformers_modules/databricks/dolly-v2-3b/f6c9be08f16fe4d3a719bee0a4a7c7415b5c65df/instruct_pipeline.py:74](https://file+.vscode-resource.vscode-cdn.net/Users/user/Documents/lili/nlp-bricks/synthetic_data_annotation/~/.cache/huggingface/modules/transformers_modules/databricks/dolly-v2-3b/f6c9be08f16fe4d3a719bee0a4a7c7415b5c65df/instruct_pipeline.py:74), in InstructionTextGenerationPipeline.__init__(self, do_sample, max_new_tokens, top_p, top_k, *args, **kwargs)
     61 def __init__(
     62     self, *args, do_sample: bool = True, max_new_tokens: int = 256, top_p: float = 0.92, top_k: int = 0, **kwargs
     63 ):
     64     """Initialize the pipeline
     65 
     66     Args:
   (...)
     72             Defaults to 0.
     73     """
---> 74     super().__init__(*args, do_sample=do_sample, max_new_tokens=max_new_tokens, top_p=top_p, top_k=top_k,
     75                      **kwargs)

File [/usr/local/lib/python3.9/dist-packages/transformers/pipelines/base.py:768](https://file+.vscode-resource.vscode-cdn.net/usr/local/lib/python3.9/dist-packages/transformers/pipelines/base.py:768), in Pipeline.__init__(self, model, tokenizer, feature_extractor, modelcard, framework, task, args_parser, device, binary_output, **kwargs)
    766     self.device = device
    767 else:
--> 768     self.device = device if framework == "tf" else torch.device("cpu" if device < 0 else f"cuda:{device}")
    769 self.binary_output = binary_output
    771 # Special handling

TypeError: '<' not supported between instances of 'str' and 'int'

OpenAI API couldn't be reached despite setting the API key in Envrionment Variables


ConnectionError Traceback (most recent call last)
Cell In[4], line 2
1 nlp.initialize()
----> 2 doc = nlp("Jack and Jill rode up the hill in Les Deux Alpes")
3 print([(ent.text, ent.label_) for ent in doc.ents])

File c:\Users\danie\anaconda3\envs\llm-ner\Lib\site-packages\spacy\language.py:1054, in Language.call(self, text, disable, component_cfg)
1052 raise ValueError(Errors.E109.format(name=name)) from e
1053 except Exception as e:
-> 1054 error_handler(name, proc, [doc], e)
1055 if not isinstance(doc, Doc):
1056 raise ValueError(Errors.E005.format(name=name, returned_type=type(doc)))

File c:\Users\danie\anaconda3\envs\llm-ner\Lib\site-packages\spacy\util.py:1704, in raise_error(proc_name, proc, docs, e)
1703 def raise_error(proc_name, proc, docs, e):
-> 1704 raise e

File c:\Users\danie\anaconda3\envs\llm-ner\Lib\site-packages\spacy\language.py:1049, in Language.call(self, text, disable, component_cfg)
1047 error_handler = proc.get_error_handler()
1048 try:
-> 1049 doc = proc(doc, **component_cfg.get(name, {})) # type: ignore[call-arg]
1050 except KeyError as e:
1051 # This typically happens if a component is not initialized
1052 raise ValueError(Errors.E109.format(name=name)) from e
...
136 )
138 return response

ConnectionError: API could not be reached after 33.754 seconds in total and attempting to connect 5 times. Check your network connection and the API's availability.
429 Too Many Requests
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

Using SpaCy LLM to generate regular text

I understand it might not necessarily be the point of the repo, but I'm working on a project where I want to use a few spacy-llm defined tasks (NER and text classification) as well as getting the model to generate text. What's the best way to do this within the framework? I'm trying to avoid having multiple ways of loading/calling the same model. I'm aiming to use the LangChain CTransformers integration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.