在人工智能快速发展的今天,大型语言模型(LLM)正在改变我们与技术交互的方式。然而,如何有效地利用LLM的能力来构建复杂的AI系统仍然是一个挑战。Ax项目应运而生,它是一个基于Stanford DSP论文的开源框架,旨在帮助开发者快速构建LLM驱动的智能代理和'代理工作流'。本文将深入探讨Ax的主要特性、使用方法和应用场景,展示其在构建下一代AI应用中的潜力。
Ax的核心理念是'代理工作流'(Agentic workflows)。这一概念源自于专家们的共识,即代理工作流是释放大型语言模型真正力量的关键。Ax团队深受Stanford DSPy论文的启发,将这一理念融入到了框架的设计中。
Ax提供了一系列强大的功能,使其成为构建LLM驱动应用的理想选择:
这些特性使Ax成为一个全面而强大的框架,能够满足从原型开发到生产部署的各种需求。
Ax引入了'提示签名'的概念,这是一种简洁而强大的方式来定义LLM任务。提示签名由任务描述、输入字段和输出字段组成,格式如下:
'任务描述' 输入字段:类型 '字段描述' -> '输出字段:类型'
这种设计使得开发者可以快速定义复杂的LLM任务,而无需编写冗长的提示文本。
Ax支持多种主流的LLM提供商,包括OpenAI、Azure OpenAI、Anthropic、Google Gemini等。这种灵活性使得开发者可以根据需求选择最适合的模型,同时也为未来的模型迁移提供了便利。
Ax提供了对多种向量数据库的支持,包括内存数据库、Weaviate、Cloudflare和Pinecone等。这使得开发者可以轻松实现文本嵌入、相似度搜索等功能,为构建复杂的AI应用奠定基础。
Ax提供了一个强大的框架来构建智能代理。以下是一个简单的例子,展示了如何创建一个研究代理和一个总结代理:
const researcher = new AxAgent(ai, { name: 'researcher', description: 'Researcher agent', signature: `physicsQuestion 'physics questions' -> answer 'reply in bullet points'` }); const summarizer = new AxAgent(ai, { name: 'summarizer', description: 'Summarizer agent', signature: `text 'text so summarize' -> shortSummary 'summarize in 5 to 10 words'` }); const agent = new AxAgent(ai, { name: 'agent', description: 'A an agent to research complex topics', signature: `question -> answer`, agents: [researcher, summarizer] }); agent.forward({ questions: 'How many atoms are there in the universe' })
这个例子展示了如何创建多个专门的代理,并将它们组合成一个更复杂的代理系统。这种方法允许开发者构建高度模块化和可扩展的AI系统。
Ax简化了RAG的实现过程。以下是一个使用AxDBManager
进行文档处理和查询的例子:
const tika = new AxApacheTika(); const text = await tika.convert('/path/to/document.pdf'); const manager = new AxDBManager({ ai, db }); await manager.insert(text); const matches = await manager.query('Find some text'); console.log(matches);
这个例子展示了如何将PDF文档转换为文本,插入向量数据库,并进行相似度查询。这种功能对于构建智能文档检索系统非常有用。
Ax支持多模态输入,这在处理图像和文本结合的任务时非常有用:
const image = fs .readFileSync('./src/examples/assets/kitten.jpeg') .toString('base64'); const gen = new AxChainOfThought(ai, `question, animalImage:image -> answer`); const res = await gen.forward({ question: 'What family does this animal belong to?', animalImage: { mimeType: 'image/jpeg', data: image } });
这个例子展示了如何将图像作为输入传递给LLM,并获取关于图像内容的回答。
Ax支持流式输出和实时验证,这对于构建响应迅速的AI应用至关重要:
gen.addStreamingAssert( 'answerInPoints', (value: string) => { const re = /^\d+\./; return value .split('\n') .map((x) => x.trim()) .filter((x) => x.length > 0) .every((x) => re.test(x)); }, 'Lines must start with a number and a dot. Eg: 1. This is a line.' ); const res = await gen.forward( { question: 'Provide a list of optimizations to speedup LLM inference.' }, { stream: true, debug: true } );
这个例子展示了如何在流式输出中添加实时验证,确保输出符合特定的格式要求。
Ax提供了一个高效的路由机制,使用嵌入而不是昂贵的LLM调用来决定如何处理用户输入:
const customerSupport = new AxRoute('customerSupport', [ 'how can I return a product?', 'where is my order?', 'can you help me with a refund?', 'I need to update my shipping address', 'my product arrived damaged, what should I do?' ]); const technicalSupport = new AxRoute('technicalSupport', [ 'how do I install your software?', 'I\'m having trouble logging in', 'can you help me configure my settings?', 'my application keeps crashing', 'how do I update to the latest version?' ]); const router = new AxRouter(ai); await router.setRoutes( [customerSupport, technicalSupport], { filename: 'router.json' } ); const tag = await router.forward('I need help with my order');
这个例子展示了如何创建一个智能路由系统,根据用户输入快速决定应该由哪个支持团队处理。
Ax提供了自动提示调优的功能,使用更大的模型来优化提示效果:
const program = new AxChainOfThought<{ question: string }, { answer: string }>( ai, `question -> answer 'in short 2 or 3 words'` ); const optimize = new AxBootstrapFewShot< { question: string }, { answer: string } >({ program, examples }); const metricFn: AxMetricFn = ({ prediction, example }) => emScore(prediction.answer as string, example.answer as string); const result = await optimize.compile(metricFn);
这个例子展示了如何使用AxBootstrapFewShot
优化器来调优提示,提高模型的性能。
Ax为构建LLM驱动的智能代理和工作流提供了一个强大而灵活的框架。通过其丰富的功能和直观的API,开发者可以快速构建复杂的AI系统,从简单的问答机器人到复杂的多代理协作系统。随着AI技术的不断发展,Ax将继续演进,为开发者提供更多创新的工具和方法,推动AI应用的边界不断扩展。
无论您是AI研究人员、产品开发者还是企业决策者,
AI小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。