Marsha基于 LLM 的编程语言

联合创作 · 2023-09-25 23:57

Marsha 是一种基于 LLM的编程语言。用简单的语法描述你想要完成的任务,提供使用示例,Marsha 编译器将指导 LLM 生成经过测试的 Python 软件。


可通过终端或 Jupyter Notebook 使用 pip 模块编译语法:



pip install git+https://github.com/alantech/marsha
python -m marsha data_mangling.mrsh


Marsha 语法看起来很像 markdown,是英语和数学符号的混合体。它有自己的文件格式 .mrsh,其中包含函数定义。由于 Marsha 目前处于 alpha 状态,因此语法可能会发生变化。


数据类型


数据类型提供函数类型安全性,有助于提高代码生成的准确性。数据类型格式几乎与 CSV 格式相同。




# type EmployeeSkills
name, skill
Bob, math
Jake, spreadsheets
Lisa, coding
Sue, spreadsheets


Marsha 还可以从 CSV 文件推断数据类型




# type EmployeesByDepartment employees_by_department.csv


Functions


函数是 Marsha 的面包和黄油,可以轻松定义不同数据类型之间的转换。Marsha 函数分为三个部分:声明、描述和示例。


声明是以为func前缀的一个 Markdown heading 部分,然后是名称、包含输入类型的括号,最后是冒号和输出类型。名称必须是单个单词,但类型不需要是经典软件类型,甚至不需要是上面定义的显式数据类型。它们本身可以是类型含义的简单描述。例如,




# func get_employee_skills(list of EmployeesByDepartment, list of DepartmentSkills): list of EmployeeSkills


下一部分是函数描述。在这里,你要解释函数应该做什么在这里,更明确地说明将减少生成输出的可变性,并提高行为的可靠性,但具体明确到什么程度,以及将多少程度留给 LLM 来决定。这类似于 SQL 和 HTML 等声明性语言,其中未指定的内容都有默认值,如select语句的排序顺序或<div>的默认样式。例如,




This function receives a list of EmployeesByDepartment and a list of DepartmentSkills. The function should be able to create a response of EmployeeSkills merging the 2 list by department. Use the pandas library.


最后一部分是示例部分。这里你提供了调用该函数的示例以及其输出应该是什么。Marsha 使用它向 LLM 提供更多信息以生成你想要的逻辑,而且还使用它生成测试套件来验证它生成的内容是否确实按照你的要求进行。这种反馈循环使 Marsha 比直接使用 LLM 本身更可靠。在某些方面,这类似于基于约束的编程语言,你可以在函数本身的定义中验证函数的行为,但它也没有基于约束的编程语言那么严格,它允许不完整的约束,而基于约束的语言在面对这种模糊性时会编译失败。例如




* get_employee_skills() = throws an error
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting')]) = throws an error
* get_employee_skills([], []) = []
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting')], []) = []
* get_employee_skills([], [DepartmentSkills('Accounting', 'math')]) = []
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting')], [DepartmentSkills('Accounting', 'math')]) = [EmployeeSkills('Joe', 'math')]
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting'), EmployeesByDepartment('Jake', 'Engineering')], [DepartmentSkills('Accounting', 'math')]) = [EmployeeSkills('Joe', 'math')]
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting'), EmployeesByDepartment('Jake', 'Engineering')], [DepartmentSkills('Accounting', 'math'), DepartmentSkills('Engineering', 'coding')]) = [EmployeeSkills('Joe', 'math'), EmployeeSkills('Jake', 'coding')]


这样就产生了




# func get_employee_skills(list of EmployeesByDepartment, list of DepartmentSkills): list of EmployeeSkills

This function receives a list of EmployeesByDepartment and a list of DepartmentSkills. The function should be able to create a response of EmployeeSkills merging the 2 list by department. Use the pandas library.

* get_employee_skills() = throws an error
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting')]) = throws an error
* get_employee_skills([], []) = []
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting')], []) = []
* get_employee_skills([], [DepartmentSkills('Accounting', 'math')]) = []
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting')], [DepartmentSkills('Accounting', 'math')]) = [EmployeeSkills('Joe', 'math')]
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting'), EmployeesByDepartment('Jake', 'Engineering')], [DepartmentSkills('Accounting', 'math')]) = [EmployeeSkills('Joe', 'math')]
* get_employee_skills([EmployeesByDepartment('Joe', 'Accounting'), EmployeesByDepartment('Jake', 'Engineering')], [DepartmentSkills('Accounting', 'math'), DepartmentSkills('Engineering', 'coding')]) = [EmployeeSkills('Joe', 'math'), EmployeeSkills('Jake', 'coding')]


目标


Marsha 语法的目的是:



  • 最小化和“obvious”,但也要防止信息不严谨或不完整而导致不可预测的行为

  • 可机械地解析语法高亮显示并快速向用户反馈正确性问题

  • 可以轻松定义示例,以减少生成错误代码的可能性,并允许生成应用程序代码可测试的测试用例

浏览 38
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报