File size: 3,857 Bytes
cc74eb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "# ⚠️ Please make sure you have adequate GPU memory.\n",
        "! pip install -U bitsandbytes -q # pip this repo at your first run\n",
        "from transformers import AutoModelForSeq2SeqLM, AutoTokenizer\n",
        "import torch\n",
        "from peft import PeftModel, PeftConfig\n",
        "import os\n",
        "\n",
        "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
        "device = \"cuda:0\"\n",
        "\n",
        "# Load the Modeler config.\n",
        "repo_id = \"GangJiang/LLM-BEM-Engineer\"\n",
        "sub = \"Modeler-Bldg_Geo\" # 4 related agent names: Modeler-Bldg_Geo; Modeler-Bldg_Cons_Ops; Modeler-Water_Sys; Modeler-Air-Sys\n",
        "config = PeftConfig.from_pretrained(repo_id, subfolder=sub)\n",
        "\n",
        "# Load the base LLM, flan-t5-xl (8-bit quantized), and tokenizer\n",
        "base_model = AutoModelForSeq2SeqLM.from_pretrained(\"google/flan-t5-xl\", load_in_8bit=True)\n",
        "tokenizer = AutoTokenizer.from_pretrained(\"google/flan-t5-xl\")\n",
        "\n",
        "# Load the Lora model\n",
        "modeler_agent_model = PeftModel.from_pretrained(base_model, repo_id, subfolder=sub)\n",
        "# Generation config\n",
        "generation_config = modeler_agent_model.generation_config\n",
        "generation_config.max_new_tokens = 3000\n",
        "generation_config.temperature = 0.1\n",
        "generation_config.top_p = 0.1\n",
        "generation_config.num_return_sequences = 1\n",
        "generation_config.pad_token_id = tokenizer.eos_token_id\n",
        "generation_config.eos_token_id = tokenizer.eos_token_id\n",
        "\n",
        "# This is the corresponding Modeler Agent input extract from the Manager Agent (details in the file: \"Manager_Agent_Inference.ipynb\").\n",
        "# For example, modeler agent: Modeler-Bldg_Geo\n",
        "\n",
        "modeler_agent_input=f\"\"\"\n",
        "Simulate a 1 story hollow square, courtyard building.\n",
        "The height of story 1 is 11.42 meters.\n",
        "The horizontal segments are 144.24 meters long and 19.33 meters wide.\n",
        "The vertical segments are 29.80 meters long and 390.88 meters wide.\n",
        "The attic height is 26.60 meters.\n",
        "The building orientation is 352 degrees to the north.\n",
        "Each story has 4 thermal zones in each orientation.\n",
        "The window-to-wall ratio is 0.80 for the north, 0.58 for the south, 0.17 for the west, and 0.78 for the east.\n",
        "\"\"\"\n",
        "modeler_agent_model.to(device)\n",
        "\n",
        "# Modeler Agent generating...\n",
        "input_ids = tokenizer(modeler_agent_input, return_tensors=\"pt\", truncation=False).to(device)\n",
        "generated_ids = modeler_agent_model.generate(input_ids = input_ids.input_ids,\n",
        "                           attention_mask = input_ids.attention_mask,\n",
        "                           generation_config = generation_config)\n",
        "generated_output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)\n",
        "\n",
        "# View the generated result of the fine-tuned corresponding Modeler (e.g., Modeler-Bldg_Geo)\n",
        "agent_action = generated_output\n",
        "print(agent_action)\n",
        "\n",
        "# Execute the modeling actions produced by the Modeler Agent\n",
        "# exec(agent_action)"
      ],
      "metadata": {
        "id": "q1wXbiWUNl1d"
      },
      "execution_count": 1,
      "outputs": []
    }
  ]
}