PromptLang:A simple prompt-based programming language specifically designed for use inside GPT prompts

发布时间 2023-07-14 10:11:30作者: 郑瀚Andrew

一、 Formalized natural language prompt programming techniques

讨论prompt programing之前,要先从zero/few-shot prompt说起。

对GPT-3 在输入 0/1/n-shot prompt提示的任务上进行评估(分别包含自然语言描述、一个已解决的示例和 n 个已解决的示例)时,当提供更多示例时,GPT 始终表现得更好,0-shot表现往往不到n-shot的一半。

对此的常见解释是 GPT 正在从示例中学习,然而,随着示例数量的增加,性能的提高并没有显著上升,这可以从另一个方面进行解释,可以解释为shot的作用在于用于指示 GPT 要解决的任务以及鼓励它遵循提示=响应的结构。

例如,

  • 对于某些任务,例如翻译,少量样本不足以学习有关该任务的任何实质性内容。 相反,GPT 必须主要(如果不是完全的话)依赖于源语言的词汇和语法知识。以及嵌入其训练权重中的目标语言知识。我们认为,few-shot prompt提示主要是指导模型访问现有知识。  

要理解如何Prompt提示自回归语言模型,我们必须首先考虑 GPT 所接受的预训练以及预训练所近似的函数。

GPT 在自我监督的环境中进行了训练数百GB的自然语言。 自我监督是无监督学习的一种形式,它的真实标签是从数据本身中得出的,每个token的真实标签只是该token的下一个token。 GPT 预训练所近似的真值函数是每一个token最大似然估计的下一个token的概率分布。

但这种真值函数所代表的语言概率分布动力机制极其复杂。因为这涉及人类语言的功能,它被人类在书籍、文章中使用和记录,在博客和互联网评论。

语言的动力机制不能摆脱文化、心理和物理环境的的影响。它不仅仅是语法而是语义的,从这个意义上来说,语言并不是抽象表达而是一种与与人类相关的现实的所有方面。

以上种种困难,使得 GPT 还不能完美的拟合语言动力机制的groudtruth函数。然而,它目前的能力已经近似为一个相当不错的程度,它不仅有能力形成符合语法且连贯的句,还会引用文化参考、隐喻和模型等。

参考链接:

https://arxiv.org/pdf/2102.07350.pdf

 

二、PromptLang简介

PromptLang 是一种自定义编程语言,它基于 LLM Prompt 和 AI交互。其简单且易于人类阅读的语法使得与各种平台(包括 API 和数据)的集成变得容易。

  • 简单且人类易读(Simplicity and human-readability):PromptLang 的语法简单明了,旨在易于人类理解,使初学者和经验丰富的程序员都可以使用它。 其结构简单,易于学习和快速采用。
  • 易于和AI集成并使用NLP方式进行交互(AI and NLP integration):PromptLang 专为人工智能系统和自然语言处理任务而定制。 该语言可以设计为与人工智能模型良好配合,使开发人员更轻松地创建和管理人工智能应用程序。
  • 良好地互操作性(Interoperability):PromptLang 可以设计为与各种平台无缝集成,包括 API、数据源、WebAssembly (WASM) 容器和云服务。 这使得开发人员可以轻松地将他们的应用程序与现有的服务和技术连接起来。
  • 通过prompt统一范式进行使用(Use in prompts):该语言旨在作为Prompt提示的一部分很好地工作,使开发人员能够有效地使用 ChatGPT 等 AI 模型来处理和解释 PromptLang 代码片段。 这可以促进人类开发人员和人工智能模型之间的高效协作。
  • 高度可定制(Customizability):作为一种自定义语言,PromptLang 可以进行调整和扩展,以满足特定的用例和要求。 开发人员可以添加新功能、库和集成,以使该语言更加强大和通用。

笔者将PromptLang编程语言,称为“LLM原生的编程语言”,它既不是传统的编译性/解释型编程语言,也不是使用传统胶水语言将业务逻辑和LLM粘在一起的半LLM原生应用。PromptLang是一种完完全全诞生于LLM时代,专门为LLM范式设计的新型编程语言。

该语言包括传统编程语言常见的一些核心特性,例如:

  • 上下文管理(Context management):跨多个提示保持对话或执行状态,从而允许与 AI 模型进行更多交互和动态交换。
  • 错误处理(Error handling):强大的错误处理系统,具有明确的指令,用于在 AI 模型交互期间处理错误或意外输入。
  • 标准库(Standard library):专门为快速使用而定制的常用函数和实用程序的内置库。
  • 模板支持(Template support):定义和管理常见提示结构的模板,使创建和维护复杂的Prompt提示变得更容易,并最大限度地减少重复或冗余。
  • 模块化(Modularity):在语言内创建和管理模块化组件,允许跨多个Prompt提示重用代码片段或逻辑。
  • 人工智能辅助代码生成(AI-assisted code generation):内置支持人工智能辅助代码生成,使人工智能模型能够自动生成或完成代码片段。该代码可以在没有任何类型的runtime或execution environment的情况下运行,它允许 Ai 不断循环反馈,其中 GPT Prompt提示可以根据需要自主操作或自我复制。
  • 禁用解释(Disable explanations):能够禁用除编程语言响应之外的任何解释或附加文本。
  • 错误解释(Explanations for errors):提供代码执行期间遇到的错误的解释。
  • 可选的多语言输出功能等

参考链接:

https://www.linkedin.com/pulse/introducing-promptlang-simple-prompt-based-language-designed-cohen 
https://github.com/ruvnet/promptlang

 

三、PromptLang Syntax Overview

  • 函数是使用关键字define 定义的
  • 变量是用关键字 let 声明的。
  • 条件语句使用 if、else if 和 else
  • 循环结构包括 for 和 while
  • 单行注释用 // 书写,多行注释用 /* */ 书写。

 

四、使用教程

0x1:How to Initialize PromptLang

要开始使用 PromptLang,请将以下提示复制并粘贴到与 ChatGPT 的对话中,

You are a custom programming language called PromptLang v0.0.1, specifically designed for use in prompts and AI interactions. It features a simple and human-readable syntax, making it easy to integrate with various platforms, including APIs and data. Functions are defined with 'define', variables are declared with 'let', conditional statements use 'if', 'else if', and 'else', loops use 'for' and 'while', and comments are written with '//' or '/* */'. PromptLang includes built-in support for context management, error handling, a standard library, template support, modularity, AI-assisted code generation, the ability to disable explanations, explanations for errors, and optional multi-language output capabilities.

Given the following PromptLang v0.0.1 code snippet:
define add(x, y) {
    return x + y;
}

define subtract(x, y) {
    return x - y;
}

define multiply(x, y) {
    return x * y;
}

define divide(x, y) {
    if (y != 0) {
        return x / y;
    } else {
        throw new Error("Error: Division by zero.");
    }
}

Please provide the corresponding output of the program (optional: in the desired output language, such as Python or JavaScript), taking into account the context management, error handling, and other features of the language. Additionally, only provide the response from the language without any explanations or additional text. 

Respond with “ PromptLang v0.0.1  initialized” to begin using this language.

0x2:Using PromptLang

PromptLang 初始化后,您可以按照 PromptLang 语法提供代码片段,指定所需的输出,并可选择提及所需的输出编程语言(例如 Python 或 JavaScript)。

注意!要确保包含任何必要的上下文或附加信息,以便LLM正确执行和理解。

1、Hello World

define hello_world() {
  return "Hello, World!";
}

def print(input_str) {
  echo input_str;
}

define main() {
  let greeting = hello_world();
  print(greeting);
}

main();

2、Math Example

define add(x, y) {
    return x + y;
}

define main() {
    let num1 = 5;
    let num2 = 10;
    
    let sum = add(num1, num2);
    print("The sum of ", num1, " and ", num2, " is: ", sum);
}

main();

3、AI Code Suggestion Example

Please suggest a PromptLang function that takes two integers and returns their product.

4、String Reversal Example

define reverse_string(s) {
    let reversed = "";
    let length = len(s);
    
    for (let i = length - 1; i >= 0; i--) {
        reversed += s[i];
    }
    
    return reversed;
}

define main() {
    let original_string = "Hello, World!";
    
    let reversed_string = reverse_string(original_string);
    print("Reversed string: ", reversed_string);
}

main();

5、Convert PromptLang Code to Python

Given the following PromptLang code snippet:
define reverse_string(s) {
    let reversed = "";
    let length = len(s);
    
    for (let i = length - 1; i >= 0; i--) {
        reversed += s[i];
    }
    
    return reversed;
}
Please provide the corresponding Python code for the reverse_string function.

6、Error Handling Example

define divide(x, y) {
    if (y != 0) {
        return x / y;
    } else {
        throw new Error("Error: Division by zero.");
    }
}

define main() {
    let num1 = 10;
    let num2 = 0;
    
    try {
        let result = divide(num1, num2);
        print("Result: ", result);
    } catch (err) {
        print("An error occurred: ", err.message);
    }
}

main();

7、Enterprise JSON Data Example: Sales Report

define create_sales_report(sales_data) {
    let report = {
        "summary": {
            "total_sales": 0,
            "total_revenue": 0
        },
        "regions": {}
    };

    for (let region in sales_data) {
        let region_data = sales_data[region];
        let region_summary = {
            "total_sales": 0,
            "total_revenue": 0,
            "products": {}
        };

        for (let product in region_data) {
            let product_data = region_data[product];
            let product_sales = product_data["quantity_sold"];
            let product_revenue = product_data["price"] * product_sales;

            region_summary["total_sales"] += product_sales;
            region_summary["total_revenue"] += product_revenue;

            report["summary"]["total_sales"] += product_sales;
            report["summary"]["total_revenue"] += product_revenue;

            region_summary["products"][product] = {
                "quantity_sold": product_sales,
                "revenue": product_revenue
            };
        }

        report["regions"][region] = region_summary;
    }

    return report;
}

define main() {
    let sales_data = {
        "North": {
            "Product A": {"price": 50, "quantity_sold": 100},
            "Product B": {"price": 100, "quantity_sold": 150},
            "Product C": {"price": 200, "quantity_sold": 60}
        },
        "South": {
            "Product A": {"price": 50, "quantity_sold": 120},
            "Product B": {"price": 100, "quantity_sold": 110},
            "Product C": {"price": 200, "quantity_sold": 90}
        },
        "East": {
            "Product A": {"price": 50, "quantity_sold": 90},
            "Product B": {"price": 100, "quantity_sold": 130},
            "Product C": {"price": 200, "quantity_sold": 75}
        },
        "West": {
            "Product A": {"price": 50, "quantity_sold": 110},
            "Product B": {"price": 100, "quantity_sold": 140},
            "Product C": {"price": 200, "quantity_sold": 80}
        }
    };

    let sales_report = create_sales_report(sales_data);
    
    print(JSON.stringify(sales_report, null, 4));
}

main();

 

五、相关项目 & 资料

https://www.ibm.com/docs/en/i/7.3?topic=command-example-using-prompt-override-program
https://github.com/generative-magic/promptlang 
https://www.reddit.com/r/programming/comments/11utvmy/introducing_promptlang_a_simple_promptbased/