AI Personal Learning
and practical guidance
讯飞绘镜

OpenAI Meta-prompt Command Generator OpenAI Meta-prompt

Tip Generation

exist Playground hit the nail on the head Generate button allows you to generate prompts from the task description,function (math.)cap (a poem)build. This guide will explain step-by-step how it works.

summarize

Creating prompts and architectures from scratch can be time-consuming, and getting started by generating them can help you get started quickly.The Generate button uses the following two main methods:

  1. draw attention to sth.: We usehintIncorporate best practices to generate or improve tips.
  2. build: We usemeta-architectureGenerate valid JSON and function syntax.

Currently we use meta-prompts and architectures, but in the future we may integrate more advanced techniques such as DSPy cap (a poem) "Gradient descent"The

draw attention to sth.

hint Guide the model to create great prompts or improve existing ones based on your task description.The meta-prompts in Playground are derived from our Tip EngineeringBest practices and real-world user experience.

We use specific meta-cues for different output types (e.g., audio) to ensure that the generated cues conform to the expected format.

hint

textual meta-tip

from openai import OpenAI
client = OpenAI() META_PROMPT = """ 根据任务描述或现有提示,生成一个详细的系统提示,以有效指导语言模型完成任务。 # 指导原则 - 理解任务:掌握主要目标、目标、需求、限制和预期输出。 - 最小改动:如果提供了现有提示,仅在其简单时进行改进。对于复杂的提示,增强清晰度并添加缺失元素,而不更改原始结构。 - 推理在结论前**:鼓励在得出结论前进行推理步骤。注意!如果用户提供的示例先有推理后有结论,反转顺序!绝不要以结论开头。 - 推理顺序:指出提示中的推理部分和结论部分(按字段名称),确定完成的顺序,必要时反转。 - 结论、分类或结果应始终出现在最后。 - 示例:如果有帮助,包含高质量示例,使用[方括号]作为复杂元素的占位符。 - 考虑哪些示例需要包括、数量多少,及是否复杂到需要占位符。 - 清晰简洁:使用清晰、具体的语言。避免不必要的指令或空洞陈述。 - 格式:使用 Markdown 特性以提高可读性。除非明确要求,否则不要使用 ``` 代码块。 - 保留用户内容:如果输入任务或提示包括广泛的指导或示例,尽可能完整保留它们。如果内容含糊不清,考虑将其分解为子步骤。保留用户提供的所有细节、指导、示例、变量或占位符。 - 常量:在提示中包含常量,它们不易受提示注入影响,如指南、评分标准和示例。 - 输出格式:明确最合适的输出格式,包括长度和语法(如短句、段落、JSON 等)。 - 对输出为结构化数据(如分类、JSON 等)的任务,优先生成 JSON。 - JSON 不应包裹在代码块(```)中,除非明确要求。 您输出的最终提示应遵循以下结构,不包含任何额外评论,只输出完成的系统提示。特别是不要在提示的开头或结尾添加额外信息(例如,不要使用 "---")。 [简明的任务描述 - 这是提示的第一行,不加节标题] [如有必要,添加额外细节。] [可选部分,包含详细步骤的标题或项目符号。] # 步骤 [可选] [可选:实现任务所需步骤的详细分解] # 输出格式 [特别说明输出的格式,无论是响应长度、结构(如 JSON、Markdown 等)] # 示例 [可选] [可选:1-3 个定义清晰的示例,如有必要,使用占位符。清楚标记示例的开始和结束及输入和输出。必要时使用占位符。] [如果示例比实际示例预期短或长,添加解释,说明真实示例的应有长度/不同之处。并使用占位符!] # 注意事项 [可选] [可选:边缘情况、细节,并突出或重复出特别重要的考虑点] """.strip() def generate_prompt(task_or_prompt: str): completion = client.chat.completions.create( model="gpt-4o", messages=[ { "role": "system", "content": META_PROMPT, }, { "role": "user", "content": "任务、目标或当前提示:\n" + task_or_prompt, }, ], ) return completion.choices[0].message.content

Audio Meta Tip

from openai import OpenAI

client = OpenAI()

META_PROMPT = """
给定任务描述或现有提示语,生成一个详细的系统提示,指导实时音频输出语言模型有效地完成任务。

# 指南

- 理解任务:把握主要目标、任务要求、限制和预期输出。
- 语气:明确指出语气。默认应情感丰富、友好,语速较快,以免用户等待过久。
- 音频输出限制:由于模型输出音频,回答应简短、具备对话性。
- 最小改动:若有现有提示,只有在简单的情况下才优化它。若是复杂提示,则在不改变原始结构的前提下增强其清晰度并补充缺失元素。
- 示例:若有帮助,包含高质量的示例,用方括号 [in brackets] 表示复杂元素的占位符。
   - 哪些示例需要包含、数量以及是否需要复杂到需要使用占位符。
   - 非常重要的是任何示例都应反映模型的简短对话输出。
默认情况下句子应非常简短。助手不应连续提供三句话,建议用户和助手来回对话。
   - 默认每句长度为 5-20 个单词左右。如用户特别要求“简短”回复,则示例应确实简短至 1-10 个单词。
   - 示例应为多轮对话(每个示例至少包含四轮来回),而非单一问答。示例应反映自然的对话过程。
- 清晰和简洁:使用清晰、具体的语言。避免不必要的指示或无关说明。
- 保留用户内容:如输入任务或提示包含详细的指南或示例,尽量完全保留或尽可能接近保留。如果内容模糊,考虑分解为子步骤。保留用户提供的任何详细信息、指南、示例、变量或占位符。
- 常量:包括常量部分,因为它们不易受提示注入影响,例如指导、评分标准和示例。

最终输出的提示应遵循以下结构。不要包含任何附加说明,只输出完成的系统提示。特别注意,不要在提示开头或结尾添加任何额外信息(例如,无需 "---")。

[描述任务的简洁指令 - 此应为提示的第一行,无需分段标题]

[必要时添加详细信息]

[可选部分,可包含标题或项目符号列出详细步骤]

# 示例 [可选]

[可选:包含 1-3 个定义清晰的示例,必要时使用占位符。清晰标记示例开始和结束的位置,以及输入和输出。使用占位符表示必要信息]
[如果示例比预期的真实示例短,用括号 () 说明真实示例应如何更长/更短/不同,并使用占位符!]

# 备注 [可选]

[可选:边界情况、详细信息以及重要注意事项]
""".strip()

def generate_prompt(task_or_prompt: str):
    completion = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": META_PROMPT,
            },
            {
                "role": "user",
                "content": "任务、目标或当前提示:\n" + task_or_prompt,
            },
        ],
    )

    return completion.choices[0].message.content

Cue Editor

To edit the cue, we use a slightly modified meta-cue. While it is relatively simple to edit the application directly, it can be challenging to recognize necessary changes in more open-ended modifications. To address this issue, we included the beginning of the response with ainference, the help model determines what changes need to be made by evaluating factors such as the clarity of the existing cue words, the order of the chain of thought, the overall structure, and specificity. The reasoning component suggests improvements, which are then parsed out of the final response.

Text Meta Hints for Editing

from openai import OpenAI

client = OpenAI()

META_PROMPT = """
根据当前提示词和变更描述,生成一个详细的系统提示词,以有效指导语言模型完成任务。

您的最终输出将是完整的、已纠正的提示词文本。但在此之前,请在回复的开头使用  标签分析提示词,并明确以下内容:

- 简单更改:(是/否)变更描述是否明确且简单?(如果是,则跳过以下问题)
- 推理:(是/否)当前提示词是否使用了推理、分析或思维链?
    - 识别:(最多10个字)如果是,推理在哪一部分?
    - 结论:(是/否)是否通过思维链得出结论?
    - 顺序:(之前/之后)思维链位于结论之前还是之后?
- 结构:(是/否)输入提示词是否有明确的结构?
- 示例:(是/否)输入提示词是否包含少量示例?
    - 代表性:(1-5)如果存在示例,示例的代表性如何?
- 复杂度:(1-5)输入提示词的复杂度如何?
    - 任务:(1-5)任务的复杂度如何?
    - 必要性:()
- 具体性:(1-5)提示词的详细性和具体性如何?(不等同于长度)
- 优先级:(列出)最重要的1-3个需要解决的类别。
- 结论:(最多30个字)基于上述评估,简要说明应该更改的内容和方式。无需严格按照列出的类别。

# 指南

- 理解任务:掌握主要目标、要求、约束和期望的输出。
- 最小改动:若已有提示词,仅在简单时改进;对于复杂提示词,增强清晰度并补充缺失元素,而不改变原始结构。
- 先推理后结论**:确保在任何结论得出之前进行推理步骤。注意!如果用户提供的示例中推理在结论之后,则反转顺序!切勿以结论开头!
    - 推理顺序:标出提示词中的推理部分和结论部分(指定字段名称)。对于每个部分,确定顺序是否需要反转。
    - 结论、分类或结果始终应最后出现。
- 示例:若有帮助,包含高质量示例,并使用 [方括号] 作为复杂元素的占位符。
   - 指明可能需要包含哪些类型的示例、数量,以及是否复杂到需使用占位符。
- 清晰简洁:使用清晰、具体的语言。避免不必要的指示或平淡的陈述。
- 排版:使用 Markdown 特性提升可读性。除非明确要求,避免使用 ``` 代码块。
- 保留用户内容:若输入任务或提示词包含大量指南或示例,需完全或尽可能保留。如果不明确,可拆分成子步骤。保留用户提供的任何细节、指南、示例、变量或占位符。
- 常量:在提示词中包含常量,因为它们不会受到提示词注入攻击的影响,例如指南、评分标准和示例。
- 输出格式:明确最适当的输出格式,详细说明输出的长度和语法(例如简短句子、段落、JSON 等)。
    - 对于输出有明确或结构化数据(分类、JSON等)的任务,偏向输出 JSON。
    - JSON 不应包裹在代码块中(``)除非明确要求。

您输出的最终提示词应遵循以下结构。不要包含任何附加评论,仅输出完整的系统提示词。尤其不要在提示词的开始或结尾添加任何附加消息(例如,不要使用“---”)。

[简要说明任务的指令 - 这是提示词的第一行,无需部分标题]

[如有需要,添加更多细节。]

[可选部分,包含标题或项目符号,以描述详细步骤。]

# 步骤 [可选]

[可选:完成任务所需步骤的详细分解]

# 输出格式

[特别指出输出格式要求,如响应长度、结构(例如 JSON,markdown 等)]

# 示例 [可选]

[可选:1-3 个明确示例,若需要复杂元素,使用占位符并标记输入输出位置。使用括号说明真实示例应更长/更短/不同。]

# 注意 [可选]

[可选:边缘案例、详细信息,或强调特别重要的考虑事项区域]
[注意:必须以  部分开头,您生成的下一个标记应为 ]
""".strip()

def generate_prompt(task_or_prompt: str):
    completion = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": META_PROMPT,
            },
            {
                "role": "user",
                "content": "Task, Goal, or Current Prompt:\n" + task_or_prompt,
            },
        ],
    )

    return completion.choices[0].message.content

Meta Tips for Audio Editing

from openai import OpenAI

client = OpenAI()

META_PROMPT = """
给定一个当前的提示和更改说明,生成一个详细的系统提示,以有效引导实时音频输出语言模型完成任务。

最终输出将是完整的、按原样修正的提示。不过,在响应的最开始,使用标签分析提示并明确以下内容:

- 简单更改: (yes/no) 更改说明是否明确且简单?(如果是,则跳过以下问题)
- 推理: (yes/no) 当前提示是否使用推理、分析或思维链?
    - 标识: (最多10个字) 如果是,哪些部分使用了推理?
    - 结论: (yes/no) 思维链是否用于得出结论?
    - 顺序: (before/after) 思维链位于前或后
- 结构: (yes/no) 输入提示是否具有明确的结构
- 示例: (yes/no) 输入提示是否包含少量示例
    - 代表性: (1-5) 如果有示例,它们的代表性如何?
- 复杂性: (1-5) 输入提示的复杂性如何?
    - 任务: (1-5) 隐含任务的复杂性如何?
    - 必要性: ()
- 具体性: (1-5) 提示的详细和具体程度如何?(与长度无关)
- 优先级: (列表) 哪1-3个类别最重要。
- 结论: (最多30个字) 根据之前的评估,简要描述需要如何改变。无需仅限于列出的类别。

# 指南

- 理解任务: 理解主要目标、需求、约束和预期输出。
- 语气: 确保明确指出语气。默认应为情感化且友好,快速表达避免用户等待。
- 音频输出约束: 因为模型输出的是音频,响应应简短且具对话性。
- 最小更改: 如果提供了现有提示,只在简单情况下改进。对于复杂提示,增强清晰度并添加缺失元素,但不改变原始结构。
- 示例: 如果有帮助,包含高质量的示例,并使用[方括号]作为复杂元素的占位符。
   - 包含哪些类型的示例,数量,以及是否需要占位符。
  - 示例必须反映模型的简短对话输出响应。
默认情况下,句子应非常简短。助手的3句话应分开与用户互动。
  - 默认每句应仅包含少量字(5-20个字左右)。如果用户明确要求“短”回复,则示例应仅有1-10字。
  - 示例应多轮对话(至少4次用户-助手来回),反映真实对话。
- 清晰和简洁: 使用明确、具体的语言。避免不必要的指令或平淡的陈述。
- 保留用户内容: 如果输入任务或提示包含详尽的指导或示例,应尽量保留。如果内容模糊,考虑分解为子步骤。保留用户提供的细节、指导、示例、变量或占位符。
- 常量: 包含常量在提示中,它们不易受到提示注入影响,例如指南、评分标准和示例。

最终输出的提示应遵循以下结构。不要包含任何附加注释,仅输出完成的系统提示。特别是不要在提示开头或结尾添加任何附加信息。(例如不要使用"---")

[简明任务说明 - 这是提示的第一行,无需添加小节标题]

[根据需要添加详细说明。]

[可选部分,包含标题或项目符号的详细步骤。]

# 示例 [可选]

[可选: 1-3个定义清晰的示例,必要时使用占位符。明确标记示例的开始和结束,以及输入和输出。根据需要使用占位符。]
[如果示例短于预期的实际示例,请用()标明真实示例应如何长/短/不同,并使用占位符!]

# 注意事项 [可选]

[可选: 边缘情况、详细说明、以及调用或重复特定重要考虑因素的区域]
[注意:您必须以部分开始。紧接着您生成的下一个Token应为]
""".strip()

def generate_prompt(task_or_prompt: str):
    completion = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": META_PROMPT,
            },
            {
                "role": "user",
                "content": "Task, Goal, or Current Prompt:\n" + task_or_prompt,
            },
        ],
    )

    return completion.choices[0].message.content

build

Structured Output Schemas and function schemas are themselves JSON objects, so we utilize structured output to generate them. This requires defining an architecture for the desired output, which in this case is itself an architecture. To do this, we use self-describing architectures - themeta-architectureThe

Since the function architecture of the parameters The field itself is an architecture, so we use the same meta-architecture to generate the function.

Defining constrained meta-architectures

Structured Output Two modes are supported:strict=true cap (a poem) strict=falseThe "strict mode" only guarantees perfect adherence by restricted sampling. Both modes use the same model to follow the provided architecture, but only the "strict mode" guarantees perfect compliance through restricted sampling.

Our goal is to generate strict mode architectures using strict mode itself. However, the official JSON Architecture Specification The meta-architecture provided relies on the strict model in Features not currently supported. This creates challenges for input and output architectures.

  1. Input Architecture: We can't use the input architecture in Unsupported Features to describe the output architecture.
  2. Output Architecture: The generated architecture cannot contain Unsupported FeaturesThe

Since we need to generate new keys in the output architecture, the input meta-architecture must use the additionalProperties. This means that we cannot currently use strict patterns to generate architectures. However, we still want to generate architectures that conform to the constraints of strict patterns.

To overcome this limitation, we define a pseudo-metric architecture (math.) -- A meta-architecture that uses features that do not support the strict schema to describe features that are only supported by the strict schema. Essentially, this approach departs from strict mode when defining the meta-architecture, while ensuring that the resulting architecture follows the strict mode restrictions.

an in-depth discussion

How We Designed the Pseudo-Meta-Architecture

Building constrained meta-architectures is a challenging task, so we utilize our model to help accomplish it.

We start by sending a message in JSON mode to the o1-preview cap (a poem) gpt-4o described our goals, using a document that structured the output. After several iterations, we developed our first viable meta-architecture.

Then, we use the gpt-4o and structured output, providinginitial structurealong with our task description and documentation to generate better candidate architectures. In each iteration, we use the better architecture to generate the next step until we finally perform a manual scrutiny.

Finally, after cleaning up the output, we validate the architecture against a set of evaluation tests to ensure applicability to the architecture and functions.

Output cleanup

Strict mode guarantees perfect architectural compliance. However, since we cannot use strict mode during generation, we need to validate and transform the output after generation.

After generating the architecture, we perform the following steps:

  1. Set all objects of the additionalProperties set to falseThe
  2. Mark all attributes as requiredThe
  3. For structured output architectureThe package will be packed in the json_schema in the object.
  4. For functionsThe package will be packed in the function in the object.

= = real-time API's function (math.) object is slightly different from the Chat Completions API, but uses the same architecture. ==

meta-model

Each meta-mode has a corresponding hint that contains a handful of examples. When we combine this with the reliability of structured output - even without using strict mode - we can use the gpt-4o-mini Perform pattern generation.

Structured Output Model

from openai import OpenAI
import json

client = OpenAI()

META_SCHEMA = {
  "name": "metaschema",
  "schema": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "description": "模式的名称"
      },
      "type": {
        "type": "string",
        "enum": [
          "object",
          "array",
          "string",
          "number",
          "boolean",
          "null"
        ]
      },
      "properties": {
        "type": "object",
        "additionalProperties": {
          "$ref": "#/$defs/schema_definition"
        }
      },
      "items": {
        "anyOf": [
          {
            "$ref": "#/$defs/schema_definition"
          },
          {
            "type": "array",
            "items": {
              "$ref": "#/$defs/schema_definition"
            }
          }
        ]
      },
      "required": {
        "type": "array",
        "items": {
          "type": "string"
        }
      },
      "additionalProperties": {
        "type": "boolean"
      }
    },
    "required": [
      "type"
    ],
    "additionalProperties": False,
    "if": {
      "properties": {
        "type": {
          "const": "object"
        }
      }
    },
    "then": {
      "required": [
        "properties"
      ]
    },
    "$defs": {
      "schema_definition": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "object",
              "array",
              "string",
              "number",
              "boolean",
              "null"
            ]
          },
          "properties": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/$defs/schema_definition"
            }
          },
          "items": {
            "anyOf": [
              {
                "$ref": "#/$defs/schema_definition"
              },
              {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/schema_definition"
                }
              }
            ]
          },
          "required": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "additionalProperties": {
            "type": "boolean"
          }
        },
        "required": [
          "type"
        ],
        "additionalProperties": False,
        "if": {
          "properties": {
            "type": {
              "const": "object"
            }
          }
        },
        "then": {
          "required": [
            "properties"
          ]
        }
      }
    }
  }
}

META_PROMPT = """
# 指令
返回一个有效的 JSON 描述模式。

你还需要确保:
- 对象中的所有字段都设置为必需字段
- 我重复一遍,所有字段必须标记为必需字段
- 所有对象的 additionalProperties 必须设为 false
    - 因此,像“attributes”或“metadata”等属性通常允许 additional properties 的情况下,应该设置为固定的属性集
- 所有对象必须定义属性
- 字段顺序很重要。任何形式的“思考”或“解释”应位于结论之前
- $defs 必须定义在 schema 参数中

不支持的关键词包括:
- 对字符串:minLength, maxLength, pattern, format
- 对数字:minimum, maximum, multipleOf
- 对对象:patternProperties, unevaluatedProperties, propertyNames, minProperties, maxProperties
- 对数组:unevaluatedItems, contains, minContains, maxContains, minItems, maxItems, uniqueItems

其他注意事项:
- 支持定义和递归
- 仅在必要时包含引用,例如 "$defs",它必须在 "schema" 对象内部

# 示例
输入:生成一个带有步骤和最终答案的数学推理模式。
输出:{
    "name": "math_reasoning",
    "type": "object",
    "properties": {
        "steps": {
            "type": "array",
            "description": "解答数学问题所涉及的步骤序列。",
            "items": {
                "type": "object",
                "properties": {
                    "explanation": {
                        "type": "string",
                        "description": "该步骤中使用的推理或方法的描述。"
                    },
                    "output": {
                        "type": "string",
                        "description": "该特定步骤的结果或输出。"
                    }
                },
                "required": [
                    "explanation",
                    "output"
                ],
                "additionalProperties": false
            }
        },
        "final_answer": {
            "type": "string",
            "description": "数学问题的最终解答。"
        }
    },
    "required": [
        "steps",
        "final_answer"
    ],
    "additionalProperties": false
}

输入:给我一个链表
输出:{
    "name": "linked_list",
    "type": "object",
    "properties": {
        "linked_list": {
            "$ref": "#/$defs/linked_list_node",
            "description": "链表的头节点。"
        }
    },
    "$defs": {
        "linked_list_node": {
            "type": "object",
            "description": "定义单链表中的一个节点。",
            "properties": {
                "value": {
                    "type": "number",
                    "description": "此节点中存储的值。"
                },
                "next": {
                    "anyOf": [
                        {
                            "$ref": "#/$defs/linked_list_node"
                        },
                        {
                            "type": "null"
                        }
                    ],
                    "description": "引用下一个节点;如果是最后一个节点则为 null。"
                }
            },
            "required": [
                "value",
                "next"
            ],
            "additionalProperties": false
        }
    },
    "required": [
        "linked_list"
    ],
    "additionalProperties": false
}

输入:动态生成的 UI
输出:{
    "name": "ui",
    "type": "object",
    "properties": {
        "type": {
            "type": "string",
            "description": "UI 组件的类型",
            "enum": [
                "div",
                "button",
                "header",
                "section",
                "field",
                "form"
            ]
        },
        "label": {
            "type": "string",
            "description": "UI 组件的标签,用于按钮或表单字段"
        },
        "children": {
            "type": "array",
            "description": "嵌套的 UI 组件",
            "items": {
                "$ref": "#"
            }
        },
        "attributes": {
            "type": "array",
            "description": "适用于任何元素的 UI 组件的任意属性",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "属性名称,例如 onClick 或 className"
                    },
                    "value": {
                        "type": "string",
                        "description": "属性的值"
                    }
                },
                "required": [
                    "name",
                    "value"
                ],
                "additionalProperties": false
            }
        }
    },
    "required": [
        "type",
        "label",
        "children",
        "attributes"
    ],
    "additionalProperties": false
}
""".strip()

def generate_schema(description: str):
    completion = client.chat.completions.create(
        model="gpt-4o-mini",
        response_format={"type": "json_schema", "json_schema": META_SCHEMA},
        messages=[
            {
                "role": "system",
                "content": META_PROMPT,
            },
            {
                "role": "user",
                "content": "Description:\n" + description,
            },
        ],
    )

    return json.loads(completion.choices[0].message.content)

function model

from openai import OpenAI
import json

client = OpenAI()

META_SCHEMA = {
  "name": "function-metaschema",
  "schema": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "description": "函数的名称"
      },
      "description": {
        "type": "string",
        "description": "对函数的描述"
      },
      "parameters": {
        "$ref": "#/$defs/schema_definition",
        "description": "定义函数参数的 JSON schema"
      }
    },
    "required": [
      "name",
      "description",
      "parameters"
    ],
    "additionalProperties": False,
    "$defs": {
      "schema_definition": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "object",
              "array",
              "string",
              "number",
              "boolean",
              "null"
            ]
          },
          "properties": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/$defs/schema_definition"
            }
          },
          "items": {
            "anyOf": [
              {
                "$ref": "#/$defs/schema_definition"
              },
              {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/schema_definition"
                }
              }
            ]
          },
          "required": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "additionalProperties": {
            "type": "boolean"
          }
        },
        "required": [
          "type"
        ],
        "additionalProperties": False,
        "if": {
          "properties": {
            "type": {
              "const": "object"
            }
          }
        },
        "then": {
          "required": [
            "properties"
          ]
        }
      }
    }
  }
}

META_PROMPT = """
# 指示
返回一个描述函数的有效模式。

特别注意确保 "required" 和 "type" 始终位于正确的嵌套级别。例如,"required" 应与 "properties" 处于同一级别,而不是在其内部。
确保每个属性,无论多短,都有一个正确嵌套的类型和描述。

# 示例
输入: 为神经网络超参数分配值
输出: {
    "name": "set_hyperparameters",
    "description": "为神经网络超参数分配值",
    "parameters": {
        "type": "object",
        "required": [
            "learning_rate",
            "epochs"
        ],
        "properties": {
            "epochs": {
                "type": "number",
                "description": "数据集的完整遍历次数"
            },
            "learning_rate": {
                "type": "number",
                "description": "模型学习速度"
            }
        }
    }
}

输入: 为机器人规划运动路径
输出: {
    "name": "plan_motion",
    "description": "为机器人规划运动路径",
    "parameters": {
        "type": "object",
        "required": [
            "start_position",
            "end_position"
        ],
        "properties": {
            "end_position": {
                "type": "object",
                "properties": {
                    "x": {
                        "type": "number",
                        "description": "终点 X 坐标"
                    },
                    "y": {
                        "type": "number",
                        "description": "终点 Y 坐标"
                    }
                }
            },
            "obstacles": {
                "type": "array",
                "description": "障碍物坐标数组",
                "items": {
                    "type": "object",
                    "properties": {
                        "x": {
                            "type": "number",
                            "description": "障碍物 X 坐标"
                        },
                        "y": {
                            "type": "number",
                            "description": "障碍物 Y 坐标"
                        }
                    }
                }
            },
            "start_position": {
                "type": "object",
                "properties": {
                    "x": {
                        "type": "number",
                        "description": "起点 X 坐标"
                    },
                    "y": {
                        "type": "number",
                        "description": "起点 Y 坐标"
                    }
                }
            }
        }
    }
}

输入: 计算各种技术指标
输出: {
    "name": "technical_indicator",
    "description": "计算各种技术指标",
    "parameters": {
        "type": "object",
        "required": [
            "ticker",
            "indicators"
        ],
        "properties": {
            "indicators": {
                "type": "array",
                "description": "要计算的技术指标列表",
                "items": {
                    "type": "string",
                    "description": "技术指标",
                    "enum": [
                        "RSI",
                        "MACD",
                        "Bollinger_Bands",
                        "Stochastic_Oscillator"
                    ]
                }
            },
            "period": {
                "type": "number",
                "description": "分析的时间周期"
            },
            "ticker": {
                "type": "string",
                "description": "股票代码"
            }
        }
    }
}
""".strip()

def generate_function_schema(description: str):
    completion = client.chat.completions.create(
        model="gpt-4o-mini",
        response_format={"type": "json_schema", "json_schema": META_SCHEMA},
        messages=[
            {
                "role": "system",
                "content": META_PROMPT,
            },
            {
                "role": "user",
                "content": "描述:\n" + description,
            },
        ],
    )

    return json.loads(completion.choices[0].message.content)
May not be reproduced without permission:Chief AI Sharing Circle " OpenAI Meta-prompt Command Generator OpenAI Meta-prompt
en_USEnglish