【Python实现】AI Agent赋能学生成绩管理系统

发布于:2025-03-20 ⋅ 阅读:(22) ⋅ 点赞:(0)


AI Agent智能体,实现了用户靠纯自然话语指令给智能体输入,智能体会自动完成对学生成绩管理系统的所有操作。

1:项目描述

此项目在传统学生成绩管理系统的基础上升级,实现AI Agent智能体在学生成绩管理系统的应用。通过自然语言 代替键盘输入、鼠标点击 智能完成增删查改的操作。

2:主要功能

1、增加学生信息
2、删除学生信息
3、修改学生信息
4、显示学生信息

3:项目演示

ai agent学生成绩管理系统

4:具体步骤

4.1:注册chatglm4

1、进入网址https://open.bigmodel.cn/
2、右上角点击控制台
3、选择glm4-flash,点击接口文档
4、查看api-key,创建api-key。(这个key一会儿到项目中调用模型的时候会用到)
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
5、在开发文档中,右侧会有函数调用的示例,点击查看。(项目中通过这部分功能对学生成绩管理系统进行赋能实现)
在这里插入图片描述

4.2 环境安装与代码编写

4.2.1安装第三方库:

pip install zhipuai

4.2.2代码编写

在这里插入图片描述

共2个py文件:
main.py:大模型与函数调用
stu.py:学生成绩管理系统的增删查改函

4.2.3运行main.py即可

main.py
from zhipuai import ZhipuAI
from stu import *
client = ZhipuAI(api_key="6d3fb3106b20422fac262a7d0e055c7f.k2OvWGIOh9g8Avci") # 请填写您自己的APIKey

tools = [
    {
        "type": "function",
        "function": {
            "name": "add",
            "description": "根据用户提供的信息增加学生",
            "parameters": {
                "type": "object",
                "properties": {
                    "sid": {
                        "type": "string",
                        "description": "学生的学号",
                    },
                    "name": {
                        "type": "string",
                        "description": "学生的姓名",
                    },
                    "score": {
                        "type": "int",
                        "description": "学生的分数",
                    },
                },
                "required": ["sid", "name", "socre"],
            },
        }
    },
{
        "type": "function",
        "function": {
            "name": "show",
            "description": "展示所有的学生信息",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": [],
            },
        }
    },
{
        "type": "function",
        "function": {
            "name": "query_name",
            "description": "需要删除学生且只提供了学生姓名时,根据用户提供的姓名查询学生学号sid",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "学生的姓名",
                    }
                },
                "required": ["name"],
            },
        }
    },
{
        "type": "function",
        "function": {
            "name": "delete",
            "description": "根据学生的学号对学生信息进行删除",
            "parameters": {
                "type": "object",
                "properties": {
                    "sid": {
                        "type": "string",
                        "description": "学生的学号",
                    }
                },
                "required": ["sid"],
            },
        }
    },
    {
        "type": "function",
        "function": {
            "name": "update",
            "description": "当需要修改学生信息,根据学生学号对学生信息进行更新",
            "parameters": {
                "type": "object",
                "properties": {
                    "sid": {
                        "type": "string",
                        "description": "学生的学号",
                    },
                    "name": {
                        "type": "string",
                        "description": "学生的姓名",
                    },
                    "score": {
                        "type": "int",
                        "description": "学生的分数",
                    },
                },
                "required": ["sid", "name", "socre"],
            },
        }
    },
]
# messages = [
#     {
#         "role": "user",
#         "content": "你能帮我查一下2024年1月1日从北京南站到上海的火车票吗?"
#     }
# ]
# response = client.chat.completions.create(
#     model="glm-4-flash", # 请填写您要调用的模型名称
#     messages=messages,
#     tools=tools,
#     tool_choice="auto",
# )
# print(response.choices[0].message)

# messages = [
#     {
#         "role": "user",
#         "content": "你能帮我查一下2024年1月1日从北京南站到上海的火车票吗?"
#     }
# ]
# response = client.chat.completions.create(
#     model="glm-4-flash", # 请填写您要调用的模型名称
#     messages=messages,
#     tools=tools,
#     tool_choice="auto",
# )
# print(response.choices[0].message)
import json
while 1:
    msg=[{
        "role": "user",
        "content": input('请输入:')}]
    response = client.chat.completions.create(
        model="glm-4-flash",  # 请填写您要调用的模型名称
        messages=msg,
        tools=tools,
        tool_choice="auto",
    )
    print(response.choices[0].message)
    if response.choices[0].message.tool_calls:
        '调用函数,反之输出内容'
        function_name = response.choices[0].message.tool_calls[0].function.name
        print(function_name)
        arguments = json.loads(response.choices[0].message.tool_calls[0].function.arguments)
        function_to_call = globals()[function_name]
        result = function_to_call(**arguments)
        print(result)

stu.py

student = {}


def add(sid: str, name: str, score: int):
    student[sid] = {"name": name, "score": score}
    return dict(code=200,msg='add success')

def delete(sid:str):
    if student[sid]:
        student.pop(sid)
        return dict(code=200, msg='delete success')
    return dict(code=500, data={'sid': sid}, msg='not found')

def query_sid(sid:str):
    if student[sid]:
        return dict(code=200, data={'sid':sid},msg='have found')
    return

def query_name(name: str):
    dic = {}
    for key in list(student.keys()):
        if student[key]['name'] == name:
            dic[key] = name
    if dic:
        if len(list(dic.keys())) >=2 :
            return dict(code=200, data=dic, msg='have found but not only one,please choose one')
        return dict(code=200, data=dic, msg='query success')
    return dict(code=200, msg='not found')

def show():
    lis = [f"sid:{key},name:{value['name']},score:{value['score']}" for key,value in student.items()]
    return dict(code=200, data=lis, msg='show success')

# 更新
def update(sid: str, name: str, score: int):
    if student[sid]:
        student[sid]['name'] = name
        student[sid]['score'] = score
        return dict(code=200, msg='update success')
    return dict(code=500, data={'sid': sid}, msg='not found')