代理提示器

提示器(Prompter)是 UFO 框架中的一个关键组件,负责为大型语言模型(LLM)构建提示以生成响应。提示器ufo/prompts文件夹中实现。每个代理都有自己的提示器类,用于定义提示的结构以及要提供给 LLM 的信息。

组件

提供给 LLM 的提示通常是一个字典列表,其中每个字典包含以下键:

描述
角色 提示中文本的角色,可以是systemuserassistant
内容 特定角色的文本内容。

提示

您可能会发现官方文档对构建提示很有帮助。

提示器类的__init__方法中,您可以定义每个组件的提示模板,并通过prompt_construction方法组合每个组件的模板来构建最终的提示消息。

系统提示

系统提示使用在config_dev.yaml文件中为每个代理配置的模板。它通常包含代理角色、操作、提示、响应格式等说明。您需要使用system_prompt_construction方法来构建系统提示。

关于 API 指令和演示示例的提示也包含在系统提示中,它们分别通过api_prompt_helperexamples_prompt_helper方法构建。以下是系统提示的子组件:

组件 描述 方法
API 代理的 API 指令。 api_prompt_helper
示例 代理的演示示例。 examples_prompt_helper

用户提示

用户提示是根据代理的观察、外部知识和Blackboard中的信息构建的。您可以使用user_prompt_construction方法来构建用户提示。以下是用户提示的子组件:

组件 描述 方法
观察 代理的观察。 user_content_construction
retrieved_docs 从外部知识库检索到的知识。 retrived_documents_prompt_helper
黑板 存储在Blackboard中的信息。 blackboard_to_prompt

参考

您可以在ufo/prompts文件夹中找到提示器的实现。以下是提示器类的基本结构:

基类:ABC

BasicPrompter 类是提示器的抽象类。

初始化 BasicPrompter。

参数
  • is_visual (bool) –

    请求是否针对视觉模型。

  • prompt_template (str) –

    提示模板的路径。

  • example_prompt_template (str) –

    示例提示模板的路径。

源代码位于prompter/basic.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(
    self, is_visual: bool, prompt_template: str, example_prompt_template: str
):
    """
    Initialize the BasicPrompter.
    :param is_visual: Whether the request is for visual model.
    :param prompt_template: The path of the prompt template.
    :param example_prompt_template: The path of the example prompt template.
    """
    self.is_visual = is_visual
    if prompt_template:
        self.prompt_template = self.load_prompt_template(prompt_template, is_visual)
    else:
        self.prompt_template = ""
    if example_prompt_template:
        self.example_prompt_template = self.load_prompt_template(
            example_prompt_template, is_visual
        )
    else:
        self.example_prompt_template = ""

api_prompt_helper()

一个辅助函数,用于构建提示的 API 列表和描述。

源代码位于prompter/basic.py
139
140
141
142
143
144
def api_prompt_helper(self) -> str:
    """
    A helper function to construct the API list and descriptions for the prompt.
    """

    pass

examples_prompt_helper()

一个辅助函数,用于构建用于上下文学习的示例提示。

源代码位于prompter/basic.py
132
133
134
135
136
137
def examples_prompt_helper(self) -> str:
    """
    A helper function to construct the examples prompt for in-context learning.
    """

    pass

load_prompt_template(template_path, is_visual=None) 静态方法

加载提示模板。

返回
  • 字典[字符串, 字符串]

    提示模板。

源代码位于prompter/basic.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@staticmethod
def load_prompt_template(template_path: str, is_visual=None) -> Dict[str, str]:
    """
    Load the prompt template.
    :return: The prompt template.
    """

    if is_visual == None:
        path = template_path
    else:
        path = template_path.format(
            mode="visual" if is_visual == True else "nonvisual"
        )

    if not path:
        return {}

    if os.path.exists(path):
        try:
            prompt = yaml.safe_load(open(path, "r", encoding="utf-8"))
        except yaml.YAMLError as exc:
            print_with_color(f"Error loading prompt template: {exc}", "yellow")
    else:
        raise FileNotFoundError(f"Prompt template not found at {path}")

    return prompt

prompt_construction(system_prompt, user_content) 静态方法

构建提示以将经验总结为示例。

参数
  • user_content (列表[字典[字符串, 字符串]]) –

    用户内容。返回:将经验总结为示例的提示。

源代码位于prompter/basic.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@staticmethod
def prompt_construction(
    system_prompt: str, user_content: List[Dict[str, str]]
) -> List:
    """
    Construct the prompt for summarizing the experience into an example.
    :param user_content: The user content.
    return: The prompt for summarizing the experience into an example.
    """

    system_message = {"role": "system", "content": system_prompt}

    user_message = {"role": "user", "content": user_content}

    prompt_message = [system_message, user_message]

    return prompt_message

retrived_documents_prompt_helper(header, separator, documents) 静态方法

构建检索文档的提示。

参数
  • header (str) –

    提示的标题。

  • separator (str) –

    提示的分隔符。

  • documents (列表[字符串]) –

    检索到的文档。返回:检索文档的提示。

源代码位于prompter/basic.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@staticmethod
def retrived_documents_prompt_helper(
    header: str, separator: str, documents: List[str]
) -> str:
    """
    Construct the prompt for retrieved documents.
    :param header: The header of the prompt.
    :param separator: The separator of the prompt.
    :param documents: The retrieved documents.
    return: The prompt for retrieved documents.
    """

    if header:
        prompt = "\n<{header}:>\n".format(header=header)
    else:
        prompt = ""
    for i, document in enumerate(documents):
        if separator:
            prompt += "[{separator} {i}:]".format(separator=separator, i=i + 1)
            prompt += "\n"
        prompt += document
        prompt += "\n\n"
    return prompt

system_prompt_construction() 抽象方法

为 LLM 构建系统提示。

源代码位于prompter/basic.py
108
109
110
111
112
113
114
@abstractmethod
def system_prompt_construction(self) -> str:
    """
    Construct the system prompt for LLM.
    """

    pass

user_content_construction() 抽象方法

为 LLM 构建完整的用户内容,包括用户提示和图像。

源代码位于prompter/basic.py
124
125
126
127
128
129
130
@abstractmethod
def user_content_construction(self) -> str:
    """
    Construct the full user content for LLM, including the user prompt and images.
    """

    pass

user_prompt_construction() 抽象方法

根据提示模板中的user字段为 LLM 构建文本用户提示。

源代码位于prompter/basic.py
116
117
118
119
120
121
122
@abstractmethod
def user_prompt_construction(self) -> str:
    """
    Construct the textual user prompt for LLM based on the `user` field in the prompt template.
    """

    pass

提示

您可以自定义提示器类以根据您的要求调整提示。