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

AI Agent赋能学生成绩管理系统

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')
相关推荐
四口鲸鱼爱吃盐2 分钟前
CVPR2025 | TAPT:用于视觉语言模型鲁棒推理的测试时对抗提示调整
网络·人工智能·深度学习·机器学习·语言模型·自然语言处理·对抗样本
傻啦嘿哟9 分钟前
python中time模块的常用方法及应用
开发语言·数据库·python
沈二到不行15 分钟前
多头注意力&位置编码:完型填空任务
人工智能·后端·deepseek
朱剑君31 分钟前
机器学习概要
人工智能·机器学习
Chen--Xing41 分钟前
洛谷 P3986 斐波那契数列
python·算法
laimaxgg43 分钟前
Qt窗口控件之字体对话框QFontDialog
开发语言·c++·qt·qt5·qt6.3
晚风_END44 分钟前
kubernetes|云原生|部署单master的kubernetes 1.25.5版本集群完全记录(使用contained 运行时)
java·运维·开发语言·云原生·容器·golang·kubernetes
Run_Teenage1 小时前
C语言每日一练——day_12(最后一天)
c语言·开发语言
Antonio9151 小时前
【Q&A】建造者模式在Qt有哪些应用
开发语言·qt·建造者模式
妮妮学代码1 小时前
C#:深入理解Thread.Sleep与Task.Delay
开发语言·c#