llama_index [Bug]: 无法找到来自cohere.custom_model_dataset的JsonlDataset,

00jrzges  于 2个月前  发布在  其他
关注(0)|答案(4)|浏览(19)

根据提供的错误信息,问题出在缺少名为cohere.custom_model_dataset的模块。这可能是由于缺少相应的依赖库导致的。

要解决这个问题,你可以尝试以下步骤:

  1. 确保你已经安装了cohere库。可以使用以下命令来安装它:
pip install cohere
  1. 如果你已经安装了cohere库但仍然遇到该错误,可能是因为你的环境中存在多个版本的cohere库。可以尝试升级或降级cohere库的版本,以确保与你的代码兼容。可以使用以下命令来升级或降级cohere库:
pip install --upgrade cohere

或者

pip install cohere==<desired_version>

<desired_version>替换为你想要安装的具体版本号。

  1. 如果上述步骤都没有解决问题,可能是因为你的代码中引用了一个不存在的模块或文件。请检查你的代码,确保所有引用的模块和文件都存在,并且路径正确。

请注意,以上步骤是基于提供的信息进行推测的解决方案。如果问题仍然存在,可能需要更多的上下文信息来进一步排查。

hsvhsicv

hsvhsicv1#

你好,@NoobPythoner!我在这里协助你解决任何bug、问题或贡献。让我们一起解决你面临的问题。

要解决在 CohereRerankerFinetuneEngine.finetune 方法中尝试从 cohere.custom_model_dataset 导入 JsonlDataset 时出现的 ModuleNotFoundError 问题,你需要确保你的环境中安装了 cohere 包。你可以使用pip安装 cohere 包:

pip install cohere

安装包后,导入语句应该可以正常工作:

from cohere.custom_model_dataset import JsonlDataset

请确保在执行脚本的相同环境中运行上述pip命令。此外,确保在 CohereRerankerFinetuneEngine 类中正确导入了 cohere 模块,如下所示:

import importlib.util
import os
from typing import Optional

from llama_index.finetuning.types import BaseCohereRerankerFinetuningEngine
from llama_index.postprocessor.cohere_rerank import CohereRerank

class CohereRerankerFinetuneEngine(BaseCohereRerankerFinetuningEngine):
    """Cohere Reranker Finetune Engine."""

    def __init__(
        self,
        train_file_name: str = "train.jsonl",
        val_file_name: Optional[str] = None,
        model_name: str = "exp_finetune",
        model_type: str = "RERANK",
        base_model: str = "english",
        api_key: Optional[str] = None,
    ) -> None:
        """Init params."""
        # This will be None if 'cohere' module is not available
        cohere_spec = importlib.util.find_spec("cohere")

        if cohere_spec is not None:
            import cohere
        else:
            # Raise an ImportError if 'cohere' is not installed
            raise ImportError(
                "Cannot import cohere. Please install the package using `pip install cohere`."
            )

        try:
            self.api_key = api_key or os.environ["COHERE_API_KEY"]
        except IndexError:
            raise ValueError(
                "Must pass in cohere api key or "
                "specify via COHERE_API_KEY environment variable "
            )
        self._model = cohere.Client(self.api_key, client_name="llama_index")
        self._train_file_name = train_file_name
        self._val_file_name = val_file_name
        self._model_name = model_name
        self._model_type = model_type
        self._base_model = base_model
        self._finetune_model = None

    def finetune(self) -> None:
        """Finetune model."""
        from cohere.custom_model_dataset import JsonlDataset

        if self._val_file_name:
            # Uploading both train file and eval file
            dataset = JsonlDataset(
                train_file=self._train_file_name, eval_file=self._val_file_name
            )
        else:
            # Single Train File Upload:
            dataset = JsonlDataset(train_file=self._train_file_name)

        self._finetune_model = self._model.create_custom_model(
            name=self._model_name,
            dataset=dataset,
            model_type=self._model_type,
            base_model=self._base_model,
        )

    def get_finetuned_model(self, top_n: int = 5) -> CohereRerank:
        """Gets finetuned model id."""
        if self._finetune_model is None:
            raise RuntimeError(
                "Finetuned model is not set yet. Please run the finetune method first."
            )
        return CohereRerank(
            model=self._finetune_model.id, top_n=top_n, api_key=self.api_key
        )

请确保你使用的是 cohere 包的正确版本,这应该是与 LlamaIndex v0.10.55 1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/finetuning/rerankers/cohere_reranker.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-finetuning/llama_index/finetuning/rerankers/cohere_reranker.py)3(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/llms/llama-index-llms-cohere/pyproject.toml)4(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/embeddings/llama-index-embeddings-cohere/pyproject.toml) 兼容的 ^5.2.5 版本。

  • 要继续与 Dosu 交流,请提及 @dosu。*
zsbz8rwp

zsbz8rwp2#

我已经安装了cohere-5.2.5,但仍然无法导入cohere.custom_model_dataset。我检查了cohere的代码,确实没有custom_model_dataset和JsonlDataset。

2ekbmq32

2ekbmq323#

我不认为我在这里犯了如此低级的错误。你是认真的吗?

yptwkmov

yptwkmov4#

看起来 cohere.custom_model_dataset 模块和 JsonlDataset 类在 cohere 包中不存在。这不是你的设置问题,而是代码或文档中的差异。
作为替代方案,你可以在 llama_index 包中使用 CohereRerankerFinetuneDataset 类,它提供了类似的功能。以下是如何修改代码以使用此类的示例:

from llama_index.finetuning.rerankers.dataset_gen import CohereRerankerFinetuneDataset

class CohereRerankerFinetuneEngine(BaseCohereRerankerFinetuningEngine):
    """Cohere Reranker Finetune Engine."""

    def __init__(
        self,
        train_file_name: str = "train.jsonl",
        val_file_name: Optional[str] = None,
        model_name: str = "exp_finetune",
        model_type: str = "RERANK",
        base_model: str = "english",
        api_key: Optional[str] = None,
    ) -> None:
        """Init params."""
        cohere_spec = importlib.util.find_spec("cohere")

        if cohere_spec is not None:
            import cohere
        else:
            raise ImportError(
                "Cannot import cohere. Please install the package using `pip install cohere`."
            )

        try:
            self.api_key = api_key or os.environ["COHERE_API_KEY"]
        except IndexError:
            raise ValueError(
                "Must pass in cohere api key or "
                "specify via COHERE_API_KEY environment variable "
            )
        self._model = cohere.Client(self.api_key, client_name="llama_index")
        self._train_file_name = train_file_name
        self._val_file_name = val_file_name
        self._model_name = model_name
        self._model_type = model_type
        self._base_model = base_model
        self._finetune_model = None

    def finetune(self) -> None:
        """Finetune model."""
        if self._val_file_name:
            dataset = CohereRerankerFinetuneDataset(
                train_file=self._train_file_name, eval_file=self._val_file_name
            )
        else:
            dataset = CohereRerankerFinetuneDataset(train_file=self._train_file_name)

        self._finetune_model = self._model.create_custom_model(
            name=self._model_name,
            dataset=dataset,
            model_type=self._model_type,
            base_model=self._base_model,
        )

这应该解决了缺少 JsonlDataset 类的问题。
@logan-markewich,这个问题有点超出我的能力范围了,你能帮忙提供指导吗?

  • 要继续与 Dosu 交流,请提及 @dosu。*

相关问题