# CodeLabDesign3 **Repository Path**: lijing_ric/code-lab-design3 ## Basic Information - **Project Name**: CodeLabDesign3 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-06-22 - **Last Updated**: 2025-06-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## **实验四 智能模型驱动** ### 1 任务一 基于纯Restful API的智能化需求建模 使用postman调用 OpenAI 的 API 生成需求模型 request body如下: ![image-20250626063803210](readme.assets/image-20250626063803210.png) 返回结果: ![image-20250626063854047](readme.assets/image-20250626063854047.png) ### 2 任务二 基于OpenAI SDK的智能化需求建模 代码如下: ```python import json from openai import OpenAI from openai.types.chat import ChatCompletion BASE_URL = "https://api.chatfire.cn/v1" API_KEY = "**********" def generate_requirements(): client: OpenAI = OpenAI(base_url=BASE_URL, api_key=API_KEY) completion: ChatCompletion = client.chat.completions.create( model="gpt-4o", response_format={"type": "json_object"}, messages=[ { "role": "system", "content": ( "你是一个需求分析师,请为图书管理系统生成详细需求模型,输出json格式:" "1. system_name: 系统名称(使用PascalCase)" "2. actors: 角色列表" "3. use_cases: 用例列表(每个用例包含name和description)" "4. classes: 核心类列表(每个类包含name, attributes, methods)" ) }, { "role": "user", "content": ( "开发一个图书馆管理系统,主要功能包括:\n" "1. 图书管理员可以添加、删除、更新图书信息\n" "2. 读者可以搜索图书、借阅图书、归还图书\n" "3. 图书借阅状态管理(借出、在库、预约)\n" "4. 借阅历史记录\n" "5. 逾期罚款计算\n" "6. 图书分类管理" ) } ], ) print(completion.choices[0].message) result = completion.choices[0].message.content return json.loads(result) if __name__ == "__main__": requirements = generate_requirements() print(json.dumps(requirements, indent=2, ensure_ascii=False)) with open("task2_library_requirements.json", "w", encoding="utf-8") as f: json.dump(requirements, f, indent=2, ensure_ascii=False) ``` 结果如下: ```json { "system_name": "LibraryManagementSystem", "actors": [ "Librarian", "Reader" ], "use_cases": [ { "name": "AddBook", "description": "Allows librarians to add new book entries to the system." }, { "name": "DeleteBook", "description": "Allows librarians to remove book entries from the system." }, { "name": "UpdateBookInformation", "description": "Allows librarians to update existing book information in the system." }, { "name": "SearchBook", "description": "Allows readers to search for books using various criteria." }, { "name": "BorrowBook", "description": "Allows readers to borrow available books from the library." }, { "name": "ReturnBook", "description": "Allows readers to return borrowed books to the library." }, { "name": "ManageBookStatus", "description": "Tracks and updates the status of books (borrowed, available, reserved)." }, { "name": "ViewBorrowHistory", "description": "Allows readers and librarians to view borrowing history." }, { "name": "CalculateOverdueFines", "description": "Calculates fines for overdue books." }, { "name": "ManageBookCategories", "description": "Allows librarians to organize and manage book categories." } ], "classes": [ { "name": "Book", "attributes": [ "bookID", "title", "author", "publisher", "ISBN", "category", "status" ], "methods": [ "getDetails", "updateInformation" ] }, { "name": "User", "attributes": [ "userID", "name", "email", "role" ], "methods": [ "login", "logout", "viewBorrowHistory" ] }, { "name": "Librarian", "attributes": [ "librarianID", "name", "email" ], "methods": [ "addBook", "deleteBook", "updateBookInformation", "manageBookCategories", "calculateOverdueFines" ] }, { "name": "Reader", "attributes": [ "readerID", "name", "email", "borrowedBooks" ], "methods": [ "searchBook", "borrowBook", "returnBook" ] }, { "name": "Loan", "attributes": [ "loanID", "bookID", "userID", "borrowDate", "returnDate", "overdueFines" ], "methods": [ "calculateFines", "manageBookStatus" ] } ] } ``` ### 3 任务三 基于LLM Agent的智能化需求建模 #### 3.1 MultiAgent Workflow设计 ```mermaid graph TD A[需求分析Agent] -->|分析报告| B[用例生成Agent] A -->|分析报告| C[类设计Agent] B -->|用例图| D[整合模型] C -->|类图| D D[最终需求模型] ``` ##### 工作流步骤: 1. **需求分析阶段**:需求分析Agent处理原始需求描述 2. **模型生成阶段**:用例生成Agent和类设计Agent并行工作 3. **模型整合阶段**:整合所有组件形成完整需求模型 4. **输出阶段**:生成JSON格式的最终模型 #### 3.2 Agent定义与职责 ##### 3.2.1 需求分析Agent ```python class Agent: def __init__(self, name: str, instructions: str, model: str = "gpt-4o"): self.name = name self.instructions = instructions self.model = model # 需求分析Agent实例 requirement_analyzer = Agent( name="Requirement_Analyzer", instructions=( "你是一个需求分析师,分析图书管理系统需求。" "输出json格式: {\"system_name\": \"名称\", \"actors\": [\"角色\"], \"features\": [\"功能\"]}" ) ) ``` **职责**: - 解析用户输入的需求描述 - 识别系统核心组件:系统名称、主要角色、关键功能 - 输出结构化分析报告(JSON格式) ##### 3.2.2 用例生成Agent ```python usecase_generator = Agent( name="UseCase_Generator", instructions=( "你是一个UML专家,基于需求分析生成用例图。" "输出json格式: {\"usecases\": [{\"name\": \"用例名\", \"actors\": [\"角色\"]}]}" "用例名使用PascalCase" ) ) ``` **职责**: - 基于需求分析报告生成用例图 - 识别系统功能用例及参与者关系 - 确保用例命名符合PascalCase规范 - 输出JSON格式的用例图 ##### 3.2.3 类设计Agent ```python class_designer = Agent( name="Class_Designer", instructions=( "你是一个系统架构师,设计图书管理系统的类图。" "输出json格式: {\"classes\": [{\"name\": \"类名\", \"attributes\": [\"属性\"], \"methods\": [\"方法\"]}]}" ) ) ``` **职责**: - 基于需求分析设计系统类图 - 识别核心类及其属性、方法 - 确保类设计符合面向对象原则 - 输出JSON格式的类图 最终生成的json如下所示: ```json { "system_name": "图书管理系统", "requirement_analysis": { "system_name": "图书管理系统", "actors": [ "管理员", "图书馆员", "用户" ], "features": [ "图书管理", "借阅管理", "用户管理" ] }, "usecase_diagram": { "usecases": [ { "name": "ManageBooks", "actors": [ "管理员", "图书馆员" ] }, { "name": "BorrowManagement", "actors": [ "用户", "图书馆员" ] }, { "name": "ManageUsers", "actors": [ "管理员", "用户" ] } ] }, "class_diagram": { "classes": [ { "name": "Book", "attributes": [ "bookId: String", "title: String", "author: String", "publisher: String", "year: Integer", "isbn: String", "copiesAvailable: Integer" ], "methods": [ "addBook()", "removeBook()", "updateBookDetails()", "getBookDetails()" ] }, { "name": "User", "attributes": [ "userId: String", "name: String", "email: String", "phoneNumber: String", "membershipType: String" ], "methods": [ "registerUser()", "removeUser()", "updateUserDetails()", "getUserDetails()" ] }, { "name": "Librarian", "attributes": [ "librarianId: String", "name: String", "staffNumber: String", "email: String" ], "methods": [ "addBook()", "removeBook()", "updateBookDetails()", "manageUserAccounts()" ] }, { "name": "Loan", "attributes": [ "loanId: String", "userId: String", "bookId: String", "loanDate: Date", "dueDate: Date", "returnDate: Date" ], "methods": [ "createLoan()", "closeLoan()", "extendLoan()", "getLoanDetails()" ] }, { "name": "Library", "attributes": [ "libraryId: String", "name: String", "address: String" ], "methods": [ "manageBooks()", "manageLoans()", "manageUsers()", "viewLibraryDetails()" ] } ] } } ```