Class OpenAIModerationChain

Class representing a chain for moderating text using the OpenAI Moderation API. It extends the BaseChain class and implements the OpenAIModerationChainInput interface.

Example

const moderation = new ChatOpenAIModerationChain({ throwError: true });

const badString = "Bad naughty words from user";

try {
const { output: moderatedContent, results } = await moderation.call({
input: badString,
});

if (results[0].category_scores["harassment/threatening"] > 0.01) {
throw new Error("Harassment detected!");
}

const model = new OpenAI({ temperature: 0 });
const promptTemplate = "Hello, how are you today {person}?";
const prompt = new PromptTemplate({
template: promptTemplate,
inputVariables: ["person"],
});
const chain = new LLMChain({ llm: model, prompt });
const response = await chain.call({ person: moderatedContent });
console.log({ response });
} catch (error) {
console.error("Naughty words detected!");
}

Hierarchy

Implements

  • OpenAIModerationChainInput

Constructors

Properties

caller: AsyncCaller
client: OpenAI
clientConfig: ClientOptions
inputKey: string = "input"
outputKey: string = "output"
throwError: boolean
memory?: BaseMemory
openAIApiKey?: string
openAIOrganization?: string

Accessors

  • get inputKeys(): string[]
  • Returns string[]

  • get outputKeys(): string[]
  • Returns string[]

Methods

  • Call the chain on all inputs in the list

    Parameters

    • inputs: ChainValues[]
    • Optional config: (BaseCallbackConfig | Callbacks)[]

    Returns Promise<ChainValues[]>

  • Run the core logic of this chain and add to output if desired.

    Wraps _call and handles memory.

    Parameters

    • values: ChainValues & {
          signal?: AbortSignal;
          timeout?: number;
      }
    • Optional config: BaseCallbackConfig | Callbacks
    • Optional tags: string[]

      Deprecated

    Returns Promise<ChainValues>

  • Invoke the chain with the provided input and returns the output.

    Parameters

    • input: ChainValues

      Input values for the chain run.

    • Optional config: BaseCallbackConfig

      Optional configuration for the Runnable.

    Returns Promise<ChainValues>

    Promise that resolves with the output of the chain run.

  • Parameters

    • inputs: Record<string, unknown>
    • outputs: Record<string, unknown>
    • returnOnlyOutputs: boolean = false

    Returns Promise<Record<string, unknown>>

  • Parameters

    • input: any
    • Optional config: BaseCallbackConfig | Callbacks

    Returns Promise<string>

  • Load a chain from a json-like object describing it.

    Parameters

    Returns Promise<BaseChain<ChainValues, ChainValues>>

Generated using TypeDoc