code_generation.py 992 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. """
  2. 代码生成服务
  3. 提供基于LLM的代码生成功能
  4. """
  5. import logging
  6. from app.core.llm.llm_service import llm_client
  7. logger = logging.getLogger("app")
  8. def code_generate_standard(describe, relation):
  9. """
  10. 生成数据标准相关的代码
  11. Args:
  12. describe: 描述文本
  13. relation: 关系字典,包含输入和输出参数
  14. Returns:
  15. str: 生成的代码
  16. """
  17. try:
  18. prompt = f"""
  19. 请根据以下描述和参数生成一个标准的Python函数:
  20. 描述: {describe}
  21. 输入参数: {relation['输入参数']}
  22. 输出参数: {relation['输出参数']}
  23. 请提供标准实现的Python代码。
  24. """
  25. result = llm_client(prompt)
  26. return result if result else "代码生成失败,请重试"
  27. except Exception as e:
  28. logger.error(f"代码生成失败: {str(e)}")
  29. return f"代码生成错误: {str(e)}"