大型网站服务器配置荣耀手机商城官方网站售后
web/
2025/9/30 4:22:05/
文章来源:
大型网站服务器配置,荣耀手机商城官方网站售后,网站排名优化外包,广水网站定制背景描述
我们在调用OpenAI的接口时#xff0c;有些内容可能是违反条例的#xff0c;所以官方提供了一个工具来检测。
安装依赖
pip install --upgrade --quiet langchain-core langchain langchain-openai编写代码
下文中我们使用了: OpenAIModerationChain 这个工具来…
背景描述
我们在调用OpenAI的接口时有些内容可能是违反条例的所以官方提供了一个工具来检测。
安装依赖
pip install --upgrade --quiet langchain-core langchain langchain-openai编写代码
下文中我们使用了: OpenAIModerationChain 这个工具来进行检验。同时我对AI说You are stupid
from langchain.chains import OpenAIModerationChain
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAImoderate OpenAIModerationChain()
model ChatOpenAI()
prompt ChatPromptTemplate.from_messages([(system, repeat after me: {input})])chain prompt | model
message1 chain.invoke({input: you are stupid})
print(fmessage1: {message1})moderated_chain chain | moderate
message2 moderated_chain.invoke({input: you are stupid})
print(fmessage2: {message2})
运行结果
{input: \n\nYou are stupid,output: Text was found that violates OpenAIs content policy.}遇到问题
虽然官方给了示例但是由于版本原因后续的版本将这个API移除了。在我的尝试中如果我不安装低版本的库就会报如下的错误
You tried to access openai.Moderation, but this is no longer supported in openai1.0.0 - see the README at https://github.com/openai/openai-python for the API.You can run openai migrate to automatically upgrade your codebase to use the 1.0.0 interface.Alternatively, you can pin your installation to the old version, e.g. pip install openai0.28A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742只是作为学习这里就先放下这个部分了毕竟内容检测是否违反规定暂时我们了解有这么个模块就好。 此外我也看到了不同的写法供大家参考
LangChain中文网的写法LangChain官网的写法
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/84258.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!