ai icon indicating copy to clipboard operation
ai copied to clipboard

How can i use moderation api from open ai with vercel ai sdk, there was nothing about this in vercel ai docs

Open Akshmit11 opened this issue 1 year ago • 2 comments

Feature Description

I am asking users to enter some text and submit it and I am using vercel ai sdk to generate response from chatgpt to that text, but I also want to check if the text contains any profanity or not. How can I achieve this?

import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';



const checkProfanity = async (text: string) => {
  try {
// **what to do about this..->-> open.moderation.create**
    const response = await openai.moderation.create({
      input: text,
    });
    const { results } = response.data;
    return results[0].flagged;
  } catch (error) {
    console.error("Error checking profanity:", error);
    return false;
  }
};


const isProfane = await checkProfanity(text);

    if (isProfane) {
      throw new Error("The text contains inappropriate content. Please modify your text and try again.");
    }

const aiResponse = await generateText({
      model: openai('gpt-3.5-turbo'),
      prompt: `my prompt comes here....

Is there any way of achieving this functionality?

Use Case

No response

Additional context

No response

Akshmit11 avatar Aug 06 '24 04:08 Akshmit11

OpenAI moderations are currently not supported by the AI SDK.

lgrammel avatar Aug 06 '24 08:08 lgrammel

Would be awesome to have it implemented

ThalusA avatar Aug 08 '24 12:08 ThalusA

+1 on moderations being added

williamlmao avatar Nov 12 '24 19:11 williamlmao

+1

sivayapps avatar Mar 23 '25 04:03 sivayapps

+1

massimobrivio avatar Mar 27 '25 09:03 massimobrivio

+1

olivermontes avatar Mar 31 '25 17:03 olivermontes

🤜

JoshuaKirby88 avatar Apr 29 '25 11:04 JoshuaKirby88

+1

Mcilie avatar May 08 '25 15:05 Mcilie

+1

tsziming avatar May 12 '25 16:05 tsziming

Still would love to see this to be implemented!

jaschahuisman avatar May 15 '25 11:05 jaschahuisman

Duplicates #2521

lgrammel avatar Jun 26 '25 08:06 lgrammel