返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

使用DeepSeek Function Calling构建智能测试助手

[复制链接]
链载Ai 显示全部楼层 发表于 6 小时前 |阅读模式 打印 上一主题 下一主题

引言:当AI遇见测试自动化

随着大语言模型(LLM)技术的突破性进展,特别是其中函数调用(Function Calling)能力的出现,软件测试这一传统而关键的软件开发环节,正逐步迎来AI化的变革与挑战。本文将探讨如何有效利用大语言模型的函数调用能力,构建智能化、高效能的测试助手。

function_calling技术解析

什么是function_calling

function_calliing是大语言模型根据用户请求智能选择并调用预定义函数的能力。模型通过理解自然语言指令,自动匹配对应的函数模板并生成结构化参数。

# 传统测试参数定义示例
test_case = {
"api":"/user/login",
"method":"OST",
"params": {"username":"test","password":"123456"}
}

# 函数调用参数定义示例
tools = [
{
"type":"function",
"function": {
"name":"execute_api_job",
"description":"执行接口测试任务",
"parameters": {
"type":"object",
"properties": {
"location": {
"type":"string",
"description":"任务名称,例如:用户中心测试任务",
}
},
"required": ["job"]
},
}
}
]

技术优势对比

传统方式
函数调用方式
人工维护测试用例
自动生成测试逻辑
固定参数组合
动态参数生成
单一断言策略
智能断言机制
线性执行流程
自适应测试路径

实现方式

定义函数

我们需要将我们要通过大模型调用的函数定义好:

tools = [
{
"type":"function",
"function": {
"name":"execute_api_job",
"description":"执行接口测试任务",
"parameters": {
"type":"object",
"properties": {
"location": {
"type":"string",
"description":"任务名称,例如:用户中心测试任务",
}
},
"required": ["job"]
},
}
},
{
"type":"function",
"function": {
"name":"get_report",
"description":"查询测试报告",
"parameters": {
"type":"object",
"properties": {
"location": {
"type":"string",
"description":"任务名称:例如:消息中心测试任务",
}
},
"required": ["job"]
},
}
},
{
"type":"function",
"function": {
"name":"analysis_report",
"description":"分析测试报告数据",
"parameters": {
"type":"object",
"properties": {
"location": {
"type":"string",
"description":"任务名称,例如:订单中心任务",
}
},
"required": ["job"]
},
}
}
]

实现函数

具体实现内容,根据自己项目来定义,这里提供简单的demo

defexecute_api_job(job):
"""模拟执行测试任务"""
returnf"已经开始执行测试任务:{job}"


defget_report(job):
"""模拟获取测试报告"""
returnf"查看{job}的测试报告,请点击https://www.baidu.com"


defanalysis_report(job):
"""模拟分析测试数据"""
# 获取报告数据
report = get_data(job)
# 将测试数据发送给AI大模型进行分析
response = send_messages(f"分析测试任务数据:{report}")
returnf'测试结果分析:\n{response}'

调用大模型

importjson
fromopenaiimportOpenAI


defsend_messages(messages):
msg = [{"role":"user","content": messages}]
response = client.chat.completions.create(
model="deepseek-chat",
messages=msg,
tools=tools
)
mess = response.choices[0].message
ifmess.tool_calls:
tool = mess.tool_calls[0]
function_name = tool.function.name
arguments = json.loads(tool.function.arguments)

# 调用函数
iffunction_name =='execute_api_job':
function_response = execute_api_job(arguments.get("location"))
eliffunction_name =='get_report':
function_response = get_report(arguments.get("location"))
eliffunction_name =='analysis_report':
function_response = analysis_report(arguments.get('location'))
else:
returnresponse.choices[0].message
returnfunction_response
else:
returnresponse.choices[0].message


client = OpenAI(
api_key="your_api_key",
base_url="https://api.deepseek.com",
)

执行

# 测试示例
if__name__ =="__main__":

whileTrue:
user_input = input("用户输入:")
ifuser_input.lower() =='exit':
break
response = send_messages(user_input)
print("助手回复:", response)
print("\n"+"="*50+"\n")

执行结果展示:


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ