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.
138 lines
6.9 KiB
138 lines
6.9 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 (get_data_datetime, get_date_type) |
|
import datetime |
|
import random |
|
import json |
|
from openai import OpenAI |
|
|
|
|
|
class ShowAI: |
|
def __init__(self, cinema_info, show_date): |
|
self.cinema = cinema_info |
|
# self.cinema_name = cinema_info.name |
|
# self.cinema_zz_code = cinema_info.zz_code |
|
# self.cinema_db_config = json.loads(cinema_info.db_config) |
|
self.user_config = json.loads(cinema_info.user_config) |
|
self.show_date = show_date |
|
self.prompt = "" |
|
self.client = OpenAI( |
|
api_key='sk-b7993bf4c8844e79bf84f08f07ff86d9', |
|
base_url='https://api.deepseek.com', |
|
) |
|
|
|
# 生成提示词 |
|
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}', |
|
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) |
|
|
|
# 处理具体要求 |
|
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 |
|
|
|
# 请求AI |
|
def get_show_result_ai(self): |
|
# 生成提示词 |
|
# self.general_prompt() |
|
# 与DeepSeek开始对话 |
|
print("与DeepSeek开始对话") |
|
response = self.client.chat.completions.create( |
|
model='deepseek-v4-pro', |
|
messages=[ |
|
{'role': 'system', 'content': 'You are a helpful assistant'}, |
|
{'role': 'user', 'content': self.prompt}, |
|
], |
|
stream=False, |
|
reasoning_effort="high", |
|
extra_body={"thinking": {"type": "enabled"}} |
|
) |
|
# print(dict(response)) |
|
# print(response.choices[0].message.content) |
|
return response.choices[0].message.content, response.model_dump_json(), response.usage.model_dump_json()
|
|
|