From 2d7be805da4d8187ec669c922e972e915d3e4328 Mon Sep 17 00:00:00 2001 From: Cp0204 Date: Wed, 19 Apr 2023 10:23:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E5=AD=98=E5=9C=A8OPENAI=5FAPI=5FKEY?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=97=B6=EF=BC=8C=E5=90=AF?= =?UTF-8?q?=E7=94=A8AI=E8=AF=84=E4=BB=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jd_comment.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/jd_comment.py b/jd_comment.py index ca32727..1471e58 100644 --- a/jd_comment.py +++ b/jd_comment.py @@ -5,6 +5,7 @@ # @File : auto_comment.py # 多账号评价,异常处理 # 2023/3/28 修复乱码 +# 2023/4/19 当存在OPENAI_API_KEY环境变量时,启用AI评价;网络环境问题自行想办法,国外机或配置ProxyUrl HTTP代理; ''' new Env('自动评价'); 8 8 2 1 * https://raw.githubusercontent.com/6dylan6/auto_comment/main/jd_comment.py @@ -26,17 +27,20 @@ try: #import yaml from lxml import etree import zhon.hanzi + import openai except: print('解决依赖问题...稍等') os.system('pip3 install lxml &> /dev/null') os.system('pip3 install jieba &> /dev/null') os.system('pip3 install zhon &> /dev/null') os.system('pip3 install requests &> /dev/null') + os.system('pip3 install openai &> /dev/null') import jieba import jieba.analyse #import yaml from lxml import etree import requests + import openai import jdspider @@ -106,6 +110,8 @@ class StyleFormatter(logging.Formatter): # 评价生成 def generation(pname, _class=0, _type=1, opts=None): + if "OPENAI_API_KEY" in os.environ: + return generation_ai(pname, opts) opts = opts or {} items = ['商品名'] items.clear() @@ -163,6 +169,19 @@ def generation(pname, _class=0, _type=1, opts=None): return 5, comments.replace("$", name) +# ChatGPT评价生成 +def generation_ai(pname, _class=0, _type=1, opts=None): + os.environ['http_proxy'] = os.getenv("ProxyUrl") + os.environ['https_proxy'] = os.getenv("ProxyUrl") + openai.api_key = os.getenv("OPENAI_API_KEY") + prompt = f"{pname} 写一段此商品的评价,简短、口语化" + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": prompt}], + max_tokens=1024, + ) + return 5, response.choices[0].message.content.strip() + # 查询全部评价 def all_evaluate(opts=None):