【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')
相关推荐
databook3 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室3 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
飞哥数智坊5 小时前
GPT-5-Codex 发布,Codex 正在取代 Claude
人工智能·ai编程
倔强青铜三5 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
虫无涯6 小时前
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
人工智能
Dm_dotnet8 小时前
公益站Agent Router注册送200刀额度竟然是真的
人工智能
算家计算8 小时前
7B参数拿下30个世界第一!Hunyuan-MT-7B本地部署教程:腾讯混元开源业界首个翻译集成模型
人工智能·开源
用户2519162427118 小时前
Python之语言特点
python
机器之心8 小时前
LLM开源2.0大洗牌:60个出局,39个上桌,AI Coding疯魔,TensorFlow已死
人工智能·openai
刘立军9 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql