1776654103

This commit is contained in:
Docker7530
2026-04-20 11:01:47 +08:00
parent ab41c81a53
commit 6b50219f55
209 changed files with 1922 additions and 1467 deletions
+43
View File
@@ -0,0 +1,43 @@
# Beam Search(束搜索)通俗解释
一句话:**一种在生成序列(文本、翻译、语音)时,比“贪心”更优、比“穷举”更快的搜索算法**。
## 1. 核心场景
AI 生成文字时,每一步都要选下一个词:
- Greedy Search(贪心):**每步只选概率最高的1个**,容易局部最优、整体很差。
- Exhaustive Search(穷举):**试所有可能**,效果最好但太慢、算力爆炸。
- **Beam Search****每步只保留概率最高的 k 个候选路径**,平衡效果与速度。
## 2. 超简单比喻
你要走迷宫找终点:
- 贪心:每步只走眼前最好的一条路。
- 束搜索(beam=k):**每步保留 top-k 条最好的路**,一起往前走,最后选整条路径概率最大的。
## 3. 关键参数
- **beam width(束宽)= k**
- k=1 = 贪心搜索
- k越大 → 效果越好、越慢
- k=无穷 = 穷举
## 4. 工作流程(极简)
1. 第一步:选出**概率最高的 k 个词**。
2. 第二步:对这 k 个词,**分别生成下一个词**,再从所有结果里**重新选 top-k**。
3. 重复直到结束,**选整条序列概率最大**的输出。
## 5. 优点 & 缺点
✅ 优点:
- 比贪心**更通顺、更少局部最优**
- 比穷举**快得多、可实际使用**
❌ 缺点:
- 仍不是全局最优
- 可能生成**重复、呆板、过于安全**的句子(现在大模型多用采样代替)
+71
View File
@@ -0,0 +1,71 @@
一种"带插件的文本生成器"。它允许你创建一个包含变量和逻辑的"模板"文件,然后在渲染时填入具体数据,最终生成想要的文本格式(如 HTML、JSON、Markdown 等)。
它的语法简单直观,主要有三种核心元素:
- **`{{ … }}`**:用于输出变量值,例如 `{{ user_name }}` 会在渲染时被具体的用户名替换。
- **`{% … %}`**:用于执行逻辑,例如循环 `{% for item in items %}` 或判断 `{% if user_logged_in %}`
- **`{# … #}`**:用于添加注释,不会出现在最终输出中。
### 提示词(Prompt)工程
主要用于**动态、精确地构建和管理提示词(Prompt)**。其核心价值在于将"提示词模板"与"可变数据"分离。
#### 1. 构建动态、结构化的提示词
在实际应用中,很少会发送固定的文本给 AI。你需要将用户问题、聊天历史、检索到的知识等动态拼接到提示词中。Jinja 让这件事变得优雅和可控。
**示例:一个智能客服的提示词模板**
```jinja
你是一名专业的客服助手,名叫"小智"。
请基于以下提供的资料,回答用户的问题。
如果资料中找不到答案,请直接说"不知道",不要编造。
### 参考资料:
{% for doc in search_results %}
- {{ doc.title }}: {{ doc.content }}
{% endfor %}
### 对话历史:
{% for msg in chat_history %}
{{ msg.role }}: {{ msg.content }}
{% endfor %}
### 用户当前的问题是:
{{ current_question }}
请给出你的回答:
```
在这个模板中,`search_results``chat_history``current_question` 都是变量。渲染时,Jinja 会循环填入所有搜索结果和对话记录,生成一个结构清晰、信息完整的提示词。
#### 2. 统一聊天模型的提示词格式(Chat Template
这是 Jinja 在 AI 领域最"杀手级"的应用。每个大模型(如 Llama、Mistral、ChatGLM)对聊天对话的输入格式要求都不同。**Chat Template** 就是用 Jinja 语法写的一段脚本,定义了如何将 `messages` 数组(包含 role 和 content 的对话)转换成模型能理解的单一字符串。
**一个简化的 Chat Template 示例:**
```jinja
{% for message in messages %}
{% if message['role'] == 'user' %}
{{ '[INST] ' + message['content'] + ' [/INST]' }}
{% elif message['role'] == 'assistant' %}
{{ ' ' + message['content'] + ' </s>' }}
{% endif %}
{% endfor %}
```
- **输入(Messages**: `[{"role": "user", "content": "你好"}, {"role": "assistant", "content": "你好!有什么可以帮你的?"}]`
- **输出(Prompt String**: `[INST] 你好 [/INST] 你好!有什么可以帮你的? </s>`
AI 框架如 Hugging Face Transformers 已经内置了对 Jinja Chat Template 的支持。你只需调用 `tokenizer.apply_chat_template(messages)`,它就会自动使用模型对应的 Jinja 模板来生成正确的输入,极大地简化了开发流程。
#### 3. 在 AI 工作流和数据转换中作为"胶水"
在复杂的 AI 应用(如 RAG 检索增强生成、Agent)和工作流平台(如阿里云的 AI Studio、Azure 的 Prompt Flow)中,Jinja 常被用作轻量级的**数据处理和格式化工具**。
例如,可以将检索到的多个相关文档片段,通过一个 Jinja 模板快速格式化为一个结构清晰的 Markdown 列表,再输入给大模型。
```
{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='') %}{%- for message in messages %}{%- if message['role'] == 'system' %}{% set ns.system_prompt = message['content'] %}{%- endif %}{%- endfor %}{{bos_token}}{{ns.system_prompt}}{%- for message in messages %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{{'<User>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is none %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls']%}{%- if not ns.is_first %}{{'<Assistant><tool▁calls▁begin><tool▁call▁begin>' + tool['type'] + '<tool▁sep>' + tool['function']['name'] + '\\n' + '```json' + '\\n' + tool['function']['arguments'] + '\\n' + '```' + '<tool▁call▁end>'}}{%- set ns.is_first = true -%}{%- else %}{{'\\n' + '<tool▁call▁begin>' + tool['type'] + '<tool▁sep>' + tool['function']['name'] + '\\n' + '```json' + '\\n' + tool['function']['arguments'] + '\\n' + '```' + '<tool▁call▁end>'}}{{'<tool▁calls▁end><end▁of▁sentence>'}}{%- endif %}{%- endfor %}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is not none %}{%- if ns.is_tool %}{{'<tool▁outputs▁end>' + message['content'] + '<end▁of▁sentence>'}}{%- set ns.is_tool = false -%}{%- else %}{% set content = message['content'] %}{% if '</think>' in content %}{% set content = message['content'].replace('</think>', '').split('<think>')[-1] %}{% endif %}{{'<Assistant>' + content + '<end▁of▁sentence>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_tool = true -%}{%- if ns.is_output_first %}{{'<tool▁outputs▁begin><tool▁output▁begin>' + message['content'] + '<tool▁output▁end>'}}{%- set ns.is_output_first = false %}{%- else %}{{'\\n<tool▁output▁begin>' + message['content'] + '<tool▁output▁end>'}}{%- endif %}{%- endif %}{%- endfor -%}{% if ns.is_tool %}{{'<tool▁outputs▁end>'}}{% endif %}{% if add_generation_prompt and not ns.is_tool %}{{'<Assistant>'}}{% endif %}
```
+77
View File
@@ -0,0 +1,77 @@
to AI
```json
{
"messages": [
{
"content": "有哪些工具可以使用",
"role": "user"
}
],
"model": "gpt-4.1-mini",
"stream": false,
"temperature": 0.7,
"tools": [
{
"type": "function",
"function": {
"description": "获取公司雇员信息",
"name": "JavaSDKMCPClient_getCompanyEmployee",
"parameters": {
"additionalProperties": false,
"type": "object",
"properties": {
"xxxRequest01": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称,如果是中文汉字请先转换为汉语拼音,例如北京:beijing"
},
"company": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "公司名称"
},
"type": {
"type": "string",
"description": "公司类型"
}
},
"required": [
"name",
"type"
],
"description": "公司信息,如果是中文汉字请先转换为汉语拼音,例如北京:jd/alibaba"
}
},
"required": [
"city",
"company"
]
},
"xxxRequest02": {
"type": "object",
"properties": {
"employeeCount": {
"type": "string",
"description": "雇员姓名"
}
},
"required": [
"employeeCount"
]
}
},
"required": [
"xxxRequest01",
"xxxRequest02"
]
}
}
}
]
}
```