1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import utils,json
- from stores.mysql_store import MysqlStore
- from models.project_data import ProjectModel, ProjectItemModel
- from ai.fast_gpt import FastGPTAi
- class Process:
- def __init__(self):
- self._logger= utils.get_logger()
- self._store= MysqlStore()
- self._ai = FastGPTAi()
- self._data={}
- def run(self, project: ProjectModel) ->bool:
- try:
- self._logger.info(f"开始处理数据:{project.project_no}")
- self._store.update_project_status(project.id,22)
- project_items = self._store.query_project_items_by_project(project.id)
- text = self.prompt_template(project,project_items)
- items = self.call_ai_process(text)
- self._store.update_project_item_standard_no_batch(items)
- self._store.update_project_status(project.id,12)
- self._logger.info(f"处理数据完成:{project.project_no}")
- return True
- except Exception as e:
- self._logger.error(f"处理数据失败:{e}")
- self._store.update_project_status(project.id,12)
- return False
- @staticmethod
- def prompt_template(project, items) ->str:
- text = f"""
- 请分析提供的json数据,要求:
- 1. 根据工作内容、类型、项目名称、规格型号、数量、单位查找计算出定额编号
- 2. 工程的工作内容及类型:{project.work_content}"""
- text += """
- 3. 提供的数据结构化信息:```typescript
- export interface item {
- i: // ID
- n: string; //物料名称
- m: string; //型号规格
- u:string; //单位
- c: float; //数量
- }
- ```
- 4. 需返回的结构体:```typescript
- export interface item {
- i: int; // ID 与提供的ID保持一致
- s: string; // 定额编号
- }
- ```
- 5. 返回结构体item的数组的json数组格式,压缩成一行。
- 6. 数据如下:
- """
- item_ai_json = ProjectItemModel.list_to_ai_json(items)
- text += json.dumps(item_ai_json,ensure_ascii=False, separators=(',', ':'))
- return text
- def call_ai_process(self, message:str) ->list[ProjectItemModel]:
- try:
- api_key = utils.get_config_value("fastgpt.api_key_process")
- self._logger.info(f"开始调用AI:\n {message}")
- json_data = self._ai.call_ai(message,api_key)
- self._logger.info(f"AI返回结果:{json_data}")
- data=[]
- for item in json_data:
- data.append(ProjectItemModel(
- item_id=item["i"],
- standard_no=item["s"]
- ))
- return data
- except Exception as e:
- self._logger.error(f"AI调用失败:{e}")
- return []
|