3 Commit-ok d43671fc48 ... b2fe048a1a

Szerző SHA1 Üzenet Dátum
  jiaqiang b2fe048a1a 依赖包文件 1 hete
  jiaqiang e40017caa1 增加整体运行接口 1 hete
  jiaqiang 33eb449f78 run_complete_agent_flow方法增加返回值 1 hete
3 módosított fájl, 202 hozzáadás és 2 törlés
  1. 2 2
      llmops/complete_agent_flow_rule.py
  2. 152 0
      llmops/main.py
  3. 48 0
      requirements.txt

+ 2 - 2
llmops/complete_agent_flow_rule.py

@@ -638,7 +638,7 @@ async def main():
     industry = "农业"
 
     # 测试文件
-    file_name = "test_temp_agriculture_transaction_flow.csv"
+    file_name = "交易流水样例数据.csv"
     curr_dir = os.path.dirname(os.path.abspath(__file__))
     file_path = os.path.join(curr_dir, "..", "data_files", file_name)
 
@@ -665,7 +665,7 @@ async def main():
         print(f"   规划步骤: {summary.get('planning_steps', 0)}")
         print(f"   指标计算: {summary.get('metrics_computed', 0)}")
         print("🎉 测试成功!")
-
+    return result
 
 if __name__ == "__main__":
     import asyncio

+ 152 - 0
llmops/main.py

@@ -0,0 +1,152 @@
+from fastapi import FastAPI, File, UploadFile, Form
+import os
+from llmops.agents.data_classify_agent import DataClassifyAgent
+from llmops.agents.data_manager import DataManager
+import csv
+from fastapi.responses import StreamingResponse
+import io
+import urllib.parse
+from llmops.complete_agent_flow_rule import run_complete_agent_flow
+from llmops.config import DEEPSEEK_API_KEY
+
+# 初始化FastAPI应用
+app = FastAPI(
+    title="智能体服务<Agents API>",
+    description="提供数据分类打标服务",
+    version="1.0"
+)
+
+@app.get("/")
+def root():
+    return {"message": "Hello, Agents for you!"}
+
+# 设置文件保存路径
+UPLOAD_FOLDER = "uploads"
+os.makedirs(UPLOAD_FOLDER, exist_ok=True)
+
+
+
+data_classify_agent = DataClassifyAgent()
+
+@app.post("/api/dataset/classify")
+async def dataset_classify(file: UploadFile = File(...), industry: str = Form(...)):
+    """
+    上传原始数据文件(格式CSV)进行分类打标
+    Args:
+        file: 用户上传的CSV数据文件
+        industry: 行业
+    """
+    # 获取文件的存储路径
+    file_path = os.path.join(UPLOAD_FOLDER, file.filename)
+    full_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_path)
+
+    print(f"上传文件绝对路径:{full_path}")
+
+    try:
+        # 将文件内容写入到上传目录中
+        with open(full_path, "wb") as f:
+            f.write(file.file.read())
+
+        # 读取文件内容
+        data_set = DataManager.load_data_from_csv_file(full_path)
+        # 对数据进行分类打标
+        data_set_classified = data_classify_agent.invoke_data_classify(industry=industry, data_set=data_set, file_name=file.filename)
+        # 格式转换 [{}] -> csv 列表形式
+        # 生成器函数,用于逐行生成 CSV 内容
+        def generate_csv_rows():
+            # 创建内存中的字符串缓冲区
+            output = io.StringIO()
+            writer = csv.writer(output)
+            output.write('\ufeff')  # BOM头,确保Excel正确显示中文
+
+            # 写入表头
+            writer.writerow(data_classify_agent.fields_order)
+            yield output.getvalue()
+            output.seek(0)
+            output.truncate(0)
+            # 逐行写入数据
+            for row in data_set_classified:
+                writer.writerow([row["txId"], row["txDate"], row["txTime"], row["txAmount"], row["txBalance"],
+                                 row["txDirection"], row["txSummary"], row["txCounterparty"], row["createdAt"],
+                                 row["businessType"]])
+                yield output.getvalue()
+                output.seek(0)
+                output.truncate(0)
+
+        # 输出文件名(打好标文件)
+        output_file = str(file.filename).split(".")[0] + "_label.csv"
+
+        # 对文件名进行URL编码,处理中文等非ASCII字符
+        encoded_filename = urllib.parse.quote(output_file)
+
+        # 使用 StreamingResponse 返回 CSV 文件
+        return StreamingResponse(
+            generate_csv_rows(),
+            media_type="text/csv;charset=utf-8-sig",
+            headers={
+                "Content-Disposition": f"attachment; filename=\"{encoded_filename}\"; filename*=UTF-8''{encoded_filename}"
+            }
+        )
+    except Exception as e:
+        print(f"上传文件分类打标异常 {e}")
+        import traceback
+        traceback.print_exc()
+
+
+@app.post("/api/report/gen")
+async def gen_report(file: UploadFile = File(...), question: str = Form(...), industry: str = Form(...)):
+    """
+    上传原始数据文件(格式CSV),输入问题和行业,生成对应的分析报告
+    Args:
+        file: 用户上传的CSV数据文件
+        question: 用户问题
+        industry: 行业
+    Returns:
+        报告的JSON结构
+    """
+    # 获取文件的存储路径
+    file_path = os.path.join(UPLOAD_FOLDER, file.filename)
+    full_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_path)
+
+    print(f"上传文件绝对路径:{full_path}")
+
+    try:
+        # 将文件内容写入到上传目录中
+        with open(full_path, "wb") as f:
+            f.write(file.file.read())
+
+        # 读取文件内容,标准化文件
+        data_set = DataManager.load_data_from_csv_file(full_path)
+        # 执行测试
+        result = await run_complete_agent_flow(
+            question=question,
+            industry=industry,
+            data=data_set,
+            file_name=file.filename,
+            api_key=DEEPSEEK_API_KEY,
+            session_id="direct-test"
+        )
+        print(result)
+        return {
+            "status": 0,
+            "message": "success",
+            "outline_draft": result["result"]["outline_draft"],
+            "computed_metrics": result["result"]["computed_metrics"]
+        }
+    except Exception as e:
+        print(f"生成流水分析报告异常: {e}")
+        import traceback
+        traceback.print_exc()
+        return {
+            "status": 1,
+            "message": "error",
+            "report": {}
+        }
+
+
+
+
+
+if __name__ == "__main__":
+    import uvicorn
+    uvicorn.run("main:app", host="0.0.0.0", port=3699, reload=True)

+ 48 - 0
requirements.txt

@@ -0,0 +1,48 @@
+annotated-doc==0.0.4
+annotated-types==0.7.0
+anyio==4.12.0
+certifi==2025.11.12
+charset-normalizer==3.4.4
+click==8.3.1
+distro==1.9.0
+exceptiongroup==1.3.1
+fastapi==0.124.4
+h11==0.16.0
+httpcore==1.0.9
+httpx==0.28.1
+idna==3.11
+jiter==0.12.0
+jsonpatch==1.33
+jsonpointer==3.0.0
+langchain==1.1.3
+langchain-core==1.2.0
+langchain-openai==1.1.3
+langgraph==1.0.5
+langgraph-checkpoint==3.0.1
+langgraph-prebuilt==1.0.5
+langgraph-sdk==0.3.0
+langsmith==0.4.59
+nodeenv==1.9.1
+openai==2.11.0
+orjson==3.11.5
+ormsgpack==1.12.1
+packaging==25.0
+pydantic==2.12.5
+pydantic_core==2.41.5
+pyright==1.1.407
+PyYAML==6.0.3
+regex==2025.11.3
+requests==2.32.5
+requests-toolbelt==1.0.0
+sniffio==1.3.1
+starlette==0.50.0
+tenacity==9.1.2
+tiktoken==0.12.0
+tqdm==4.67.1
+typing-inspection==0.4.2
+typing_extensions==4.15.0
+urllib3==2.6.2
+uuid_utils==0.12.0
+uvicorn==0.38.0
+xxhash==3.6.0
+zstandard==0.25.0