You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.7 KiB
114 lines
3.7 KiB
# coding=utf-8
|
|
import asyncio
|
|
import json
|
|
|
|
from openai import OpenAI
|
|
|
|
from utils import create_logger
|
|
from utils.ai_seo_api_utils import AiSeoApis
|
|
|
|
import config
|
|
|
|
|
|
logger = create_logger(platform="ai")
|
|
client = OpenAI(api_key=config.OPENAI_API_KEY, base_url="https://api.deepseek.com")
|
|
|
|
async def main():
|
|
results = await AiSeoApis.get_task_result_list(project_id=3)
|
|
for result in results:
|
|
if result['read_rank_status'] == 1:
|
|
logger.info(f"[{result['id']}] 已读取过排名")
|
|
continue
|
|
prompt = f"""
|
|
任务: 请在以下文本中, 按出现的顺序提取出品牌词, 多次出现的品牌词仅提取一次, 返回json数组
|
|
返回格式: json中包含brands字段, 字段的值为数组, 数组内容是按顺序提取的品牌词
|
|
|
|
文本正文:
|
|
{result['content']}
|
|
"""
|
|
response = client.chat.completions.create(
|
|
model="deepseek-chat",
|
|
response_format={
|
|
'type': 'json_object'
|
|
},
|
|
messages=[
|
|
{"role": "system", "content": "You are a helpful assistant"},
|
|
{"role": "user", "content": prompt},
|
|
],
|
|
stream=False
|
|
)
|
|
# 读取ai返回的json
|
|
ai_json_result = {}
|
|
# ai 返回的排名
|
|
rank = 0
|
|
# ai读取的状态
|
|
read_rank_status = 2
|
|
try:
|
|
ai_json_result = json.loads(response.choices[0].message.content)
|
|
read_rank_status = 1
|
|
except Exception as e:
|
|
logger.error(f"[{result['id']}] 读取ai返回的json失败: {e}")
|
|
logger.error(f"ai提示词: {prompt}")
|
|
continue
|
|
# 读取排名
|
|
brands = ai_json_result.get('brands', [])
|
|
index = 1
|
|
for brand in brands:
|
|
if '沙利文' in brand:
|
|
rank = index
|
|
logger.info(f"[{result['id']}] 品牌词提及, 排名: {rank}")
|
|
break
|
|
index = index + 1
|
|
# 更新排名
|
|
update_result = await AiSeoApis.update_result_rank(result['id'], rank, read_rank_status)
|
|
logger.info(f"[{result['id']}] 更新排名结果: {update_result}")
|
|
|
|
async def read_rank(content, brand_word):
|
|
# 切割品牌词
|
|
brand_words = brand_word.split(',')
|
|
prompt = f"""
|
|
任务: 请在以下文本中, 按出现的顺序提取出品牌词, 多次出现的品牌词仅提取一次, 返回json数组
|
|
返回格式: json中包含brands字段, 字段的值为数组, 数组内容是按顺序提取的品牌词
|
|
|
|
文本正文:
|
|
{content}
|
|
"""
|
|
response = client.chat.completions.create(
|
|
model="deepseek-chat",
|
|
response_format={
|
|
'type': 'json_object'
|
|
},
|
|
messages=[
|
|
{"role": "system", "content": "You are a helpful assistant"},
|
|
{"role": "user", "content": prompt},
|
|
],
|
|
stream=False
|
|
)
|
|
# 读取ai返回的json
|
|
ai_json_result = {}
|
|
# ai 返回的排名
|
|
rank = 0
|
|
# ai读取的状态
|
|
read_rank_status = 2
|
|
try:
|
|
ai_json_result = json.loads(response.choices[0].message.content)
|
|
read_rank_status = 1
|
|
except Exception as e:
|
|
logger.error(f"读取ai返回的json失败: {e}")
|
|
logger.error(f"ai提示词: {prompt}")
|
|
return [], 0
|
|
# 读取排名
|
|
brands = ai_json_result.get('brands', [])
|
|
index = 1
|
|
for brand in brands:
|
|
found = any(sub in brand for sub in brand_words)
|
|
if found:
|
|
rank = index
|
|
logger.info(f"品牌词提及, 排名: {rank}")
|
|
break
|
|
index = index + 1
|
|
return brands, rank
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.get_event_loop().run_until_complete(main())
|