智能体

在UFO中,有四种类型的智能体:HostAgentAppAgentFollowerAgentEvaluationAgent。每个智能体在UFO系统中都有特定的角色,并负责用户交互过程的不同方面

代理 描述
主控智能体 将用户请求分解为子任务,并选择合适的应用程序来完成请求。
应用智能体 在选定的应用程序上执行操作。
跟随智能体 遵循用户指示完成任务。
评估智能体 评估会话或回合的完整性。

在正常工作流程中,只有HostAgentAppAgent参与用户交互过程。FollowerAgentEvaluationAgent用于特定任务。

请参见下面UFO中智能体的编排

主要组件

UFO中的智能体由以下主要组件组成,以履行其在UFO系统中的角色

组件 描述
状态 表示智能体的当前状态,并确定要处理请求的下一个动作和智能体。
内存 存储有关用户请求、应用程序状态和其他相关数据的信息。
黑板 存储智能体之间共享的信息。
提示器 根据用户请求和应用程序状态为语言模型生成提示。
处理器 处理智能体的工作流程,包括处理用户请求、执行动作和内存管理。

参考

以下是UFO中Agent类的参考。UFO中的所有智能体都继承自Agent类,并实现必要的方法以履行其在UFO系统中的角色。

基类:ABC

BasicAgent类是智能体的抽象类。

初始化BasicAgent。

参数
  • name (str) –

    智能体的名称。

源代码位于agents/agent/basic.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def __init__(self, name: str) -> None:
    """
    Initialize the BasicAgent.
    :param name: The name of the agent.
    """
    self._step = 0
    self._complete = False
    self._name = name
    self._status = self.status_manager.CONTINUE.value
    self._register_self()
    self.retriever_factory = retriever.RetrieverFactory()
    self._memory = Memory()
    self._host = None
    self._processor: Optional[BaseProcessor] = None
    self._state = None
    self.Puppeteer: puppeteer.AppPuppeteer = None

blackboard property

获取黑板。

返回
  • Blackboard

    黑板。

default_state property

获取智能体的默认状态。

返回
  • AgentState

    智能体的默认状态。

host property writable

获取智能体的主机。

返回
  • HostAgent

    智能体的主机。

memory property writable

获取智能体的记忆。

返回
  • Memory

    智能体的记忆。

name property

获取智能体的名称。

返回
  • 字符串

    智能体的名称。

processor property writable

获取处理器。

返回
  • BaseProcessor

    处理器。

state property

获取智能体的状态。

返回
  • AgentState

    智能体的状态。

status property writable

获取智能体的状态。

返回
  • 字符串

    智能体的状态。

status_manager property

获取状态管理器。

返回
  • AgentStatus

    状态管理器。

step property writable

获取智能体的步骤。

返回
  • int

    智能体的步骤。

add_memory(memory_item)

更新智能体的记忆。

参数
  • memory_item (MemoryItem) –

    要添加的记忆项。

源代码位于agents/agent/basic.py
204
205
206
207
208
209
def add_memory(self, memory_item: MemoryItem) -> None:
    """
    Update the memory of the agent.
    :param memory_item: The memory item to add.
    """
    self._memory.add_memory_item(memory_item)

build_experience_retriever()

构建经验检索器。

源代码位于agents/agent/basic.py
349
350
351
352
353
def build_experience_retriever(self) -> None:
    """
    Build the experience retriever.
    """
    pass

build_human_demonstration_retriever()

构建人工演示检索器。

源代码位于agents/agent/basic.py
355
356
357
358
359
def build_human_demonstration_retriever(self) -> None:
    """
    Build the human demonstration retriever.
    """
    pass

build_offline_docs_retriever()

构建离线文档检索器。

源代码位于agents/agent/basic.py
337
338
339
340
341
def build_offline_docs_retriever(self) -> None:
    """
    Build the offline docs retriever.
    """
    pass

build_online_search_retriever()

构建在线搜索检索器。

源代码位于agents/agent/basic.py
343
344
345
346
347
def build_online_search_retriever(self) -> None:
    """
    Build the online search retriever.
    """
    pass

clear_memory()

清除智能体的记忆。

源代码位于agents/agent/basic.py
218
219
220
221
222
def clear_memory(self) -> None:
    """
    Clear the memory of the agent.
    """
    self._memory.clear()

create_puppeteer_interface()

创建傀儡接口。

源代码位于agents/agent/basic.py
256
257
258
259
260
def create_puppeteer_interface(self) -> puppeteer.AppPuppeteer:
    """
    Create the puppeteer interface.
    """
    pass

delete_memory(step)

删除智能体的记忆。

参数
  • step (int) –

    要删除的记忆项的步骤。

源代码位于agents/agent/basic.py
211
212
213
214
215
216
def delete_memory(self, step: int) -> None:
    """
    Delete the memory of the agent.
    :param step: The step of the memory item to delete.
    """
    self._memory.delete_memory_item(step)

get_cls(name) classmethod

从注册表中检索智能体类。

参数
  • name (str) –

    智能体类的名称。

返回
  • Type['BasicAgent']

    智能体类。

源代码位于agents/agent/basic.py
376
377
378
379
380
381
382
383
@classmethod
def get_cls(cls, name: str) -> Type["BasicAgent"]:
    """
    Retrieves an agent class from the registry.
    :param name: The name of the agent class.
    :return: The agent class.
    """
    return AgentRegistry().get_cls(name)

get_prompter() abstractmethod

获取代理的提示。

返回
  • 字符串

    提示。

源代码位于agents/agent/basic.py
132
133
134
135
136
137
138
@abstractmethod
def get_prompter(self) -> str:
    """
    Get the prompt for the agent.
    :return: The prompt.
    """
    pass

get_response(message, namescope, use_backup_engine, configs=configs) classmethod

获取提示的响应。

参数
  • message (List[dict]) –

    LLM 的消息。

  • namescope (str) –

    LLM 的命名空间。

  • use_backup_engine (bool) –

    是否使用备用引擎。

  • configs

    配置。

返回
  • 字符串

    响应。

源代码位于agents/agent/basic.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
@classmethod
def get_response(
    cls,
    message: List[dict],
    namescope: str,
    use_backup_engine: bool,
    configs=configs,
) -> str:
    """
    Get the response for the prompt.
    :param message: The message for LLMs.
    :param namescope: The namescope for the LLMs.
    :param use_backup_engine: Whether to use the backup engine.
    :param configs: The configurations.
    :return: The response.
    """
    response_string, cost = llm_call.get_completion(
        message, namescope, use_backup_engine=use_backup_engine, configs=configs
    )
    return response_string, cost

handle(context)

处理智能体。

参数
  • context (Context) –

    智能体的上下文。

源代码位于agents/agent/basic.py
243
244
245
246
247
248
def handle(self, context: Context) -> None:
    """
    Handle the agent.
    :param context: The context for the agent.
    """
    self.state.handle(self, context)

message_constructor() abstractmethod

构建消息。

返回
  • List[Dict[str, Union[str, List[Dict[str, str]]]]]

    消息。

源代码位于agents/agent/basic.py
140
141
142
143
144
145
146
@abstractmethod
def message_constructor(self) -> List[Dict[str, Union[str, List[Dict[str, str]]]]]:
    """
    Construct the message.
    :return: The message.
    """
    pass

print_response()

打印响应。

源代码位于agents/agent/basic.py
361
362
363
364
365
def print_response(self) -> None:
    """
    Print the response.
    """
    pass

process(context)

处理代理。

源代码位于agents/agent/basic.py
250
251
252
253
254
def process(self, context: Context) -> None:
    """
    Process the agent.
    """
    pass

process_asker(ask_user=True)

请求处理。

参数
  • ask_user (bool, default: True ) –

    是否向用户提问。

源代码位于agents/agent/basic.py
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def process_asker(self, ask_user: bool = True) -> None:
    """
    Ask for the process.
    :param ask_user: Whether to ask the user for the questions.
    """

    _ask_message = "Could you please answer the following questions to help me understand your needs and complete the task?"
    _none_answer_message = "The answer for the question is not available, please proceed with your own knowledge or experience, or leave it as a placeholder. Do not ask the same question again."

    if self.processor:
        question_list = self.processor.question_list

        if ask_user:
            utils.print_with_color(
                _ask_message,
                "yellow",
            )

        for index, question in enumerate(question_list):
            if ask_user:
                answer = question_asker(question, index + 1)
                if not answer.strip():
                    continue
                qa_pair = {"question": question, "answer": answer}

                utils.append_string_to_file(
                    configs["QA_PAIR_FILE"], json.dumps(qa_pair)
                )

            else:
                qa_pair = {
                    "question": question,
                    "answer": _none_answer_message,
                }

            self.blackboard.add_questions(qa_pair)

process_comfirmation() abstractmethod

确认处理。

源代码位于agents/agent/basic.py
306
307
308
309
310
311
@abstractmethod
def process_comfirmation(self) -> None:
    """
    Confirm the process.
    """
    pass

process_resume()

恢复处理。

源代码位于agents/agent/basic.py
262
263
264
265
266
267
def process_resume(self) -> None:
    """
    Resume the process.
    """
    if self.processor:
        self.processor.resume()

reflection()

TODO: 反思动作。

源代码位于agents/agent/basic.py
224
225
226
227
228
229
def reflection(self) -> None:
    """
    TODO:
    Reflect on the action.
    """
    pass

response_to_dict(response) staticmethod

将响应转换为字典。

参数
  • response (str) –

    响应。

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

    字典。

源代码位于agents/agent/basic.py
169
170
171
172
173
174
175
176
@staticmethod
def response_to_dict(response: str) -> Dict[str, str]:
    """
    Convert the response to a dictionary.
    :param response: The response.
    :return: The dictionary.
    """
    return utils.json_parser(response)

set_memory_from_list_of_dicts(data)

从字典列表设置记忆。

参数
  • data (List[Dict[str, str]]) –

    字典列表。

源代码位于agents/agent/basic.py
194
195
196
197
198
199
200
201
202
def set_memory_from_list_of_dicts(self, data: List[Dict[str, str]]) -> None:
    """
    Set the memory from the list of dictionaries.
    :param data: The list of dictionaries.
    """

    assert isinstance(data, list), "The data should be a list of dictionaries."

    self._memory.from_list_of_dicts(data)

set_state(state)

设置智能体的状态。

参数
  • state (AgentState) –

    智能体的状态。

源代码位于agents/agent/basic.py
231
232
233
234
235
236
237
238
239
240
241
def set_state(self, state: AgentState) -> None:
    """
    Set the state of the agent.
    :param state: The state of the agent.
    """

    assert issubclass(
        type(self), state.agent_class()
    ), f"The state is only for agent type of {state.agent_class()}, but the current agent is {type(self)}."

    self._state = state