【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')
相关推荐
袁庭新5 分钟前
职场人为什么必须学AI?
人工智能·aigc
zhangfeng113317 分钟前
在 R 语言里,`$` 只有一个作用 按名字提取“列表型”对象里的单个元素 对象 $ 名字
开发语言·windows·r语言
gptplus30 分钟前
【重要通知】ChatGPT Plus将于9月16日调整全球充值定价,低价区将被弃用,开发者如何应对?
人工智能·gpt·chatgpt
亚里随笔33 分钟前
小型语言模型:智能体AI的未来?
人工智能·语言模型·自然语言处理·llm·rlhf·agentic
文弱书生65634 分钟前
5.后台运行设置和包设计与实现
服务器·开发语言·c#
独行soc34 分钟前
2025年渗透测试面试题总结-67(题目+回答)
网络·python·安全·web安全·网络安全·adb·渗透测试
mit6.82434 分钟前
[code-review] AI聊天接口 | 语言模型通信器
人工智能·语言模型·代码复审
编码浪子38 分钟前
趣味学RUST基础篇(异步补充)
开发语言·后端·rust
songroom1 小时前
Rust : 关于Deref
开发语言·后端·rust
qq_401700411 小时前
QT子线程与GUI线程安全交互
开发语言·qt