拓冰建站拓冰建站
首页 / 资讯中心 / 正文

FastAPi Function calling 和提示词的工程计算

services进行提示词staticmethod async def resume_submission_detail(id: int): resume_submission_records await ( ResumeSubmissionRecords.get_or_none(idid) ) # 获取职位信息 job await Job.get_or_none(idresume_submission_records.job_id) # 获取求职者的信息 job_seeker await JobSeeker.get_or_none(idresume_submission_records.job_seeker_id) resume_basic_info await ResumeBasicInfo.get_or_none(job_seeker_idjob_seeker.id) jobintention await JobIntention.get_or_none(job_seeker_idjob_seeker.id) work_experiences await WorkExperience.filter(job_seeker_idjob_seeker.id) work_experience_list [] for work_experience in work_experiences: work_experience_data { 公司名称:work_experience.company_name, 所属行业: work_experience.industry, 职位名称: work_experience.position, 入职时间: work_experience.entry_time, 离职时间: work_experience.leave_time, 工作内容: work_experience.work_content, 工作业绩: work_experience.performance, 拥有技能列表: work_experience.skills, } work_experience_list.append(work_experience_data) project_experiences await ProjectExperience.filter(job_seeker_idjob_seeker.id) project_experience_list [] for project_experience in project_experiences: project_experience_data { 项目名称: project_experience.project_name, 项目角色: project_experience.project_role, 项目开始时间: project_experience.start_time, 项目结束时间: project_experience.end_time, 项目描述: project_experience.project_desc, 项目业绩: project_experience.project_performance, } project_experience_list.append(project_experience_data) education_experiences await EducationExperience.filter(job_seeker_idjob_seeker.id) education_experience_list [] for education_experience in education_experiences: education_experience_data { 学历名称: education_experience.education_name, 学制类型: education_experience.study_type, 学校名称: education_experience.school_name, 专业名称: education_experience.major_name, 入学时间: education_experience.entry_date, 毕业时间: education_experience.graduate_date, 在校经历: education_experience.school_experience, } education_experience_list.append(education_experience_data) # 专业技能, 优势,语言 prompt f ## 角色设定: 你是一个经验丰富的人力资源专家 ## 任务描述: 根据求职者简历内容和岗位的职位描述,分析岗位匹配度和理由 ## 输入数据: 1:岗位的职位描述: 1.1:职位名称: {job.job_name} 1.2:工作地点: {job.work_location} 1.3:最低薪资:{job.min_salary} 1.4:最高薪资:{job.max_salary} 1.5:经验要求:{job.exp_require} 1.6:学历要求:{job.edu_require} 1.7:性别要求:{job.gender_require} 1.8:职位描述:{job.job_desc} 1.9:任职要求:{job.duty_require} 2:求职者简历内容: 2.1:求职者性别(1-男2-女):{resume_basic_info.gender} 2.2:求职者出生日期:{resume_basic_info.birth_date} 2.3:求职者政治面貌(1-群众 2-党员 3-其他):{resume_basic_info.political_status} 2.4:求职者婚姻状况(1-未婚 2-已婚 3-保密):{resume_basic_info.marital_status} 2.5:求职者期望职位列表:{,.join([item[name] for item in jobintention.expect_position])} 2.6:求职者期望薪资:{jobintention.expect_salary} 2.7:求职者工作性质(1-全职 2-兼职 3-其他):{jobintention.work_type} 2.8:求职者期望城市列表:{jobintention.expect_city} 2.9:求职者期望行业列表:{jobintention.expect_industry} 2.10:求职者工作经历:{work_experience_list} 2.11:求职者项目经历:{project_experience_list} 2.12:求职者教育经历:{education_experience_list} ## 输出格式 以JSON格式输出,包含以下字段: job_matching_degree:岗位匹配度(0-100)百分比: matching_reason: 重要禁止输出任何思考、推理、标签直接输出JSON结果不要输出其他文字。 ## 输出示例: {{job_matching_degree: 80%, matching_reason: 求职者期望职位列表与岗位职位描述匹配度高:项目经历匹配}} client OpenAI( # 若没有配置环境变量请用百炼API Key将下行替换为api_keysk-xxx api_keyos.getenv(DASHSCOPE_API_KEY), base_urlhttp://localhost:11434/v1, ) completions client.chat.completions.create( modelqwen3:8b, messages[{role: user, content: prompt}] ) logger.debug(fcompletions:{completions}) res completions.choices[0].message.content return res对大模型进行提示词验算不断优化在app的llm进行Function calling天气入门案例import json import random from openai import OpenAI def get_current_weather(arguments): weather_conditions [晴天,多云,雨天] random_weather random.choice(weather_conditions) location arguments[location] return f{location}今天是{random_weather}。 tools [ { type: function, function: { name: get_current_weather, description: 当你想查询指定城市天气时非常有用。, parameters: { type: object, properties: { location: { type: string, description: 城市或县区比如北京市杭州市余杭区等。, } }, required: [location], }, }, } ] client OpenAI( # 建议通过环境变量配置API Key避免硬编码。 api_keysk-ws-H.ELRIXEP.1FU3.MEQCIEDGBtjTnhBB7RAVMJNlUeCSOzLtXKpPU1NavE_kx0kQAiARdmFvK_yHxTdBFndpyXzzjSZvNzXT15669ajCMdg-OQ, # API Key与地域强绑定请确保base_url与API Key的地域一致。 base_urlhttps://dashscope.aliyuncs.com/compatible-mode/v1, ) messages [] def get_ai_response(messages): completion client.chat.completions.create( # 模型列表https://help.aliyun.com/zh/model-studio/getting-started/models modelqwen-plus, messagesmessages, temperature0.75, toolstools ) return completion user_message {role: user,content: 北京今天的天气} messages.append(user_message) completion get_ai_response(messages) print(completion.model_dump_json()) messages.append(completion.choices[0].message) #判断第一次大模型返回的结果是否需要调用工具 if completion.choices[0].message.tool_calls is None: print(不需要调用工具) print(completion.choices[0].message.content) else: print(需要调用工具) tool_calls completion.choices[0].message.tool_calls for tool_call in tool_calls: tool_id tool_call.id func_name tool_call.function.name func_arguments tool_call.function.arguments print(f大模型告诉程序要调用这个工具{func_name}参数是{func_arguments}) function_mapping { get_current_weather:get_current_weather } print(type(func_arguments)) tool_result function_mapping[func_name](json.loads(func_arguments)) print(f工具返回的结果是{tool_result}) tool_message { content: tool_result, role:tool, tool_call_id: tool_id } messages.append(tool_message) completion get_ai_response(messages) print(completion.model_dump_json()) print(f最终的结果是{completion.choices[0].message.content})学历验证案例读取# 初始化客户端 import json import os import random import redis import requests from openai import OpenAI redis_client redis.Redis(host127.0.0.1, port6379, db0, decode_responsesTrue, protocol2 ) # 模拟天气查询工具{location:北京} def get_current_weather(arguments): weather_conditions [晴天, 多云, 雨天] random_weather random.choice(weather_conditions) location arguments[location] return f{location}今天是{random_weather}。 # 学历验证 def academic_credential_verification(arguments): api_key os.getenv(MY_API_KEY) if not api_key: raise RuntimeError( ❌ 未读取到环境变量 MY_API_KEY。\n 排查(1) 系统变量已设置但【运行本脚本的终端/VSCode 未重启】→ 重启后再跑\n 或 (2) 在项目根目录建 .env 写入 MY_API_KEY你的Key并 pip install python-dotenv 后加 load_dotenv()。\n Key 样例MY_KEY_2JqvjXq52SZ3KmJrZEK0vMtkFu002U ) vcode arguments[vcode] cache_key fboss:llm:academic_credential_verification:{vcode} redis_verification_data redis_client.get(cache_key) if redis_verification_data is None: BASE_URL https://www.apimy.cn/api/xxw/bgcx payload {key: os.getenv(MY_API_KEY), vcode: vcode} headers {Content-Type: application/json} response requests.post(BASE_URL, jsonpayload, headersheaders, timeout30) response.raise_for_status() data response.json() redis_client.set(cache_key, json.dumps(data, ensure_asciiFalse)) return json.dumps(data, ensure_asciiFalse) else: return redis_verification_data tools [ { type: function, function: { name: get_current_weather, description: 当你想查询指定城市的天气时非常有用。, parameters: { type: object, properties: { location: { type: string, description: 城市或县区比如北京市、杭州市、余杭区等。, } }, required: [location], }, }, }, { type: function, function: { name: academic_credential_verification, description: 当你想查询学历或者验证学历时非常有用。, parameters: { type: object, properties: { vcode: { type: string, description: 学历验证码, } }, required: [vcode], }, }, } ] # 1. 准备工作初始化客户端 client OpenAI( # 建议通过环境变量配置API Key避免硬编码。 api_keysk-ws-H.ELRIXEP.1FU3.MEQCIEDGBtjTnhBB7RAVMJNlUeCSOzLtXKpPU1NavE_kx0kQAiARdmFvK_yHxTdBFndpyXzzjSZvNzXT15669ajCMdg-OQ, # API Key与地域强绑定请确保base_url与API Key的地域一致。 base_urlhttps://dashscope.aliyuncs.com/compatible-mode/v1, ) messages [] def get_ai_response(messages): completion client.chat.completions.create( # 模型列表https://help.aliyun.com/zh/model-studio/getting-started/models modelqwen-plus, messagesmessages, temperature0.75, toolstools ) return completion user_message {role: user, content: 帮我查询一下学历, 验证码是:AVL6Y3H8092BV3U2} messages.append(user_message) completion get_ai_response(messages) print(completion.model_dump_json()) messages.append(completion.choices[0].message) # 判断第一次大模型返回的结果:是否需要调用工具 if completion.choices[0].message.tool_calls is None: print(不需要调用工具) print(completion.choices[0].message.content) else: print(需要调用工具) tool_calls completion.choices[0].message.tool_calls for tool_call in tool_calls: tool_id tool_call.id func_name tool_call.function.name func_arguments tool_call.function.arguments print(f大模型告诉程序要调用这个工具:{func_name},参数是:{func_arguments}) function_mapping { get_current_weather: get_current_weather, academic_credential_verification: academic_credential_verification } print(type(func_arguments)) tool_result function_mapping[func_name](json.loads(func_arguments)) print(f工具返回的结果是:{tool_result}) tool_message { content: tool_result, role: tool, tool_call_id: tool_id } messages.append(tool_message) completion get_ai_response(messages) print(completion.model_dump_json()) print(f最终的结果是:{completion.choices[0].message.content})并在梦远数据进行学历接口密钥验证总结项目基于阿里云百炼兼容接口封装大模型调用实现 Function Calling 工具调用能力搭建分层 LLM 业务案例。基础演示案例完成天气查询工具封装可自动识别用户意图并执行工具、回填结果再生成回答。学历核验工具对接梦远数据接口通过 Redis 缓存核验结果同时增加环境变量密钥读取与异常捕获逻辑。招聘业务服务采用 TortoiseORM 异步多表查询拉取岗位、求职者全量结构化简历数据用于 AI 分析。简历匹配模块设计标准化 HR 提示词约束大模型仅输出指定 JSON返回 0-100% 岗位匹配度与完整匹配理由。学历接口调试时暴露密钥读取、JSON 传参两大问题修正环境变量名与表单请求格式后解决 403 报错。整套代码统一封装工具映射与消息流转逻辑实现自动工具调度可复用至招聘场景多类数据核验需求。本地 Ollama 与云端通义千问双客户端兼容异步简历查询与同步工具测试脚本分离便于分场景调试迭代。后续可优化提示词权重分层、增加密钥缺失拦截、统一工具异常返回格式提升匹配结果精准度与接口稳定性。
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门