跳到主要内容

快速开始

警告

⚠️ 本项目仍在开发中。我们正努力尽快发布Flappy的首个版本。敬请期待!文档和代码示例将很快提供。

Flappy是一个用于构建基于大语言模型(LLM)的AI Agent/AI应用程序的开发框架,同时支持多种不同编程语言。请指定您正在使用的语言以开始使用。

安装

npm install @plesito/node-flappy@next
# or yarn add @pleisto/node-flappy@next

创建LLM适配器

Flappy支持多种LLM实现,例如OpenAI的ChatGPT和hugingface的transformers。您也可以通过实现LLMBase接口来创建自己的LLM适配器。

// you need to manually install `openai` package.
import { env } from 'node:process'
import OpenAI from 'openai'
import { ChatGPT } from '@pleisto/node-flappy'

const chatGpt = new ChatGPT(
new OpenAI({
apiKey: env.OPENAI_API_KEY!,
baseURL: env.OPENAI_API_BASE!
})
)

定义你的 Agent

在人工智能中,Agent 是一个计算机程序或系统,其设计目的是感知其环境,做出决定,并采取行动以实现特定的目标或一组目标。Agent 自主运行,意味着它不直接受人类操作员的控制。

在 Flappy 的生态系统中,Agent 作为 LLM 的多功能通道进行操作。它的设计目的是进行各种任务的处理 - 结构化数据,调用外部 API,或者对 LLM 生成的 Python 代码进行沙盒处理 - 根据需要以任何组合进行。它是一个灵活的工具,而不是一个僵化的齿轮,能够适应您对更高效和安全的 LLM 交互的需求。

关键概念

函数

函数作为 Flappy 框架中你的 Agent 的基础。在这个文档中,我们将介绍 InvokeFunctionSythesizedFunction,这两种你可以定义和使用的基础函数类型。

  • InvokeFunction 使 Agent 能够与环境进行交互,类似于 Langchain 的 Agent 中的工具。它由输入和输出参数定义,这些参数的结构必须清晰,以便语言学习模型(LLM)能够有效地进行交互。理解这些参数以及函数在与用户请求和世界交互中的角色是非常重要的,它对于成本效益的任务计划至关重要。
  • SythesizedFunction 是由 LLM 扮演的一种函数。你只需要定义它的描述和它的输入和输出的结构。然后,LLM 将尝试按照定义的格式处理输入并产生预期格式的输出。这使得 SythesizedFunction 在 LLM 执行结构化数据提取任务,或者预期 LLM 输出结构化数据的场景中尤其有用。

代码解释器

Flappy 中的代码解释器作为 ChatGPT 代码解释器的等效替代品,使语言模型能够通过 Python 代码满足复杂的用户需求。Flappy 的代码解释器与市场上其他开源实现的区别在于其沙盒化的安全特性。这确保了它满足生产环境部署所必需的严格的安全需求。

import { createFlappyAgent,  createInvokeFunction,  createSynthesizedFunction, createCodeInterpreter,  z } from '@pleisto/node-flappy'

enum Verdict {
Innocent = 'Innocent',
Guilty = 'Guilty',
Unknown = 'Unknown'
}

const MOCK_LAWSUIT_DATA =
"As Alex Jones continues telling his Infowars audience about his money problems and pleads for them to buy his products, his own documents show life is not all that bad — his net worth is around $14 million and his personal spending topped $93,000 in July alone, including thousands of dollars on meals and entertainment. The conspiracy theorist and his lawyers file monthly financial reports in his personal bankruptcy case, and the latest one has struck a nerve with the families of victims of Sandy Hook Elementary School shooting. They're still seeking the $1.5 billion they won last year in lawsuits against Jones and his media company for repeatedly calling the 2012 massacre a hoax on his shows. “It is disturbing that Alex Jones continues to spend money on excessive household expenditures and his extravagant lifestyle when that money rightfully belongs to the families he spent years tormenting,” said Christopher Mattei, a Connecticut lawyer for the families. “The families are increasingly concerned and will continue to contest these matters in court.” In an Aug. 29 court filing, lawyers for the families said that if Jones doesn’t reduce his personal expenses to a “reasonable” level, they will ask the bankruptcy judge to bar him from “further waste of estate assets,” appoint a trustee to oversee his spending, or dismiss the bankruptcy case. On his Infowars show Tuesday, Jones said he’s not doing anything wrong."

const agent = createFlappyAgent({
llm: chatGpt,
// Define your agent's features.
features: [
createCodeInterpreter(),
createSynthesizedFunction({
name: 'getMeta',
description: 'Extract meta data from a lawsuit full text.',
args: z.object({
lawsuit: z.string().describe('Lawsuit full text.')
}),
returnType: z.object({
verdict: z.nativeEnum(Verdict),
plaintiff: z.string(),
defendant: z.string(),
judgeOptions: z.array(z.string())
})
}),
createInvokeFunction({
name: 'getLatestLawsuitsByPlaintiff',
description: 'Get the latest lawsuits by plaintiff.',
args: z.object({
plaintiff: z.string(),
arg1: z.boolean().describe('For demo purpose. set to False'),
arg2: z.array(z.string()).describe('ignore it').optional()
}),
returnType: z.string(),
resolve: async args => {
// Do something
// e.g. query SQL database
console.debug('getLatestLawsuitsByPlaintiff called', args)
return MOCK_LAWSUIT_DATA
}
})
]
})

调用你的代理

创建并执行一个行动计划

增强语言模型(ALMs)将大型语言模型(LLMs)的推理能力与允许知识检索和行动执行的工具相结合。现有的ALM系统在从这些工具中拉取观察结果的同时触发LLM的思考流程。具体来说,LLM调用外部工具进行推理,获取工具的响应后暂停,然后根据所有先前的响应令牌决定下一步行动。尽管这种范例直接且易于实施,但通常会因冗余提示和重复执行导致巨大的计算复杂性。

Flappy使用ReWOO代替ReAct来将LLM输出的Token数最小化从而降低成本。在此基础之上,Agent具备了根据用户的Prompt自主设计执行计划的能力。它需确定一系列需要调用的函数,以满足Prompt的要求。随后,系统按照这个精心制定的计划进行执行,从而进一步提升了我们系统的效率。

agent.executePlan('Find the latest case with the plaintiff being families of victims and return its metadata.')

直接调用 synthesized function

你也可以直接调用sythesized function 而无需创建或执行动作计划。

agent.callFeature('getMeta', {lawsuit: MOCK_LAWSUIT_DATA})

调用 Code Interpreter

Code Interpreter 当前需要被直接调用. We are working on a better way to integrate it with the agent.

agent.executePlan(
'There are some rabbits and chickens in a barn. What is the number of chickens if there are 396 legs and 150 heads in the barn?'
)

// 也可以直接调用

agent.callFeature('pythonSandbox', {
code: 'There are some rabbits and chickens in a barn. What is the number of chickens if there are 396 legs and 150 heads in the barn?'
})