当存在OPENAI_API_KEY环境变量时,启用AI评价

This commit is contained in:
Cp0204 2023-04-19 10:23:38 +08:00 committed by GitHub
parent 1c7fcb1c8f
commit 2d7be805da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 0 deletions

View File

@ -5,6 +5,7 @@
# @File : auto_comment.py # @File : auto_comment.py
# 多账号评价,异常处理 # 多账号评价,异常处理
# 2023/3/28 修复乱码 # 2023/3/28 修复乱码
# 2023/4/19 当存在OPENAI_API_KEY环境变量时启用AI评价网络环境问题自行想办法国外机或配置ProxyUrl HTTP代理
''' '''
new Env('自动评价'); new Env('自动评价');
8 8 2 1 * https://raw.githubusercontent.com/6dylan6/auto_comment/main/jd_comment.py 8 8 2 1 * https://raw.githubusercontent.com/6dylan6/auto_comment/main/jd_comment.py
@ -26,17 +27,20 @@ try:
#import yaml #import yaml
from lxml import etree from lxml import etree
import zhon.hanzi import zhon.hanzi
import openai
except: except:
print('解决依赖问题...稍等') print('解决依赖问题...稍等')
os.system('pip3 install lxml &> /dev/null') os.system('pip3 install lxml &> /dev/null')
os.system('pip3 install jieba &> /dev/null') os.system('pip3 install jieba &> /dev/null')
os.system('pip3 install zhon &> /dev/null') os.system('pip3 install zhon &> /dev/null')
os.system('pip3 install requests &> /dev/null') os.system('pip3 install requests &> /dev/null')
os.system('pip3 install openai &> /dev/null')
import jieba import jieba
import jieba.analyse import jieba.analyse
#import yaml #import yaml
from lxml import etree from lxml import etree
import requests import requests
import openai
import jdspider import jdspider
@ -106,6 +110,8 @@ class StyleFormatter(logging.Formatter):
# 评价生成 # 评价生成
def generation(pname, _class=0, _type=1, opts=None): def generation(pname, _class=0, _type=1, opts=None):
if "OPENAI_API_KEY" in os.environ:
return generation_ai(pname, opts)
opts = opts or {} opts = opts or {}
items = ['商品名'] items = ['商品名']
items.clear() items.clear()
@ -163,6 +169,19 @@ def generation(pname, _class=0, _type=1, opts=None):
return 5, comments.replace("$", name) 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): def all_evaluate(opts=None):