【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')
相关推荐
长袖格子衫几秒前
第五节:对象与原型链:JavaScript 的“类”与“继承”
开发语言·javascript·原型模式
咖啡の猫2 分钟前
JavaScript基础-全局作用域
开发语言·javascript
Oliverro7 分钟前
嵌入式音视频通话EasyRTC基于WebRTC技术驱动智能带屏音箱:开启智能交互新体验
人工智能·音视频
xyzcto12 分钟前
使用python脚本连接SQL Server数据库导出表结构
数据库·python·sqlserver
梦醒沉醉41 分钟前
Python教程(四)——数据结构
python
Adolf_199344 分钟前
django的权限角色管理(RBAC)
数据库·python·django
終不似少年遊*1 小时前
MindSpore框架学习项目-ResNet药物分类-模型优化
人工智能·深度学习·机器学习·计算机视觉·分类·数据挖掘·华为云
weixin_307779131 小时前
使用FastAPI和Apache Flink构建跨环境数据管道
redis·python·云计算·fastapi·aws
GpuGeek1 小时前
一文走进GpuGeek | conda常用命令
人工智能·conda·gpu算力·云平台
Code_流苏1 小时前
《Python星球日记》 第55天:迁移学习与预训练模型
python·深度学习·微调·resnet·迁移学习·预训练模型·超参数优化