|
| 1 | +# coding=utf-8 |
| 2 | +from functools import reduce |
| 3 | +from typing import List |
| 4 | + |
| 5 | +from langchain_core.messages import BaseMessage, HumanMessage, AIMessage |
| 6 | + |
| 7 | +from application.flow.i_step_node import NodeResult |
| 8 | +from application.flow.step_node.image_generate_step_node.i_image_generate_node import IImageGenerateNode |
| 9 | +from setting.models_provider.tools import get_model_instance_by_model_user_id |
| 10 | + |
| 11 | + |
| 12 | +class BaseImageGenerateNode(IImageGenerateNode): |
| 13 | + def save_context(self, details, workflow_manage): |
| 14 | + self.context['answer'] = details.get('answer') |
| 15 | + self.context['question'] = details.get('question') |
| 16 | + self.answer_text = details.get('answer') |
| 17 | + |
| 18 | + def execute(self, model_id, prompt, negative_prompt, dialogue_number, dialogue_type, history_chat_record, chat_id, |
| 19 | + chat_record_id, |
| 20 | + **kwargs) -> NodeResult: |
| 21 | + |
| 22 | + tti_model = get_model_instance_by_model_user_id(model_id, self.flow_params_serializer.data.get('user_id')) |
| 23 | + history_message = self.get_history_message(history_chat_record, dialogue_number) |
| 24 | + self.context['history_message'] = history_message |
| 25 | + question = self.generate_prompt_question(prompt) |
| 26 | + self.context['question'] = question |
| 27 | + message_list = self.generate_message_list(question, history_message) |
| 28 | + self.context['message_list'] = message_list |
| 29 | + self.context['dialogue_type'] = dialogue_type |
| 30 | + print(message_list) |
| 31 | + print(negative_prompt) |
| 32 | + image_urls = tti_model.generate_image(question, negative_prompt) |
| 33 | + self.context['image_list'] = image_urls |
| 34 | + answer = '\n'.join([f"" for path in image_urls]) |
| 35 | + return NodeResult({'answer': answer, 'chat_model': tti_model, 'message_list': message_list, |
| 36 | + 'history_message': history_message, 'question': question}, {}) |
| 37 | + |
| 38 | + def generate_history_ai_message(self, chat_record): |
| 39 | + for val in chat_record.details.values(): |
| 40 | + if self.node.id == val['node_id'] and 'image_list' in val: |
| 41 | + if val['dialogue_type'] == 'WORKFLOW': |
| 42 | + return chat_record.get_ai_message() |
| 43 | + return AIMessage(content=val['answer']) |
| 44 | + return chat_record.get_ai_message() |
| 45 | + |
| 46 | + def get_history_message(self, history_chat_record, dialogue_number): |
| 47 | + start_index = len(history_chat_record) - dialogue_number |
| 48 | + history_message = reduce(lambda x, y: [*x, *y], [ |
| 49 | + [self.generate_history_human_message(history_chat_record[index]), |
| 50 | + self.generate_history_ai_message(history_chat_record[index])] |
| 51 | + for index in |
| 52 | + range(start_index if start_index > 0 else 0, len(history_chat_record))], []) |
| 53 | + return history_message |
| 54 | + |
| 55 | + def generate_history_human_message(self, chat_record): |
| 56 | + |
| 57 | + for data in chat_record.details.values(): |
| 58 | + if self.node.id == data['node_id'] and 'image_list' in data: |
| 59 | + image_list = data['image_list'] |
| 60 | + if len(image_list) == 0 or data['dialogue_type'] == 'WORKFLOW': |
| 61 | + return HumanMessage(content=chat_record.problem_text) |
| 62 | + return HumanMessage(content=data['question']) |
| 63 | + return HumanMessage(content=chat_record.problem_text) |
| 64 | + |
| 65 | + def generate_prompt_question(self, prompt): |
| 66 | + return self.workflow_manage.generate_prompt(prompt) |
| 67 | + |
| 68 | + def generate_message_list(self, question: str, history_message): |
| 69 | + return [ |
| 70 | + *history_message, |
| 71 | + question |
| 72 | + ] |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def reset_message_list(message_list: List[BaseMessage], answer_text): |
| 76 | + result = [{'role': 'user' if isinstance(message, HumanMessage) else 'ai', 'content': message.content} for |
| 77 | + message |
| 78 | + in |
| 79 | + message_list] |
| 80 | + result.append({'role': 'ai', 'content': answer_text}) |
| 81 | + return result |
| 82 | + |
| 83 | + def get_details(self, index: int, **kwargs): |
| 84 | + return { |
| 85 | + 'name': self.node.properties.get('stepName'), |
| 86 | + "index": index, |
| 87 | + 'run_time': self.context.get('run_time'), |
| 88 | + 'history_message': [{'content': message.content, 'role': message.type} for message in |
| 89 | + (self.context.get('history_message') if self.context.get( |
| 90 | + 'history_message') is not None else [])], |
| 91 | + 'question': self.context.get('question'), |
| 92 | + 'answer': self.context.get('answer'), |
| 93 | + 'type': self.node.type, |
| 94 | + 'message_tokens': self.context.get('message_tokens'), |
| 95 | + 'answer_tokens': self.context.get('answer_tokens'), |
| 96 | + 'status': self.status, |
| 97 | + 'err_message': self.err_message, |
| 98 | + 'image_list': self.context.get('image_list'), |
| 99 | + 'dialogue_type': self.context.get('dialogue_type') |
| 100 | + } |
0 commit comments