├── README.md └── function_call.py /README.md: -------------------------------------------------------------------------------- 1 | Function的调用时Agent实现很重要的一步,只有了解了这个原理才可以更好的创建Agent。 2 | 3 | 此代码从零构建了Agent中最重要的function call。 4 | 5 | # 场景 6 | 7 | Agent的目标:可以回答用户关于天气的问题。 8 | 9 | 流程: 10 | 11 | 1. 思考: 用户输入问题,先进行分析 12 | 13 | 2. 行动: 如果问到了天气问题,则分析出需要调用的function以及function要传入的参数 14 | 15 | 3. 响应:function返回后,将答案整理好回复给用户。 16 | 17 | # 流程如下 18 | 19 | ![image](https://github.com/user-attachments/assets/5a8c510c-fd35-46c2-956c-95ad1fc4f98e) 20 | 21 | # 运行逻辑 22 | 23 | 1. 提问让模型进行思考 24 | ``` 25 | question="北京天气怎么样" 26 | messages = [{"role": "system", "content": system_prompt}, 27 | {"role": "user", "content": question}] 28 | message = send_messages(messages) 29 | print(f"Model-1th>\n {message.content}") 30 | ``` 31 | 32 | 2. 如果找到了json则说明有工具可以调用 33 | ``` 34 | 抽取文本返回中的json 35 | """ 36 | { 37 | "function_name":"get_weather", 38 | "function_params":{ 39 | "location":"北京" 40 | } 41 | } 42 | """ 43 | ``` 44 | 45 | 3. 调用工具 46 | ``` 47 | tianqi = get_weather("北京") 48 | ``` 49 | 50 | 4.将调用工具的结果返回给模型进行回答 51 | ``` 52 | messages.append({"role": "assistant", "content": f"调用Action的结果:{tianqi}"}) 53 | message = send_messages(messages) 54 | print(f"Model-second>\n {message.content}") 55 | ``` 56 | 57 | # 运行结果 58 | ``` 59 | Model-1th> 60 | thought:我应该调用工具查询北京的天气情况 61 | Action: 62 | { 63 | "function_name":"get_weather", 64 | "function_params":{ 65 | "location":"北京" 66 | } 67 | } 68 | 69 | Model-second> 北京今天的天气晴朗。 70 | ``` 71 | -------------------------------------------------------------------------------- /function_call.py: -------------------------------------------------------------------------------- 1 | from openai import OpenAI 2 | 3 | def send_messages(messages): 4 | response = client.chat.completions.create( 5 | model="deepseek-chat", 6 | messages=messages 7 | ) 8 | return response.choices[0].message 9 | 10 | client = OpenAI( 11 | api_key="xxx", 12 | base_url="https://api.deepseek.com", 13 | ) 14 | 15 | def get_weather(location): 16 | return "天气晴朗" 17 | 18 | 19 | system_prompt=""" 20 | 你在运行一个“思考”,“工具调用”,“响应”循环。每次只运行一个阶段 21 | 22 | 1.“思考”阶段:你要仔细思考用户的问题 23 | 2.“工具调用阶段”:选择可以调用的工具,并且输出对应工具需要的参数 24 | 3.“响应”阶段:根据工具调用返回的结果,回复用户问题。 25 | 26 | 已有的工具如下: 27 | get_weather: 28 | e.g. get_weather:天津 29 | 返回天津的天气情况 30 | 31 | Example: 32 | question:天津的天气怎么样? 33 | thought:我应该调用工具查询天津的天气情况 34 | Action: 35 | { 36 | "function_name":"get_response_time" 37 | "function_params":{ 38 | "location":"天津" 39 | } 40 | } 41 | 调用Action的结果:“天气晴朗” 42 | Answer:天津的天气晴朗 43 | """ 44 | 45 | #1.提问让模型进行思考 46 | question="北京天气怎么样" 47 | messages = [{"role": "system", "content": system_prompt}, 48 | {"role": "user", "content": question}] 49 | message = send_messages(messages) 50 | print(f"Model-1th>\n {message.content}") 51 | 52 | # 2.如果找到了json则说明有工具可以调用 53 | # 抽取文本返回中的json 54 | """ 55 | { 56 | "function_name":"get_weather", 57 | "function_params":{ 58 | "location":"北京" 59 | } 60 | } 61 | """ 62 | # 3.调用工具 63 | tianqi = get_weather("北京") 64 | 65 | # 4.将调用工具的结果返回给模型进行回答 66 | messages.append({"role": "assistant", "content": f"调用Action的结果:{tianqi}"}) 67 | message = send_messages(messages) 68 | print(f"Model-second>\2 {message.content}") 69 | 70 | 71 | --------------------------------------------------------------------------------