Skip to content

Commit e77c00e

Browse files
📝 Update Python documentation with new sections and improved clarity
- Enhanced the Agent section by adding details on frameworks like LangChain and AutoGen. - Updated the historical context of Python versions, emphasizing the transition to Python 3. - Improved the organization of environment management tools, clarifying their advantages and disadvantages. - Added new code examples for function calling and prompt engineering, demonstrating practical applications. - Streamlined the overall structure for better readability and user navigation.
1 parent c77ea44 commit e77c00e

File tree

3 files changed

+483
-376
lines changed

3 files changed

+483
-376
lines changed

docs/docs/机器学习/模型部署与应用/Agent开发.md

Lines changed: 146 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ Agent 的意义 :在 AI 时代,Agent 可以利用大模型处理复杂任务
1515
- 优点:简单易用。
1616
- 缺点:功能有限,收费昂贵,云端执行非自主(必须联网),不可控。不同的低代码平台有差异,不易迁移。插件大多需要独立的Token,平台可用的大模型也比较有限。
1717

18-
框架开发:LangChain
18+
框架开发:LangChain、AutoGen等
1919

2020
- 优点:功能强大,可以满足复杂需求。代码可以灵活复用。
2121
- 缺点:需要一定的技术门槛。
2222

2323

24-
### 规则时代
24+
### Agent之前:规则时代
2525
```bash showLineNumbers
2626
# 规则化的自然语言,一切基于有穷的规则(关键词识别)。
2727
小爱同学,关灯
@@ -30,6 +30,7 @@ Agent 的意义 :在 AI 时代,Agent 可以利用大模型处理复杂任务
3030

3131
例如:小爱同学,我要睡觉了。
3232
```
33+
3334
如果想要提升模型的智能化能力,只能是工程师编写更多更复杂的关键词判断逻辑才能提升智能水平,且提升幅度有限。不能理解复杂的语义。
3435

3536
### Agent 1.0
@@ -84,26 +85,149 @@ Agent 的意义 :在 AI 时代,Agent 可以利用大模型处理复杂任务
8485

8586
Langchain 的安装与使用可以参考[Langchain 官方文档](https://python.langchain.com/docs/introduction/)
8687

87-
### 模型调用
88-
89-
### 提示词
90-
91-
### 消息修剪
92-
93-
### 记忆层
94-
95-
### 工具层
96-
97-
### 社区工具
98-
99-
#### 自定义工具
100-
101-
### 图谱
102-
103-
### 创建节点
88+
## 大模型调用工具
89+
90+
大模型本身不具备执行能力,需要调用工具。其本质都是将工具封装后传入大模型上下文,然后大模型返回需要调用的工具的名称与参数。再由系统执行。
91+
92+
### 提示词工程
93+
94+
```python showLineNumbers
95+
import openai
96+
import re
97+
import datetime
98+
99+
# 设置 OpenAI API Key
100+
openai.api_key = "YOUR_API_KEY"
101+
102+
# 实际业务函数:获取天气信息
103+
def get_current_weather(location):
104+
# 模拟获取天气的逻辑
105+
return f"{location}的当前天气是晴朗。"
106+
107+
# 新增的工具:获取当前时间
108+
def get_current_time():
109+
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
110+
return f"当前时间是: {now}"
111+
112+
def prompt_engineered_function_call(user_input):
113+
"""
114+
使用提示词工程实现函数调用:
115+
1. 当用户输入中涉及“天气”关键词时,输出调用 get_current_weather 的指令,
116+
格式:CALL get_current_weather(location="<城市名称>")
117+
2. 当用户输入中涉及“时间”关键词时,输出调用 get_current_time 的指令,
118+
格式:CALL get_current_time()
119+
3. 如果用户的问题不涉及“天气”或“时间”,则直接回答。
120+
"""
121+
prompt = f"""
122+
你是一个智能助手,以下是你的工作规则:
123+
1. 当用户提问包含“天气”关键词时,请按照格式输出函数调用指令,格式如下:
124+
CALL get_current_weather(location="<城市名称>")
125+
2. 当用户提问包含“时间”关键词时,请按照格式输出函数调用指令,格式如下:
126+
CALL get_current_time()
127+
3. 如果用户的问题不涉及“天气”或“时间”,请直接给出答案,不要输出任何函数调用指令。
128+
129+
请根据以下用户输入返回结果:
130+
用户输入:{user_input}
131+
"""
132+
response = openai.ChatCompletion.create(
133+
model="gpt-3.5-turbo", # 或其他支持的模型
134+
messages=[
135+
{"role": "system", "content": "你是一个智能助手,根据规则输出相应格式。"},
136+
{"role": "user", "content": prompt}
137+
],
138+
temperature=0.2
139+
)
140+
return response.choices[0].message.content.strip()
141+
142+
if __name__ == '__main__':
143+
user_query = input("请输入查询内容: ")
144+
model_reply = prompt_engineered_function_call(user_query)
145+
print("模型回复:", model_reply)
146+
147+
# 判断模型回复是否为函数调用指令
148+
if model_reply.startswith("CALL get_current_weather"):
149+
# 解析 location 参数
150+
match = re.search(r'location="(.+?)"', model_reply)
151+
if match:
152+
location = match.group(1)
153+
result = get_current_weather(location)
154+
print("调用函数结果:", result)
155+
else:
156+
print("无法解析函数调用参数。")
157+
elif model_reply.startswith("CALL get_current_time"):
158+
result = get_current_time()
159+
print("调用函数结果:", result)
160+
else:
161+
print("直接回答:", model_reply)
162+
```
104163

105-
### 添加边
164+
### function calling
165+
166+
```python showLineNumbers
167+
import openai
168+
import json
169+
170+
# 请设置你的 OpenAI API Key
171+
openai.api_key = "YOUR_API_KEY"
172+
173+
# 定义实际业务函数:获取天气信息
174+
def get_current_weather(location):
175+
# 模拟获取天气信息的逻辑
176+
return f"{location}的当前天气是晴朗。"
177+
178+
# 定义大模型可调用的函数描述(Function Schema)
179+
functions = [
180+
{
181+
"name": "get_current_weather",
182+
"description": "获取指定城市的天气",
183+
"parameters": {
184+
"type": "object",
185+
"properties": {
186+
"location": {"type": "string", "description": "城市名称"}
187+
},
188+
"required": ["location"]
189+
}
190+
}
191+
]
192+
193+
def large_model_integration(user_input):
194+
"""
195+
模拟大模型处理用户输入,
196+
若识别到需要调用天气查询函数,则使用函数调用功能。
197+
"""
198+
# 调用大模型接口,启用函数调用
199+
response = openai.ChatCompletion.create(
200+
model="gpt-4-0613", # 模型支持函数调用
201+
messages=[{"role": "user", "content": user_input}],
202+
functions=functions,
203+
function_call="auto" # 模型自动决定是否调用函数
204+
)
205+
206+
message = response["choices"][0]["message"]
207+
208+
# 判断是否触发了函数调用
209+
if message.get("function_call"):
210+
func_name = message["function_call"]["name"]
211+
arguments = message["function_call"]["arguments"]
212+
213+
# 解析函数参数
214+
args = json.loads(arguments)
215+
216+
# 根据函数名称调用对应的函数
217+
if func_name == "get_current_weather":
218+
result = get_current_weather(**args)
219+
return f"大模型调用函数 {func_name} 得到结果: {result}"
220+
else:
221+
return "大模型触发未知函数调用。"
222+
else:
223+
# 如果大模型没有调用函数,则直接返回回答
224+
return message.get("content", "大模型未生成有效回复。")
225+
226+
if __name__ == '__main__':
227+
user_query = input("请输入查询内容: ")
228+
result = large_model_integration(user_query)
229+
print(result)
106230

107-
### 添加条件边
231+
```
108232

109-
### RAG
233+
### Mcp

0 commit comments

Comments
 (0)