|
| 1 | +# coding=utf-8 |
| 2 | +import base64 |
| 3 | +import time |
| 4 | +from functools import reduce |
| 5 | +from imghdr import what |
| 6 | +from typing import List, Dict |
| 7 | + |
| 8 | +from django.db.models import QuerySet |
| 9 | +from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage, AIMessage |
| 10 | + |
| 11 | +from application.flow.i_step_node import NodeResult, INode |
| 12 | +from application.flow.step_node.video_understand_step_node.i_video_understand_node import IVideoUnderstandNode |
| 13 | +from knowledge.models import File |
| 14 | +from models_provider.tools import get_model_instance_by_model_workspace_id |
| 15 | + |
| 16 | + |
| 17 | +def _write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow, answer: str): |
| 18 | + chat_model = node_variable.get('chat_model') |
| 19 | + message_tokens = node_variable['usage_metadata']['output_tokens'] if 'usage_metadata' in node_variable else 0 |
| 20 | + answer_tokens = chat_model.get_num_tokens(answer) |
| 21 | + node.context['message_tokens'] = message_tokens |
| 22 | + node.context['answer_tokens'] = answer_tokens |
| 23 | + node.context['answer'] = answer |
| 24 | + node.context['history_message'] = node_variable['history_message'] |
| 25 | + node.context['question'] = node_variable['question'] |
| 26 | + node.context['run_time'] = time.time() - node.context['start_time'] |
| 27 | + if workflow.is_result(node, NodeResult(node_variable, workflow_variable)): |
| 28 | + node.answer_text = answer |
| 29 | + |
| 30 | + |
| 31 | +def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INode, workflow): |
| 32 | + """ |
| 33 | + 写入上下文数据 (流式) |
| 34 | + @param node_variable: 节点数据 |
| 35 | + @param workflow_variable: 全局数据 |
| 36 | + @param node: 节点 |
| 37 | + @param workflow: 工作流管理器 |
| 38 | + """ |
| 39 | + response = node_variable.get('result') |
| 40 | + answer = '' |
| 41 | + for chunk in response: |
| 42 | + answer += chunk.content |
| 43 | + yield chunk.content |
| 44 | + _write_context(node_variable, workflow_variable, node, workflow, answer) |
| 45 | + |
| 46 | + |
| 47 | +def write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow): |
| 48 | + """ |
| 49 | + 写入上下文数据 |
| 50 | + @param node_variable: 节点数据 |
| 51 | + @param workflow_variable: 全局数据 |
| 52 | + @param node: 节点实例对象 |
| 53 | + @param workflow: 工作流管理器 |
| 54 | + """ |
| 55 | + response = node_variable.get('result') |
| 56 | + answer = response.content |
| 57 | + _write_context(node_variable, workflow_variable, node, workflow, answer) |
| 58 | + |
| 59 | + |
| 60 | +def file_id_to_base64(file_id: str): |
| 61 | + file = QuerySet(File).filter(id=file_id).first() |
| 62 | + file_bytes = file.get_bytes() |
| 63 | + base64_video = base64.b64encode(file_bytes).decode("utf-8") |
| 64 | + return [base64_video, what(None, file_bytes)] |
| 65 | + |
| 66 | + |
| 67 | +class BaseVideoUnderstandNode(IVideoUnderstandNode): |
| 68 | + def save_context(self, details, workflow_manage): |
| 69 | + self.context['answer'] = details.get('answer') |
| 70 | + self.context['question'] = details.get('question') |
| 71 | + if self.node_params.get('is_result', False): |
| 72 | + self.answer_text = details.get('answer') |
| 73 | + |
| 74 | + def execute(self, model_id, system, prompt, dialogue_number, dialogue_type, history_chat_record, stream, chat_id, |
| 75 | + model_params_setting, |
| 76 | + chat_record_id, |
| 77 | + video, |
| 78 | + **kwargs) -> NodeResult: |
| 79 | + # 处理不正确的参数 |
| 80 | + if video is None or not isinstance(video, list): |
| 81 | + video = [] |
| 82 | + workspace_id = self.workflow_manage.get_body().get('workspace_id') |
| 83 | + video_model = get_model_instance_by_model_workspace_id(model_id, workspace_id, |
| 84 | + **model_params_setting) |
| 85 | + # 执行详情中的历史消息不需要图片内容 |
| 86 | + history_message = self.get_history_message_for_details(history_chat_record, dialogue_number) |
| 87 | + self.context['history_message'] = history_message |
| 88 | + question = self.generate_prompt_question(prompt) |
| 89 | + self.context['question'] = question.content |
| 90 | + # 生成消息列表, 真实的history_message |
| 91 | + message_list = self.generate_message_list(video_model, system, prompt, |
| 92 | + self.get_history_message(history_chat_record, dialogue_number), video) |
| 93 | + self.context['message_list'] = message_list |
| 94 | + self.context['video_list'] = video |
| 95 | + self.context['dialogue_type'] = dialogue_type |
| 96 | + if stream: |
| 97 | + r = video_model.stream(message_list) |
| 98 | + return NodeResult({'result': r, 'chat_model': video_model, 'message_list': message_list, |
| 99 | + 'history_message': history_message, 'question': question.content}, {}, |
| 100 | + _write_context=write_context_stream) |
| 101 | + else: |
| 102 | + r = video_model.invoke(message_list) |
| 103 | + return NodeResult({'result': r, 'chat_model': video_model, 'message_list': message_list, |
| 104 | + 'history_message': history_message, 'question': question.content}, {}, |
| 105 | + _write_context=write_context) |
| 106 | + |
| 107 | + def get_history_message_for_details(self, history_chat_record, dialogue_number): |
| 108 | + start_index = len(history_chat_record) - dialogue_number |
| 109 | + history_message = reduce(lambda x, y: [*x, *y], [ |
| 110 | + [self.generate_history_human_message_for_details(history_chat_record[index]), |
| 111 | + self.generate_history_ai_message(history_chat_record[index])] |
| 112 | + for index in |
| 113 | + range(start_index if start_index > 0 else 0, len(history_chat_record))], []) |
| 114 | + return history_message |
| 115 | + |
| 116 | + def generate_history_ai_message(self, chat_record): |
| 117 | + for val in chat_record.details.values(): |
| 118 | + if self.node.id == val['node_id'] and 'video_list' in val: |
| 119 | + if val['dialogue_type'] == 'WORKFLOW': |
| 120 | + return chat_record.get_ai_message() |
| 121 | + return AIMessage(content=val['answer']) |
| 122 | + return chat_record.get_ai_message() |
| 123 | + |
| 124 | + def generate_history_human_message_for_details(self, chat_record): |
| 125 | + for data in chat_record.details.values(): |
| 126 | + if self.node.id == data['node_id'] and 'video_list' in data: |
| 127 | + video_list = data['video_list'] |
| 128 | + if len(video_list) == 0 or data['dialogue_type'] == 'WORKFLOW': |
| 129 | + return HumanMessage(content=chat_record.problem_text) |
| 130 | + file_id_list = [video.get('file_id') for video in video_list] |
| 131 | + return HumanMessage(content=[ |
| 132 | + {'type': 'text', 'text': data['question']}, |
| 133 | + *[{'type': 'video_url', 'video_url': {'url': f'./oss/file/{file_id}'}} for file_id in file_id_list] |
| 134 | + |
| 135 | + ]) |
| 136 | + return HumanMessage(content=chat_record.problem_text) |
| 137 | + |
| 138 | + def get_history_message(self, history_chat_record, dialogue_number): |
| 139 | + start_index = len(history_chat_record) - dialogue_number |
| 140 | + history_message = reduce(lambda x, y: [*x, *y], [ |
| 141 | + [self.generate_history_human_message(history_chat_record[index]), |
| 142 | + self.generate_history_ai_message(history_chat_record[index])] |
| 143 | + for index in |
| 144 | + range(start_index if start_index > 0 else 0, len(history_chat_record))], []) |
| 145 | + return history_message |
| 146 | + |
| 147 | + def generate_history_human_message(self, chat_record): |
| 148 | + |
| 149 | + for data in chat_record.details.values(): |
| 150 | + if self.node.id == data['node_id'] and 'video_list' in data: |
| 151 | + video_list = data['video_list'] |
| 152 | + if len(video_list) == 0 or data['dialogue_type'] == 'WORKFLOW': |
| 153 | + return HumanMessage(content=chat_record.problem_text) |
| 154 | + video_base64_list = [file_id_to_base64(video.get('file_id')) for video in video_list] |
| 155 | + return HumanMessage( |
| 156 | + content=[ |
| 157 | + {'type': 'text', 'text': data['question']}, |
| 158 | + *[{'type': 'video_url', |
| 159 | + 'video_url': {'url': f'data:video/{base64_video[1]};base64,{base64_video[0]}'}} for |
| 160 | + base64_video in video_base64_list] |
| 161 | + ]) |
| 162 | + return HumanMessage(content=chat_record.problem_text) |
| 163 | + |
| 164 | + def generate_prompt_question(self, prompt): |
| 165 | + return HumanMessage(self.workflow_manage.generate_prompt(prompt)) |
| 166 | + |
| 167 | + def generate_message_list(self, video_model, system: str, prompt: str, history_message, video): |
| 168 | + if video is not None and len(video) > 0: |
| 169 | + # 处理多张图片 |
| 170 | + videos = [] |
| 171 | + for img in video: |
| 172 | + if isinstance(img, str) and img.startswith('http'): |
| 173 | + videos.append({'type': 'video_url', 'video_url': {'url': img}}) |
| 174 | + else: |
| 175 | + file_id = img['file_id'] |
| 176 | + file = QuerySet(File).filter(id=file_id).first() |
| 177 | + video_bytes = file.get_bytes() |
| 178 | + base64_video = base64.b64encode(video_bytes).decode("utf-8") |
| 179 | + video_format = what(None, video_bytes) |
| 180 | + videos.append( |
| 181 | + {'type': 'video_url', 'video_url': {'url': f'data:video/{video_format};base64,{base64_video}'}}) |
| 182 | + messages = [HumanMessage( |
| 183 | + content=[ |
| 184 | + {'type': 'text', 'text': self.workflow_manage.generate_prompt(prompt)}, |
| 185 | + *videos |
| 186 | + ])] |
| 187 | + else: |
| 188 | + messages = [HumanMessage(self.workflow_manage.generate_prompt(prompt))] |
| 189 | + |
| 190 | + if system is not None and len(system) > 0: |
| 191 | + return [ |
| 192 | + SystemMessage(self.workflow_manage.generate_prompt(system)), |
| 193 | + *history_message, |
| 194 | + *messages |
| 195 | + ] |
| 196 | + else: |
| 197 | + return [ |
| 198 | + *history_message, |
| 199 | + *messages |
| 200 | + ] |
| 201 | + |
| 202 | + @staticmethod |
| 203 | + def reset_message_list(message_list: List[BaseMessage], answer_text): |
| 204 | + result = [{'role': 'user' if isinstance(message, HumanMessage) else 'ai', 'content': message.content} for |
| 205 | + message |
| 206 | + in |
| 207 | + message_list] |
| 208 | + result.append({'role': 'ai', 'content': answer_text}) |
| 209 | + return result |
| 210 | + |
| 211 | + def get_details(self, index: int, **kwargs): |
| 212 | + return { |
| 213 | + 'name': self.node.properties.get('stepName'), |
| 214 | + "index": index, |
| 215 | + 'run_time': self.context.get('run_time'), |
| 216 | + 'system': self.node_params.get('system'), |
| 217 | + 'history_message': [{'content': message.content, 'role': message.type} for message in |
| 218 | + (self.context.get('history_message') if self.context.get( |
| 219 | + 'history_message') is not None else [])], |
| 220 | + 'question': self.context.get('question'), |
| 221 | + 'answer': self.context.get('answer'), |
| 222 | + 'type': self.node.type, |
| 223 | + 'message_tokens': self.context.get('message_tokens'), |
| 224 | + 'answer_tokens': self.context.get('answer_tokens'), |
| 225 | + 'status': self.status, |
| 226 | + 'err_message': self.err_message, |
| 227 | + 'video_list': self.context.get('video_list'), |
| 228 | + 'dialogue_type': self.context.get('dialogue_type') |
| 229 | + } |
0 commit comments