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.
262 lines
14 KiB
262 lines
14 KiB
from django.db.models import Q |
|
from ai.models import PromptTemplate |
|
from ai.utils.show_database import GetData |
|
from ai.utils.datetime_format import * |
|
from ai.utils.show_func import * |
|
from ai.utils.basic_func import * |
|
import datetime |
|
import random |
|
import json |
|
from openai import OpenAI |
|
|
|
|
|
class ShowAI: |
|
def __init__(self, cinema_info, show_date): |
|
self.cinema = cinema_info |
|
self.user_config = json.loads(cinema_info.user_config) |
|
self.show_date = show_date |
|
self.prompt = "" |
|
self.model = get_current_model() |
|
self.ai_model_name = self.model.model_name |
|
self.client = OpenAI( |
|
api_key=self.model.api_key, |
|
base_url=self.model.base_url, |
|
) |
|
|
|
# 生成提示词 |
|
# def general_prompt(self): |
|
# data = GetData(self.cinema) |
|
# # 准备各种日期时间 |
|
# history_start, history_end, target_start, target_end = get_data_datetime(self.show_date) |
|
# temp = datetime.datetime.strftime(data.get_template_date(history_start, history_end), '%Y-%m-%d') |
|
# history_date, target_date, target_date_is_holiday, template_date, template_date_is_holiday = get_date_type( |
|
# self.show_date, temp) |
|
# # print(history_start, history_end, target_start, target_end, history_date, show_date, template_date) |
|
# |
|
# # 开始处理提示词 |
|
# prompt_list = [] |
|
# # 处理排片数据 |
|
# pre_data = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='PreData')).first().prompt_val # 获取提示词模板 |
|
# pre_data = pre_data.replace('{history_show}', data.get_show_data(history_start, history_end)) # 处理排片数据 |
|
# pre_data = pre_data.replace('{history_sales}', data.get_sell_data(history_start, history_end)) # 处理销售数据 |
|
# pre_data = pre_data.replace('{target_already_show}', |
|
# '\t无' if data.get_show_data(target_start, |
|
# target_end) is False else data.get_show_data( |
|
# target_start, target_end)) # 处理目标日期已排场次数据 |
|
# pre_data = pre_data.replace('{reference_date}', history_date) # 处理参考日期 |
|
# pre_data = pre_data.replace('{template_date}', template_date) # 处理模板日期 |
|
# pre_data = pre_data.replace('{target_date}', target_date) # 处理目标日期 |
|
# prompt_list.append(pre_data) |
|
# |
|
# # 处理角色说明 |
|
# role = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Role')).first().prompt_val # 获取提示词模板 |
|
# role = role.replace('{template_date}', template_date) |
|
# prompt_list.append(role) |
|
# |
|
# # 处理影片和影厅信息 |
|
# movie_hall = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='MovieHall')).first().prompt_val # 获取提示词模板 |
|
# movie_hall = movie_hall.replace('{available_movie}', data.get_available_movie(self.show_date)) # 处理上映影片 |
|
# movie_hall = movie_hall.replace('{handle_movie}', data.get_handle_movie(self.show_date)) # 处理可排影片 |
|
# movie_hall = movie_hall.replace('{hall_info}', data.get_hall_media_type()) # 处理影厅信息 |
|
# free_hall = self.user_config['holiday_date_free_hall'] if target_date_is_holiday else self.user_config[ |
|
# 'holiday_date_free_hall'] |
|
# movie_hall = movie_hall.replace('{free_hall}', |
|
# '\t无' if len(free_hall) == 0 else '\n'.join( |
|
# f'\t{h};' for h in free_hall)) # 处理不可排片影厅 |
|
# prompt_list.append(movie_hall) |
|
# |
|
# # 影片热度和排片占比 |
|
# movie_hot = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Hot')).first().prompt_val # 获取提示词模板 |
|
# movie_hot = movie_hot.replace('{movie_hot}', '\n'.join(get_all_movie_hot_info(self.cinema, self.show_date))) |
|
# prompt_list.append(movie_hot) |
|
# |
|
# # 处理具体要求 |
|
# rules = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Rules')).first().prompt_val # 获取提示词模板 |
|
# rules = rules.replace('{template_date}', template_date) # 处理模板日期 |
|
# # 获取新片 |
|
# # new_movie_str_list = [] |
|
# # new_movie = data.get_new_movie(self.show_date) |
|
# # for new in new_movie: |
|
# # new_movie_str_list.append( |
|
# # f"\t增加{new}的场次,建议排{str(random.randint(2, 4))}场,可根据影片热度和口碑适当增加;") |
|
# # rules = rules.replace('{new_movie}', |
|
# # '\t无新片上映' if len(new_movie_str_list) == 0 else '\n'.join( |
|
# # new_movie_str_list)) # 处理新片数据 |
|
# prompt_list.append(rules) |
|
# |
|
# # 处理基本规则 |
|
# basic = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Basic')).first().prompt_val # 获取提示词模板 |
|
# start = self.user_config['holiday_date_start'] if target_date_is_holiday else self.user_config[ |
|
# 'work_date_start'] |
|
# end = self.user_config['holiday_date_end'] if target_date_is_holiday else self.user_config['work_date_end'] |
|
# basic = basic.replace('{start}', start) |
|
# basic = basic.replace('{end}', end) |
|
# prompt_list.append(basic) |
|
# |
|
# # 处理增减场次规则 |
|
# add_reduce = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='AddReduce')).first().prompt_val # 获取提示词模板 |
|
# prompt_list.append(add_reduce) |
|
# |
|
# # 处理黄金时段 |
|
# prime_time = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='PrimeTime')).first().prompt_val # 获取提示词模板 |
|
# prompt_list.append(prime_time) |
|
# |
|
# # 处理暑期档 |
|
# summer = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Summer')).first().prompt_val # 获取提示词模板 |
|
# prompt_list.append(summer) |
|
# |
|
# # 处理输出目标 |
|
# output = PromptTemplate.objects.filter( |
|
# Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Output')).first().prompt_val # 获取提示词模板 |
|
# prompt_list.append(output) |
|
# |
|
# # 打印提示词 |
|
# self.prompt = '\n'.join(prompt_list) |
|
# print(self.prompt) |
|
# # 关闭数据库连接 |
|
# data.exit() |
|
# return self.prompt |
|
|
|
# 生成提示词(对应V1.2版本) |
|
def general_prompt(self): |
|
data = GetData(self.cinema) |
|
# 准备各种日期时间 |
|
template_show, template_date = get_template_show_tomorrow(self.cinema) |
|
history_start, history_end, target_start, target_end = get_data_datetime_template(self.show_date, template_date) |
|
# temp = datetime.datetime.strftime(data.get_template_date(history_start, history_end), '%Y-%m-%d') |
|
history_date, target_date, target_date_is_holiday, template_date, template_date_is_holiday = get_date_type( |
|
self.show_date, template_date) |
|
# print(history_start, history_end, target_start, target_end, history_date, show_date, template_date) |
|
|
|
# 开始处理提示词 |
|
prompt_list = [] |
|
# 处理排片数据 |
|
pre_data = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='PreData')).first().prompt_val # 获取提示词模板 |
|
pre_data = pre_data.replace('{template_show}', template_show) # 处理排片数据 |
|
pre_data = pre_data.replace('{history_sales}', |
|
data.get_sell_data_by_movie(history_start, history_end)) # 处理销售数据 |
|
get_movie_hot_info_from_bi(self.cinema, self.show_date) # 获取全部影片的热度信息 |
|
movie_data_old = get_all_movie_hot_info('today', 'old') |
|
movie_data_new = get_all_movie_hot_info('today', 'new') |
|
|
|
pre_data = pre_data.replace('{hot_movie_old}', |
|
'\n'.join(movie_data_old) if len(movie_data_old) > 0 else '\t无') |
|
pre_data = pre_data.replace('{hot_movie_new}', |
|
'\n'.join(movie_data_new) if len(movie_data_new) > 0 else '\t无') |
|
if get_runtime_val('real_target') == '1': |
|
pre_data = pre_data.replace('{target_already_show}', |
|
'\t无' if data.get_show_data(target_start, |
|
target_end) is False else data.get_show_data( |
|
target_start, target_end)) # 处理目标日期已排场次数据 |
|
else: |
|
pre_data = pre_data.replace('{target_already_show}', '无') # 处理目标日期已排场次数据 |
|
# pre_data = pre_data.replace('{reference_date}', history_date) # 处理参考日期 |
|
pre_data = pre_data.replace('{template_date}', template_date) # 处理模板日期 |
|
pre_data = pre_data.replace('{target_date}', target_date) # 处理目标日期 |
|
prompt_list.append(pre_data) |
|
|
|
# 处理角色说明 |
|
role = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Role')).first().prompt_val # 获取提示词模板 |
|
role = role.replace('{template_date}', template_date) |
|
prompt_list.append(role) |
|
|
|
# 处理影片和影厅信息 |
|
movie_hall = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='MovieHall')).first().prompt_val # 获取提示词模板 |
|
# movie_hall = movie_hall.replace('{available_movie}', data.get_available_movie(self.show_date)) # 处理上映影片 |
|
movie_hall = movie_hall.replace('{handle_movie}', data.get_handle_movie(self.show_date)) # 处理可排影片 |
|
movie_hall = movie_hall.replace('{hall_info}', data.get_hall_media_type()) # 处理影厅信息 |
|
free_hall = self.user_config['holiday_date_free_hall'] if target_date_is_holiday else self.user_config[ |
|
'holiday_date_free_hall'] |
|
movie_hall = movie_hall.replace('{free_hall}', |
|
'\t无' if len(free_hall) == 0 else '\n'.join( |
|
f'\t{h};' for h in free_hall)) # 处理不可排片影厅 |
|
prompt_list.append(movie_hall) |
|
|
|
# 处理具体要求 |
|
rules = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Rules')).first().prompt_val # 获取提示词模板 |
|
rules = rules.replace('{template_date}', template_date) # 处理模板日期 |
|
rules = rules.replace('{available_movie}', data.get_available_movie(self.show_date)) # 处理上映影片 |
|
new_suggestion = get_new_suggestion(self.cinema, self.show_date) |
|
rules = rules.replace('{new_movie_suggestion}', '\t无' if new_suggestion is None else new_suggestion) # 处理新片推荐数量 |
|
prompt_list.append(rules) |
|
|
|
# 处理基本规则 |
|
basic = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Basic')).first().prompt_val # 获取提示词模板 |
|
start = self.user_config['holiday_date_start'] if target_date_is_holiday else self.user_config[ |
|
'work_date_start'] |
|
end = self.user_config['holiday_date_end'] if target_date_is_holiday else self.user_config['work_date_end'] |
|
basic = basic.replace('{start}', start) |
|
basic = basic.replace('{end}', end) |
|
prompt_list.append(basic) |
|
|
|
# 处理集团排片指导 |
|
movie_guidance = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Guide')).first().prompt_val # 获取提示词模板 |
|
guidance = get_movie_guidance(self.cinema, self.show_date) |
|
movie_guidance = movie_guidance.replace('{movie_guidance}', guidance) |
|
prompt_list.append(movie_guidance) |
|
|
|
# 处理增减场次规则 |
|
add_reduce = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='AddReduce')).first().prompt_val # 获取提示词模板 |
|
prompt_list.append(add_reduce) |
|
|
|
# 处理黄金时段 |
|
prime_time = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='PrimeTime')).first().prompt_val # 获取提示词模板 |
|
prompt_list.append(prime_time) |
|
|
|
# 处理暑期档 |
|
summer = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Summer')).first().prompt_val # 获取提示词模板 |
|
prompt_list.append(summer) |
|
|
|
# 处理输出目标 |
|
output = PromptTemplate.objects.filter( |
|
Q(del_flag=False) & Q(prompt_type='show') & Q(prompt_key='Output')).first().prompt_val # 获取提示词模板 |
|
prompt_list.append(output) |
|
|
|
# 打印提示词 |
|
self.prompt = '\n'.join(prompt_list) |
|
print('开始打印提示词\n', self.prompt) |
|
# 关闭数据库连接 |
|
data.exit() |
|
return self.prompt |
|
|
|
# 请求AI |
|
def get_show_result_ai(self, prompt=None): |
|
# 生成提示词 |
|
# self.general_prompt() |
|
# 与AI开始对话 |
|
print(f"与{self.ai_model_name}开始对话") |
|
response = self.client.chat.completions.create( |
|
model=self.ai_model_name, |
|
messages=[ |
|
{'role': 'system', 'content': '你是中文AI助手。要求:1.所有思考过程必须使用中文。2.所有tool reasoning必须使用中文。3.所有规划必须使用中文。4.即使用户使用英文提问,也必须中文推理。5.最终输出使用中文。'}, |
|
{'role': 'user', 'content': self.prompt if prompt is None else prompt} , |
|
], |
|
stream=False, |
|
reasoning_effort="high", |
|
extra_body={"thinking": {"type": "enabled"}}, |
|
response_format={ |
|
'type': 'json_object' |
|
} |
|
) |
|
res_json = response.model_dump_json() |
|
res_obj = json.loads(res_json) |
|
print(res_obj) |
|
return res_obj['choices'][0]['message']['content'], res_json, response.usage.model_dump_json()
|
|
|