skip navigation
skip mega-menu

一个带有AutoGen代理的Cobol到Python转换器

In this story, we are going to explore a Cobol to Python converter written with Microsoft’s AutoGen framework. AutoGen 是一个基于python的框架,允许使用不同的会话模式编排多种类型的代理.

About AutoGen

AutoGen有以下几种基本代理:

  • ConversableAgent 这是具有基本功能的代理,也是所有其他代理的基类 AutoGen agents. 它包含从其他代理发送和接收消息的基本功能, to initiate or continue a conversation.

  • UserProxyAgent — is a proxy agent for humans, 默认情况下,在每个交互回合请求人工输入作为代理的应答,并且还具有执行代码和调用函数的能力. 我们将在转换器中使用这种类型的代理来启动和终止对话或执行一些代码.

  • AssistantAgent — the agent which interacts with the LLM and typically generates text. 它既不执行代码,也不与用户交互. 我们已经使用这个代理来生成Python代码、生成单元测试和代码审查.

You can also create a GroupChat which allows you to manage a group of AssistantAgent’s which interact with each other.

最后,AutoGen还允许创建您自己的自定义代理, 但是我们没有在这个转换器中使用这个功能.

这些对话模式可以是:

  • 用户代理和助理座席之间的双向聊天

  • 群组聊天涉及一个用户代理和一组助理座席

  • 多组聊天涉及多个相互交互的代理组

欲知详情,请浏览AutoGen网页: http://microsoft.github.io/autogen/docs/Use-Cases/agent_chat 

What is the goal of the Cobol Converter?

这个命令行工具的目标是读取Cobol文件,并通过文档和单元测试将其转换为Python代码. 它的第二个目标是将Python代码转换为基于REST的应用程序 FastAPI.

High-level workflow

How does the Cobol Converter work?

The Cobol converter uses two types of tools:

因此,我们结合了AutoGen代理,这些代理由LLM (gpt-4 - 1106预览版)和用于检查和格式化代码的传统工具支持.

在这个应用程序中有两个AutoGen代理集合(团队)在工作.

  • Cobol conversion team

  • REST conversion team

Agents Teams

Cobol Conversion Team

Cobol转换团队通过文档和单元测试将Cobol转换为Python. 除了接收用户输入的用户代理代理之外,它还有三个代理:

  • Cobol转换代理——负责使用LLM进行Cobol转换

  • 单元测试代理——用于使用LLM从Python代码生成单元测试

  • 代码审查器——用于审查转换后的Python代码和单元测试

REST Conversion Team

REST转换团队将转换后的Python代码作为输入,并将其转换为REST接口. 如果应用程序是某种命令行应用程序, 它变成了一个基于REST接口的应用程序 FastAPI.

Full workflow of the Cobol Agent

Cobol到Python的转换工作流由一个循环组成,该循环处理每个Cobol文件,并使用两个代理集成和传统工具(Black和Pylint)。.

下面是这个工作流的注释版本:

Full conversion workflow

Cobol转换器工作流有以下几个主要阶段:

  1. 初始循环处理目录中的每个Cobol文件.

  2. 这是Cobol转换团队的任务块. 在这个任务块中,Cobol代码被转换,单元测试被创建,代码被审查. When the Cobol Conversion Team stops, 它从代理中提取所有相关的Python代码或文本块.

  3. 在这个任务块中,代码审查被写入磁盘. Python代码也被格式化并写入磁盘. 代码也用Pylint进行分析,分析的结果被写入磁盘. 单元测试也被执行,其输出保存在一个文件中.

  4. 这是REST转换团队的任务块. 它有两个相互交互的代理:REST代码转换器和代码审查器. 在他们生成代码之后,Python代码将与代码审查一起被提取.

  5. 在这个块中,代码审查被写入磁盘, REST接口实际上是在进程中执行的——以查看代码是否编译并运行. 然后关闭该进程,格式化并写入磁盘. 然后使用Pylint来分析代码,并将分析结果写入磁盘.

在处理完所有Cobol文件后,转换器完成.

The Cobol Converter Output

For a small Cobol file like this one, you should get an output like this one:

Conversion output example

The files are:

  • rest_critique_write_student_2.txt - REST实现的代码审查

  • rest_write_student_2.py — The REST based implementation

  • rest_write_student_2.py_lint.REST实现的静态代码分析结果

  • test_write_student_2.py — The unit tests for the converted file

  • test_write_student_2.py_lint -转换文件的单元测试的静态代码分析报告

  • test_write_student_2.py_test_output.test_write_student_2的执行日志.py

  • write_student_2.py — The Cobol conversion file

  • write_student_2.py_lint.write_student_2的静态代码分析.py

如果您对转换后的文件感兴趣,请查看以下Google Drive链接: http://drive.google.com/drive/u/2/folders/1F7dqo5F2_zDzD8GcLlQFj70ZLdox5SL9 

Implementation

这个命令行工具的全部代码可以在这个存储库中找到: http://github.com/onepointconsulting/cobol-converter 

Installation, Configuration, Running

The Cobol code converter is a Python 3.11 application which requires Conda to be installed.

安装说明可以在 README of this project.

项目的配置依赖于 .env file, similar to the .env_local file that you can find in this project.

Cobol文件是从项目根文件夹下的目录中读取的. 该目录由SOURCE_CODE_DIR环境变量引用.

应用程序的主要入口点是这个文件: http://github.com/onepointconsulting/cobol-converter/blob/main/cobol_converter/cobol_converter_main.py

这个主入口点接受三个参数,它们决定如何写入输出文件:overwrite(覆盖输出文件), clear (clears the output files), Only_new(只写尚未翻译的文件)

Prompts

我们已经将代理提示从代码中分离出来. 所有代理和用户代理的提示都在这里 tool filehttp://github.com/onepointconsulting/cobol-converter/blob/main/prompts.toml 

以下是Cobol转换团队使用的一些提示符:

[agents]

   [agents.python_coder]

   system_message = """你是一个有用的人工智能助手.

You convert Cobol code into Python code. Please do not provide unit tests. 而是提供一个main方法来运行应用程序.

Also do not omit any code for brevity. We want to see the whole code."""

   [agents.python_unit_tester]

   system_message = """你是一个有用的人工智能助手.

您可以基于会话中的Python代码的单元测试库创建单元测试.

请将您正在测试的原始Python代码复制到您的响应中.

请确保导入单元测试库. Provide a main method to run the tests."""

   [agents.code_critic]

   system_message = """Critic. 你是一个有帮助的助手,在评估给定代码的质量方面非常熟练,可以提供从1(差)到10(好)的分数,同时提供清晰的基本原理. 您必须考虑为每个评估编写最佳实践. 具体来说,您可以在以下维度上仔细评估代码

—bugs (bug):是否有bug、逻辑错误、语法错误或打字错误? 有没有什么原因导致代码无法编译? How should it be fixed? 如果有bug存在,那么bug得分必须小于5分.

-目标遵从性(符合性):Cobol代码转换的好坏?

-数据编码(encoding):您能找到的单元测试有多好?


你必须为以上每一个维度提供一个分数.

{bug: 0,转换:0,遵从性:0,类型:0,编码:0,美观:0}

Do not suggest code.

Finally, based on the critique above, 建议编码员应该采取哪些具体的行动来改进代码.

如果单元测试已经可用并且看起来正常,则回复终止"""

Agent Teams Setup

代理团队设置在这两个文件中:

Cobol转换团队在这个文件中设置: http://github.com/onepointconsulting/cobol-converter/blob/main/cobol_converter/service/agent_setup.py 

REST转换团队在此文件中设置: http://github.com/onepointconsulting/cobol-converter/blob/main/cobol_converter/service/agent_rest_setup.py 

Main Workflow Implementation

工作流的主要实现可以在这个文件中找到: http://github.com/onepointconsulting/cobol-converter/blob/main/cobol_converter/service/cobol_conversion_service.py 

下面的链接指向处理每个Cobol文件并执行Cobol到Python转换的方法, Unit test generation, formatting, 静态代码分析和REST接口创建

http://github.com/onepointconsulting/cobol-converter/blob/main/cobol_converter/service/cobol_conversion_service.py#L94 

有关代码的更多详细信息,请查看 the code repository.

Takeaways

Cobol Converter是可以在短时间内将Cobol转换为Python的第一步. 较小的、较简单的程序可以正确地翻译,甚至可以执行.g. the write_student_2 example). However more complicated programmes (like e.g. (井字游戏)在翻译成Python时在功能上是不相等的(i.e. you could start the program, 但游戏是不可玩的)——即使输出的语法是正确的.

The LLM we used was gpt-4–1106-preview. We have not used any other models. 可能有其他模型在将Cobol转换为Python方面做得更好.

Ways to improve the conversion

我们可以想出几种方法来提高转化率:

  • 一个经过微调的LLM,专门研究Cobol方言的转换. 这将是非常重要的,特别是如果你打算做很多转换.

  • 在脚本生成过程中建立人工反馈. 人工反馈团队应该至少由一名精通Cobol和目标语言的开发人员组成. No matter how good an LLM is these days, 它们仍然可能以意想不到的方式或与您的业务目标不一致的方式生成代码. 如果人类开发人员的反馈可以输入到代码生成过程中,这将是非常有价值的.

如果您选择对模型进行微调,那么随着时间的推移,您将最终能够通过使用任何成功转换的结果作为新数据来微调您的LLM来改进您的模型.

Gil Fernandes, Onepoint Consulting

Subscribe to our newsletter

Sign up here