async runWithInputs()

in src/nodes/server/text-generation.ts [32:98]


  async runWithInputs(inputs: Inputs) {
    const {
      text,
      apikey,
      modelid,
      temperature,
      repetition_penalty,
      max_new_tokens,
      return_full_text,
    } = inputs;

    const _modelid = modelid?.trim();

    if (this.hf && apikey) {
      this.hf = new HfInference(apikey);
    }

    if (!text) {
      this.dispatchEvent(
        new CustomEvent("outputs", { detail: { results: null } })
      );
      return;
    }

    if (this.cachedOutput && compareObjects(this.cachedInputs, inputs)) {
      this.dispatchEvent(
        new CustomEvent("outputs", { detail: this.cachedOutput })
      );
      return;
    }

    try {
      const textGenerationRes = await this.hf?.textGeneration({
        model: _modelid,
        inputs: text,
        parameters: {
          repetition_penalty: repetition_penalty,
          max_new_tokens: max_new_tokens,
          return_full_text: return_full_text,
          temperature: temperature,
        },
      });
      if (!textGenerationRes) {
        throw new Error("Invalid response");
      }

      this.cachedOutput = {
        results: textGenerationRes.generated_text,
      };
      this.cachedInputs = inputs;

      this.dispatchEvent(
        new CustomEvent("outputs", { detail: this.cachedOutput })
      );
    } catch (error: any) {
      this.dispatchEvent(
        new CustomEvent("outputs", {
          detail: {
            error: {
              title: "Error",
              message: error.message,
            },
          },
        })
      );
    }
  }