microsoft / TaskWeaver

A code-first agent framework for seamlessly planning and executing data analytics tasks.
https://microsoft.github.io/TaskWeaver/
MIT License
5.28k stars 665 forks source link

Cannot find self defined plugin when executing code #423

Open irmowan opened 1 week ago

irmowan commented 1 week ago

I write a plugin under my project/plugins folder:

from taskweaver.plugin import Plugin, register_plugin
from typing import List
try:
    from snownlp import SnowNLP
except ImportError:
    raise ImportError("Please install snownlp with `pip install snownlp`")

@register_plugin
class SentimentAnalysis(Plugin):
    def _init(self) -> None:
        pass

    def sentiment_to_score(self, sentiment: float) -> int:
        """Map (0,1) to 1-5
        """
        return int(sentiment  * 5) + 1

    def __call__(self, inputs: List[str]):
        results = []
        for review in inputs:
            s = SnowNLP(review)
            rating = self.sentiment_to_score(s.sentiments)
            results.append(rating)
        return results    

Here is the yaml file:

name: sentiment_analysis
enabled: true
required: false
description: >-
  sentiment_analysis processes a list of text inputs and assigns a sentiment score from 1 to 5, based on the polarity of the text.
examples:
  sentiment_scores = sentiment_analysis(["Very good product", "Bad quality", "A little noisy"])

parameters:
  - name: inputs
    type: list
    required: true
    description: >-
      A list of sentences to be analyzed for sentiment polarity.

returns:
  - name: sentiment_scores
    type: list
    description: >-
      A list of sentiment scores (from 1 to 5, integer) for each input string, based on the analysis.

However, the taskweaver cannot find this plugin: image

This is the folder structure. image

liqul commented 1 week ago

The symptom suggests that TaskWeaver can find your plugin (because the code had been generated correctly), but calling it led to an error. Please first check the FAQ here to see if the problem can be fixed. There is also a way of debugging the plugin in the FAQ.