<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>简单之美</title>
	<atom:link href="http://shiyanjun.cn/feed" rel="self" type="application/rss+xml" />
	<link>http://shiyanjun.cn</link>
	<description>简单之美，难得简单，享受简单的唯美。</description>
	<lastBuildDate>Wed, 04 Mar 2026 07:04:53 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.2</generator>
	<item>
		<title>大模型（LLMs）盘点跟踪</title>
		<link>http://shiyanjun.cn/archives/2559.html</link>
		<comments>http://shiyanjun.cn/archives/2559.html#comments</comments>
		<pubDate>Wed, 04 Mar 2026 06:53:32 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[LLM]]></category>
		<category><![CDATA[人工智能]]></category>
		<category><![CDATA[LMM]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2559</guid>
		<description><![CDATA[发布时间 LLM 模型参数量 组织名称 论文/模型特点 2026-03 GPT-5.3 Instant Ope [&#8230;]]]></description>
				<content:encoded><![CDATA[<table style="border: solid thin #868686;width: 100%" border="1" cellpadding="2" cellspacing="0">
<tbody>
<tr>
<td style="width: 10%;text-align: center;border: solid thin #868686" valign="top"><b>发布时间</b></td>
<td style="width: 15%;text-align: center;border: solid thin #868686" valign="top"><b>LLM</b></td>
<td style="width: 10%;text-align: center;border: solid thin #868686" valign="top"><b>模型参数量</b></td>
<td style="width: 10%;text-align: center;border: solid thin #868686" valign="top"><b>组织名称</b></td>
<td style="width: 55%;text-align: center;border: solid thin #868686" valign="top"><b>论文/模型特点</b></td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2026-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-5.3 Instant</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]优化升级了ChatGPT“即时响应”模型，提升有用程度与准确率。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2026-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GLM-5</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">智谱AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://z.ai/blog/glm-5">介绍</a>]定位为最新一代旗舰级对话、编程与智能体模型，主攻复杂系统工程与长程Agent任务。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2026-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Qwen3.5-Plus</td>
<td style="width: 10%;border: solid thin #868686" valign="top">3970亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Alibaba</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]性能碾压GPT-5.2和Gemini 3 Pro，全球最强开源大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2026-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-5.2-Codex</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]目前最先进的编程智能体，在长周期任务处理、大规模代码重构等实现显著提升。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2026-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Claude Sonnet 4.6</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top"> Anthropic</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]在编程、操作电脑、长文本推理、智能体规划、知识工作和设计等方面实现全面升级。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2026-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">CClaude Opus 4.6</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top"> Anthropic</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]Claude Opus 4.5的深度进化版，首次实现百万 token 级别上下文支持。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2026-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gemini 3.5 Pro</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]基于稀疏MoE架构，超万亿参数，支持多模态处理，100万Token上下文，强大逻辑扐能力。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GLM-4.7</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">智谱AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]编程性能超越GPT-5.2，在数学推理和智能体任务上全面进化。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GLM-4.6</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">智谱AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]采用MoE架构，支持用于复杂推理和工具使用的思考模式，及用于即时响应的非思考模式。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-07</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GLM-4.5</td>
<td style="width: 10%;border: solid thin #868686" valign="top">3350亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">智谱AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]在Agentic Coding等关键能力上取得了突破性进展。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Claude 4</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top"> Anthropic </td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]包括Claude Opus 4（当下最强编程模型，价格昂贵）和Claude Sonnet 4（性能卓越、兼具高推理能力和高效率）。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gemini 2.5 Flash</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]Gemini 2.5 Pro的轻量级版本，定位为平衡质量、成本与延迟的混合推理模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gemini 2.5 Pro</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">介绍</a>]支持文本、图像、音频、视频及代码库等多模态数据处理，内置“思考链”机制提升推理能力，拥有100万token的上下文窗口。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Grok-3</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">xAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://x.ai/blog/grok-3">介绍</a>]具有更高的推理、计算和适应能力，使用约 20 万个 GPU 训练。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">DeepSeek-R1</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">DeepSeek</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/abs/2501.12948">论文</a>]无需大量 SFT 就能实现与 OpenAI o1 相当的推理能力。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2025-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Kimi K1.5</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Kimi Team</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/abs/2501.12599">论文</a>]多模态思考模型，性能比肩 OpenAI o1。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">DeepSeek-V3</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">DeepSeek</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/abs/2412.19437">论文</a>]V3 表现接近 GPT-4o 和 Claude-3.5-Sonnet，更注重综合场景适用性。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">o3</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://openai.com/index/o3-mini-system-card/">介绍</a>]o3 在编程和数学能力方面达到 AGI 门槛。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gemini 2.0 Pro/Flash/Flash-Lite</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://blog.google/technology/google-deepmind/google-gemini-ai-update-december-2024/#">介绍</a>]Gemini 2.0 全家桶，推理能和多模态功能增强。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Phi-4</td>
<td style="width: 10%;border: solid thin #868686" valign="top">14B</td>
<td style="width: 10%;border: solid thin #868686" valign="top">MicroSoft</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://techcommunity.microsoft.com/blog/aiplatformblog/introducing-phi-4-microsoft%E2%80%99s-newest-small-language-model-specializing-in-comple/4357090">介绍</a>]phi-4 是微软小模型 phi 系列最后一个模型，增强了复杂推理能力。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">o1</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://openai.com/index/learning-to-reason-with-llms/">介绍</a>]首个具有“推理”能力的模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-08</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Grok-2</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">xAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://x.ai/blog/grok-2">介绍</a>]Grok-2 系列模型具有聊天、编码和推理等功能，包括 Grok-2 和 Grok-2 mini 两个版本。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Chameleon</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Meta</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/abs/2405.09818">论文</a>]混合模态基座模型，只支持图像文本，不支持语音。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-4o</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://openai.com/index/hello-gpt-4o/">介绍</a>]OpenAI 的首个整合文本、视觉和音频多模态输入与输出的模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Arctic</td>
<td style="width: 10%;border: solid thin #868686" valign="top">4800亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Snowflake</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www.snowflake.com/blog/arctic-open-efficient-foundation-language-models-snowflake/">介绍</a>]迄今为止最大 MOE 模型，以 128 位专家和 4800亿参数开源，击败 Llama 3、Mixtral。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Phi-3</td>
<td style="width: 10%;border: solid thin #868686" valign="top">3.8B/7B/14B</td>
<td style="width: 10%;border: solid thin #868686" valign="top">MicroSoft</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://azure.microsoft.com/en-us/blog/introducing-phi-3-redefining-whats-possible-with-slms/">介绍</a>]在手机端表现出色，性能接近 GPT-3.5 水平。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Command R+</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1040亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Cohere</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://huggingface.co/CohereForAI/c4ai-command-r-plus">介绍</a>]首个击败 GPT-4 的开源 LLM。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Llama 3</td>
<td style="width: 10%;border: solid thin #868686" valign="top">4000亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Meta</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://ai.meta.com/blog/meta-llama-3/">介绍</a>]开源了 3B 和 70B 两款，400B 将会是首个开源的 GPT-4 级别 LLM。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-4 Turbo</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[论文]超越 Claude 3 Opus，比 GPT-4 系列性能有所提升。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">DBRX</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1320亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Databricks</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www.databricks.com/blog/introducing-dbrx-new-state-art-open-llm">论文</a>]开源，采用细粒度 MOE 架构，推理速度比 LLaMA 2-70B 快两倍，整体性能超越 GPT-3.5。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Grok-1</td>
<td style="width: 10%;border: solid thin #868686" valign="top">3140亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">xAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://x.ai/blog/grok-os">介绍</a>]目前参数量最大的开源模型，基于 MOE 架构。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Inflection-2.5</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Inflection AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://inflection.ai/inflection-2-5">介绍</a>]性能媲美 GPT-4，仅用四成训练计算量。最大亮点：结合了高 IQ 和高 EQ。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Claude 3</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Anthropic</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www.anthropic.com/news/claude-3-family">介绍</a>]多模态大模型，包含 Opus/Sonnet/Haiku 三种模型，Opus 在行业基准测试中表现优于 GPT-4 和 Gemini Ultra。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Mistral Large</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Mistral AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://mistral.ai/news/mistral-large/">介绍</a>]常识推理、知识问答表现出色，评分超过 Gemini Pro/Claude 2，仅次于 GPT-4。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Genie</td>
<td style="width: 10%;border: solid thin #868686" valign="top">110亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2402.15391.pdf">论文</a>]基础世界模型，能够合成可操控的虚拟世界。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Sora</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2402.17177">论文</a>]文生视频模型。60s 超长长度。模拟世界模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gemma</td>
<td style="width: 10%;border: solid thin #868686" valign="top">70亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://storage.googleapis.com/deepmind-media/gemma/gemma-report.pdf">介绍</a>]目前最强开源大模型，7B 性能超越 Llama 2 13B。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gemini 1.5 Pro</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://storage.googleapis.com/deepmind-media/gemini/gemini_v1_5_report.pdf">论文</a>]支持 100万 token 上下文，基于 Transformer+MoE 架构。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Aya</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">C4AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://cohere.com/research/aya">介绍</a>]开源大模型，覆盖 101 种语言。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Lumiere</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2401.12945.pdf">论文</a>]文生视频模型，是一个大规模视频扩散模型，采用全新“Space-Time U-Net”基础架构，时间更长更连贯。时长直接碾压 Gen-2 和 Pika。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Code Llama 70B</td>
<td style="width: 10%;border: solid thin #868686" valign="top">700亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Meta</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2308.12950.pdf">论文</a>]目前评分最高的开源大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Stable Code 3B</td>
<td style="width: 10%;border: solid thin #868686" valign="top">30亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Stability AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://stability.ai/news/stable-code-2024-llm-code-completion-release">介绍</a>]多 PL 支持，可与 Code Llama 7B 媲美。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Stable LM 2</td>
<td style="width: 10%;border: solid thin #868686" valign="top">16亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Stability AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://stability.ai/news/introducing-stable-lm-2">介绍</a>]支持非中文的多语言开源小模型，性能接近 MS 的 Phi-2 模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">AMIE</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2401.05654.pdf">论文</a>]医疗大模型，基于 PaLM 2 开发。诊断医学推理、对话。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Baichuan 3</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">百川智能</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[论文]在中文任务上超越 GPT-4。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Qwen-VL-Max</td>
<td style="width: 10%;border: solid thin #868686" valign="top">96亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Alibaba</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2308.12966.pdf">论文</a>]信息推理，扩展创作，视觉定位，针对画面区域问答。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2024-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">DeepSeek LLM</td>
<td style="width: 10%;border: solid thin #868686" valign="top">670亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">DeepSeek</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2401.02954.pdf">论文</a>]在代码、数学、推理任务中均超越 LLaMA-2 70B。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Phi-2</td>
<td style="width: 10%;border: solid thin #868686" valign="top">27亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">MicroSoft</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www.microsoft.com/en-us/research/blog/phi-2-the-surprising-power-of-small-language-models/">介绍</a>]用于研究，在常识推理、语言理解、数学、代码任务上性能优异。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Imagen 2</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2205.11487.pdf">论文</a>]文生图模型。理解复杂抽象概念，惊人的准确性。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gemini</td>
<td style="width: 10%;border: solid thin #868686" valign="top">18/32.5亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2312.11805.pdf">论文</a>]包含三个版本 Ultra/Pro/Nano。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Qwen-VL-Plus</td>
<td style="width: 10%;border: solid thin #868686" valign="top">96亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Alibaba</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2308.12966.pdf">论文</a>]细节识别，处理图像中文本的能力显著提升。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Claude 2.1</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Anthropic</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www.anthropic.com/news/claude-2-1">介绍</a>]闭源。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-11</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gen-2</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Runway</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://research.runwayml.com/gen2">介绍</a>]文生视频模型。生成视频最长达 18 秒。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-10</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Pika</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">pika_labs</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2310.04329">论文</a>]文生视频模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">DALL-E 3</td>
<td style="width: 10%;border: solid thin #868686" valign="top">35亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://cdn.openai.com/papers/dall-e-3.pdf">论文</a>]多模态预训练模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-4V</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2309.17421.pdf">论文</a>]情绪识别、支持提供图像输入，推理能力突出。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Mistral 7B</td>
<td style="width: 10%;border: solid thin #868686" valign="top">73亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Mistral AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2401.04088.pdf">论文</a>]开源，基于 Apache 2.0 许可发布，性能优于 Llama 2 13B。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Qwen-VL</td>
<td style="width: 10%;border: solid thin #868686" valign="top">70亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Alibaba</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2308.12966.pdf">论文</a>]大规模视觉语言模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Falcon</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1800亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">TII</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2311.16867.pdf">论文</a>]开源，基于 3.5万亿 token 训练，击败 Llama 2。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Baichuan 2</td>
<td style="width: 10%;border: solid thin #868686" valign="top">130亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">百川智能</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2309.10305.pdf">论文</a>]支持多语言，支持 2-192K 超长上下文。领先 Llama2。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-09</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT4All</td>
<td style="width: 10%;border: solid thin #868686" valign="top">130亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Nomic AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2311.04931.pdf">论文</a>]开源，基于 Llama 微调。PC 上运行，不需要显卡。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-08</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Guanaco</td>
<td style="width: 10%;border: solid thin #868686" valign="top">650亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">开源</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2305.14314.pdf">论文</a>]基于 Llama 衍生，对扩展对话的适应性强。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-07</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Med-PaLM M</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2307.14334.pdf">论文</a>]多模态生成模型，可灵活编码和解释生物医学数据。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-07</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Pangu 3.0</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Huawei</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="">论文</a>]分三层：L0-基础大模型/L1-行业大模型/L2-场景模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-07</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Llama2</td>
<td style="width: 10%;border: solid thin #868686" valign="top">700亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Meta</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2307.09288.pdf">论文</a>]基于 2万亿 token 训练。强大的逻辑表达能力。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-07</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Claude 2</td>
<td style="width: 10%;border: solid thin #868686" valign="top">860亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Anthropic</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www-cdn.anthropic.com/files/4zrzovbb/website/bd2a28d2535bfb0494cc8e2a3bf135d2e7523226.pdf">论文</a>]支持 200K 上下文，注重有用性、诚实性和无害性。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">MPT</td>
<td style="width: 10%;border: solid thin #868686" valign="top">300亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">MosaicML</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www.mosaicml.com/mpt">介绍</a>]开源。Transformer 模型。80K Token 上下文，训练成本低。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">RAPHAEL</td>
<td style="width: 10%;border: solid thin #868686" valign="top">30亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">商汤</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2305.18295.pdf">论文</a>]文生图模型。可生成多种艺术风格图像。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">PaLM 2</td>
<td style="width: 10%;border: solid thin #868686" valign="top">3400亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2305.10403.pdf">论文</a>]基于 Transformer。四个版本：Gecko/Otter/Bison/Unicorn。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Command</td>
<td style="width: 10%;border: solid thin #868686" valign="top">520亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Cohere</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://docs.cohere.com/docs/the-command-model">介绍</a>]文本生成模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">EmbodiedGPT</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">HKU</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2305.15021.pdf">论文</a>]基于多模态思维链的端到端具身智能大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">PanGu-Σ</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1.0万亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Huawei</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2303.10845.pdf">论文</a>]具有稀疏架构的大语言模型，可能接近 GPT-3.5。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">BloombergGPT</td>
<td style="width: 10%;border: solid thin #868686" valign="top">500亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Bloomberg News</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2303.17564.pdf">论文</a>]面向金融领域的第一个大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">CodeGeeX</td>
<td style="width: 10%;border: solid thin #868686" valign="top">130亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">智普 AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2303.17568.pdf">论文</a>]基于 Transformer，支持代码自动生成、代码解释、代码翻译。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Vicuna</td>
<td style="width: 10%;border: solid thin #868686" valign="top">330亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">LMSYS Org</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://lmsys.org/blog/2023-03-30-vicuna/">介绍</a>]使用 ShareGPT 会话对 Llama 微调，接近 GPT-4 约 90%。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-4</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1.5万亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2303.08774.pdf">论文</a>]图像理解分析、高级推理、多语言支持。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">PaLM-E</td>
<td style="width: 10%;border: solid thin #868686" valign="top">5620亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2303.03378.pdf">论文</a>]多模态具身视觉语言模型：理解图像、生成语言，引导机器人执行任务。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-3.5</td>
<td style="width: 10%;border: solid thin #868686" valign="top">20亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[论文]更强自适应和泛化能力。支持语言生成、机器翻译、问答。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Jurassic-2</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1780亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">AI21 Labs</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www.ai21.com/blog/introducing-j2">介绍</a>]多语言支持，包含是三个版本：Large/Grande/Jumbo。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Claude v1</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Anthropic</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://www.anthropic.com/news/introducing-claude">介绍</a>]支持 100K 上下文，概括、总结和翻译更长文本。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gen-1</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Runway</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2302.03011.pdf">介绍</a>]文生视频模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2023-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Llama</td>
<td style="width: 10%;border: solid thin #868686" valign="top">650亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Meta</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2302.13971.pdf">论文</a>]基于开源数据训练，基于 Transformer 训练，超越 GPT-3。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Alpaca</td>
<td style="width: 10%;border: solid thin #868686" valign="top">70亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Stanford</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2212.10560.pdf">论文</a>]基于 Llama-7B 衍生的模型，训练成本奇低。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">OPT-IML</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1750亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Meta</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2212.12017.pdf">论文</a>]问答、文本摘要、翻译。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-11</td>
<td style="width: 15%;border: solid thin #868686" valign="top">BLOOM</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1760亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">BigScience</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2211.05100.pdf">论文</a>]开源，基于 59 种语言、数百个来源数据集训练。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-11</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Galactica</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1200亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Meta</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2211.09085.pdf">论文</a>]面向科学领域的开源大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-08</td>
<td style="width: 15%;border: solid thin #868686" valign="top">AlexaTM 20B</td>
<td style="width: 10%;border: solid thin #868686" valign="top">200亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Amazon</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2208.01448.pdf">论文</a>]基于 Transformer 的多语言 seq2seq 大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-06</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Parti</td>
<td style="width: 10%;border: solid thin #868686" valign="top">200亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2206.10789.pdf">论文</a>]基于自回归模型、图像标记器结合的文生图大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Imagen</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google DeepMind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2205.11487.pdf">论文</a>]文生图模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">DALL-E 2</td>
<td style="width: 10%;border: solid thin #868686" valign="top">35亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2204.13807.pdf">论文</a>]文生图模型。生成高质量图像。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">PaLM</td>
<td style="width: 10%;border: solid thin #868686" valign="top">5400亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2204.02311.pdf">论文</a>]多语言理解、翻译、自动生成代码、修复代码 Bug。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Jurassic-1</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1780亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">AI21 Labs</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://uploads-ssl.webflow.com/60fd4503684b466578c0d307/61138924626a6981ee09caf6_jurassic_tech_paper.pdf">论文</a>]模型大小与 GPT-3 类似。最复杂的 </td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-NeoX-20B</td>
<td style="width: 10%;border: solid thin #868686" valign="top">200亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">EleutherAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2204.06745.pdf">论文</a>]代码生成大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GLM</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1300亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">智谱 AI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2103.10360.pdf">论文</a>]开源，支持高精度千亿中英双语语言模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">CodeGen</td>
<td style="width: 10%;border: solid thin #868686" valign="top">110亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Saleforce</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2203.13474.pdf">论文</a>]代码生成大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Chinchilla</td>
<td style="width: 10%;border: solid thin #868686" valign="top">700亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google Deepmind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2203.15556.pdf">论文</a>]减少的模型尺寸，降低推理成本，优于 Gopher。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-03</td>
<td style="width: 15%;border: solid thin #868686" valign="top">AlphaCode</td>
<td style="width: 10%;border: solid thin #868686" valign="top"></td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google Deepmind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2203.07814.pdf">论文</a>]代码生成大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">LaMDA</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1370亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2201.08239.pdf">论文</a>]基于 Transformer，通过对话训练，出色上下文理解能力。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2022-01</td>
<td style="width: 15%;border: solid thin #868686" valign="top">MT-NLG</td>
<td style="width: 10%;border: solid thin #868686" valign="top">5300亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">NVIDIA</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2201.11990.pdf">论文</a>]自然语言生成模型，更快的学习能力。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2021-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Gopher</td>
<td style="width: 10%;border: solid thin #868686" valign="top">2800亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google Deepmind</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2112.11446.pdf">论文</a>]在知识密集领域，如事实检测和常识上表现有所提高。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2021-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Ernie 3.0 Titan</td>
<td style="width: 10%;border: solid thin #868686" valign="top">2600亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Baidu</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2112.12731.pdf">论文</a>]文心大模型，最强大的中文预训练模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2021-12</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GLaM</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1.2万亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2112.06905.pdf">论文</a>]多模态大模型，通用稀疏语言模型，基于 MoE 架构。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2021-11</td>
<td style="width: 15%;border: solid thin #868686" valign="top">M6</td>
<td style="width: 10%;border: solid thin #868686" valign="top">10万亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Alibaba</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2103.00823.pdf">论文</a>]支持多模态、多任务，尤其擅长设计、写作、问答。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2021-07</td>
<td style="width: 15%;border: solid thin #868686" valign="top">Codex</td>
<td style="width: 10%;border: solid thin #868686" valign="top">120亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2107.03374.pdf">论文</a>]代码生成大模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2021-04</td>
<td style="width: 15%;border: solid thin #868686" valign="top">PanGu-α</td>
<td style="width: 10%;border: solid thin #868686" valign="top">2000亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Huawei</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2104.12369.pdf">论文</a>]针对行业友好、专业的大模型，可提供端到端 AI 解决方案。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2021-02</td>
<td style="width: 15%;border: solid thin #868686" valign="top">DALL-E</td>
<td style="width: 10%;border: solid thin #868686" valign="top">120亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2102.12092.pdf">论文</a>]文生图模型。多模态预训练模型。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2020-10</td>
<td style="width: 15%;border: solid thin #868686" valign="top">mT5</td>
<td style="width: 10%;border: solid thin #868686" valign="top">130亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">Google</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2010.11934.pdf">论文</a>]文本生成模型，支持分类、相似度计算、文本生成。</td>
</tr>
<tr>
<td style="width: 10%;border: solid thin #868686" valign="top">2020-05</td>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-3</td>
<td style="width: 10%;border: solid thin #868686" valign="top">1750亿</td>
<td style="width: 10%;border: solid thin #868686" valign="top">OpenAI</td>
<td style="width: 55%;border: solid thin #868686" valign="top">[<a target="_blank" href="https://arxiv.org/pdf/2005.14165.pdf">论文</a>]支持语言生成、文本分类、问答，图像生成功能更成熟。</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2559.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>基于 CrewAI 构建应用的决策要点分析</title>
		<link>http://shiyanjun.cn/archives/2827.html</link>
		<comments>http://shiyanjun.cn/archives/2827.html#comments</comments>
		<pubDate>Sat, 15 Mar 2025 06:30:06 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[CrewAI]]></category>
		<category><![CDATA[人工智能]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2827</guid>
		<description><![CDATA[在使用 CrewAI 框架来实现我们需求之前，需要很好地评估 CrewAI 框架提供的各种能力，并选择一些与我 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>在使用 CrewAI 框架来实现我们需求之前，需要很好地评估 CrewAI 框架提供的各种能力，并选择一些与我们的需求相匹配的能力子集合来构建 AI Agent 应用，否则在实现的过程中，可能会遇到各种无法达到预期的困难。<br />
我们首先要明确的是，为了更好地满足实际应用场景的需要，只有选择最合适的技术组件和方法，在应用场景下需求不断细化明确的过程中，向着更合适的方向进行架构迭代优化，这个过程中甚至可能需要微调或替换各种技术组件（框架的某些特性/能力支持）。<br />
本文大部分内容，是基于 CrewAI 官网文档 “Evaluating Use Cases for CrewAI” 一节的内容，经过重新组织而完成的。</p>
<p><strong style="font-size: 20px">1. CrewAI 决策框架</Strong></p>
<p>在使用 CrewAI 框架实现我们的应用之前，CrewAI 给出了一个 “Complexity vs. Precision” 矩阵，它基于复杂性与精确性两个维度，划分了 4 个象限，并针对不同象限对复杂性、精确性的要求，提供了使用 CrewAI 的特性的指导和建议。4 个象限划分，如下图所示：</p>
<p><img src="https://mintlify.s3.us-west-1.amazonaws.com/crewai/complexity_precision.png" alt="Complexity vs. Precision Matrix for CrewAI Applications" width="615" height="392" class="alignnone size-full" /></p>
<p>通过这个矩阵可以很好地评估，我们应该如何选择 CrewAI 具备的能力，从而更好地满足应用需求。<br />
下面先给出，在 CrewAI Application 的上下文之内，“复杂性” 和 “精确性” 具体包含哪些要点，这样能够更好地将我们面对的具体应用场景的要求，更准确地映射到 “复杂性” 和 “精确性” 对应的维度上。</p>
<p><strong style="font-size: 18px">1.1 复杂性与精确性</Strong></p>
<p>复杂性（Complexity）包括如下几个要点：</p>
<ul>
<li style="line-height: 1.5">应用所要求的不同步骤/操作的数量</li>
<li style="line-height: 1.5">需要执行的任务（Task）的多样性</li>
<li style="line-height: 1.5">应用中不同组件或不同步骤之间的相互依赖程度</li>
<li style="line-height: 1.5">是否有条件约束或分支逻辑的要求</li>
<li style="line-height: 1.5">待构建的整个 Workflow 的复杂程度</li>
</ul>
<p>精确性（Precision）包括如下几个要点：</p>
<ul>
<li style="line-height: 1.5">最终输出结果的精确性要求</li>
<li style="line-height: 1.5">对结构化（Structured）的、可预测（Predictable）的结果的要求</li>
<li style="line-height: 1.5">可再现性（Reproducibility）要求</li>
<li style="line-height: 1.5">程序运行过程中，每一个执行步骤的可控制程度要求</li>
<li style="line-height: 1.5">对可变化结果输出的容忍性（Tolerance）</li>
</ul>
<p><strong style="font-size: 18px">1.2 复杂性 vs 精确性四象限</Strong></p>
<p><strong style="font-size: 16px">1.2.1 象限一：低复杂性+低精确性</Strong></p>
<p><strong style="font-size: 14px">1、象限特征</Strong></p>
<ul>
<li style="line-height: 1.5">简单的、直接的任务</li>
<li style="line-height: 1.5">能够容忍输出结果的可变性</li>
<li style="line-height: 1.5">应用执行的步骤数非常有限</li>
<li style="line-height: 1.5">创意类或探索类应用</li>
</ul>
<p><strong style="font-size: 14px">2、应用场景参考</Strong></p>
<ul>
<li style="line-height: 1.5">基础的内容生成类应用</li>
<li style="line-height: 1.5">想法头脑风暴</li>
<li style="line-height: 1.5">简单的提取摘要类任务</li>
<li style="line-height: 1.5">创意写作助手</li>
</ul>
<p>  <strong style="font-size: 14px">3、策略推荐</Strong><br />
  选择简单的 Crew，或者简单的 Agent 来构建应用。</p>
<p><strong style="font-size: 16px">1.2.2 象限二：低复杂性+高精确性</Strong></p>
<p>  <strong style="font-size: 14px">1、象限特征</Strong></p>
<ul>
<li style="line-height: 1.5">具有强制性、结构化输出要求的简单 Workflow</li>
<li style="line-height: 1.5">具有可再现性结果输出需求的场景</li>
<li style="line-height: 1.5">有限步骤，但要求满足高精确性</li>
<li style="line-height: 1.5">常常包含数据处理、数据转换要求的场景</li>
</ul>
<p>  <strong style="font-size: 14px">2、应用场景参考</Strong></p>
<ul>
<li style="line-height: 1.5">数据抽取与转换</li>
<li style="line-height: 1.5">表单填充与校验</li>
<li style="line-height: 1.5">结构化内容生成（JSON、XML）</li>
<li style="line-height: 1.5">简单分类任务</li>
</ul>
<p>    <strong style="font-size: 14px">3、策略推荐</Strong><br />
    选择能够直接与 LLM 交互/调用的 Flow，或者具有结构化输出的 Crew。</p>
<p><strong style="font-size: 16px">1.2.3 象限三：高复杂性+低精确性</Strong></p>
<p>  <strong style="font-size: 14px">1、象限特征</Strong></p>
<ul>
<li style="line-height: 1.5">具有很多步骤的多阶段处理应用场景</li>
<li style="line-height: 1.5">创意或探索类输出</li>
<li style="line-height: 1.5">各个组件之间具有复杂的交互</li>
<li style="line-height: 1.5">能够容忍最终结果的可变性</li>
</ul>
<p>  <strong style="font-size: 14px">2、应用场景参考</Strong></p>
<ul>
<li style="line-height: 1.5">调研与分析场景</li>
<li style="line-height: 1.5">内容创建 Pipeline</li>
<li style="line-height: 1.5">数据探查分析</li>
<li style="line-height: 1.5">解决创意类问题</li>
</ul>
<p>    <strong style="font-size: 14px">3、策略推荐</Strong><br />
    选择具有多个专业化（Specialized）Agent 构建而成的复杂 Crew。</p>
<p><strong style="font-size: 16px">1.2.4 象限四：高复杂性+高精确性</Strong></p>
<p>  <strong style="font-size: 14px">1、象限特征</Strong></p>
<ul>
<li style="line-height: 1.5">结果要求结构化输出的复杂 Workflow</li>
<li style="line-height: 1.5">具有严格精确性要求的、多个步骤之间相互依赖的场景</li>
<li style="line-height: 1.5">同时满足复杂处理和精确结果要求的场景</li>
<li style="line-height: 1.5">常被认为是至关重要（mission-critical）类的应用场景</li>
</ul>
<p>  <strong style="font-size: 14px">2、应用场景参考</Strong></p>
<ul>
<li style="line-height: 1.5">企业决策支持系统</li>
<li style="line-height: 1.5">复杂处理 Pipeline</li>
<li style="line-height: 1.5">多阶段文档处理</li>
<li style="line-height: 1.5">例行化（受监管类）行业应用场景</li>
</ul>
<p>    <strong style="font-size: 14px">3、策略推荐</Strong><br />
    选择使用 Flow，能够支持编排（Orchestrating）具有带校验步骤的多个 Crew。</p>
<p><strong style="font-size: 20px">2. 实用的评估框架</Strong></p>
<p>为了能够在实际使用 CrewAI 之前进行评估，可以参考下面的评估流程：</p>
<p><strong style="font-size: 18px">2.1 评估复杂性要求</Strong></p>
<p>通过考虑以下因素，在 1-10 的范围内对应用程序的复杂性进行打分（打完分后，计算平均得分作为总体的复杂性分数）：<br />
  <strong style="font-size: 16px">2.1.1 步骤数量：要求有多少个不同的操作步骤？</Strong></p>
<ul>
<li style="line-height: 1.5">1-3 步：低复杂性（1-3）</li>
<li style="line-height: 1.5">4-7 步：中复杂性（4-7）</li>
<li style="line-height: 1.5">8+ 步：高复杂性（8-10）</li>
</ul>
<p>  <strong style="font-size: 16px">2.1.2 相互依赖性：不同部分相互依赖程度如何？</Strong></p>
<ul>
<li style="line-height: 1.5">依赖非常少：低依赖性（1-3）</li>
<li style="line-height: 1.5">一些依赖：中依赖性（4-7）</li>
<li style="line-height: 1.5">很多复杂依赖：高依赖性（8-10）</li>
</ul>
<p>  <strong style="font-size: 16px">2.1.3 条件逻辑：需要多少分支和决策？</Strong></p>
<ul>
<li style="line-height: 1.5">线性流程：低复杂性（1-3）</li>
<li style="line-height: 1.5">一些分支：中复杂性（4-7）</li>
<li style="line-height: 1.5">复杂决策树：高复杂性（8-10）</li>
</ul>
<p>  <strong style="font-size: 16px">2.1.4 领域知识：所需要的知识有多么专业？</Strong></p>
<ul>
<li style="line-height: 1.5">通用知识：低复杂性（1-3）</li>
<li style="line-height: 1.5">一些专业知识：中复杂性（4-7）</li>
<li style="line-height: 1.5">在多个领域具有深厚的专业知识：高复杂性（8-10）</li>
</ul>
<p><strong style="font-size: 18px">2.2 评估精确性要求</Strong></p>
<p>通过考虑以下因素，在 1-10 的范围内对精确性要求进行评分（打完分后，计算平均得分作为总体的精确性要求得分）：<br />
  <strong style="font-size: 16px">2.2.1 必须输出内容的结构化程度如何？</Strong></p>
<ul>
<li style="line-height: 1.5">自由形式的文本输出：低精确性（1-3）</li>
<li style="line-height: 1.5">半结构化输出：中精确性（4-7）</li>
<li style="line-height: 1.5">严格结构化输出（JSON、XML）：高精确性（8-10）</li>
</ul>
<p>  <strong style="font-size: 16px">2.2.2 精确度要求：事实的准确性有多么重要？</Strong></p>
<ul>
<li style="line-height: 1.5">创意类内容：低精确性（1-3）</li>
<li style="line-height: 1.5">信息类内容：中精确性（4-7）</li>
<li style="line-height: 1.5">关键类信息：高精确性（8-10）</li>
</ul>
<p>  <strong style="font-size: 16px">2.2.3 可再现性要求：各批次输出的结果需要有多么强的一致性要求？</Strong></p>
<ul>
<li style="line-height: 1.5">能够接收可变性：低精确性（1-3）</li>
<li style="line-height: 1.5">有一定的一致性要求：中精确性（4-7）</li>
<li style="line-height: 1.5">需要精确的可再现性：高精确性（8-10）</li>
</ul>
<p>  <strong style="font-size: 16px">2.2.4 错误容忍度：错误结果的影响是什么？</Strong></p>
<ul>
<li style="line-height: 1.5">影响不大：低精确性（1-3）</li>
<li style="line-height: 1.5">中度影响：中精确性（4-7）</li>
<li style="line-height: 1.5">影响很大：高精确性（5-10）</li>
</ul>
<p>  <strong style="font-size: 18px">2.3 映射到 “Complexity vs. Precision” 矩阵</Strong></p>
<p>  根据上面两个步骤得到的总体复杂性得分、精确性要求得分，映射到对应的四象限的某一个象限，得出适合的当前要构建的应用的选型方法：</p>
<ul>
<li style="line-height: 1.5">低复杂性 (1-4), 低精确性 (1-4)：Simple Crews</li>
<li style="line-height: 1.5">低复杂性 (1-4), 高精确性 (5-10)：Flows with direct LLM calls</li>
<li style="line-height: 1.5">高复杂性 (5-10), 低精确性 (1-4)：Complex Crews</li>
<li style="line-height: 1.5">高复杂性 (5-10), 高精确性 (5-10)：Flows orchestrating Crews</li>
</ul>
<p>  <strong style="font-size: 16px">2.4 需要考虑的其他因素</Strong></p>
<ul>
<li style="line-height: 1.5"><strong>开发时间</strong>：Crew 一般用于快速构建原型</li>
<li style="line-height: 1.5"><strong>运维要求</strong>：Flow 能够更好地支持长期可维护性</li>
<li style="line-height: 1.5"><strong>团队专业性</strong>：考虑团队对其他方法的熟悉程度</li>
<li style="line-height: 1.5"><strong>可扩展性要求</strong>：Flow 能够更好地支持扩展以适应复杂应用场景</li>
<li style="line-height: 1.5"><strong>集成要求</strong>：考虑解决方案与现有系统怎样进行集成</li>
</ul>
<p><strong style="font-size: 20px">3. Crew 与 Flow 选择参考</Strong></p>
<p>Crew 与 Flow 是 CrewAI 框架中两个非常核心的组件，通过它们可以构建满足各种需求的应用程序。深入理解在什么场景下使用 Crew 或 Flow 更加合适，能够更好地指导我们做出更佳的决策。</p>
<p><strong style="font-size: 18px">3.1 Crew 是什么？</Strong></p>
<p>在 CrewAI中，Crew 代表了一组协同工作的 Agent，它们能够共同完成一系列任务。每个 Crew 都定义了任务执行、Agent 协作和整体 Workflow 的策略。<br />
有关 Crew 更详细的说明和使用，可以参考官网文档 <a target="_blank" href="https://docs.crewai.com/concepts/crews">https://docs.crewai.com/concepts/crews</a>。</p>
<p><strong style="font-size: 18px">3.2 Flow 是什么？</Strong></p>
<p>Flow 是 CrewAI 提供的一个强大的特性，它旨在简化 AI Workflow 的创建和管理。Flow 允许开发人员高效地组合并协调编码任务（Task）和 Crew，为构建复杂的 AI 自动化应用程序提供了一个强大的框架。<br />
Flow 允许我们创建结构化的、事件驱动的 Workflow，并提供了一种无缝的方式来连接多个任务、管理状态和控制 AI 应用程序中的执行流。借助 Flow，我们可以轻松设计和实现具有多个步骤的 Workflow，更加充分地发挥出 CrewAI 的潜力。<br />
有关 Flow 更详细的说明和使用，可以参考官网文档 <a target="_blank" href="https://docs.crewai.com/concepts/flows">https://docs.crewai.com/concepts/flows</a>。</p>
<p><strong style="font-size: 18px">3.3 使用 Crew 的最佳场景</Strong></p>
<ul>
<li style="line-height: 1.5">需要更智能的协作：具有不同专业化能力的多个 Agent 共同协作完成任务</li>
<li style="line-height: 1.5">待解决的问题更依赖思维涌现：解决方案受益于不同的视角和方法</li>
<li style="line-height: 1.5">具有创造性或分析性能力的任务：工作涉及研究、内容创建或分析</li>
<li style="line-height: 1.5">强调灵活性胜过严格的结构：Workflow 受益于 Agent 的自主性</li>
<li style="line-height: 1.5">输出结构具有一定的灵活性：能够接受输出结构有一定的变化</li>
</ul>
<p><strong style="font-size: 18px">3.4 使用 Flow 的最佳场景</Strong></p>
<ul>
<li style="line-height: 1.5">需要对任务执行做精确控制：Workflow 需要精确排序和状态管理</li>
<li style="line-height: 1.5">应用具有复杂的状态：需要跨多个步骤对状态进行维护与转换</li>
<li style="line-height: 1.5">需要结构化的、可预测的输出：应用程序要求一致性、格式化的结果</li>
<li style="line-height: 1.5">Workflow 包含条件逻辑：对于不同的中间结果需要有不同的执行路径</li>
<li style="line-height: 1.5">需要将 AI 与过程性代码相结合：方案要求兼具 AI 能力与传统编程能力</li>
</ul>
<p><strong style="font-size: 18px">3.5 混合使用 Crew 与 Flow 的最佳场景</Strong></p>
<ul>
<li style="line-height: 1.5">复杂多阶段处理场景：使用 Flow 编排整体流程，使用 Crew 实现复杂子任务</li>
<li style="line-height: 1.5">具有创意性和结构性要求的场景：使用 Crew 实现创意性的部分，使用 Flow 实现结构化处理的部分</li>
<li style="line-height: 1.5">企业级 AI 应用场景：使用 Flow 管理状态和处理流程，使用 Crew 实现具有专业性要求的任务</li>
</ul>
<p><strong style="font-size: 20px">4. 总结</Strong></p>
<p>使用 CrewAI 框架构建 AI Agent 应用程序，需要在 Crew 和 Flow 之间进行选择，或者将它们组合在一起来使用，这样一个关键的架构决策过程，能够影响到我们使用 CrewAI 应用程序的有效性、可维护性和可扩展性。通过对对应场景的复杂性和精确性进行评估，能够更好地做出与特定要求相匹配的明智决策。<br />
一个推荐的方法是，随着需求的不断细化而不断调整我们的决策，以满足不同阶段的不同需要。我们可以从最简单的解决方案开始，随着经验的积累和需求的明确，不断地调整和完善架构，最终达到我们期望实现的业务目标或系统目标。</p>
<p><strong style="font-size: 20px">5. 参考链接</Strong></p>
<ul>
<li><a target="_blank" href="https://docs.crewai.com/guides/concepts/evaluating-use-cases#practical-evaluation-framework">Evaluating Use Cases for CrewAI</a></li>
<li><a target="_blank" href="https://docs.crewai.com/concepts/crews">https://docs.crewai.com/concepts/crews</a></li>
<li><a target="_blank" href="https://docs.crewai.com/concepts/flows">https://docs.crewai.com/concepts/flows</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2827.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>探索 CrewAI Knowledge 实现 RAG 应用</title>
		<link>http://shiyanjun.cn/archives/2823.html</link>
		<comments>http://shiyanjun.cn/archives/2823.html#comments</comments>
		<pubDate>Wed, 12 Mar 2025 05:31:05 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[Agent]]></category>
		<category><![CDATA[CrewAI]]></category>
		<category><![CDATA[人工智能]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2823</guid>
		<description><![CDATA[CrewAI 的 Knowledge 特性能够使我们非常方便地访问外部信息源，这些信息源可以是特定领域的数据， [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>CrewAI 的 Knowledge 特性能够使我们非常方便地访问外部信息源，这些信息源可以是特定领域的数据，也可以是 Agent 为完成某个 Task 而需要指定会话上下文数据。可见，通过 Knowledge 特性我们可以实现基于 RAG 架构的 AI Agent 应用。<br />
CrewAI 支持的 Knowledge Source 主要包括两大类：<br />
一类是 Text Source，其中有 Raw String、Text File、PDF Document；<br />
一类是 Structured Data，其中有 CVS File、Excel SpreadSheet、JSON Document。<br />
当然，为了实现 AI Agent 应用的 Task 在执行过程中能够访问外部信息源，CrewAI 框架还支持其他各种的信息源，在 CrewAI 官网文档中可以在 “Tools” 一节中看到这些内置实现好的 Tool，能够非常方便地开箱即用，实现类似 RAG 的功能。<br />
下面列出一些可能会比较常用的 Tool 集合：</p>
<ul>
<li><a target="_blank" href="https://docs.crewai.com/tools/csvsearchtool">CSV RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/directorysearchtool">Directory RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/docxsearchtool">DOCX RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/jsonsearchtool">JSON RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/mdxsearchtool">MDX RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/mysqltool">MySQL RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/pdfsearchtool">PDF RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/pgsearchtool">PG RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/qdrantvectorsearchtool">Qdrant Vector Search Tool</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/txtsearchtool">TXT RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/xmlsearchtool">XML RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/websitesearchtool">Website RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/youtubechannelsearchtool">YouTube Channel RAG Search</a></li>
<li><a target="_blank" href="https://docs.crewai.com/tools/youtubevideosearchtool">YouTube Video RAG Search</a></li>
</ul>
<p>下面，我们通过使用 PDFSearchTool 来快速探索 CrewAI 支持 RAG 的能力，以及探索 StringKnowledgeSource、PDFKnowledgeSource 这两个非常基础的 Knowledge Source 的使用，以此来了解 CrewAI 对 RAG 能力的支持和使用方法。</p>
<p><strong style="font-size: 20px">PDFSearchTool 使用示例</Strong></p>
<p>我们通过 CrewAI 提供的 PDFSearchTool 来快速实现一个简单的用户问答系统：回答用户的提问，都是基于给定的 PDF 文档的内容来进行回答，这样就把给定的文档作为会话的知识库了（这里，我是基于 <a target="_blank" href="https://github.com/drewzeee/crewai-rag.git">https://github.com/drewzeee/crewai-rag.git</a> 这个例子简单改了一下，基本业务逻辑未变，但是使用了本地 Ollama 管理的 DeepSeek-R1 模型来进行推理）。这个例子的基本功能是：<br />
设置两个 Agent，一个是 Research Agent，负责检索给定的 PDF 文档，获取与用户提问相关的内容片段；另一个是 Professional Writer Agent，它根据 Research Agent 检索得到的内容，写一封满足指定要求的 Email 正文。<br />
具体实现的完整代码，如下所示：</p>
<pre class="brush: python; title: ; notranslate">
from crewai import Agent, Crew, Process, Task, LLM
from crewai_tools import PDFSearchTool
import litellm

litellm._logging._turn_on_debug()

# Initialize local LLM
llm = LLM(
    api_key=&quot;NA&quot;,
    temperature=0,
    model=&quot;ollama/deepseek-r1:1.5b&quot;,
    base_url=&quot;http://localhost:11434&quot;)
print(&quot;Local LLM: &quot;, llm)

# PDF SOURCE: https://www.gpinspect.com/wp-content/uploads/2021/03/sample-home-report-inspection.pdf
pdf_search_tool = PDFSearchTool(
    pdf=&quot;./example_home_inspection.pdf&quot;,
    config=dict(
        llm=dict(
            provider=&quot;ollama&quot;,
            config=dict(
                model=&quot;deepseek-r1:1.5b&quot;,
            ),
        ),
        embedder=dict(
            provider=&quot;ollama&quot;,
            config=dict(
                model=&quot;nomic-embed-text:latest&quot;,
            ),
        ),
    )
)

research_agent = Agent(
    role=&quot;Research Agent&quot;,
    goal=&quot;Search through the PDF to find relevant answers&quot;,
    allow_delegation=False,
    verbose=True,
    backstory=(
        &quot;&quot;&quot;
        The research agent is adept at searching and
        extracting data from documents, ensuring accurate and prompt responses.
        &quot;&quot;&quot;
    ),
    tools=[pdf_search_tool],
    llm=llm
)

professional_writer_agent = Agent(
    role=&quot;Professional Writer&quot;,
    goal=&quot;Write professional emails based on the research agent's findings&quot;,
    allow_delegation=False,
    verbose=True,
    backstory=(
        &quot;&quot;&quot;
        The professional writer agent has excellent writing skills and is able to craft
        clear and concise emails based on the provided information.
        &quot;&quot;&quot;
    ),
    tools=[],
    llm=llm
)

# --- Tasks ---
answer_customer_question_task = Task(
    description=(
        &quot;&quot;&quot;
        Answer the customer's questions based on the home inspection PDF.
        The research agent will search through the PDF to find the relevant answers.
        Your final answer MUST be clear and accurate, based on the content of the home
        inspection PDF.

        Here is the customer's question:
        {customer_question}
        &quot;&quot;&quot;
    ),
    expected_output=&quot;&quot;&quot;
        Provide clear and accurate answers to the customer's questions based on
        the content of the home inspection PDF.
        &quot;&quot;&quot;,
    tools=[pdf_search_tool],
    agent=research_agent,
)

write_email_task = Task(
    description=(
        &quot;&quot;&quot;
        - Write a professional email to a contractor based
            on the research agent's findings.
        - The email should clearly state the issues found in the specified section
            of the report and request a quote or action plan for fixing these issues.
        - Ensure the email is signed with the following details:

            Best regards,

            Brandon Hancock,
            Hancock Realty
        &quot;&quot;&quot;
    ),
    expected_output=&quot;&quot;&quot;
        Write a clear and concise email that can be sent to a contractor to address the
        issues found in the home inspection report.
        &quot;&quot;&quot;,
    tools=[],
    agent=professional_writer_agent,
)

crew = Crew(
    agents=[research_agent, professional_writer_agent],
    tasks=[answer_customer_question_task, write_email_task],
    process=Process.sequential,
)

customer_question = input(
    &quot;Which section of the report would you like to generate a work order for?\n&quot;
)
result = crew.kickoff_for_each(inputs=[{&quot;customer_question&quot;: &quot;Bathroom and Components&quot;}])
print(result)
</pre>
<p>上面代码中，我们输入的用户问题为：Bathroom and Components，亦即从 PDF 中的这个段落里检索相关内容，并最终生成一个 Work Order 的 Email 正文。<br />
运行代码，我们可以通过日志来看一看程序执行过程中的一些关键内容：</p>
<p><strong style="font-size: 16px">1、使用自定义 LLM 与 Embedding 模型</Strong></p>
<p>在 PDFSearchTool 中通过 config 来指定自己想使用的 LLM 和 Embedding 模型，如下所示：</p>
<pre class="brush: python; title: ; notranslate">
  config=dict(
      llm=dict(
          provider=&quot;ollama&quot;,
          config=dict(
              model=&quot;deepseek-r1:1.5b&quot;,
          ),
      ),
      embedder=dict(
          provider=&quot;ollama&quot;,
          config=dict(
              model=&quot;nomic-embed-text:latest&quot;,
          ),
      ),
  )
</pre>
<p>如果不指定默认的 Embedding 模型，PDFSearchTool 默认会使用 OpenAI 的 Embedding 模型，直接在本地调试运行可能会调用 OpenAI 的 API 失败而无法使用。另外，在运行程序之前，我们需要在本地通过 Ollama 下载 nomic-embed-text 模型，并启动 Ollama 服务，这样程序才能正常运行。</p>
<p><strong style="font-size: 16px">2、Research Agent 检索 PDF 文档</Strong></p>
<p>程序运行过程中，会把 PDF 文档内容的 Embedding 数据写入 Chromadb，如下所示：</p>
<pre class="brush: plain; title: ; notranslate">
Local LLM:  &lt;crewai.llm.LLM object at 0x122f65e20&gt;
/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/embedchain/embedder/ollama.py:27: LangChainDeprecationWarning: The class `OllamaEmbeddings` was deprecated in LangChain 0.3.1 and will be removed in 1.0.0. An updated version of the class exists in the :class:`~langchain-ollama package and should be used instead. To use it run `pip install -U :class:`~langchain-ollama` and import as `from :class:`~langchain_ollama import OllamaEmbeddings``.
  embeddings = OllamaEmbeddings(model=self.config.model, base_url=config.base_url)
Inserting batches in chromadb: 100%|██████████| 3/3 [02:14&lt;00:00, 44.84s/it]
</pre>
<p>我们可以看到，在当前目录下生成了一个 db 文件目录，里面保存对应的数据：</p>
<pre class="brush: plain; title: ; notranslate">
db
├── 3b95287f-b015-43cd-937c-4a45617dff1c
│   ├── data_level0.bin
│   ├── header.bin
│   ├── length.bin
│   └── link_lists.bin
└── chroma.sqlite3
</pre>
<p><strong style="font-size: 16px">3、Research Agent 检索 PDF 文档</Strong></p>
<p>Research Agent 检索 PDF 文档，检索关键词是 Roof，通过运行日志可以看到得到的内容，如下所示：</p>
<pre class="brush: plain; title: ; notranslate">
# Agent: Research Agent
## Final Answer:
The home inspection PDF provides detailed information about the bathroom system, including water heater,龙头, Tank, Shower, and Sink. It also outlines fixtures like showerheads, vanities, and basic components such as flushers and toilet tanks.
</pre>
<p>这是 Research Agent 从 PDF 文档中检索到的相关内容。</p>
<p><strong style="font-size: 16px">4、Professional Writer Agent 与 LLM 交互</Strong></p>
<p>将 Research Agent 检索得到的结果内容作为 Professional Writer Agent 的会话上下文信息，执行具体的任务，我们可以看到此时发送给 LLM 的请求信息，如下所示：</p>
<pre class="brush: plain; title: ; notranslate">
POST Request Sent from LiteLLM:
curl -X POST \
http://localhost:11434/api/generate \
-d '{'model': 'deepseek-r1:1.5b', 'prompt': &quot;### System:\nYou are Professional Writer. \n        The professional writer agent has excellent writing skills and is able to craft \n        clear and concise emails based on the provided information.\n        \nYour personal goal is: Write professional emails based on the research agent's findings\nTo give my best complete final answer to the task respond using the exact following format:\n\nThought: I now can give a great answer\nFinal Answer: Your final answer must be the great and the most complete as possible, it must be outcome described.\n\nI MUST use these formats, my job depends on it!\n\n### User:\n\nCurrent Task: \n        - Write a professional email to a contractor based \n            on the research agent's findings.\n        - The email should clearly state the issues found in the specified section \n            of the report and request a quote or action plan for fixing these issues.\n        - Ensure the email is signed with the following details:\n        \n            Best regards,\n\n            Brandon Hancock,\n            Hancock Realty\n        \n\nThis is the expected criteria for your final answer: \n        Write a clear and concise email that can be sent to a contractor to address the \n        issues found in the home inspection report.\n        \nyou MUST return the actual complete content as the final answer, not a summary.\n\nThis is the context you're working with:\nThe home inspection PDF provides detailed information about the bathroom system, including water heater,龙头, Tank, Shower, and Sink. It also outlines fixtures like showerheads, vanities, and basic components such as flushers and toilet tanks.\n\nBegin! This is VERY important to you, use the tools available and give your best Final Answer, your job depends on it!\n\nThought:\n\n### User:\nI did it wrong. Invalid Format: I missed the 'Action:' after 'Thought:'. I will do right next, and don't use a tool I have already used.\n\nIf you don't need to use any more tools, you must give your best complete final answer, make sure it satisfies the expected criteria, use the EXACT format below:\n\n```\nThought: I now can give a great answer\nFinal Answer: my best complete final answer to the task.\n\n```\n\n&quot;, 'options': {'temperature': 0, 'stop': ['\nObservation:']}, 'stream': False}'
</pre>
<p>可以看到，以 “This is the context you&#8217;re working with:” 开始的内容，就是 Research Agent 指派的 Task 执行检索 PDF 文档的结果内容，它作为 Professional Writer Agent 执行 Task 的上下文信息，提供了与用户提出的问题相关的会话上下文。</p>
<p><strong style="font-size: 16px">5、Professional Writer Agent 生成最终的结果</Strong></p>
<p>可以通过运行日志看到，Professional Writer Agent 按照指定的要求，生成了最终的 Work Order Email 正文，如下所示：</p>
<pre class="brush: plain; title: ; notranslate">
# Agent: Professional Writer
## Final Answer:
Brandon Hancock, Hancock Realty
Dear Brandon Hancock,

I am writing to address the findings from the bathroom system inspection report. The key issues identified include water heater performance,龙头 pressure, Tank leaks, Shower fixtures not functioning properly, and potential issues with vanities or flushers.

We request quotes for component replacements and an action plan to address these problems promptly. Please let us know when you can provide more details on the specific components that need attention.

Best regards,
Brandon Hancock
Hancock Realty
</pre>
<p>至此，基于 PDFSearchTool，并且具有两个 Agent 的例子，两个 Agent 进行协作解决了用户提出的问题。</p>
<p><strong style="font-size: 20px">CrewAI Knowledge 使用示例</Strong></p>
<p>在 CrewAI 中，Agent 和 Crew 都具有 knowledge_sources 属性，所以 CrewAI 能够分别支持 Agent 和 Crew 两个不同级别的 Knowledge 设置，它们使 Knowledge 限定在了指定的作用域之内。<br />
使用 CrewAI Knowledge 特性的过程中，会遇到各种问题，我们可以通过例子来具体说明使用 StringKnowledgeSource 和 PDFKnowledgeSource。</p>
<p><strong style="font-size: 18px">使用 StringKnowledgeSource 示例</Strong></p>
<p>根据给定的字符串，回答用户的提问，这个例子比较简单，示例代码如下所示：</p>
<pre class="brush: python; title: ; notranslate">
...

from crewai import Agent, Task, Crew, Process, LLM
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource

content = &quot;Users name is John. He is 30 years old and lives in San Francisco.&quot;
string_source = StringKnowledgeSource(
    content=content,
)

# Create an agent with the knowledge store
agent = Agent(
    role=&quot;About User&quot;,
    goal=&quot;You know everything about the user.&quot;,
    backstory=&quot;&quot;&quot;You are a master at understanding people and their preferences.&quot;&quot;&quot;,
    verbose=True,
    allow_delegation=False,
    llm=llm,
    knowledge_sources=[string_source],
    embedder=dict(
            provider=&quot;ollama&quot;,
            config=dict(
                model=&quot;nomic-embed-text:latest&quot;,
                base_url=&quot;http://localhost:11434&quot;,
            ),
        ),
)

task = Task(
    description=&quot;Answer the following questions about the user: {question}&quot;,
    expected_output=&quot;An answer to the question.&quot;,
    agent=agent,
)

crew = Crew(
    agents=[agent],
    tasks=[task],
    verbose=True,
    process=Process.sequential,
)

result = crew.kickoff(inputs={&quot;question&quot;: &quot;What city does John live in and how old is he?&quot;})
</pre>
<p>我们可以看到，Agent 基于输入的字符串信息作为会话上下文，正确回答了用户提出的问题：</p>
<pre class="brush: plain; title: ; notranslate">
# Agent: About User
## Final Answer:
John is 30 years old and lives in San Francisco.
</pre>
<p>我们需要注意两个问题：</p>
<p><strong style="font-size: 16px">1、指定自己的 Embedding 模型配置</Strong></p>
<p>通过 Agent 的 embedder 来指定希望使用的 Embedding 模型的配置，具体参考前面说明，不再赘述。</p>
<p><strong style="font-size: 16px">2、Knowledge Source 文本长度对 Agent 有影响</Strong></p>
<p>上面代码中，我们使用的 StringKnowledgeSource 对应的字符串文本长度比较短，在运行过程中不会出现问题。但是如果文本长度比过长，就会报错，这时需要对 StringKnowledgeSource 进行分块配置，需要指定对应的配置，示例如下：</p>
<pre class="brush: python; title: ; notranslate">
content = &quot;... ...（长度很长的字符串文本）&quot;
string_source = StringKnowledgeSource(
    content=content,
    chunk_size=256,  # Maximum size of each chunk (default: 4000)
    chunk_overlap=32  # Overlap between chunks (default: 200)
)
</pre>
<p>需要注意的是，对所有的 Knowledge Source，如果内容过长都需要进行分块配置，否则程序报错。以我使用的为例，主要是在写入 Chromadb 时超时，错误信息如下：</p>
<pre class="brush: plain; title: ; notranslate">
[2025-03-12 13:08:02][ERROR]: Failed to upsert documents: timed out in upsert.
Traceback (most recent call last):
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpx/_transports/default.py&quot;, line 72, in map_httpcore_exceptions
    yield
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpx/_transports/default.py&quot;, line 236, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py&quot;, line 256, in handle_request
    raise exc from None
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py&quot;, line 236, in handle_request
    response = connection.handle_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_sync/connection.py&quot;, line 103, in handle_request
    return self._connection.handle_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_sync/http11.py&quot;, line 136, in handle_request
    raise exc
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_sync/http11.py&quot;, line 106, in handle_request
    ) = self._receive_response_headers(**kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_sync/http11.py&quot;, line 177, in _receive_response_headers
    event = self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_sync/http11.py&quot;, line 217, in _receive_event
    data = self._network_stream.read(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_backends/sync.py&quot;, line 126, in read
    with map_exceptions(exc_map):
         ^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/contextlib.py&quot;, line 158, in __exit__
    self.gen.throw(value)
  File &quot;/Users/dean/anaconda3/envs/crewai/lib/python3.12/site-packages/httpcore/_exceptions.py&quot;, line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ReadTimeout: timed out
</pre>
<p>根据实际情况进行分块配置，就能解决这个问题。</p>
<p><strong style="font-size: 18px">使用 PDFKnowledgeSource 示例</Strong></p>
<p>使用 PDFKnowledgeSource，示例代码如下所示：</p>
<pre class="brush: python; title: ; notranslate">
...

from crewai.knowledge.source.pdf_knowledge_source import PDFKnowledgeSource

# Create a PDF knowledge source
pdf_source = PDFKnowledgeSource(
    file_paths=[&quot;kde_example_home_inspection.pdf&quot;],
    # chunk_size=256,  # Maximum size of each chunk (default: 4000)
    # chunk_overlap=32  # Overlap between chunks (default: 200)
)

# Create an agent with the knowledge store
agent = Agent(
    role=&quot;Report Analyst&quot;,
    goal=&quot;You know everything about the report.&quot;,
    backstory=&quot;&quot;&quot;You are a master at understanding the content of the given report.&quot;&quot;&quot;,
    verbose=True,
    allow_delegation=False,
    llm=llm,
    knowledge_sources=[pdf_source],
    embedder=dict(
            provider=&quot;ollama&quot;,
            config=dict(
                model=&quot;nomic-embed-text:latest&quot;,
                base_url=&quot;http://localhost:11434&quot;,
            ),
        ),
)

task = Task(
    description=&quot;Answer the following questions about the report: {question}&quot;,
    expected_output=&quot;An answer to the question.&quot;,
    agent=agent,
)

crew = Crew(
    agents=[agent],
    tasks=[task],
    verbose=True,
    process=Process.sequential,
)

crew.kickoff(inputs={&quot;question&quot;: &quot;What is the percentage of the lighting use typically?&quot;})
</pre>
<p><strong style="font-size: 20px">总结</Strong></p>
<p>在使用过程中发现，如果 Agent 的属性 role 的内容为中文时，程序就会报错。在跟踪代码过程中发现，会把 role 的内容经过处理作为 collection name，而中文的 role 内容转换成的 collection name 是无法通过 CrewAI 框架代码的校验的，直接会报错。出现这个问题有可能和环境有关，也可能是其它原因，具体解决办法就是改成英文描述，即可暂时解决这个问题。</p>
<p>在使用 AI Agent 实现我们计划的功能的过程中，如果发现达不到预期，这时往往可能有如下几个原因：<br />
1、对自己项目的应用场景的要求并未十分明确，比如应用复杂度、结果精确度，只有选择框架提供的合适的特性才能满足我们项目的要求；<br />
2、和选择的 LLM 支持的语言有关，比如有的 LLM 对英文支持较好，有的对中文支持较好；<br />
3、评估所选择的 LLM 推理能力，如果推理能力不好，也比较难达到预期结果；<br />
4、设置 Agent、Task 的提示词需要一定的技巧，描述不清楚可能推理出来的结果就不好。</p>
<p><strong style="font-size: 20px">参考链接</Strong></p>
<ul>
<li><a target="_blank" href="https://docs.crewai.com/concepts/knowledge">https://docs.crewai.com/concepts/knowledge</a></li>
<li><a target="_blank" href="https://github.com/drewzeee/crewai-rag.git">https://github.com/drewzeee/crewai-rag.git</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2823.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用 CrewAI 开发 AI Agent 应用</title>
		<link>http://shiyanjun.cn/archives/2813.html</link>
		<comments>http://shiyanjun.cn/archives/2813.html#comments</comments>
		<pubDate>Wed, 19 Feb 2025 07:54:22 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[CrewAI]]></category>
		<category><![CDATA[人工智能]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2813</guid>
		<description><![CDATA[CrewAI 是一个构建 AI Agent 应用的框架，可以非常方便地开发 AI Agent 应用，支持多个  [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>CrewAI 是一个构建 AI Agent 应用的框架，可以非常方便地开发 AI Agent 应用，支持多个 Agent 之间协作以实现特定的任务。有关 CrewAI 中关于 Crew、Agent、Task 等概念，可以参考官网文档了解，下面是官网给出的一个 CrewAI 框架的设计概览，如下图所示：<br />
<img src="https://mintlify.s3.us-west-1.amazonaws.com/crewai/crewAI-mindmap.png" alt="CrewAI Framework Overview" width="645" height="442" class="alignnone size-full" /><br />
在使用 CrewAI 开发 AI Agent 应用时，为了方便快速测试流程，可以采用直观的编码方式来体验或验证基本流程，也可以采用工程的方式构建更加复杂的 AI Agent 应用。下面，我们基于 CrewAI 分别通过这两种方式实现一个简单的 AI Agent 应用。</p>
<p><strong style="font-size: 20px">直接编码开发构建</Strong></p>
<p>下面，我们通过一个简单的例子，通过直观编码的来实现基本的 AI Agent，通过一个 Agent 和一个 Task 完成报表的查询和可视化输出。为了方便，可以使用 JupyterLab 开发和调试程序。</p>
<p><strong style="font-size: 16px">1.准备工作</Strong></p>
<p>在本地使用 Ollama 来运行大模型，从 Ollama 官网下载并安装 Ollama，为方便调试先安装 DeepSeek R1 7B 的模型，大约 5G 大小：</p>
<pre class="brush: bash; title: ; notranslate">
ollama run deepseek-r1:7b
</pre>
<p>在命令行中可以输入自然语言进行推理，说明安装成功。<br />
然后，在本地安装 CrewAI 的工具包（这里我使用了一个 Anaconda 虚拟环境）：</p>
<pre class="brush: bash; title: ; notranslate">
conda create -n crewai python=3.12
conda activate CrewAI
pip install 'crewai[tools]'
</pre>
<p>下面可以开发我们的程序，如果需要可以打开 CrewAI 的 Debug 功能：</p>
<pre class="brush: python; title: ; notranslate">
from langchain_openai import ChatOpenAI
from crewai import LLM

# Turn debug for LLM
import litellm
litellm._logging._turn_on_debug()
</pre>
<p><strong style="font-size: 16px">2.初始化大模型 LLM</Strong></p>
<p>可以使用本地的 LLM，实现代码如下所示：</p>
<pre class="brush: python; title: ; notranslate">
# Initialize local LLM
llm = ChatOpenAI(
    api_key = &quot;NA&quot;,
    model = &quot;ollama/deepseek-r1:7b&quot;,
    base_url = &quot;http://localhost:11434/v1&quot;)
print(&quot;Local LLM: &quot;, llm)
</pre>
<p>也可以使用调用 API 的方式，代码如下所示：</p>
<pre class="brush: python; title: ; notranslate">
# Connect remote LLM API
llm = LLM(
    api_key = &quot;sk-xxxxxxx&quot;,
    model = &quot;deepseek/Pro/deepseek-ai/DeepSeek-R1&quot;,
    base_url = &quot;https://api.xxxx.com/v1&quot;,
    temperature=0.3)
print(&quot;Remote LLM: &quot;, llm)
</pre>
<p>使用大模型服务提供商提供的 API，一般需要指定 API Key、模型名称、Base URL，根据自己的情况进行选择。这里需要说明的是，使用 CrewAI 时模型名称 model 的值要求满足格式“LLM 提供商名称/模型名称”，否则运行代码时无法通过验证，具体地 LLM 提供商可以查看 LiteLLM 网站 <a target="_blank" href="https://docs.litellm.ai/docs/providers/">https://docs.litellm.ai/docs/providers/</a> 支持哪些 LLM Provider。</p>
<p><strong style="font-size: 16px">3.实现 CrewAI Agent、Task</Strong></p>
<p>创建一个 CrewAI 应用，需要实现 Agent、Task，还有构成它们的 Tools。我们实现的简单功能是，通过向 AI Agent 提问，查询指定的接口获取统计结果数据，并实现简单的可视化。这里，抽象出一个 Tool、一个 Task、一个 Agent，对应的实现代码如下所示：</p>
<pre class="brush: python; title: ; notranslate">
# Create every agent &amp; tool &amp; task
from crewai import Agent, Task, Crew
from crewai.tools import BaseTool

# Define available tools
class ReportQueryTool(BaseTool):
    name: str = &quot;报表查询工具&quot;
    description: str = &quot;根据输入日期范围，查询统计结果数据&quot;

    def _run(self, start_date: str, end_date: str) -&gt; str:
        &quot;&quot;&quot;
        根据日期范围查询，结果是按天、城市统计的活跃用户数报表结果数据。
        参数:
            - start_date: 开始日期，格式为 yyyyMMdd
            - end_date: 截止日期，格式为 yyyyMMdd
        返回:
            - 查询结果以 JSON 格式字符串返回，结果包含按天、城市两个维度的活跃用户数统计结果
        &quot;&quot;&quot;
        return '[{&quot;dt&quot;:&quot;20240101&quot;,&quot;city&quot;:&quot;BJ&quot;,&quot;active_user_cnt&quot;:12765},{&quot;dt&quot;:&quot;20240101&quot;,&quot;city&quot;:&quot;SH&quot;,&quot;active_user_cnt&quot;:823222},{&quot;dt&quot;:&quot;20240102&quot;,&quot;city&quot;:&quot;BJ&quot;,&quot;active_user_cnt&quot;: 53025},{&quot;dt&quot;:&quot;20240102&quot;,&quot;city&quot;:&quot;SH&quot;,&quot;active_user_cnt&quot;:92001}]'

# Define agent
report_agent = Agent(
    role = &quot;数据报表分析助手&quot;,
    goal = &quot;负责查询统计结果报表数据，并进行可视化&quot;,
    backstory = &quot;使用提供的工具查询统计结果数据，然后自主实现数据可视化&quot;,
    llm = llm,
    tools = [ReportQueryTool()],
    verbose = True
)
print(&quot;Agent(report_agent): &quot;, report_agent)

# Define our task
report_task = Task(
    description = &quot;查询2024年1月用户活跃统计结果，然后自主生成简单的可视化表格&quot;,
    agent = report_agent,
    tools = [ReportQueryTool()],
    expected_output=&quot;包含日期、城市、活跃用户数三个字段的简单表格&quot;
)
print(&quot;Task(report_task): &quot;, report_task)
</pre>
<p>上面代码中，ReportQueryTool 模拟输入一个日期范围作为筛选条件，模拟返回查询到的统计结果记录行，使用 JSON 格式返回。然后配置 Agent 和 Task 都与该 Tool 关联。</p>
<p><strong style="font-size: 16px">4.实现 CrewAI 执行入口程序 Crew</Strong></p>
<p>每个 CrewAI 程序都包含一个执行入口，使用 CrewAI 提供的 Crew 类来实现，代码如下所示：</p>
<pre class="brush: python; title: ; notranslate">
# Define crew
crew = Crew(
    agents = [report_agent],
    tasks = [report_task],
    verbose = True,
    planning = False,
    full_output = True)

result = crew.kickoff()
print(result)
</pre>
<p>运行上面程序，可以看到执行过程，能够看到程序与 LLM 交互来实现推理并得到最终结果，示例结果输出如下所示：</p>
<pre class="brush: plain; title: ; notranslate">
# Agent: 数据报表分析助手
## Task: 查询用户活跃统计结果，然后自主生成简单的可视化表格


# Agent: 数据报表分析助手
## Using tool: 报表查询工具
## Tool Input:
&quot;{\&quot;start_date\&quot;: \&quot;2024-01-01\&quot;, \&quot;end_date\&quot;: \&quot;2024-01-31\&quot;}&quot;
## Tool Output:
[{&quot;dt&quot;:&quot;20240101&quot;,&quot;city&quot;:&quot;BJ&quot;,&quot;active_user_cnt&quot;:12765},{&quot;dt&quot;:&quot;20240101&quot;,&quot;city&quot;:&quot;SH&quot;,&quot;active_user_cnt&quot;:823222},{&quot;dt&quot;:&quot;20240102&quot;,&quot;city&quot;:&quot;BJ&quot;,&quot;active_user_cnt&quot;: 53025},{&quot;dt&quot;:&quot;20240102&quot;,&quot;city&quot;:&quot;SH&quot;,&quot;active_user_cnt&quot;:92001}]


# Agent: 数据报表分析助手
## Final Answer:
| 日期       | 城市 | 活跃用户数 |
|------------|------|------------|
| 2024-01-01 | BJ   | 12,765    |
| 2024-01-01 | SH   | 823,222   |
| 2024-01-02 | BJ   | 53,025    |
| 2024-01-02 | SH   | 92,001    |
```


| 日期       | 城市 | 活跃用户数 |
|------------|------|------------|
| 2024-01-01 | BJ   | 12,765    |
| 2024-01-01 | SH   | 823,222   |
| 2024-01-02 | BJ   | 53,025    |
| 2024-01-02 | SH   | 92,001    |
```
</pre>
<p><strong style="font-size: 20px">通过工程方式开发构建</Strong></p>
<p>对于复杂系统，可以将一些经常修改的内容，如提示词等放进配置文件里面，以方便按需修改，通过通过工程的方式构建就会比较方便一些。<br />
使用 CrewAI 的 CLI 命令可以创建一个 CrewAI 项目：</p>
<pre class="brush: bash; title: ; notranslate">
crewai create crew simple_report_app
</pre>
<p>可以看到生成的项目结构，如下所示：</p>
<pre class="brush: plain; title: ; notranslate">
simple_report_app/
├── .gitignore
├── pyproject.toml
├── README.md
├── .env
└── src/
    └── simple_report_app/
        ├── __init__.py
        ├── main.py
        ├── crew.py
        ├── tools/
        │   ├── custom_tool.py
        │   └── __init__.py
        └── config/
            ├── agents.yaml
            └── tasks.yaml
</pre>
<p>接下来，把前面我实现的代码，拆分并转换成对应的 YAML 配置文件、代码文件即可。</p>
<p><strong style="font-size: 16px">main.py</Strong></p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python

import sys
from simple_report_app.crew import SimpleReporterCrew

def run():
  &quot;&quot;&quot;
  Run the crew.
  &quot;&quot;&quot;
  inputs = {
    'topic': 'AI Agents'
  }
  SimpleReporterCrew().crew().kickoff(inputs=inputs)
</pre>
<p><strong style="font-size: 16px">crew.py</Strong></p>
<pre class="brush: python; title: ; notranslate">
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from custom_tool import ReportQueryTool

@CrewBase
class SimpleReporterCrew():
  &quot;&quot;&quot;SimpleReporter crew&quot;&quot;&quot;

  @agent
  def reporter(self) -&gt; Agent:
    return Agent(
      config = self.agents_config['reporter'],
      verbose = True,
      tools = [ReportQueryTool()]
    )

  @task
  def report_task(self) -&gt; Task:
    return Task(
      config =s elf.tasks_config['report_task'],
    )

  @crew
  def crew(self) -&gt; Crew:
    &quot;&quot;&quot;Creates the SimpleReporter crew&quot;&quot;&quot;
    return Crew(
      agents = self.agents, # Automatically created by the @agent decorator
      tasks = self.tasks, # Automatically created by the @task decorator
      process = Process.sequential,
      verbose = True,
    )
</pre>
<p><strong style="font-size: 16px">custom_tool.py</Strong></p>
<pre class="brush: python; title: ; notranslate">
from crewai.tools import BaseTool

# Define available tools
class ReportQueryTool(BaseTool):
    name: str = &quot;报表查询工具&quot;
    description: str = &quot;根据输入日期范围，查询统计结果数据&quot;

    def _run(self, start_date: str, end_date: str) -&gt; str:
        &quot;&quot;&quot;
        根据日期范围查询，结果是按天、城市统计的活跃用户数报表结果数据。
        参数:
            - start_date: 开始日期，格式为 yyyyMMdd
            - end_date: 截止日期，格式为 yyyyMMdd
        返回:
            - 查询结果以 JSON 格式字符串返回，结果包含按天、城市两个维度的活跃用户数统计结果
        &quot;&quot;&quot;
        return '[{&quot;dt&quot;:&quot;20240101&quot;,&quot;city&quot;:&quot;BJ&quot;,&quot;active_user_cnt&quot;:12765},{&quot;dt&quot;:&quot;20240101&quot;,&quot;city&quot;:&quot;SH&quot;,&quot;active_user_cnt&quot;:823222},{&quot;dt&quot;:&quot;20240102&quot;,&quot;city&quot;:&quot;BJ&quot;,&quot;active_user_cnt&quot;: 53025},{&quot;dt&quot;:&quot;20240102&quot;,&quot;city&quot;:&quot;SH&quot;,&quot;active_user_cnt&quot;:92001}]'
</pre>
<p><strong style="font-size: 16px">agents.yaml</Strong></p>
<pre class="brush: plain; title: ; notranslate">
reporter:
  role: &gt;
    数据报表分析助手
  goal: &gt;
    负责查询统计结果报表数据，并进行可视化
  backstory: &gt;
    使用提供的工具查询统计结果数据，然后自主实现数据可视化
</pre>
<p><strong style="font-size: 16px">tasks.yaml</Strong></p>
<pre class="brush: plain; title: ; notranslate">
report_task:
  description: &gt;
    查询2024年1月用户活跃统计结果，然后自主生成简单的可视化表格
  expected_output: &gt;
    包含日期、城市、活跃用户数三个字段的简单表格
  agent: reporter
</pre>
<p>我们可以修改项目配置文件 pyproject.toml 指定相应的配置，然后安装相关依赖：</p>
<pre class="brush: bash; title: ; notranslate">
cd simple_report_app
crewai install
</pre>
<p>如果安装没有问题，可以执行运行命令：</p>
<pre class="brush: bash; title: ; notranslate">
crewai run
</pre>
<p>输出和前面直接编码的方式，结果类似。因为每次都和 LLM 交互进行推理，有可能出现不一样的结果。如果得到的结果不满足预期，就要调整对应的提示词，最终达到我们期望的结果。</p>
<p><strong style="font-size: 20px">参考链接</Strong></p>
<ul>
<li><a target="_blank" href="https://docs.crewai.com/introduction">https://docs.crewai.com/introduction</a></li>
<li><a target="_blank" href="https://docs.crewai.com/installation">https://docs.crewai.com/installation</a></li>
<li><a target="_blank" href="https://docs.crewai.com/quickstart">https://docs.crewai.com/quickstart</a></li>
<li><a target="_blank" href="https://ollama.com">https://ollama.com</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2813.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GPT 系列模型技术演化之路</title>
		<link>http://shiyanjun.cn/archives/2749.html</link>
		<comments>http://shiyanjun.cn/archives/2749.html#comments</comments>
		<pubDate>Fri, 01 Mar 2024 03:19:46 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[LLM]]></category>
		<category><![CDATA[人工智能]]></category>
		<category><![CDATA[GPT]]></category>
		<category><![CDATA[GPT-1]]></category>
		<category><![CDATA[GPT-2]]></category>
		<category><![CDATA[GPT-3]]></category>
		<category><![CDATA[GPT-3.5]]></category>
		<category><![CDATA[GPT-4]]></category>
		<category><![CDATA[GPT-4V]]></category>
		<category><![CDATA[InstructGPT]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2749</guid>
		<description><![CDATA[GPT 系列模型对应的论文及 Tech Report，参考如下表所示： GPT 模型 Paper/Tech R [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>GPT 系列模型对应的论文及 Tech Report，参考如下表所示：</p>
<table style="border: solid thin #868686;width: 100%" border="1" cellpadding="2" cellspacing="0">
<tbody>
<tr>
<td style="width: 15%;text-align: center;border: solid thin #868686" valign="top"><b>GPT 模型</b></td>
<td style="width: 85%;text-align: center;border: solid thin #868686" valign="top"><b>Paper/Tech Report</b></td>
</tr>
<tr>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-1</td>
<td style="width: 85%;border: solid thin #868686" valign="top"><a target="_blank" href="https://arxiv.org/pdf/1810.04805.pdf">Improving Language Understanding by Generative Pre-Training</a></td>
</tr>
<tr>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-2</td>
<td style="width: 85%;border: solid thin #868686" valign="top"><a target="_blank" href="https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf">Language Models are Unsupervised Multitask Learners</a></td>
</tr>
<tr>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-3</td>
<td style="width: 85%;border: solid thin #868686" valign="top"><a target="_blank" href="https://arxiv.org/pdf/2005.14165.pdf">Language Models are Few-Shot Learners</a></td>
</tr>
<tr>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-3.5</td>
<td style="width: 85%;border: solid thin #868686" valign="top"><a target="_blank" href="https://arxiv.org/pdf/2203.02155.pdf">Training language models to follow instructions with human feedback</a></td>
</tr>
<tr>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-4</td>
<td style="width: 85%;border: solid thin #868686" valign="top"><a target="_blank" href="https://arxiv.org/pdf/2303.08774.pdf">GPT-4 Technical Report</a></td>
</tr>
<tr>
<td style="width: 15%;border: solid thin #868686" valign="top">GPT-4V</td>
<td style="width: 85%;border: solid thin #868686" valign="top"><a target="_blank" href="https://arxiv.org/pdf/2309.17421.pdf">The Dawn of LMMs: Preliminary Explorations with GPT-4V(ision)</a></td>
</tr>
</tbody>
</table>
<p>下面，对 GPT 系列每一个模型的网络架构和相关优化技术要点进行详细说明。</p>
<p><strong style="font-size: 20px">GPT-1 模型</strong></p>
<p>GPT-1 模型的基本架构，以及基于预训练模型进行微调应用于下游 NLP 任务的流程，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-1-Model-Architecture-and-Downstream-NLP-Tasks.png" alt="GPT-1-Model-Architecture-and-Downstream-NLP-Tasks" width="1024" height="417" class="alignnone size-large wp-image-2753" /><br />
GPT-1 模型的训练过程，可以分为两个阶段：</p>
<ul>
<li>无监督预训练（Unsupervised Pre-training）</li>
</ul>
<p>GPT-1 模型采用了基于自注意力（Self-Attention）机制的 Transformer 模型，模型架构只包括解码器（Encoder）部分。<br />
在预训练过程中，使用了掩码自注意力（Masked Self-Attention）机制实现自注意力计算，在计算自注意力权重时，模型只能关注当前位置及前面的位置，而不考虑未来的位置。<br />
GPT-1 预训练任务的目标是学习语言的通用表示，而不是将输入序列映射到目标序列，所以 GPT-1 模型选择了只采用解码器（Encoder）部分的神经网络架构。</p>
<ul>
<li>有监督微调（Supervised Fine-tuning）</li>
</ul>
<p>GPT-1 模型预训练完成后，模型为了能够更好地支持下游多种不同类型的 NLP 应用任务，模型需要进行有监督微调，即根据具体 NLP 任务的需求对数据进行标注，然后在进行模型的微调训练。模型微调的过程也称为迁移学习，利用已经预训练好的模型，能够节省巨大的计算成本，而且能够将预训练得到的模型的知识能力迁移到下游应用场景中，比如推理（Inference）、情感分析、相似度计算（Similarity Computation）、多项选择（Multiple Choice）、文本分类（Text Classification）、对话问答（Chat Q/A）等。<br />
在微调阶段，对特定任务的输入进行转换时，需要保证预训练阶段使用的词汇表和编码方案与微调阶段保持一致，否则得到的微调模型使用时会出现无法保持一致的语言表示、理解能力。</p>
<p><strong style="font-size: 18px">采用 BPE（Byte-Pair Encoding）分词算法</strong></p>
<p>GPT-1 采用 BPE（Byte-Pair Encoding）分词算法，它是一种基于数据压缩的可逆分词（Reversible Tokenization）方法，可以有效地减小词汇表的大小，从而减少模型需要学习的参数量。<br />
BPE 分词算法的基本步骤，如下所示：</p>
<ol>
<li>建立初始词表：将每个词/字符加入词表；</li>
<li>统计字符出现频率；</li>
<li>合并高频的字符对；</li>
<li>重复上面步骤 2 → 3。</li>
</ol>
<p>最后，将原始的文本压缩成立一种更短的形式，同时还保存了词/字符间频繁出现的模式。<br />
BPE 算法非常适合处理英文文本，而中文和英文完全不同，处理中文分词会更加复杂。</p>
<p><strong style="font-size: 18px">采用掩码多头注意力机制</strong></p>
<p>GPT-1 采用了掩码自注意力（Masked Self-Attention）机制，掩码的含义是，用来控制模型在处理序列数据时忽略无效的部分，具体来说就是一个与输入序列的长度相同的、由 0 和 1 组成的矩阵，其中 0 表示对应位置是无效的，1 表示对应位置是有效的。掩码自注意力，是将输入序列中右侧部分的无效位置屏蔽掉，这样在进行计算时无效位置的权重就会变得非常小，从而将其对最终结果的影响降到最小。通过使用掩码自注意力机制，使模型在训练过程中能够更好地捕捉到序列中的有效信息。<br />
对于掩码多头注意力（Masked Multi-head Attention）机制，我们可以通过下图来说明它的处理机制：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/Masked-Multi-Head-Attention-Mechanism.png" alt="Masked-Multi-Head-Attention-Mechanism" width="768" height="436" class="alignnone size-large wp-image-2754" /><br />
上图中，使用了一种 Look Ahead Masking 的方法，对于未来出现的 token 会将它们的权重值置为无效，而对其它已经出现过的 token 使用正常的多头注意力机制来计算权重。</p>
<p><strong style="font-size: 20px">GPT-2 模型</strong></p>
<p>GPT-2 也采用了只包含解码器（Decoder）部分的 Transformer 架构，而且是单向的，它使用了不同规模的参数量进行模型的训练，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-2-Models-with-Different-Size.png" alt="GPT-2-Models-with-Different-Size" width="768" height="281" class="alignnone size-large wp-image-2755" /><br />
如图所示，总计包括了 4 种不同参数规模的模型：</p>
<ul>
<li>SMALL：117M 参数</li>
<li>MEDIUM：345M 参数</li>
<li>LARGE：762 M 参数</li>
<li>EXTRA LARGE：1542M 参数</li>
</ul>
<p>对于每一种参数规模的模型，对应的超参数情况，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-2-Models-with-Different-Hyperparameters.png" alt="GPT-2-Models-with-Different-Hyperparameters" width="240" height="128" class="alignnone size-full wp-image-2756" /></p>
<p><strong style="font-size: 18px">自注意力层前后都进行层归一化（Layer Normalization）</strong></p>
<p>与 GPT-1 不同，GPT-2 对每个 Encoder Block 的自注意力层，前后分别都进行了层归一化（Layer Normalization）操作，即在每一层的输入和输出都有一个 Layer Normalization 子层，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-2-Model-Architecture.png" alt="GPT-2-Model-Architecture" width="390" height="512" class="alignnone size-large wp-image-2757" /><br />
在输入自注意力层之前新增 Layer Normalization 层，能够将输入数据的均值和方差分别标准化为 0 和 1，使数据在不同的尺度上保持一致。而且，这种策略能够缓解梯度消失和梯度爆炸的问题。同时，层归一化有助于优化器在更新权重时找到合适的方向，提高模型的训练稳定性和收敛速度。</p>
<p><strong style="font-size: 18px">采用 BBPE（Byte-level BPE）分词算法</strong></p>
<p>GPT-2 采用了 BBPE（Byte-level BPE）分词算法，它是 BPE 算法的一种变体，从字符级别扩展到字节（Byte）级别。<br />
BBPE 可以跨语言共用词表，压缩词表的大小，对于同样的训练集，BBPE 模型可能比 BPE 模型的表现更好。然而使用 BBPE 算法使得序列比 BPE 更长，导致训练/推理时间会更长。</p>
<p><strong style="font-size: 18px">采用相对位置编码(Learnable Relative Positional Encoding)</strong></p>
<p>GPT-2 采用了一种可学习的相对位置编码（Learnable Relative Positional Encoding）方法，这种方法不仅考虑了输入数据 token 之间的绝对距离，还关注了它们的相对位置关系，从而能够更好地捕获长程依赖关系，提高模型的性能。</p>
<p><strong style="font-size: 18px">采用掩码自注意力机制</strong></p>
<p>GPT-2 采用了掩码自注意力（Masked Self-Attention）机制，下图对比了自注意力（Self-Attention）与掩码自注意力（Masked Self-Attention）之间的区别：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/Self-Attention-vs-Masked-Self-Attention.png" alt="Self-Attention-vs-Masked-Self-Attention" width="565" height="188" class="alignnone size-large wp-image-2758" /><br />
在预训练阶段，模型训练过程中会学习语言的通用表示，掩码自注意力（Masked Self-Attention）能够保证模型在生成文本时只关注当前位置以及之前位置的上下文，最小化无效部分带来的影响，有助于得到的模型遵循自然语言的生成规律。<br />
在 GPT-2 模型训练中，使用单向语言建模任务来学习文本的上下文信息，这使得 GPT-2 在文本摘要、机器翻译、对话生成等 NLP 任务上表现出色。</p>
<p><strong style="font-size: 18px">零样本学习（Zero-shot Learning）</strong></p>
<p>在 GPT-2 中，多任务学习在无监督预训练阶段学习了大量的自然语言文本，所以 GPT-2 模型的零样本学习能力，可以直接迁移到下游的不同应用领域。下面是 GPT-2 在多任务情况下零样本学习结果表现，如图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-2-Zero-shot-Learning-Result.png" alt="GPT-2-Zero-shot-Learning-Result" width="1024" height="226" class="alignnone size-large wp-image-2759" /></p>
<p><strong style="font-size: 20px">GPT-3 模型</strong></p>
<p>GPT-3 模型通过预训练过程训练，得到预训练模型，下游各种应用任务基于该预训练模型，不需要任何的进行梯度更新或模型微调，只需要基于上下文学习（In Context Learning，ICL）来适应不同类型的任务，而且表现非常好，这离不开基础预训练模型的优异性能。<br />
GPT-3 模型，是基于优化的类 Sparse Transformer 解码器架构的超大规模模型，拥有参数量 1750 亿。这种类 Sparse Transformer 模式在 Transformer 的各层中交替使用密集（Dense）和局部带状稀疏（Locally Banded Sparse）注意力模式，大大提高了 GPT-3 处理长序列的计算效率。GPT-3 模型的架构，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-3-Model-Architecture.png" alt="GPT-3-Model-Architecture" width="440" height="778" class="alignnone size-large wp-image-2760" /><br />
训练 GPT-3 模型，使用了 5 个数据集：Common Crawl (filtered)、WebText2、Books1、Books2、Wikipedia（其中未公开 Books1 和 Books2 的数据来源），各个数据集的信息如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-3-Training-Using-Datasets.png" alt="GPT-3-Training-Using-Datasets" width="609" height="164" class="alignnone size-full wp-image-2761" /><br />
GPT-3 模型训练了 8 种不同尺寸的模型，训练过程中分别使用的超参数情况，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-3-Models-with-Different-Hyperparameters.png" alt="GPT-3-Models-with-Different-Hyperparameters" width="765" height="207" class="alignnone size-full wp-image-2762" /><br />
其中，最大的 GPT-3 模型有 1750 亿参数。</p>
<p><strong style="font-size: 18px">引入稀疏注意力机制</strong></p>
<p>GPT-3 在 Transformer 网络的各层中引入了交替的密集和局部带状稀疏注意力模式，这种方式能够使 GPT-3 更有效地处理长序列，从而减少计算复杂度及内存空间占用，因此提高了模型的可扩展性。<br />
局部带状稀疏（Locally Banded Sparse）注意力的计算过程如下：</p>
<ol>
<li>设置 Window 大小，确定每个 token 能够关注的上线文范围</li>
<li>基于 Window 大小范围内的 token 创建自注意力矩阵</li>
<li>计算局部带状注意力</li>
</ol>
<p><strong style="font-size: 18px">使用上下文学习（In Context Learning，ICL）方法</strong></p>
<p>为了避免在微调阶段调整模型参数，GPT-3 模型更加重视基于内容的学习方法：在任务适应阶段，模型采用了零样本（Zero-shot）、单样本（One-shot）和少样本（Few-shot）的学习策略，突破了传统微调方法的限制。与传统微调方法相比，零样本（Zero-shot）、单样本（One-shot）和少样本（Few-shot）的学习策略具有非常明显的优势。<br />
零样本（Zero-shot）学习，是指训练模型在没有接触过任何与目标任务相关的标签数据的情况下，仅凭借在预训练阶段学到的知识来解决问题。使用这种策略，模型需要理解任务描述，并在没有任何样本的情况下完成下游任务。GPT-3 模型在零样本（Zero-shot）学习方面表现优异。<br />
单样本（One-shot）学习，是指在仅给定一个与目标任务相关的示例样本的情况下进行推理。使用这种策略的关键是，让模型从有限的信息中快速学习并适应新任务。GPT-3 模型可以在极少的标签数据情况下快速适应新任务，降低了在特定任务上进行监督学习所花费的时间和训练代价。<br />
少样本（Few-shot）学习，是指训练模型在给定少量与目标任务相关的标签样本示例后进行推理。GPT-3 模型在少样本学习任务上的表现也很好，这是因为 GPT-3 预训练模型已经具有很强的泛化能力，可以在只具有少量标签数据的情况下经过推理得到高质量的结果。<br />
GPT-3 在 LAMBADA 数据集上测试了模型的性能，结果如下表所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-3-Performance-on-LAMBADA-dataset.png" alt="GPT-3-Performance-on-LAMBADA-dataset" width="566" height="141" class="alignnone size-full wp-image-2763" /><br />
通过上表可见，GPT-3 模型极大地改进了在 LAMBADA 数据集上的性能，相比于 SOTA 方法。在 StoryCloze 和 HellaSwag 两个数据集也获得了比较可观的性能。</p>
<p><strong style="font-size: 20px">GPT-3.5 模型</strong></p>
<p>从 GPT-3 开始，OpenAI 开始采用部分闭源策略，一些关键的技术细节并未全面公开。<br />
ChatGPT 模型是基于 GPT-3.5 进行构建和改进的，而 InstructGPT 则建立在 GPT-3 之上，它们都通过引入人工反馈机制来对原始模型进行微调和优化。基于 GPT-3 微调得到的模型是 GPT-3.5-Turbo，然后又有了 InstructGPT 模型，它也被称为 GPT-3.5。<br />
GPT-3.5 引入了 GitHub 等代码数据集，以及 StackExchange 等对话论坛和视频字幕数据集。GPT-3.5 模型在训练过程中也使用了大量的数据集，这些数据集的多样性和丰富性保证了模型能够更好地理解和生成各种类型的文本，从而使模型在各种应用场景中都具有较好的表现。<br />
为了降低下游使用预训练模型的成本，提高基于预训练模型更容易适应下游各种类型任务，基础预训练模型的性能非常关键，在数据集方面可能需要从如下几个角度来不断优化：</p>
<ol>
<li>提高预训练数据的数量与质量</li>
<li>对预训练数据集进行去重处理</li>
<li>提高预训练数据集的多样性</li>
</ol>
<p>InstructGPT 主要通过微调（fine-tuning）的方式，来更好地对齐人类的意图。为了训练 InstructGPT，OpenAI 训练了 3 个模型（模型参数分别为 1.3B、6B、175B），模型架构与 GPT-3 相同，这三个模型如下所示：</p>
<ul>
<li>SFT 模型（Supervised Fine-Tuning）</li>
<li>RM 模型（Reward Modeling）</li>
<li>RL 模型（Reinforcement Learning）</li>
</ul>
<p><strong style="font-size: 18px">InstructGPT 数据集</strong></p>
<p>InstructGPT 使用的数据集概要情况，如下表所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/InstructGPT-Dataset-Info.png" alt="InstructGPT-Dataset-Info" width="728" height="161" class="alignnone size-full wp-image-2770" /><br />
SFT 数据集和 RM 数据集，训练集和验证集都使用了 OpenAI API 客户调用获取，以及标注人员手动编写的 Prompt 提示数据，而 PPO 数据集都是使用 OpenAI API 客户调用获取的数据。<br />
另外，InstructGPT 训练也考虑了标注数据的多样性，收集到的训练数据跨多个不同的类别，如下表所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/InstructGPT-Annotations-Dataset-Category-Distributions.png" alt="InstructGPT-Annotations-Dataset-Category-Distributions" width="651" height="305" class="alignnone size-full wp-image-2771" /><br />
上表中包含了 RM 数据集和 SFT 数据集跨多个不同类别，PPO 数据集也是基于类似的要求准备的，分布的类别具有多样性的特点。</p>
<p><strong style="font-size: 18px">InstructGPT 训练过程</strong></p>
<p>上面三个阶段的微调方法和过程，可以通过下图给出的步骤来简要说明：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/InstructGPT-Training-3-Steps.png" alt="InstructGPT-Training-3-Steps" width="922" height="538" class="alignnone size-large wp-image-2764" /><br />
分别对应于上面提到的三个模型（SFT 模型、RM 模型、RL 模型），InstructGPT 的训练过程主要包括如下三个步骤：<br />
<strong>Step 1:</strong> Collect demonstration data, and train a supervised policy.<br />
<strong>Step 2:</strong> Collect comparison data, and train a reward model.<br />
<strong>Step 3:</strong> Optimize a policy against the reward model using PPO.<br />
下面详细说明各个阶段具体是如何进行训练的：</p>
<p><strong style="font-size: 16px">1. 有监督微调（Supervised Fine-Tuning）</strong></p>
<p>有监督微调使用了 SFT 数据集，该数据集的来源如下：</p>
<ul>
<li>通过 OpenAI 的 API 调用得到 Prompt 提示数据</li>
<li>专门的标注人员编写的 Prompt 提示数据</li>
</ul>
<p>基于上面两种数据来源，最终用于有监督微调的训练数据有 13K 个 Prompt 提示数据条目，其中训练数据中也包括 Prompt 提示对应的答案。<br />
有监督微调阶段，使用了 SFT 数据集对预训练的 GPT-3 模型进行了微调，这个经过微调的模型被称为 SFT 模型，参数为 1750 亿，它是一个具有基本预测能力的 Baseline 模型。<br />
根据论文中描述，有监督微调过程采用了余弦学习率衰减策略，训练了 16 轮，同时为了防止过拟合，设置残差的 Dropout 参数值为 0.2。训练 SFT 模型使用了 13K 个样本，由于训练数据时候可能会导致微调的模型过拟合，而 InstructGPT 并不会直接使用该微调 SFT 模型，而是作为一个 Baseline 模型以支持另外两个模型的训练。</p>
<p><strong style="font-size: 16px">2. 奖励建模（Reward Modeling）</strong></p>
<p>奖励建模阶段使用 RM 数据集，该数据集通过 OpenAI API 获取，以及标注人员编写，共计提供了 33K 个 Prompt 提示数据条目。<br />
在考虑到人类不可能为所有问题编写答案的情况下，训练一个奖励模型用于理解人类的偏好是很有必要的。InstructGPT 基于 GPT-3 微调模型（即 SFT 模型），训练了一个奖励模型，该模型相对较小，具有 60 亿参数，较小的模型能够节省一定的算力资源。<br />
奖励建模（Reward Modeling）的核心思想是，通过人工排序信息将其转化为可优化的奖励分数，并利用这些分数来调整 GPT-3 的模型参数。为了实现这一目标，InstructGPT 采用了一种创新的奖励建模算法，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/InstructGPT-Reward-Modeling-Ranking-Algorithm.png" alt="InstructGPT-Reward-Modeling-Ranking-Algorithm" width="770" height="545" class="alignnone size-full wp-image-2779" /><br />
在使用 RLHF 学习的情况下，排序提供了一种弱监督信号，也就是说是以人类的偏好作为奖励。排序能更好地评估每个答案的质量，从而提高模型的泛化能力，最终奖励模型逐渐学会了按照人类偏好进行打分。</p>
<p><strong style="font-size: 16px">3. 强化学习（Reinforcement Learning）</strong></p>
<p>强化学习训练阶段，使用 PPO 数据集，该数据集只是通过 OpenAI API 获取的 Prompt 提示数据，共计 31K 个，被用于微调 SFT 模型，将标注人员提供的不带标注的提示，输入 SFT 模型，此时不再让人类来反馈，而是直接使用前面训练得到的 RM 模型来对 SFT 模型的推理结果进行评估反馈，从而不断优化模型参数。然后，根据优化后的策略再次生成推理结果，再由 RM 模型进行评估反馈，持续优化 SFT 模型参数，如此循环持续优化策略至最优。<br />
在强化学习过程中采用了 PPO 算法，SFT 模型是前面第一步中 “有监督微调” 中经过微调的 GPT-3 得到的，该 SFT 模型作为 PPO 算法的策略网络。基于 PPO 算法的 InstructGPT 强化学习过程，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/InstructGPT-PPO-based-RLHF.png" alt="InstructGPT-PPO-based-RLHF" width="678" height="554" class="alignnone size-full wp-image-2772" /><br />
对于每个 Prompt 提示，输入到当前的强化学习模型中，生成一个对应的输出 y；接着，将 (x，y) 输入到 RM 模型中，以计算评估其得分。通过优化如下目标函数：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/InstructGPT-RLHF-Objective-Function.png" alt="InstructGPT-RLHF-Objective-Function" width="598" height="72" class="alignnone size-full wp-image-2773" /><br />
使新训练得到的 RL 模型，能够生成人类认为排序较高的推理结果。</p>
<p><strong style="font-size: 20px">GPT-4 模型</strong></p>
<p>GPT-4 是一个多模态大模型（Large Multimodal Model，LMM），能够处理输入的文本和图片，并生成文本输出，GPT-4 的性能和效果，显示出其卓越的生成和理解能力。由于 OpenAI 并没有公布 GPT-4 的技术细节，只能通过发布的 GPT-4 技术报告来粗略推测。<br />
GPT-4 的核心原理是，基于 Decoder-only 的 Transformer 自回归语言模型，即通过给定的文本序列，预测下一个词的概率分布，从而生成新的文本。GPT-4 采用了大规模的无监督预训练和有监督微调的方法，即先在海量的通用文本语料上进行预训练，学习文本的通用特征和规律，然后在特定的下游任务上进行微调，学习任务的特定知识，从而实现对任意文本的生成和理解。</p>
<p><strong style="font-size: 18px">基本特点</strong></p>
<ul>
<li>规模巨大，参数众多。</li>
</ul>
<p>GPT-4 拥有超过 1000 亿个参数，是 GPT-3 的 3 倍，是 GPT-2 的 300 倍，是 GPT-1 的 3000 倍。</p>
<ul>
<li>数据丰富，覆盖广泛。</li>
</ul>
<p>GPT-4 使用了超过 2.5 PB 的数据进行预训练，其中包括了 Common Crawl（1.8PB）、Wikipedia（60TB）、Reddit（40TB）、BooksCorpus（30TB）、Open Images（20TB）、LibriSpeech（10TB）、YouTube（10TB）等多种类型和格式的数据。GPT-4 的数据覆盖了文本、图像、音频、视频等多个领域，涵盖了人类的各种知识和信息，显示出其广泛的适应和理解能力。</p>
<ul>
<li>模型通用，任务多样。</li>
</ul>
<p>GPT-4 采用了通用语言模型的架构，即不针对特定的任务或领域进行优化，而是通过大规模的预训练，学习文本的通用特征和规律，然后通过少量的微调，适应不同的下游任务和领域。GPT-4 可以处理多种类型和格式的数据，如文本、图像、音频、视频等，也可以处理多种任务，如聊天机器人、搜索引擎、写作助手、教育辅导、娱乐创作、知识问答等，显示出其灵活的生成和理解能力。</p>
<ul>
<li>性能卓越，效果惊艳。</li>
</ul>
<p>GPT-4 在多个性能基准测试中，都取得了令人瞩目的成绩，超越了人类水平或现有最先进的模型。<br />
例如，在 MMLU（大规模多任务语言理解）测试中，GPT-4 以 95.0% 的成绩超越了人类专家，这一测试综合了数学、物理、历史等 57 个科目；在 UltraMMMU（超大规模多模态语言理解）测试中，GPT-4 以 75.4% 的成绩超越了 GPT-3.5，这一测试综合了文本、图像、音频、视频等多种类型和格式的数据 。</p>
<p><strong style="font-size: 18px">技术方案</strong></p>
<p><strong>Zero-shot 及 Few-shot 学习能力</strong></p>
<p>Zero-shot 以及 Few-shot 学习能力的提升，理论依据最可能是由于 LLM 模型的涌现能力（Emergent Ability）。<br />
涌现能力（Emergent Ability）是指模型在训练过程中，自动地学习到一些高级的、复杂的功能或行为，而这些功能或行为并没有被直接编码或指定。这种能力使模型能够在处理新的、未知的任务时表现更加出色，因为它是通过自适应地学习获得了新的功能或行为，而不需要重新训练或修改模型。<br />
模型产生涌现能力，主要取决于 4 分方面：</p>
<ul>
<li style="line-height: 1.5">模型参数量超大。GPT-4 很可能使用一些策略提高了模型的推理速度，所以文本模型参数大概是千亿级别甚至非常接近万亿级别。如果 GPT-4 使用了 CLIP 做图像编码，据 OpenAI 论文公布，目前最大的图像编码器是扩大了 64 倍的残差网络，那么 GPT-4 的图像编码大概有 16 亿。当然也可能采用其它图像编码结构，比如基于 Transformer 的 KOSMOS-1 就是一个不错的选择。</li>
<li style="line-height: 1.5">模型的架构。GPT-4 大幅提升了对长文本的处理能力，有可能使用了 Transformer-XL 或 Sparse Transformer 架构；GPT-4 更有可能是在 ChatGPT 的基础上迭代而得到的；GPT-4 还可能使用了原生的 Transformer，并增加了更多的层数，head 数量以及隐藏层节点数。GPT-4 支持多模态能力，可能是基于多模态算法 CLIP 实现的图像编码器；另外，微软的 KOSMOS-1 也展示出了和 GPT-4 类似的多模态语言模型能力，GPT-4 有可能借鉴了 KOSMO-1 的思想。</li>
<li style="line-height: 1.5">高质量的训练数据。</li>
<li style="line-height: 1.5">先进的训练策略。GPT-4 基本保持了和 ChatGPT 相同的训练策略，遵循了“预训练+提示+预测”的范式，改进包括：引入基于规则的奖励模型（Rule Based Reward Model，RBRM）、引入了多模态的提示学习、引入了思维链（Chain of Thought，COT）。</li>
</ul>
<p><strong>逻辑推理能力</strong></p>
<p>OpenAI 为了提升 GPT-4 的推理能力，很有可能使用了近年来 LLM 非常重要的思维链（Chain of Thought，COT）以及自提升（Self-Improve）方法。<br />
思维链（Chain of Thought）是指人们在进行思考时，由于某个观点、想法或感知刺激而引发的一系列相关思维关联，这些关联可以通过人们的记忆、经验、知识、情感和意识等方面来建立和加强，最终形成了一个有机的思维链，帮助人们理解和解决问题，做出决策和行动。<br />
LLM 和思维链的结合，可以让模型使用无监督的数据进行自我提升（Self-Improve），计算过程如下图所示：<br />
<img src="http://shiyanjun.cn/wp-content/uploads/2024/03/GPT-4-Self-Improve.png" alt="GPT-4-Self-Improve" width="888" height="333" class="alignnone size-full wp-image-2786" /><br />
计算过程描述如下：</p>
<ol>
<li>基于思维链构建 Prompt 提示；</li>
<li>根据不同的温度系数，模型生成多个不同的包含推理过程的 Path；</li>
<li>使用投票的方式选择最有可能的正确答案；</li>
<li>将包含这个正确答案的所有 Path 用来优化 LLM。</li>
</ol>
<p><strong>理解图像能力</strong></p>
<p>GPT-4 支持图像格式的图表输入，OpenAI 的多模态算法 CLIP 可以通过对比学习将图像和文本映射到同一特征空间，那么结合 CLIP 的图像编码器便可以实现 GPT-4 的图像输入，训练一个可以和 GPT 的文字特征对齐的图像编码器，然后将 CLIP 的图像编码器的输出作为图像 token，最后再加一个 Embedding 层将这个 token 编码为 GPT-4 的特征向量。<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/03/GPT-4-CLIP.png" alt="GPT-4-CLIP" width="714" height="469" class="alignnone size-full wp-image-2785" /><br />
GPT-4 还可以理解包含了很多细节的学术图片。在学术图片中，图中代指的符号，目标之间的位置关系都是十分重要的，如果 GPT-4 仅仅通过一个图像编码就能捕获这些细节信息，那么这个图像编码器一定也展现出了非常强的涌现能力，这个图像编码器也大概率是千亿规模的参数量。</p>
<p><strong>更安全的文本生成能力</strong></p>
<p>GPT-4 的技术报告中指出，文本模型会存在下面几类的风险输出：幻觉、有害内容、歧视、虚假信息、暴力、隐私、网络安全等。GPT-4 聘请了 50 余名来自不同领域专家扮演红队进行对抗测试。红队的工作是提出有危险性的问题，以测试 GPT-4 给出的输出，并尝试攻克它。通过领域专家的对抗，OpenAI 也采集了大量不同方向的领域专家数据来提升 GPT-4 的安全性。<br />
幻觉（Hallicination）是生成模型都非常难以解决的问题，它指的是模型产生的荒谬的或者不真实的内容。GPT-4 采用了两个策略来解决幻觉问题：</p>
<ul>
<li>利用 ChatGPT 的数据进行训练；</li>
<li>采用 NLP 技术来检测模型产生的幻觉样本，包括自动评估和人工评估。</li>
</ul>
<p>对于可能出现的其它风险输出，OpenAI 并没有详细的介绍它的技术方案，不过从他们的技术方案中，我们可以看出他们大概使用了下面几类方法：</p>
<ul>
<li>使用 RBRM（Rule-Based Reward Model）来检测可能出现的风险；</li>
<li>通过提示学习让模型学习拒绝回答此类问题；</li>
<li>利用红队发现这些可能存在的问题；</li>
<li>过滤训练数据，删除可能触发风险问题的样本；</li>
<li>训练奖励模型，让模型惩罚有危害的输出内容。</li>
</ul>
<p><strong>更强的编程能力</strong></p>
<p>GPT-4 在编程能力上比 ChatGPT 有了巨大的提升，一方面可能使用思维链掌握了更强的逻辑分析能力，另一方面有可能借鉴了 OpenAI 著名的代码生成算法 CodeX。CodeX 是 GPT-3 在代码生成领域的衍生版本，也是 Copilot 插件背后的基础算法。CodeX 采用了 GPT 系列 Decoder-only 的 Transformer 架构体系，模型的参数量有从 12M  到 12B 等多个不同的版本。CodeX 的训练分成预训练和微调两个阶段:<br />
在预训练阶段，OpenAI 从 Github 上爬取了大量的 Python 文件，经过清洗后得到了一个大小为 159GB 的训练集;<br />
在微调阶段，OpenAI 从竞赛网站、面试网站、Github 的单元测试脚本中收集了大约 40000 条数据。<br />
CodeX 和 GPT-4 都是 GPT-3 的下一代模型，让 GPT-4 使用 CodeX 现成的思想和数据，并提高模型的编程能力，是非常合理的。</p>
<p><strong>多语言能力</strong></p>
<p>GPT-4 的在其它语种上能力的大幅提升，可能使用的技术方案如下：</p>
<ul>
<li style="line-height: 1.5">提升了其它语种的训练数据；</li>
<li style="line-height: 1.5">更大规模的模型让 GPT-4 在小语种上涌现了更多的能力；</li>
<li style="line-height: 1.5">加入了针对小语种的任务，例如利用现有平行语料构建基于提示学习的机器翻译任务，使用机器翻译引擎将部分数据翻译成小语种等。</li>
</ul>
<p><strong>处理更长序列的能力</strong></p>
<p>这里的长序列包含两个方面，一方面是 GPT-4 是支持多轮对话的，另一方面是 GPT-4 支持更长的输入数据，下面讨论它们可能使用的技术：</p>
<ul>
<li>支持多轮对话</li>
</ul>
<p>GPT-4 支持连续对话，可能使用了 Transformer-XL，Transformer-XL 的重要改进是提出了片段递归的机制。片段递归机制类似于 Transformer 和 RNN 的结合体，它的核心思想是对于一个长度不限的变长数据，在计算的时候也是固定每个片段的长度并计算这个片段的特征，然后在计算下一个片段时将前面片段的特征加到当前片段上，从而让模型可以处理任意长度的特征。</p>
<ul>
<li>支持更长的输入数据</li>
</ul>
<p>GPT-3 就是使用的普通 Transformer 和 Sparse Transformer 的混合模式，所以 Sparse Transformer 也是非常有可能被 GPT-4 用来处理长输入文本的一个模型。Sparse Transformer 的特点是只关注 Top-k 个贡献最大的特征的状态，它使用稀疏注意力机制替代了 Transformer 的密集注意力。</p>
<p><strong style="font-size: 20px">GPT-4V 模型</strong></p>
<p>GPT-4V 在处理任意交错的多模态输入和其能力的通用性方面，具有前所未有的能力，使 GPT-4V 成为一个强大的多模态通用系统。<br />
另外，GPT-4V 独特的理解输入图像上绘制的视觉标记的能力，可以催生出新的人机交互方法，比如视觉参考提示（Visual Referring Prompting）。</p>
<p><strong style="font-size: 18px">输入模式（Input Modes）</strong></p>
<p>GPT-4V 支持的输入模式包括：</p>
<ul>
<li>作为单模型语言模型的文本输入</li>
<li>单个图像-文本对输入</li>
<li>使用交错的图像-文本对输入</li>
<li>使用多个图像作为输入</li>
</ul>
<p><strong style="font-size: 18px">工作模式和提示技术</strong></p>
<ul>
<li>遵循文本指令（Following Text Instructions）</li>
</ul>
<p>GPT-4V 的一个独特优势是其通用性，通过其强大的理解和遵循文本指令的能力实现。指令提供了一种自然的方式来定义和定制任意视觉语言用例所需的输出文本。<br />
在输入端，GPT-4V 可以通过理解详细的指令来执行具有挑战的任务，比如，通过提供中间步骤的指令来更好地解释抽象推理问题。GPT-4V 从指令中学习新任务的能力，在适应各种未见过的应用和任务方面显示出巨大的潜力。</p>
<ul>
<li>视点与视觉参考提示（Visual Pointing and Visual Referring Prompting）</li>
</ul>
<p>GPT-4V 特别擅长理解和处理直接绘制在图像上的视点。鉴于在图像上绘制的灵活性，这种能力可以作为未来野外人机交互的一种自然方法。<br />
通过探索一种新的提示方法，称为视觉参考提示（Visual Referring Prompting），它是一种人工智能技术，结合了自然语言处理和计算机视觉的能力，以理解和响应用户对特定视觉场景中的对象的描述。在这种技术中，模型需要根据自然语言提示（prompt）来定位和描述图像或视频中的特定对象或区域。Visual Referring Prompting 这种技术在增强现实、虚拟现实、机器人导航、智能助手等领域有广泛的应用潜力。</p>
<ul>
<li>视觉 + 文本提示（Visual + Text Prompting）</li>
</ul>
<p>视觉参考提示（Visual Referring Prompting）可以与其他图像文本提示顺畅地一起使用，呈现出一种细致的界面，简洁地表示出感兴趣的问题。GPT-4V 的通用性和灵活性使其具备了一种类似人类的多模态指令理解能力，以及适应未见过的任务的前所未有的能力。</p>
<ul>
<li>上下文少样本学习（In-context Few-shot Learning）</li>
</ul>
<p>GPT-4V 支持上下文少样本学习能力，通过给定几个上下文示例，而无需参数更新，就可以生成期望的推理输出。</p>
<p>目前，与 GPT-4V 相关的资料，更多出自论文《The Dawn of LMMs: Preliminary Explorations with GPT-4V(ision)》，该论文更多的是在探索 GPT-4V 的能力，以及在基于 GPT-4V 的能力能实现哪些可能的场景化应用。<br />
想要更多的推测 GPT-4V 的技术要点和原理，可能更多需要详细了解该模型发布之前的几代模型的能力，尤其是 GPT-4。另外，GPT-4V 在视觉方面相关能力的逐步提升，尤其是人机互动，可能会是一个重要的点。</p>
<p><strong style="font-size: 20px">参考资源</strong></p>
<ul>
<li><a target="_blank" href="https://www.cs.ubc.ca/~amuham01/LING530/papers/radford2018improving.pdf">GPT: Improving Language Understanding by Generative Pre-Training</a></li>
<li><a target="_blank" href="https://arxiv.org/abs/2005.14165">Language models are few-shot learners</a></li>
<li><a target="_blank" href="http://jalammar.github.io/illustrated-gpt2/">The Illustrated GPT-2</a></li>
<li><a target="_blank" href="http://jalammar.github.io/how-gpt3-works-visualizations-animations/">How GPT3 Works &#8211; Visualizations and Animations</a></li>
<li><a target="_blank" href="https://data-science-blog.com/blog/tag/engineer/">Positional encoding, residual connections, padding masks: covering the rest of Transformer components</a></li>
<li><a target="_blank" href="https://arxiv.org/abs/2303.08774">GPT-4 Technical Report</a></li>
<li><a target="_blank" href="https://openai.com/research/gpt-4">https://openai.com/research/gpt-4</a></li>
<li><a target="_blank" href="https://openai.com/blog/gpt-4-api-general-availability">https://openai.com/blog/gpt-4-api-general-availability</a></li>
<li><a target="_blank" href="https://zhuanlan.zhihu.com/p/621328705">https://zhuanlan.zhihu.com/p/621328705</a></li>
<li><a target="_blank" href="https://zhuanlan.zhihu.com/p/630548590">https://zhuanlan.zhihu.com/p/630548590</a></li>
<li><a target="_blank" href="https://zhuanlan.zhihu.com/p/680426850">https://zhuanlan.zhihu.com/p/680426850</a></li>
<li>ChatGPT 原理与架构：大模型的预训练、迁移和中间件编程</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2749.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>多模态 AI Agent 系统设计和潜在应用场景</title>
		<link>http://shiyanjun.cn/archives/2724.html</link>
		<comments>http://shiyanjun.cn/archives/2724.html#comments</comments>
		<pubDate>Wed, 28 Feb 2024 13:16:15 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[Agent]]></category>
		<category><![CDATA[LLM]]></category>
		<category><![CDATA[人工智能]]></category>
		<category><![CDATA[VLM]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2724</guid>
		<description><![CDATA[目前，已经有研究提出了多模态 Agent AI（Multimodal Agent AI，MAA）的概念，类似这 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>目前，已经有研究提出了多模态 Agent AI（Multimodal Agent AI，MAA）的概念，类似这样的一个 MAA 系统，能够基于对多模态感知输入的理解，在一个给定的环境中生成有效的行为。例如，下面是一个交互增强的 Agent 系统，如图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/MAA-AI-System-2D3D-Cross-reality-Interaction.gif" alt="MAA-AI-System-2D3D-Cross-reality-Interaction" width="970" height="579" class="alignnone size-full wp-image-2725" /><br />
上面这个多模态的 Agent AI 系统展示了基于 2D/3D 在跨现实（ross-reality）中实现生成，和进行编辑交互。我们对图中上面的会议室场景，说明如下：<br />
首先，在物理世界交互中，通过人类输入的指令，使用 DALLE-2 模型，通过文生图得到一个会议室场景图片。<br />
然后，通过 Knowledge Agent 问答系统，得到一个与会议相关的各种元素，如投影仪、桌子、椅子、白板等等。<br />
接着，通过虚拟现实（Virtual Reality） Agent 能够看到一个虚拟的会议室场景。<br />
最后，通过模拟器或一些 AR/MR 设备实现从物理世界与虚拟世界的交互，可以操作 AR/MR 设备完成特定任务，如远程会议的“现场”开会任务。<br />
另外两个例子（2D 到 3D 的交互；物理世界公交车场景到游戏场景的生成与交互）也是一样的，都实现了从物理世界到虚拟世界的映射与交互。</p>
<p><strong style="font-size: 20px">新的 Agent 范式</strong></p>
<p>通过上面图示的例子，我们可以看到，要实现一个基于多模态跨现实的 Agent AI 系统，涉及到很多 Agent 之间的集成（Integration）与交互（Interaction）。一个新的可供参考的 Agent 范式（Agent Paradigm）——实现多模态“全才” Agent（Multimodal Generalist Agent）系统，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Multi-modal-Generalist-Agent-Paradigm.png" alt="Multi-modal-Generalist-Agent-Paradigm" width="791" height="192" class="alignnone size-full wp-image-2726" /><br />
该范式包含 5 个模块：</p>
<ul>
<li style="line-height: 1.5">环境、感知、任务规划、技能观察（Environment, Perception, Task-planning, Skill Observation）</li>
<li style="line-height: 1.5">Agent 学习（Learning）</li>
<li style="line-height: 1.5">Agent 记忆（Memory）</li>
<li style="line-height: 1.5">Agent 行动（Action）</li>
<li style="line-height: 1.5">Agent 认知（Cognition）</li>
</ul>
<p>Agent 在上图定义的这样一个交互式闭环（Interactive Closed-loop）中，不断学习，提高认知，最后能够做出更优的决策并执行。<br />
用于 Agent 的学习/优化策略和基本机制，可以参考如下技巧：</p>
<ul>
<li>强化学习（Reinforcement Learning，RL）</li>
<li>模拟学习（Imitation Learning，IL）</li>
<li>上下文学习（In-Context Learning，ICL）</li>
<li>时间优化（Temporal Optimization）</li>
<li>空间优化（Spatial Optimization）</li>
</ul>
<p>基于多模态跨现实的 Agent AI 系统，需要集成大量的、不同类型的 Agent，在完成各自任务的同时，它们之间也会通过进行交互来不断学习、共同完成 AI 任务。各种不同类型的 Agent，如下所示：</p>
<ul>
<li>Generalist Agents</li>
<li>Embodied Agents</li>
<li>Action Agents</li>
<li>Interactive Agents</li>
<li>Simulation and Environments Agents</li>
<li>AR/VR/mixed-reality Agents</li>
<li>Knowledge Agents</li>
<li>Logic Agents</li>
<li>Agents for Emotional Reasoning</li>
<li>Neuro-Symbolic Agents</li>
<li>LLMs and VLMs Agent</li>
</ul>
<p>在开发复杂的多模态 AI Agent 系统时，集成视觉和语言的理解非常关键，包括生成图像标题、可视化问答、视频语言生成、视频理解等等。交互式多模态 Agent 包含 4 个基本核心模块，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Interactive-Multimodal-Agents-4-Main-Pillars.png" alt="Interactive-Multimodal-Agents-4-Main-Pillars" width="649" height="249" class="alignnone size-full wp-image-2727" /><br />
这 4 个核心模块及其功能，说明如下：</p>
<ul>
<li>交互服务（Interaction Service）</li>
</ul>
<p>交互服务，有助于建立一个统一的自动化行动（Automated Actions）、认知和决策平台。</p>
<ul>
<li>音频服务（Audio Service）</li>
</ul>
<p>音频服务，将音频和语音处理集成到应用程序和服务中。</p>
<ul>
<li>视觉服务（Vision Service）</li>
</ul>
<p>视觉服务，识别和分析在图像、视频和数字墨水中出现的内容。</p>
<ul>
<li>语言服务（Language Service）</li>
</ul>
<p>语言服务，从结构化、半结构化文本内容中抽取语义。</p>
<p><strong style="font-size: 20px">Agent AI 应用任务</strong></p>
<p><strong style="font-size: 18px">1、使用 Agent 改善游戏中 NPC 的行为</strong></p>
<p>面向游戏领域的 Agent 应用比较常见，比如游戏中的 NPC（Non-Player Characters）的行为主要由开发者精心制作的预定义脚本决定，所以这些脚本预定义好的行为都是可预测的，完全不能根据游戏中玩家（Player）的操作在动态的游戏环境中产生更高级的变化和行为，这极大地阻碍了动态游戏环境中的沉浸式体验。因此，如果利用 LLM 来引导 NPC 的行为，使其具备自主性和适应性，就能够使互动更加微妙和有趣。<br />
基于 AI 驱动的 NPC 可以从游戏玩家的行为中学习，逐渐适应动态变化的策略，从而提供具有挑战性且更少可预测性的游戏体验。<br />
下面是一个使用 GPT-4V 模型，对游戏 Minecraft Dungeons 进行交互体验改进的 Agent 实现的应用任务：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/NPC-Agent-Application-Task-based-GPT-4V.png" alt="NPC-Agent-Application-Task-based-GPT-4V" width="1315" height="748" class="alignnone size-full wp-image-2728" /><br />
 图中，通过不断地与 GPT-4V 互动反馈，得到一些基于 LLM 驱动的新场景和行为，最后一个生成的图片中 NPC 在战斗后的状态和场景，很难甚至无法通过预定义的脚本来实现。</p>
<p><strong style="font-size: 18px">2、使用 Agent 分析游戏</strong></p>
<p> LLM 可以作为一个强大的语言工具，支持分析游戏中的文本数据，包括聊天日志、玩家反馈，以及叙述内容。<br />
 VLM 可以用来解析玩家游戏过程中，交互得到的大量图像、视频数据，以帮助分析游戏世界中的用户意图和动作。</p>
<p> <strong style="font-size: 18px">3、游戏场景合成</strong></p>
<p>场景合成在创建和增强沉浸式游戏环境的过程中，具有非常重要的作用。场景合成包括游戏中 3D 场景和环境的自动生成，或者半自动生成，比如地形的生成、对象的放置、逼真照明的创建，有时甚至还包括动态的天气系统。<br />
另外，现代游戏制作通常以广阔、开放的世界环境为主要特色，而人工设计这些景观可能耗费大量时间和资源。通过使用场景合成能力，自动生成地形，同时利用程序或 AI 驱动的技术，使用较少的手动操作就可以生成复杂、逼真的景观/场景。<br />
LLM 和 VLM 能够利用丰富的知识来设置各种规则，使输出的设计在视觉上更加新颖独特，而不像使用传统方式得到的更多是一些千篇一律、平淡无奇的创意。<br />
例如，下图是基于 Minecraft Dungeons 游戏视频，使用了 VLM 模型预测生成的图像：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Masked-Video-Prediction-On-Unseen-Minecraft-Videos.png" alt="Masked-Video-Prediction-On-Unseen-Minecraft-Videos" width="801" height="872" class="alignnone size-full wp-image-2729" /><br />
上图中，通过屏蔽掉视频中画面的一些部分，使用 GPT-4V 预测生成的结果，可以对比一下原始图像和预测生成图像之间不同。</p>
<p><strong style="font-size: 18px">4、机器人</strong></p>
<p>机器人（Robotics）是通过与环境不断地进行交互、获得反馈，持续学习改进性能，以完成人类期望它完成的任务。</p>
<ul>
<li>视觉运动控制（Visual Motor Control）</li>
</ul>
<p>视觉运动控制（Visual Motor Control）是指，将视觉感知和运动动作相结合，使机器人能够更有效地执行任务。这种集成至关重要，因为它使机器人能够理解来自其环境的视觉数据，并相应地调整其动作以与环境精确地互动。视觉运动控制，有助于机器人适应环境状态快速变化的动态环境，能够基于视觉反馈对运动动作进行实时调整。<br />
在安全操作的背景下，视觉信息对于检测执行错误、确认每个机器人动作序列先后关系，显得至关重要。在不受控的环境中，比如机器人对室内环境比较陌生，由于家具形状的变化、照明的变化等不可预测的因素，它们的行为可能会面临意想不到的结果。所以，利用视觉反馈不断验证每一步的结果，能够确保机器人系统稳健可靠地运行。</p>
<ul>
<li>语言条件操纵（Language Conditioned Manipulation）</li>
</ul>
<p>语言条件操纵，需要机器人系统具有基于语言指令解释和执行任务的能力，通常有必要为人机交互提供一个直观和用户友好的界面，通过自然语言命令，用户可以以类似于人类交流的方式向机器人指定目标和任务，降低操作机器人系统的难度。</p>
<ul>
<li>技能优化（Skill Optimization）</li>
</ul>
<p>最近的研究强调了 LLM 在机器人任务规划中的有效性。然而为了使任务更好地执行，尤其是那些涉及抓取等物理交互的任务，需要让机器人对环境有更深入的了解，而不仅仅是简单地解释人类指令。<br />
对于机器人精确的接触点、手臂姿势等，这些对人类来说是直观的，但想要通过语言表达却非常困难，有一定的挑战性。因此，让机器人从场景中捕捉这些细微的间接线索，并将其有效转化为机器人的技能仍然是一个重大的挑战。机器人界也越来越专注于收集经过增强的数据集，或开发从人类的演示中直接获得技能的方法，包括示范学习、模仿学习，这对优化机器人的技能非常有帮助。</p>
<p>最近的研究已经证明，LLM/VLM 在特定环境中与人类交互学习过程中，机器人 Agent 表现出了巨大的潜力。一些和 LLM/VLM 有关的话题有：</p>
<ul>
<li>多模态系统（Multimodal Systems）</li>
<li>任务规划和技能训练（Task Planning and Skill Training）</li>
<li>在线优化（On-site Optimization）</li>
<li>对话 Agent（Conversation Agents）</li>
<li>导航 Agent（Navigation Agents）</li>
</ul>
<p><strong style="font-size: 18px">5、医疗保健（Healthcare）</strong></p>
<p>在医疗保健领域，LLM 和 VLM 可以充当诊断试剂、患者护理助理，甚至治疗辅助工具。虽然 AI 试剂在改善患者护理和挽救生命方面有巨大的潜力，但也存在可能的巨大风险，即滥用或仓促部署 AI 试剂可能危及全球千百万病人。</p>
<p><strong style="font-size: 20px">参考资源</strong></p>
<ul>
<li><a target="_blank" href="https://arxiv.org/abs/2401.03568">Agent AI: Surveying the Horizons of Multimodal Interaction</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2724.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>开源 AI Agent：HuggingGPT 基本原理浅析</title>
		<link>http://shiyanjun.cn/archives/2710.html</link>
		<comments>http://shiyanjun.cn/archives/2710.html#comments</comments>
		<pubDate>Tue, 27 Feb 2024 12:35:13 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[Agent]]></category>
		<category><![CDATA[人工智能]]></category>
		<category><![CDATA[HuggingGPT]]></category>
		<category><![CDATA[LLM]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2710</guid>
		<description><![CDATA[HuggingGPT 是浙江大学、微软亚洲研究院合作开发的开源项目，以 ChatGPT 和 Hugging F [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>HuggingGPT 是浙江大学、微软亚洲研究院合作开发的开源项目，以 ChatGPT 和 Hugging Face 为基础构建的一个 AI Agent 框架，融合了 LLM 和 AI 领域模型的能力，用来解决不同领域和不同模态的 AI 任务。<br />
HuggingGPT 是一个以 LLM（比如 ChatGPT）为控制器，以专家模型（HuggingFace）为执行任务的 AI Agent 系统，主要通过连接到各个领域内的专家模型以尝试自动地解决各种复杂的 AI 任务。HuggingGPT 以自然语言为接口，通过 ChatGPT 进行任务规划、模型选择，并通过使用专家模型处理对应领域的问题，生成最终结果，从而能够很好地解决 AI 任务。<br />
HuggingGPT 对应的相关代码和工具，都托管在 Github 上，对应的项目名是 JARVIS：<a target="_blank" href="https://github.com/microsoft/JARVIS">https://github.com/microsoft/JARVIS</a>。</p>
<p><strong style="font-size: 20px">HuggingGPT 设计概览</strong></p>
<p>HuggingGPT 的总体处理流程，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/HuggingGPT-Core-Components.png" alt="HuggingGPT-Core-Components" width="762" height="340" class="alignnone size-full wp-image-2711" /><br />
通过上图可以看到，HuggingGPT 的处理过程可以分为如下 4 个阶段：</p>
<ul>
<li>任务规划（Task Planning）</li>
</ul>
<p>使用一个 LLM（ChatGPT）分析用户的请求，了解用户的意图。通过用户输入到 LLM（ChatGPT）的 Prompt，根据对话结果将用户任务分解为可执行的多个任务。</p>
<ul>
<li>模型选择（Model Selection）</li>
</ul>
<p>为了解决计划任务，LLM（ChatGPT）通过一定的策略，选择托管在 Hugging Face 网站上的某个领域的专家模型，来执行某个特定的任务。</p>
<ul>
<li>任务执行（Task Execution）</li>
</ul>
<p>调用并执行选择的某个专家模型，执行任务并将结果返回给 LLM（ChatGPT）。</p>
<ul>
<li>响应生成（Response Generation）</li>
</ul>
<p>最后，利用 LLM（ChatGPT）集成来自所有专家模型的推理结果，并统一为用户生成响应内容。</p>
<p>HuggingGPT 的上述 4 个阶段，更详细的处理过程，可以参考下图：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/HuggingGPT-Overview.png" alt="HuggingGPT-Overview" width="727" height="722" class="alignnone size-full wp-image-2712" /></p>
<p><strong style="font-size: 20px">任务规划</strong></p>
<p>LLM（ChatGPT） 接受用户的请求，将其分解为一系列结构化的任务。复杂的请求通常涉及多个任务，LLM 需要确定这些任务的依赖关系和执行顺序。为了使 LLM 进行有效的任务规划，HuggingGPT 在 Prompt 提示设计中采用了基于规范化指令（Specification-based Instruction）和基于演示的解析（Demonstration-based Parsing）。<br />
基于规范的指令任务规范为任务提供了统一的模板，允许大型语言模型通过槽位归档进行任务解析。HuggingGPT 为任务解析设计了 4 个字段，分别是任务类型、任务编号、任务依赖、任务参数，各个字段分别说明如下：</p>
<ul>
<li style="line-height: 1.5">任务类型：使用 “task” 字段表示，任务类型涵盖语言、视觉、视频、音频等不同任务。</li>
<li style="line-height: 1.5">任务编号：使用 “id” 字段表示，任务编号为任务规划提供了一个唯一的标识符，用于引用相关任务及其生成的资源。</li>
<li style="line-height: 1.5">任务依赖：使用 “dep” 字段表示，任务依赖定义了执行所需的先决条件任务。只有在完成所有先决条件相关任务后，才会启动该任务。</li>
<li style="line-height: 1.5">任务参数：使用 “args” 字段表示，任务参数包含执行任务所需的参数列表。它包含 3 个子字段，根据任务类型填充文本、图像和音频资源，它们是从用户的请求或相关任务的生成资源中解析的。</li>
</ul>
<p>任务规划阶段，生成的规范化指令模板，示例如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Task-Planning-Example.jpg" alt="Task-Planning-Example" width="755" height="532" class="alignnone size-full wp-image-2713" /><br />
生成的任务列表描述格式为：</p>
<pre class="brush: plain; gutter: false; title: ; wrap-lines: true; notranslate">
[{
	&quot;task&quot;: task,
	&quot;id&quot;,
	task_id,
	&quot;dep&quot;: dependency_task_ids,
	&quot;args&quot;: {
		&quot;text&quot;: text,
		&quot;image&quot;: URL,
		&quot;audio&quot;: URL,
		&quot;video&quot;: URL
	}
}]
</pre>
<p>可以在上图中看到，演示部分（Demonstrations）给出了 3 个具体的任务的实例描述，使用 JSON 格式，如下所示：</p>
<ul>
<li style="line-height: 1.5">图片 e1.jpg 中有几个实体</li>
</ul>
<pre class="brush: plain; gutter: false; title: ; wrap-lines: true; notranslate">
[{
	&quot;task&quot;: &quot;object-detection&quot;,
	&quot;id&quot;: 0,
	&quot;dep&quot;: [-1],
	&quot;args&quot;: {
		&quot;im age&quot;: &quot;e1.jpg&quot;
	}
}]
</pre>
<ul>
<li style="line-height: 1.5">图片 e2.jpg 中是什么动物，以及它们在做什么</li>
</ul>
<pre class="brush: plain; gutter: false; title: ; wrap-lines: true; notranslate">
[{
	&quot;task&quot;: &quot;image-to-text&quot;,
	&quot;id&quot;: 0,
	&quot;dep&quot;: [-1],
	&quot;args&quot;: {
		&quot;im age&quot;: &quot;e2.jpg&quot;
	}
}, {
	&quot;task&quot;: &quot;image-cls&quot;,
	&quot;id&quot;: 1,
	&quot;dep&quot;: [-1],
	&quot;args&quot;: {
		&quot;image&quot;: &quot;e2.jpg&quot;
	}
}, {
	&quot;task&quot;: &quot;object-detection&quot;,
	&quot;id&quot;: 2,
	&quot;dep&quot;: [-1],
	&quot;args&quot;: {
		&quot;image&quot;: &quot;e2.jpg&quot;
	}
}, {
	&quot;task&quot;: &quot;vi- sual-quesrion-answering&quot;,
	&quot;id&quot;: 3,
	&quot;dep&quot;: [-1],
	&quot;args&quot;: {
		&quot;text&quot;: &quot;what’s the animal doing?&quot;,
		&quot;image&quot;: &quot;e2.jpg&quot;
	}
}]
</pre>
<ul>
<li style="line-height: 1.5">先生成图片 e3.jpg，基于该图片生成文本 “a girl reading a book”，并以一个新图片的形式做应答</li>
</ul>
<pre class="brush: plain; gutter: false; title: ; wrap-lines: true; notranslate">
[{
	&quot;task&quot;: &quot;pose-detection&quot;,
	&quot;id&quot;: 0,
	&quot;dep&quot;: [-1],
	&quot;args&quot;: {
		&quot;im age&quot;: &quot;e3.jpg&quot;
	}
}, {
	&quot;task&quot;: &quot;pose-text-to-image&quot;,
	&quot;id&quot;: 1,
	&quot;dep&quot;: [0],
	&quot;args&quot;: {
		&quot;text&quot;: &quot;a girl reading a book&quot;,
		&quot;image&quot;: &quot;&lt;re- source&gt;-0&quot;
	}
}]
</pre>
<p><strong style="font-size: 20px">模型选择</strong></p>
<p>在 HuggingFace Hub 上托管的 LLM 模型（专家模型）具有全面的模型描述，这些描述通常由开发人员提供。这些描述的内容包含：模型功能、体系结构、支持的语言和域、许可等，它们能够有效地支持 HuggingGPT 根据用户请求和模型描述的相关性为任务选择正确的模型。<br />
在解析任务列表并完成任务规划后，接下来 HuggingGPT 就需要匹配任务和 LLM 模型（专家模型），即为任务列表中的每个任务选择合适的 LLM 模型（专家模型）。为了获取 LLM 模型，首先需要从 HuggingFace Hub 上获得专家模型的描述信息，然后通过上下文中的任务模型分配机制为任务动态选择模型。具体来说，需要根据模型的任务类型筛选模型，只保留与当前任务类型匹配的模型，对剩下的模型需要根据下载量排序，并选择 top-K 个模型作为 HuggingGPT 可以选择的候选模型集合。<br />
模型选择阶段，生成的规范化指令模板，示例如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Model-Selection-Example.jpg" alt="Model-Selection-Example" width="753" height="247" class="alignnone size-full wp-image-2714" /><br />
HuggingGPT 选择后输出的格式必须是严格的 JSON 格式，格式如下所示：</p>
<pre class="brush: plain; gutter: false; title: ; wrap-lines: true; notranslate">
{
	&quot;id&quot;: &quot;id&quot;,
	&quot;reason&quot;: &quot;your detail reason for the choice&quot;
}
</pre>
<p>最终生成的专家模型候选集合，包含 top-K 个模型，以列表形式显示如下：</p>
<pre class="brush: plain; gutter: false; title: ; wrap-lines: true; notranslate">
{
	&quot;model_id&quot;: model id #1, &quot;metadata&quot;: meta-info # 1,
	&quot;description&quot;: description of model #1} {&quot;model_id&quot;: model id # 2,
	&quot;metadata&quot;: meta - info #2, &quot;description&quot;: description of model # 2
}
·········
{
	&quot;model_id&quot;: model id# K,
	&quot;metadata&quot;: meta - info# K,
	&quot;description&quot;: description of model# K
}
</pre>
<p><strong style="font-size: 20px">任务执行</strong></p>
<p>任务被分配给最合适的模型以后，接着就需要使用选定的专家模型，来对任务进行推理以执行任务。具体地，HuggingGPT 会把任务的参数输入给选定的专家模型，执行这些模型来获取推理结果，然后发送回 LLM。由于任务在与专家模型交互过程中，专家模型动态输出结果，所以 HuggingGPT 必须在任务开始执行就计划好，动态地在需要的时候提供任务所需的特定资源，以完成任务的推理执行。<br />
理想情况下，我们只使用 HuggingFace 上的远端服务提供的推理端点（Inference Endpoints）就能完成任务执行。但现实情况并不总是这样，比如某个任务需要使用的专家模型并不存在，这样就不得不在本地部署了推理端点，是任务正常执行。所以，为了加快任务处理速度并保证计算的稳定性，HuggingGPT 会在混合推理端点上运行这些模型。通过将任务参数作为输入，模型计算推理结果，然后将其发送回 LLM。</p>
<p><strong style="font-size: 20px">响应生成</strong></p>
<p>经过任务执行节点，在所有任务执行完成后，HuggingGPT 进入响应生成阶段。HuggingGPT 将前面三个阶段（任务规划、模型选择、任务执行）的所有信息集成在一起，包括计划任务列表、为任务选择的模型以及模型的推理结果，还有推理的结果，HuggingGPT 会依赖推理结果做出最终决策的支持。这些推理结果以结构化格式呈现，例如，对象检测模型中具有检测概率的边界框、问答模型中的答案分布等，HuggingGPT 允许 LLM 接收这些推理结果作为输入，并将具有一定置信度的响应结果进行汇总，返回给最终的用户。<br />
下面是在响应生成阶段，生成的规范化指令模板，示例如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Response-Generation-Example.jpg" alt="Response-Generation-Example" width="754" height="172" class="alignnone size-full wp-image-2715" /><br />
收集到前三个阶段的信息，格式模板如下：</p>
<ul>
<li>用户输入（User Input）： {{ User Input }}</li>
<li>任务规划（Task Planning）：{{ Tasks }}</li>
<li>模型选择（Model Selection）{{ Model Assignment }}</li>
<li>任务执行（Task Execution）{{ Predictions }}</li>
</ul>
<p>上面可注入的 Slot（injectable slots），及双花括号内部的标识，会被替换成最终的文本内容，然后输入给 LLM，汇总得到用户需要的最终答案。</p>
<p><strong style="font-size: 20px">HuggingGPT 局限</strong></p>
<p>虽然 HuggingGPT 提出了 AI 解决方案的新范式，但仍然存在一些限制，如下所示：</p>
<ul>
<li>任务规划严重依赖 LLM 的能力</li>
</ul>
<p>大模型并不能 100% 确保生成的计划始终是可行且最优的，因此，探索优化 LLM 的方法至关重要，进而提升规任务划能力。</p>
<ul>
<li>效率</li>
</ul>
<p>效率的瓶颈主要在使用大型语言模型的推理上。对于每一轮用户请求，HuggingGPT 都需要在任务规划、模型选择和响应生成阶段至少与大型语言模型进行一次交互，大大增加了响应延迟，并导致用户体验下降。</p>
<ul>
<li>最大上下文长度的限制</li>
</ul>
<p>受 LLM 可以接受的最大 token 数量的限制，HuggingGPT 面临着最大上下文长度的限制。通过使用会话窗口，在任务规划阶段只跟踪了会话上下文，这样做在一定程度上能够缓解这个问题。</p>
<ul>
<li>系统稳定性</li>
</ul>
<p>包括两个方面：一是在大型语言模型的推理过程中通常是不可控的。LLM 在推理时偶尔会不符合指令，并且输出格式可能会超出预期，从而导致程序工作流中出现异常；二是 HuggingFace 的推理端点上托管的专家模型的状态不可控。HuggingFace 上的专家模型可能会受到网络延迟或服务状态的影响，导致任务执行阶段出现错误。</p>
<p>HuggingGPT 是一个 AI 智能体的实现，这个系统来解决 AI 任务。它以语言为接口将 LLM 与 AI 模型（专家模型）连接起来，所以，LLM 可以被视为管理 AI 模型的控制器，并且可以利用来自 ML 社区（如 Hugging Face）的模型来自动解决用户的不同需求。<br />
通过利用 LLM 在理解和推理方面的优势，HuggingGPT 可以分析用户的意图，并据此将其分解为多个子任务；然后，基于 Hugging Face 上专家模型的描述，HuggingGPT 能够为每个任务选择并分配最合适的模型，最后集成不同模型的推理结果，以生成最终响应结果内容。通过利用来自 ML 社区的众多 AI 模型的能力，HuggingGPT 在解决具有挑战性的 AI 任务方面展示了巨大的潜力，同时也为实现 AGI 做了一次有意义的探索。</p>
<p><strong style="font-size: 20px">参考资源</strong></p>
<ul>
<li><a target="_blank" href="https://export.arxiv.org/abs/2303.17580v3">HuggingGPT: Solving AI Tasks with ChatGPT and its Friends in Hugging Face</a></li>
<li><a target="_blank" href="https://github.com/microsoft/JARVIS">https://github.com/microsoft/JARVIS</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2710.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>自注意力（Self-Attention）的计算过程</title>
		<link>http://shiyanjun.cn/archives/2688.html</link>
		<comments>http://shiyanjun.cn/archives/2688.html#comments</comments>
		<pubDate>Mon, 26 Feb 2024 08:45:02 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[人工智能]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2688</guid>
		<description><![CDATA[在深度学习中，很多 LLM 的训练都使用 Transformer 架构，而在 Transformer 架构中计 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>在深度学习中，很多 LLM 的训练都使用 Transformer 架构，而在 Transformer 架构中计算的过程涉及到的最关键的就是注意力，它是整个过程中重要的基础。注意力抽象出了 3 个重要的概念，在计算过程中对应着 3 个矩阵，如下所示：</p>
<ul>
<li>Query：在自主提示下，自主提示的内容，对应着矩阵 Q</li>
<li>Keys：在非自主提示下，进入视觉系统的线索，对应着矩阵 K</li>
<li>Values：使用 Query 从 Keys 中匹配得到的线索，基于这些线索得到的进入视觉系统中焦点内容，对应着矩阵 V</li>
</ul>
<p>我们要训练的模型，输入的句子有 n 个 token，而通过选择并使用某个 Embedding 模型获取到每个 token 的 Word Embedding，每个 Word Embedding 是一个 d 维向量。本文我们详细说明自注意力（Self-Attention）的计算过程，在进行解释说明之前，先定义一些标识符号以方便后面阐述使用：</p>
<ul>
<li>X：输入训练数据的 Embedding 是一个 n x d 矩阵</li>
<li>Q：查询矩阵，矩阵形状 n x dq</li>
<li>K：键矩阵，矩阵形状 n x dk，其中 dk=dq</li>
<li>V：值矩阵，矩阵形状 n x dv</li>
</ul>
<p>计算自注意力（Self-Attention）的基本流程，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Computing-Self-Attention.png" alt="Computing-Self-Attention" width="674" height="491" class="alignnone size-full wp-image-2689" /><br />
计算过程及其示例演示，描述如下：</p>
<p><strong style="line-height: 1.7;font-size: 18px">1. 计算生成 Embedding 矩阵</strong></p>
<p>X 是基于输入数据得到的 n x d 的 Embedding 矩阵：通过选择并使用某个 Embedding 模型，输入数据经过分词得到 n 个 token，再获取到每个 token 的 Embedding 向量，然后将这 n 个 Embedding 向量拼接成一个 n x d 的 Embedding 矩阵。<br />
假设我们输入的是一个句子：</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
Life is short, less is more.
</pre>
<p>比如，要训练模型以便下游执行机器翻译任务，上面句子对应的 Embedding 是一个 6 x 16 的 Tensor，即 n=6，d=16，如下所示：</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
tensor([[ 0.3374, -0.1778, -0.3035, -0.5880,  0.3486,  0.6603, -0.2196, -0.3792,
          0.7671, -1.1925,  0.6984, -1.4097,  0.1794,  1.8951,  0.4954,  0.2692],
        [-1.3250,  0.1784, -2.1338,  1.0524, -0.3885, -0.9343, -0.4991, -1.0867,
          0.8805,  1.5542,  0.6266, -0.1755,  0.0983, -0.0935,  0.2662, -0.5850],
        [ 0.2553, -0.5496,  1.0042,  0.8272, -0.3948,  0.4892, -0.2168, -1.7472,
         -1.6025, -1.0764,  0.9031, -0.7218, -0.5951, -0.7112,  0.6230, -1.3729],
        [ 0.8768,  1.6221, -1.4779,  1.1331, -1.2203,  1.3139,  1.0533,  0.1388,
          2.2473, -0.8036, -0.2808,  0.7697, -0.6596, -0.7979,  0.1838,  0.2293],
        [-1.3250,  0.1784, -2.1338,  1.0524, -0.3885, -0.9343, -0.4991, -1.0867,
          0.8805,  1.5542,  0.6266, -0.1755,  0.0983, -0.0935,  0.2662, -0.5850],
        [ 0.5146,  0.9938, -0.2587, -1.0826, -0.0444,  1.6236, -2.3229,  1.0878,
          0.6716,  0.6933, -0.9487, -0.0765, -0.1526,  0.1167,  0.4403, -1.4465]])
torch.Size([6, 16])
</pre>
<p><strong style="line-height: 1.7;font-size: 18px">2. 计算生成 Q、K、V 矩阵</strong></p>
<p>定义 3 个矩阵 Wq、Wk、Wv 它们是权重矩阵，输入 X 与这 3 个矩阵的转置分别相乘，得到 Q、K、V。其中，Q 是 n x dq 矩阵，K 是 n x dk 矩阵，V 是 n x dv 矩阵。计算的公式如下所示（().T 表示对矩阵的转置操作）：</p>
<ul>
<li>Q = X · (Wq).T</li>
<li>K = X · (Wk).T</li>
<li>V = X · (Wv).T</li>
</ul>
<p>Wq、Wk、Wv 是三个参数矩阵，通过在模型进行训练的过程中，经过学习会得到这 3 个矩阵对应的参数值。<br />
在模型训练过程中，参数矩阵可以采用随机策略生成，这里我们随机生成 Wq（8 x 16 矩阵）、Wk（8 x 16 矩阵）、Wv（12 x 16 矩阵），使用 PyTorch 生成：</p>
<pre class="brush: python; title: ; notranslate">
Wq = torch.rand(8, 16)
print(Wq.T)
print(Wq.T.shape)

Wk = torch.rand(8, 16)
print(Wk.T)
print(Wk.T.shape)

Wv = torch.rand(12, 16)
print(Wv.T)
print(Wv.T.shape)
</pre>
<p>从而可以分别得到这 3 个矩阵的转置的结果，如下所示：</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
tensor([[0.9882, 0.7577, 0.6397, 0.5757, 0.6042, 0.0206, 0.6886, 0.4270],
        [0.8363, 0.4536, 0.8954, 0.2785, 0.9836, 0.3247, 0.9024, 0.8210],
        [0.9010, 0.4130, 0.2979, 0.1946, 0.1444, 0.9355, 0.1123, 0.3605],
        [0.3950, 0.5585, 0.6314, 0.5382, 0.9010, 0.5855, 0.2685, 0.4516],
        [0.8809, 0.1170, 0.5028, 0.1291, 0.9221, 0.4695, 0.6591, 0.7056],
        [0.1084, 0.5578, 0.1239, 0.1242, 0.9043, 0.5201, 0.1735, 0.1853],
        [0.5432, 0.6681, 0.3786, 0.1746, 0.5713, 0.8118, 0.9247, 0.6339],
        [0.2185, 0.9275, 0.1661, 0.3302, 0.9546, 0.0585, 0.6166, 0.3894],
        [0.3834, 0.3443, 0.7211, 0.5370, 0.8339, 0.1142, 0.3608, 0.7398],
        [0.3720, 0.6800, 0.5449, 0.8443, 0.8730, 0.3338, 0.5325, 0.2288],
        [0.5374, 0.9998, 0.5490, 0.6937, 0.4675, 0.2122, 0.6559, 0.5185],
        [0.9551, 0.2855, 0.3483, 0.8831, 0.1163, 0.7579, 0.3232, 0.5489],
        [0.7475, 0.9753, 0.5024, 0.1861, 0.4938, 0.8533, 0.1126, 0.0977],
        [0.4979, 0.2518, 0.3445, 0.5422, 0.5938, 0.0149, 0.5034, 0.1364],
        [0.8549, 0.7204, 0.6437, 0.0556, 0.1594, 0.0757, 0.5091, 0.6918],
        [0.2438, 0.6959, 0.9856, 0.7868, 0.2132, 0.0131, 0.5101, 0.3545]])
torch.Size([16, 8])
tensor([[0.7969, 0.7674, 0.5840, 0.6569, 0.0975, 0.3534, 0.1829, 0.1836],
        [0.0061, 0.4058, 0.1227, 0.3704, 0.2956, 0.6638, 0.2956, 0.2010],
        [0.2528, 0.1548, 0.9587, 0.3630, 0.9027, 0.4563, 0.8646, 0.9603],
        [0.0882, 0.5201, 0.9914, 0.0578, 0.3112, 0.1091, 0.8010, 0.6861],
        [0.6997, 0.8773, 0.1547, 0.3629, 0.9167, 0.3069, 0.8044, 0.4209],
        [0.4855, 0.9577, 0.5185, 0.2974, 0.4139, 0.7274, 0.0733, 0.8046],
        [0.4067, 0.1226, 0.2337, 0.2275, 0.4362, 0.5164, 0.7355, 0.2621],
        [0.4168, 0.2742, 0.9794, 0.0484, 0.6996, 0.6845, 0.6248, 0.0638],
        [0.1092, 0.8893, 0.7788, 0.8916, 0.4265, 0.2073, 0.1638, 0.0036],
        [0.6418, 0.7444, 0.7945, 0.0532, 0.4958, 0.9727, 0.5158, 0.7032],
        [0.5125, 0.8095, 0.6613, 0.9964, 0.8463, 0.2913, 0.6000, 0.3051],
        [0.1549, 0.2511, 0.4502, 0.2377, 0.6671, 0.6066, 0.2299, 0.8070],
        [0.6881, 0.9308, 0.7815, 0.4616, 0.4801, 0.2557, 0.2890, 0.9271],
        [0.4900, 0.0890, 0.5085, 0.9079, 0.6904, 0.2588, 0.9078, 0.6647],
        [0.0164, 0.4759, 0.3176, 0.6650, 0.9355, 0.7239, 0.4596, 0.9296],
        [0.7690, 0.5104, 0.7582, 0.3573, 0.6260, 0.3604, 0.4947, 0.3848]])
torch.Size([16, 8])
tensor([[0.9357, 0.8048, 0.0204, 0.9148, 0.2590, 0.4970, 0.1098, 0.9920, 0.4196,
         0.5242, 0.5921, 0.8188],
        [0.2616, 0.0649, 0.8290, 0.1705, 0.7162, 0.3552, 0.6353, 0.4791, 0.0050,
         0.5153, 0.0056, 0.5773],
        [0.4344, 0.8322, 0.1063, 0.0943, 0.5689, 0.2576, 0.3719, 0.7945, 0.1368,
         0.5047, 0.5577, 0.7870],
        [0.8323, 0.3672, 0.2062, 0.8800, 0.8181, 0.7346, 0.0574, 0.9323, 0.8588,
         0.7175, 0.3350, 0.8855],
        [0.2410, 0.9012, 0.5058, 0.2614, 0.8286, 0.4564, 0.6951, 0.1144, 0.0121,
         0.3116, 0.4620, 0.9941],
        [0.8815, 0.8146, 0.6522, 0.5325, 0.5292, 0.4009, 0.6766, 0.8039, 0.2541,
         0.5315, 0.7872, 0.3705],
        [0.6226, 0.2077, 0.7905, 0.9981, 0.7914, 0.8474, 0.5674, 0.0651, 0.0475,
         0.5021, 0.3279, 0.5148],
        [0.4902, 0.4474, 0.4298, 0.3005, 0.1387, 0.1203, 0.8267, 0.3650, 0.7690,
         0.7111, 0.1213, 0.2103],
        [0.9279, 0.5746, 0.2427, 0.9657, 0.0221, 0.8265, 0.2993, 0.2984, 0.8418,
         0.1939, 0.5302, 0.9562],
        [0.8751, 0.6429, 0.4570, 0.8973, 0.0927, 0.9441, 0.9564, 0.0324, 0.5438,
         0.1091, 0.3608, 0.6591],
        [0.2943, 0.0369, 0.6638, 0.8862, 0.7759, 0.1928, 0.1189, 0.0290, 0.2486,
         0.0931, 0.2668, 0.4172],
        [0.5485, 0.5224, 0.2187, 0.6483, 0.9598, 0.0263, 0.9508, 0.0179, 0.3788,
         0.7101, 0.3473, 0.6253],
        [0.5583, 0.7605, 0.0657, 0.2746, 0.3617, 0.5696, 0.8715, 0.1132, 0.5291,
         0.8978, 0.2165, 0.9961],
        [0.9096, 0.7823, 0.7387, 0.8148, 0.7766, 0.1197, 0.0552, 0.2206, 0.7095,
         0.9959, 0.9389, 0.7036],
        [0.7810, 0.7459, 0.1691, 0.1575, 0.1427, 0.7091, 0.4556, 0.3352, 0.5086,
         0.6785, 0.5344, 0.7429],
        [0.9049, 0.5791, 0.2186, 0.2087, 0.4906, 0.1012, 0.2310, 0.7797, 0.2669,
         0.3981, 0.2346, 0.9616]])
torch.Size([16, 12])
</pre>
<p>接着，分别计算 Q、K、V 这三个矩阵，代码如下所示：</p>
<pre class="brush: python; title: ; notranslate">
Q = X.matmul(Wq.T)
print(Q)
print(Q.shape)

K = X.matmul(Wk.T)
print(K)
print(K.shape)

V = X.matmul(Wv.T)
print(V)
print(V.shape)
</pre>
<p>计算得到 Q、K、V 三个矩阵的结果，如下所示：</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
tensor([[ 0.3018,  0.5771,  0.8287, -0.3224,  0.9979, -1.3807,  0.7953,  0.3018],
        [-2.4223, -1.3309,  0.0371,  0.4128, -0.2362, -1.7722, -0.9576, -0.6908],
        [-1.5899, -2.5706, -3.0113, -3.2927, -4.0568, -0.3453, -3.0388, -2.1831],
        [ 1.4786,  2.4595,  3.2942,  2.1628,  4.1394,  0.7536,  2.8714,  3.5802],
        [-2.4223, -1.3309,  0.0371,  0.4128, -0.2362, -1.7722, -0.9576, -0.6908],
        [-0.3082, -0.5900, -0.9257, -0.7688,  1.8828, -1.6065, -0.8011, -0.4114]])
torch.Size([6, 8])
tensor([[ 1.1813,  1.3559,  0.2174,  3.4764,  1.1683, -0.6475,  0.9448,  0.1573],
        [-1.9191, -0.0077, -1.5536, -0.7241, -1.9709, -1.2558, -1.5606, -1.4531],
        [-2.6464, -2.5228, -3.2055, -1.5096, -2.4377, -2.7335, -1.7701, -0.5160],
        [-0.2554,  2.9326,  1.8757,  1.9398, -0.1509,  1.9660, -0.5092, -0.2307],
        [-1.9191, -0.0077, -1.5536, -0.7241, -1.9709, -1.2558, -1.5606, -1.4531],
        [-0.6116,  1.3902, -0.1460,  0.0244, -0.5577,  1.5972, -2.2190, -0.0214]])
torch.Size([6, 8])
tensor([[ 1.5465,  1.5362,  1.3274,  0.9452,  0.5531,  0.1000, -1.5909,  0.8779,
          0.8645,  1.1643,  2.2148,  1.3088],
        [-1.0912, -2.8470, -0.4005,  0.6766, -1.7351,  1.0082, -1.1248, -3.2161,
          0.5959, -2.3485, -1.7592, -1.2618],
        [-3.7293, -2.7705, -2.1744, -2.7340, -1.0410, -1.8867, -4.0902, -0.3303,
         -3.1343, -2.4864, -1.1285, -3.5427],
        [ 4.1125,  0.5593,  2.1795,  4.3551,  1.7887,  3.0898,  1.5155,  3.2049,
          2.5387,  2.0061,  1.2701,  2.4616],
        [-1.0912, -2.8470, -0.4005,  0.6766, -1.7351,  1.0082, -1.1248, -3.2161,
          0.5959, -2.3485, -1.7592, -1.2618],
        [ 0.2002,  1.3752, -0.0809, -1.2746, -2.3948, -0.3425,  1.5967,  0.5399,
          0.9113,  0.0962,  0.7300, -1.0553]])
torch.Size([6, 12])
</pre>
<p><strong style="line-height: 1.7;font-size: 18px">3. 计算注意力分数矩阵</strong></p>
<p>上一步已经得到 Q、K，其中 Q 是 n x dq 矩阵，K 是 n x dk 矩阵，这里 dq = dk。<br />
注意力分数矩阵（Attention Score Matrix）的计算，直接使用 Q 与 K 的转置相乘，得到的结果是一个 n x n 矩阵 Q(K).T。<br />
使用 PyTorch 计算 Q·(K).T，代码如下：</p>
<pre class="brush: python; title: ; notranslate">
QKT = Q.matmul(K.T)
print(QKT)
print(QKT.shape)
</pre>
<p>计算结果，如下所示：</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
tensor([[  3.0568,  -3.5501,  -4.6460,  -0.7954,  -3.5501,  -4.0441],
        [ -3.3648,   9.4917,  16.4977,  -5.2158,   9.4917,  -0.9232],
        [-25.1955,  26.4776,  42.6549, -17.1836,  26.4776,   6.2588],
        [ 20.9407, -28.3288, -43.0230,  15.7782, -28.3288,  -5.4665],
        [ -3.3648,   9.4917,  16.4977,  -5.2158,   9.4917,  -0.9232],
        [ -1.6198,   2.7452,   7.8636,  -7.8188,   2.7452,  -2.3448]])
torch.Size([6, 6])
</pre>
<p><strong style="line-height: 1.7;font-size: 18px">4. 标准化注意力分数矩阵，并与 V 相乘得到自注意力结果</strong></p>
<p>针对矩阵 QKT 进行标准化 Softmax 操作，得到归一化的结果矩阵 A，A 的大小为 n x n。<br />
最后，n x dv 的矩阵 V，乘以 n x n 的矩阵 A，得到了最后的自注意力结果矩阵 Z。<br />
计算 Z = AT(Q, K, V) 的计算公式如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Attention-AT-QKV.png" alt="Attention-AT-QKV" width="260" height="60" class="alignnone size-full wp-image-2690" /><br />
前面计算已经得到 QKT 矩阵，n=6，dk=8，则 A 的大小也是 6 x 6。</p>
<pre class="brush: python; title: ; notranslate">
dk = 16
A = F.softmax(QKT/(dk**0.50), dim=0)
print(A)
</pre>
<p>然后再乘以矩阵 V：</p>
<pre class="brush: python; title: ; notranslate">
Z = A.matmul(V)
print(Z)
print(Z.shape)
</pre>
<p>结果示例如下所示：</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
tensor([[-2.9351e-02,  6.1129e-02,  5.4583e-02,  9.4816e-02,  4.9514e-02,
          1.2487e-01,  2.9251e-02,  6.3045e-03, -2.7838e-02,  2.7134e-02,
          1.2357e-01,  4.0422e-02],
        [-2.4056e-01,  3.3915e-02,  1.9047e-03,  1.1973e-01, -1.5679e-02,
          1.4004e-01, -9.8062e-02, -8.4042e-02, -1.5060e-01, -1.1663e-02,
          1.6010e-01, -2.3983e-02],
        [-7.0469e+00, -5.9786e+00, -6.3531e+00, -3.7648e+00, -1.4788e+00,
         -5.4479e+00, -6.4546e+00, -6.2927e+00, -2.4734e+00, -3.2765e+00,
         -5.1672e+00, -6.0859e+00],
        [ 3.8240e+00,  1.7311e+00,  2.8731e+00,  2.2339e+00,  4.9503e+00,
          2.7620e+00,  3.9712e+00,  1.2559e+00,  3.1700e+00,  1.4015e+00,
          1.8649e+00,  2.7100e+00],
        [-2.4056e-01,  3.3915e-02,  1.9047e-03,  1.1973e-01, -1.5679e-02,
          1.4004e-01, -9.8062e-02, -8.4042e-02, -1.5060e-01, -1.1663e-02,
          1.6010e-01, -2.3983e-02],
        [-1.2892e-01,  4.9015e-02,  2.3402e-02,  9.5212e-02, -1.8994e-02,
          1.2503e-01, -3.3910e-02, -1.9795e-02, -1.0338e-01,  1.0985e-02,
          1.5254e-01,  6.4172e-03]])
torch.Size([6, 12])
</pre>
<p>或者，我们可以直接使用 PyTorch 的 torch.nn.functional.scaled_dot_product_attention() 函数，从 Q、K、V 一步就可以计算得到，如下所示：</p>
<pre class="brush: python; title: ; notranslate">
import torch.nn.functional as F

Z = F.scaled_dot_product_attention(Q, K, V)
print(Z)
print(Z.shape)
</pre>
<p><strong style="font-size: 20px">参考资源</strong></p>
<ul>
<li><a target="_blank" href="https://sebastianraschka.com/blog/2023/self-attention-from-scratch.html">Understanding and Coding the Self-Attention Mechanism of Large Language Models From Scratch</a></li>
<li><a target="_blank" href="https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html">https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2688.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>理解注意力机制</title>
		<link>http://shiyanjun.cn/archives/2651.html</link>
		<comments>http://shiyanjun.cn/archives/2651.html#comments</comments>
		<pubDate>Fri, 23 Feb 2024 13:15:47 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[人工智能]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2651</guid>
		<description><![CDATA[在深度学习中，Transformer 架构被广泛使用，而它所基于的注意力机制是最核心的部分，这里通过参考网上各 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>在深度学习中，Transformer 架构被广泛使用，而它所基于的注意力机制是最核心的部分，这里通过参考网上各种介绍注意力机制的资料，经过简化并重新组织内容，来说明注意力机制到底是一种什么样的机制。</p>
<p><strong style="font-size: 20px">注意力（Attention）框架</strong></p>
<p>19 世纪 90 年代，美国心理学家威廉·詹姆斯（William James）提出了视觉注意力的工作原理类似于聚光灯，他认为：我们在日常中会聚焦一些事物，在这个焦点上可以清楚地看到一些物体；而在这个焦点周围的区域（称为边缘）仍然可见其他一些物体但不是很清楚。基于这个注意力的原理，后来提出了双组件（two‐component）的框架，其中两个非常重要的概念就是：非自主性提示和自主性提示，通过这两种方式都能够引导我们注意力关注焦点的改变。</p>
<ul>
<li>非自主性提示</li>
</ul>
<p>我们在所处环境中，时刻都能不由自主地目及一些事物，还有另外一些事物，这时进入视觉系统的物体如果有特别突出的特征，我们就会将注意力的焦点放在其上面，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Attention-Eye-Coffee.png" alt="Attention-Eye-Coffee" width="464" height="314" class="alignnone size-full wp-image-2653" /><br />
上面指定了 5 个物品：一份报纸、一篇论文、一杯咖啡、一个笔记本、一本书，其中装有红色咖啡的杯子的特征最突出，我们不自主地看向这些物品时会把注意力焦点放到咖啡杯上面，通过默认的提示选择了所关注的物品，它应该并不受我们意识控制，这就是非自主提示。</p>
<ul>
<li>自主性提示</li>
</ul>
<p>自主提示，就是我们有意识地将注意力改变，选择聚集到一个并非是由于特征突出才关注的物体上。当我们可以将注意力焦点改变时，比如从上图显眼突出的红色咖啡杯，将注意力集中到最下面的一本书上，这时相当于强制给了大脑一个暗示，要将注意力焦点转移到一本书上，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Attention-Eye-Book.png" alt="Attention-Eye-Book" width="442" height="326" class="alignnone size-full wp-image-2655" /><br />
使用神经网络来设计注意力机制的框架，就是基于上述非自主性提示和自主性提示的原理，来选择要聚焦的观察对象。具体抽象出来的元素就是最基本的 Key、Query、Value，如下所示：</p>
<ul>
<li style="line-height: 1.5">Keys（键）：在非自主提示下，进入视觉系统的的所有元素的线索，称为 Keys。</li>
<li style="line-height: 1.5">Query（查询）：在自主提示下，自主提示的内容或元素的线索，称为 Query。</li>
<li style="line-height: 1.5">Values（值)：在由自主提示 Query 限制或者强制下改变注意力的焦点，也就是经过从 Keys 中进行匹配 Query，所得到的进入视觉系统的内容，称为 Values。</li>
</ul>
<p>注意力机制的框架，可以用下图来表示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Attention-QKV.png" alt="Attention-QKV" width="447" height="258" class="alignnone size-full wp-image-2656" /><br />
上图表达了：<br />
在注意力机制中，Key 和 Value 是成对的，而 Query 及其查询的结果是前面所有 Keys/Values 对的一个子集。<br />
通过注意力汇聚（Attention Pooling）将 Query（自主性提示）和 Keys（非自主性提示）结合在一起，实现对 Values（感官输入）的选择。</p>
<p><strong style="font-size: 20px">注意力汇聚（Attention Pooling）</strong></p>
<p>通过上面说明注意力的框架，接下来要理解注意力是如何进行汇聚（Pooling）的，即通过什么样的过程使我们的视觉系统聚焦到了最后的物体上（如上面部分：将没有特别突出特征的一本书作为焦点）。从直观感觉上来说，貌似应该是 Query 与 Keys 越接近的话，注意力越集中，也就是说权重越大。<br />
使用 Nadaraya–Watson 核回归模型，能够非常清楚地解释注意力机制。Nadaraya 和 Watson 提出，根据输入位置来对输出进行加权，则可以得到下面的公式推导：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Attention-Nadaraya-Watson-Kernel-Regression.png" alt="Attention-Nadaraya-Watson-Kernel-Regression" width="335" height="246" class="alignnone size-full wp-image-2657" /><br />
其中，在推导的第二步代入了高斯核（Gaussian kernel）函数（也可以使用其它的核函数：Boxcar/Constant/Epanechikov，具体说明可以参考这里 <a target="_blank" href="https://d2l.ai/chapter_attention-mechanisms-and-transformers/attention-pooling.html#kernels-and-data">https://d2l.ai/chapter_attention-mechanisms-and-transformers/attention-pooling.html#kernels-and-data</a>）。<br />
上面公式中，x 对应于 Query，xi 对应于 Key。如果 xi 越是接近 x，则分配给 xi 这个 Key 所对应的结果 yi 的权重就越大。同样可以推导出，一个 Key 越接近给定的 Query，那么分配给这个 Key 对应的 Value 的注意力权重就会越大，即获得了更多的注意力。<br />
上面公式给出了对注意力汇聚（Attention Pooling）的建模：通过使用 x 与 xi 之间关系的来表示注意力的权重 α(x-xi)，即使用 Query 与 Key 之间关系表示注意力汇聚（Attention Pooling）。可以看出，它是不带参数的注意力汇聚（Attention Pooling）模型，对应带参数的注意力汇聚（Attention Pooling）模型，也可以同样推导出来，如下所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Attention-Nadaraya-Watson-Kernel-Regression-with-Param.png" alt="Attention-Nadaraya-Watson-Kernel-Regression-with-Param" width="360" height="240" class="alignnone size-full wp-image-2658" /><br />
其中，公式中的 w 是参数，这个参数 w 可以通过学习得到，所以使用带参数的注意力模型也能够对注意力汇聚（Attention Pooling）进行同样的建模。<br />
上面，使用高斯核来对 Query 和 Key 之间的关系建模，高斯核指数部分可以视为注意力评分函数（Attention Scoring Function），然后把该数的输出作为后面 Softmax 函数的输入进行计算，这样将得到与 Key 对应的 Value 的概率分布（即注意力权重）。最后，注意力汇聚（Attention Pooling）的输出就是基于这些注意力权重的值的加权平均和。<br />
计算注意力汇聚（Attention Pooling）结果的过程，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Computing-Attention-Pooling.png" alt="Computing-Attention-Pooling" width="500" height="292" class="alignnone size-full wp-image-2659" /><br />
前面我们使用 Nadaraya–Watson 核回归模型，使用了一个高斯核 α(q, k) 基于相似度来评估注意力权重，注意力评分函数（Attention Scoring Function）如下所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Gaussian-Kernel-α-q-k.png" alt="Gaussian-Kernel-α-q-k" width="250" height="48" class="alignnone size-full wp-image-2666" /><br />
不带指数部分的表达式，如下所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/α-q-k-Function.png" alt="α-q-k-Function" width="454" height="72" class="alignnone size-full wp-image-2667" /><br />
进而，注意力汇聚（Attention Pooling）函数可以表示为：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Attention-Pooling-fα-q-k.png" alt="Attention-Pooling-f(α-q-k)" width="336" height="53" class="alignnone size-full wp-image-2668" /><br />
选择不同的注意力评分函数，会导致不同的注意力汇聚（Attention Pooling）操作。</p>
<p><strong style="font-size: 20px">自注意力（Self-Attention）</strong></p>
<p>从形式上看，给定一个由 Token 组成的输入序列 x1, x2,&#8230;, xn，该序列的自注意力输出序列为 y1, y2,&#8230;, yn，根据注意力汇聚（Attention Pooling）公式：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Attention-Pooling-formula.png" alt="Attention-Pooling-formula" width="285" height="125" class="alignnone size-full wp-image-2662" /><br />
我们可以得到 yi 的表达式为：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Self-Attention-yi.png" alt="Self-Attention-yi" width="280" height="14" class="alignnone size-full wp-image-2661" /><br />
在自注意力中，Query、Keys 和 Values 都来自同一组输入，所以称为自注意力（Self-Attention）。通过下面的动态图像中提供的句子，我们可以理解一下自注意力（Self-Attention）的原理：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Self-Attention-Head-View.gif" alt="Self-Attention-Head-View" width="425" height="374" class="alignnone size-full wp-image-2660" /><br />
当聚焦到前面一列头部的某个 Token 时，它会在后面一列（也是同一个句子中的 Token 序列）找到与该 Token 更相关的其它 Token，或者可以说句子中的每个 Token 都与前面当前所聚焦 Token 有一个相关数值，值越大表示越相关，对应的注意力权重也越大。当然，同一个 Token 与它自己最相关，通常相关值最大。</p>
<p><strong style="font-size: 20px">多头注意力（Multi-Head Attention）</strong></p>
<p>多头注意力（Multi-Head Attention）融合了来自于多个注意力汇聚（Attention Pooling）的不同知识，虽然它使用的是相同的注意力机制，但是能够比单个注意力汇聚（Attention Pooling）学习到更多的行为，比如，在句子内部能够捕获可变范围的多个依赖关系。<br />
下面使用全连接层（FC）作为 Head，多个全连接层依次连接形成多头注意力（Multi-Head Attention），如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Multi-Head-Attention.png" alt="Multi-Head-Attention" width="466" height="236" class="alignnone size-full wp-image-2660" /><br />
上图中，在实践中是可以实现对多头注意力的多个 Head 进行并行计算处理的。<br />
为了更形象的说明多头注意力，我们引用一个三维的说明图片（来自 peltarion.com），更直观地理解，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/multi-head-attention-peltarion.png" alt="multi-head-attention-peltarion" width="614" height="681" class="alignnone size-full wp-image-2674" /></p>
<p>输入序列（如一个句子），其中的每个 Token 都获取到对应的 Word Embedding，然后输入到神经网络中（比如 Transformer 架构的网络）。在这个过程中，每一个 Head 都可以自己独立地去学习得到自己独有的特征，类似于只有一个 Self-Attention 的情况。而多个 Head 并行学习过程中，可能相当于有多个 Self-Attention 的情况，整体来看 Query、Keys、Values 在不同的 Head 上都可能会有部分互相不同，从而学习到的特征也就不同。<br />
比如，在机器翻译任务中，使用多头注意力能够学习并捕捉到输入序列中的不同类型信息：一个注意力头可能学习句子的语法结构，而另一个注意力头可能学习句子中的于语义信息，这样更有利于模型生成准确、自然的翻译结果，从而提高了模型的性能。</p>
<p><strong style="font-size: 20px">参考资源</strong></p>
<ul>
<li><a target="_blank" href="https://zh.d2l.ai/chapter_attention-mechanisms/index.html">https://zh.d2l.ai/chapter_attention-mechanisms/index.html</a></li>
<li><a target="_blank" href="https://d2l.ai/chapter_attention-mechanisms-and-transformers/index.html">https://d2l.ai/chapter_attention-mechanisms-and-transformers/index.html</a></li>
<li><a target="_blank" href="https://armanasq.github.io/nlp/self-attention/">https://armanasq.github.io/nlp/self-attention/</a></li>
<li><a target="_blank" href="https://theaisummer.com/self-attention/">https://theaisummer.com/self-attention/</a></li>
<li><a target="_blank" href="https://peltarion.com/blog/data-science/self-attention-video">https://peltarion.com/blog/data-science/self-attention-video</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2651.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>什么是 AI 智能体（AI Agent）</title>
		<link>http://shiyanjun.cn/archives/2632.html</link>
		<comments>http://shiyanjun.cn/archives/2632.html#comments</comments>
		<pubDate>Sun, 18 Feb 2024 12:43:25 +0000</pubDate>
		<dc:creator><![CDATA[Yanjun]]></dc:creator>
				<category><![CDATA[Agent]]></category>
		<category><![CDATA[人工智能]]></category>
		<category><![CDATA[LLM]]></category>

		<guid isPermaLink="false">http://shiyanjun.cn/?p=2632</guid>
		<description><![CDATA[目前 LLM 技术发展非常迅速，虽然 LLM 看似已经具备了丰富的知识与足够的智慧，但是在一些场景下我们可能需 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>目前 LLM 技术发展非常迅速，虽然 LLM 看似已经具备了丰富的知识与足够的智慧，但是在一些场景下我们可能需要更加精确的答案，而不是得到一些幻觉类答案，或者答案不够实时，或者人类诉求太过复杂以至于 LLM 无法理解，等等，这些问题也是目前阻止很多 AI 应用落地的主要原因。<br />
基于 AI Agent（AI 智能体）自身所具备的能力，同时借助于 LLM 所释放的潜力，或许在不久的将来能够不断优化改进，达到满足人类更方便、更智能地使用 AI 完成各种任务的需求，实现普惠 AI 的目标。<br />
下面，首先了解一下 LLM 和 AI Agent 有什么不同：<br />
人类与 LLM 之间的交互，是基于给定的 Prompt 提示词来实现的，而对于 Prompt 的设计不同 LLM 给出的对话回答质量也是不同的，所以需要人类通过一些特定的方法或经过多次尝试，才有可能逐步提高对话的精确度和满意度。可见，目前基于 LLM 的应用作为工具，能够在一定程度上提高人类日常生活、工作等的效率，同时反过来也对人类使用 LLM 提出了一定的要求，而且这一部分工作更多的是需要人类主动请求，而 LLM 被动执行动作来完成一次一次地交互。<br />
AI Agent 提供了更广泛的功能，特别是在与环境的交互、主动决策和执行各种任务方面。在基于 LLM 的场景下，我们给 AI Agent 设定一个目标，它就能够针对这个目标独立思考并执行动作，对给定任务进行详细拆解，得到最终计划的所有步骤，从而根据外部环境的反馈以及自己的自主思考，创建更加合适的 Prompt 输入给 LLM 以实现既定的问答目标。简单来说，不需人类的参与， AI Agent 就能够完全独立地完成预先设定的目标。</p>
<p><strong style="font-size: 20px">什么是 AI Agent</strong></p>
<p>AI Agent 是一种能够感知环境、制定决策、执行动作的人工智能体，它能够通过独立思考、进行规划并调用合适的工具去逐步实现给定的目标，整个过程完全不需要人类参与。大模型（LLM）的出现、快速发展与完善，AI Agent 更有潜力借助 LLM 来实现对通用问题的解决与自动化处理，所以我们可以认为目前 AI Agent 基本是基于 LLM（LLM-Based） 的 AI 智能体。<br />
AI Agent 的演化经过了如下几个阶段：</p>
<ul>
<li>Symbolic Agents</li>
</ul>
<p>在 AI 研究的早期阶段，最主要的方法是符号 AI，通过采用逻辑规则和符号表示来封装知识并进行推理。在 Symbolic Agents 这个阶段主要专注解决的问题是：转换问题、表示/推理问题。</p>
<ul>
<li>Reactive Agents</li>
</ul>
<p>Reactive Agent 不使用复杂的符号推理，主要关注 Agent 与其所在的环境（Environment）之间的交互，强调快速和实时响应。</p>
<ul>
<li>RL-Based Agents</li>
</ul>
<p>在 RL-Based Agents 阶段，主要关注点是如何让 Agent 通过与环境（Environment）的交互进行学习，使其在特定任务中获得最大的累积奖励，从而使 Agent 能够在未知环境中自主学习并执行 Action，学习过程中无需人工干预。</p>
<ul>
<li>Agents with Transfer Learning and Meta Learning</li>
</ul>
<p>传统的 RL 学习需要 Agent 耗时处理大量样本和训练，通过引入迁移学习，实现知识共享和迁移，提高了 Agent 的性能表现和泛化能力。在此基础上又引入了元学习，使 Agent 能基于少量样本迅速推断出新任务的最优策略。</p>
<ul>
<li>LLM-Based Agents</li>
</ul>
<p>LLM-Based Agent 以 LLM 为核心大脑组件（中央控制器），通过多模态感知（Multimodal Perception）和工具利用（Tool Utilization）等策略来扩展其 Perception 和 Action 空间。通过使用 CoT（Chain-of-Thought）和问题分解等技术，使 LLM-Based Agent 具有推理和规划能力。同时，LLM-Based Agent 也能够与环境进行交互，通过不断地从反馈中学习，从而优化策略并做出决策，执行下一个 Action。另外，基于 LLM-Based Agent 具有更广泛的应用场景。</p>
<p><strong style="font-size: 20px">LLM-Based Agent 基本框架</strong></p>
<p>LLM-Based Agent 的基本概念框架，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/LLM-Based-Agent-Framework.png" alt="LLM-Based-Agent-Framework" width="889" height="524" class="alignnone size-full wp-image-2633" /><br />
其中包括三个主要的组件：</p>
<ul>
<li>大脑（Brain）</li>
</ul>
<p>Brain 组件主要由核心的 LLM 组成，它包含的能力有：存储知识和记忆、信息处理与决策、推理与规划。</p>
<ul>
<li>感知（Perception）</li>
</ul>
<p>Perception 组件主要将 Agent 的感知空间，从语言文字领域扩展到多模态领域（包括语言、视觉、听觉等等）。</p>
<ul>
<li>行动（Action）</li>
</ul>
<p>在 Action 组件中，Agent 从 Brain 模块接收 Action 序列，执行与环境交互的任务。<br />
通过上图和对三个组件的描述，我们可以看到存在这样一个自动化的环路：Environment → Perception → Brain → Action → Environment，Agent 通过自动地持续与环境交互（感知环境输入/执行 Action 输出到环境）不断学习，并不断优化策略执行新的 Action。</p>
<p><strong style="font-size: 20px">LLM-Based Agent 应用场景</strong></p>
<p>目前，LLM-Based Agent 的应用场景主要包括三类：</p>
<ul>
<li>Single Agent</li>
</ul>
<p>Single Agent，即单智能体。解决这一类场景的应用主要专注于解决任务，即任务导向，如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Single-Agent.png" alt="Single-Agent" width="679" height="276" class="alignnone size-full wp-image-2634" /><br />
任务导向：在任务导向的部署中，Agent 遵循用户的指令，负责诸如目标分解、子目标序列规划、环境交互式探索等任务，直至最终实现目标。<br />
创新导向：在对智能程度要求更高的领域，比如尖端科学，Agent 的潜力还没有完全实现，还有很大的潜力。<br />
生命周期导向：构建一个具有普适能力的智能体，能够在开放、未知的世界中不断探索、创造新技能，并保持长期的生命周期，目前这还是一个巨大的挑战。</p>
<ul>
<li>Agent-Agent</li>
</ul>
<p>Agent-Agent，即多智能体。多智能体应用场景会存在多个智能体，它们之间存在互动，主要包括两种互动形式：合作型互动（Cooperative Interaction）、对抗型互动（Adversarial Interaction），如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Agent-Agent.png" alt="Agent-Agent" width="954" height="380" class="alignnone size-full wp-image-2635" /></p>
<ul>
<li>Agent-Human</li>
</ul>
<p>Agent-Human，即人机互动智能体。Agent 与人类合作完成任务，在这个过程中，人类的参与能有效地指导和监督 Agent 的行为，确保它们的能力满足人类的需求和目标。如下图所示：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/Agent-Human.png" alt="Agent-Human" width="866" height="364" class="alignnone size-full wp-image-2636" /><br />
Instructor-Executor 范式：人类直接提供清晰具体的指令，Agent 理解来自人类的自然语言命令，并将其转化为相应的 Action。<br />
Equal Partnership 范式：包含两种情形，一种是 Agent 能够从人类的表达中感知并理解情感和情绪，最终创造出情感共鸣的对话；另一种是 Agent 以与人类对等的身份共同参与任务的执行。</p>
<p><strong style="font-size: 20px">AI Agent Society</strong></p>
<p>AI 智能体社会（AI Agent Society），这个概念表达了 LLM-Based Agent 与模拟环境进行交互，像人类一样规划目标、做出决策、执行 Action，并参与社交活动。而未来人类也可能参与到这样一个智能体社会中，与 AI Agent 形成一个互相协作、共同演化的整体。下图展示了这种愿景：<br />
<img src="http://photos.shiyanjun.cn/wp-content/uploads/2024/02/AI-Agent-Society.jpg" alt="AI-Agent-Society" width="792" height="503" class="alignnone size-full wp-image-2637" /><br />
上图中展示了一个节日的场景，模拟了一个 AI Agent 社会应用场景：<br />
在厨房内：一个 Agent 负责点菜，另一个 Agent 负责规划（做菜需要哪些食材），然后完成做菜的任务；<br />
在音乐会上：三个 Agent 作为一个乐队进行协同表演；<br />
在户外：两个 Agent 在讨论制作灯笼，规划需要的材料，使用工具来计算成本开销。<br />
人类用户：也可以参与到这个社会场景中，与 AI Agent 一起协同、社交。</p>
<p><strong style="font-size: 20px">参考资源</strong></p>
<ul>
<li><a target="_blank" href="https://arxiv.org/abs/2304.03442">Generative Agents: Interactive Simulacra of Human Behavior</a></li>
<li><a target="_blank" href="https://arxiv.org/abs/2309.07864">The Rise and Potential of Large Language Model Based Agents: A Survey</a></li>
<li><a target="_blank" href="https://baijiahao.baidu.com/s?id=1778067603786228822">全球 AI Agent 大盘点，大语言模型创业一定要参考的 60 个 AI 智能体</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://shiyanjun.cn/archives/2632.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
