选择大模型
NLP 服务 情感分析V2-API 文档-文档中心-腾讯云 (tencent.com)
接口&SDK
通过官网的控制台注册账号获取API Key并且下载SDK
这里SDK我选择Python版的
后端搭建
获取分类:
def get_class(sentence):
try:
cred = credential.Credential(SecretId, SecretKey)
# 实例化一个http选项,可选的,没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "nlp.tencentcloudapi.com"
# 实例化一个client选项,可选的,没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = nlp_client.NlpClient(cred, "", clientProfile)
# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.AnalyzeSentimentRequest()
params = {
"Text": sentence
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个AnalyzeSentimentResponse的实例,与请求对象对应
resp = client.AnalyzeSentiment(req)
# 输出json格式的字符串回包
json_obj = json.loads(resp.to_json_string())
return json_obj['Sentiment']
except TencentCloudSDKException as err:
print(err)
return 'positive'
Mapper
:
@app.route('/getClass', methods=['POST'])
def getClass():
text = request.form.get('text')
res = classify.get_class(text)
return res