{ "cells": [ { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting tiktoken\n", " Obtaining dependency information for tiktoken from https://files.pythonhosted.org/packages/b8/eb/234646d9eefda8a500d0fd88b05bf625a90ed18054124349db26e558276e/tiktoken-0.5.1-cp311-cp311-win_amd64.whl.metadata\n", " Downloading tiktoken-0.5.1-cp311-cp311-win_amd64.whl.metadata (6.8 kB)\n", "Collecting regex>=2022.1.18 (from tiktoken)\n", " Obtaining dependency information for regex>=2022.1.18 from https://files.pythonhosted.org/packages/b8/ad/3398312096118c4e62a5827664e52a04d5068e84d04142dd4a0da8a567ae/regex-2023.10.3-cp311-cp311-win_amd64.whl.metadata\n", " Downloading regex-2023.10.3-cp311-cp311-win_amd64.whl.metadata (41 kB)\n", " ---------------------------------------- 0.0/42.0 kB ? eta -:--:--\n", " ------------------ ------------------- 20.5/42.0 kB 320.0 kB/s eta 0:00:01\n", " -------------------------------------- 42.0/42.0 kB 503.7 kB/s eta 0:00:00\n", "Requirement already satisfied: requests>=2.26.0 in c:\\users\\16178\\anaconda3\\envs\\llama\\lib\\site-packages (from tiktoken) (2.31.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\16178\\anaconda3\\envs\\llama\\lib\\site-packages (from requests>=2.26.0->tiktoken) (2.0.4)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\16178\\anaconda3\\envs\\llama\\lib\\site-packages (from requests>=2.26.0->tiktoken) (3.4)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\16178\\anaconda3\\envs\\llama\\lib\\site-packages (from requests>=2.26.0->tiktoken) (1.26.16)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\16178\\anaconda3\\envs\\llama\\lib\\site-packages (from requests>=2.26.0->tiktoken) (2023.7.22)\n", "Downloading tiktoken-0.5.1-cp311-cp311-win_amd64.whl (759 kB)\n", " ---------------------------------------- 0.0/759.8 kB ? eta -:--:--\n", " --- ------------------------------------ 61.4/759.8 kB 1.7 MB/s eta 0:00:01\n", " ----------------- ---------------------- 327.7/759.8 kB 4.1 MB/s eta 0:00:01\n", " ------------------------------------ --- 686.1/759.8 kB 5.4 MB/s eta 0:00:01\n", " ---------------------------------------- 759.8/759.8 kB 5.4 MB/s eta 0:00:00\n", "Downloading regex-2023.10.3-cp311-cp311-win_amd64.whl (269 kB)\n", " ---------------------------------------- 0.0/269.6 kB ? eta -:--:--\n", " ------------------------------------ --- 245.8/269.6 kB 7.4 MB/s eta 0:00:01\n", " ---------------------------------------- 269.6/269.6 kB 4.1 MB/s eta 0:00:00\n", "Installing collected packages: regex, tiktoken\n", "Successfully installed regex-2023.10.3 tiktoken-0.5.1\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install tiktoken chromadb" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# you need openai key\n", "import os\n", "os.environ['OPENAI_API_KEY'] = ''" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import PromptTemplate\n", "from langchain.chains import LLMChain\n", "from langchain.llms import OpenAI\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.callbacks import get_openai_callback\n", "from langchain.schema import StrOutputParser" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "category = \"\"\"\n", "[Environmental, Education, Health Care, Criminal Justice, Taxation, Transportation, \n", "Housing, Civil Rights, Labor and Employment, Budget and Appropriations, Public Safety, Technology and Innovation, \n", "Immigration, Economic Development, Social Services]\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "tagging = \"\"\"\n", "[Climate Change Mitigation,\n", "Renewable Energy Initiatives,\n", "Biodiversity Conservation,\n", "Water Pollution Control,\n", "Sustainable Agriculture Practices,\n", "Air Quality Improvement,\n", "Waste Reduction Programs,\n", "Coastal Erosion Management,\n", "Environmental Impact Assessments,\n", "Wildlife Habitat Protection,\n", "Curriculum Development,\n", "Digital Learning Resources,\n", "Teacher Professional Development,\n", "Early Childhood Education,\n", "Special Education Services,\n", "Literacy Programs,\n", "Vocational Training,\n", "Education Technology Integration,\n", "School Infrastructure Upgrades,\n", "Student Mental Health Support,\n", "Universal Health Coverage,\n", "Healthcare Access for All,\n", "Mental Health Services Expansion,\n", "Disease Prevention Programs,\n", "Elderly Care Services,\n", "Healthcare Quality Standards,\n", "Health Information Technology,\n", "Maternal and Child Health,\n", "Public Health Emergency Preparedness,\n", "Healthcare Workforce Training,\n", "Police Reforms\n", "Prisoner Rehabilitation\n", "Community Policing Initiatives\n", "Restorative Justice Programs\n", "Criminal Sentencing Reform\n", "Victim Support Services\n", "Legal Aid for the Underprivileged\n", "Juvenile Justice System Overhaul\n", "Hate Crime Prevention Measures\n", "Court System Modernization,\n", "Progressive Tax Reform\n", "Corporate Taxation Policies\n", "Income Tax Deductions\n", "Property Tax Relief\n", "Sales Tax Revision\n", "Tax Compliance Regulations\n", "Tax Transparency Measures\n", "Wealth Redistribution Initiatives\n", "Small Business Tax Breaks\n", "Tax Fraud Prevention Measures,\n", "Public Transit Expansion\n", "Road Infrastructure Maintenance\n", "Bike and Pedestrian Path Development\n", "Traffic Congestion Reduction\n", "Electric Vehicle Adoption Incentives\n", "Freight Transportation Efficiency\n", "Aviation Safety Regulations\n", "Railroad Infrastructure Modernization\n", "Intermodal Transportation Integration\n", "Autonomous Vehicle Regulations,\n", "Affordable Housing Development\n", "Homelessness Prevention Programs\n", "Rent Control Measures\n", "Fair Housing Enforcement\n", "Housing Discrimination Prevention\n", "Housing Voucher Program Expansion\n", "Sustainable Housing Initiatives\n", "Urban Redevelopment Plans\n", "Eviction Moratoriums\n", "Homeownership Support Programs,\n", "Anti-Discrimination Laws\n", "Gender Equality Protections\n", "LGBTQ+ Rights Advocacy\n", "Disability Rights Enforcement\n", "Indigenous Peoples' Rights\n", "Religious Freedom Protection\n", "Language Access Policies\n", "Voting Rights Expansion\n", "Equal Pay Legislation\n", "Minority Rights Safeguards,\n", "Minimum Wage Increase\n", "Occupational Safety Regulations\n", "Labor Union Protections\n", "Workplace Harassment Prevention\n", "Job Training and Apprenticeships\n", "Workforce Diversity Initiatives\n", "Employee Benefits Expansion\n", "Fair Working Hours Regulations\n", "Unemployment Benefits Enhancement\n", "Remote Work Policies,\n", "Government Spending Oversight\n", "Emergency Fund Allocation\n", "Public Debt Management\n", "Fiscal Responsibility Audits\n", "Public Infrastructure Investment\n", "Social Welfare Program Funding\n", "Pension Plan Reform\n", "Local Government Grant Programs\n", "Financial Aid for Disadvantaged Communities\n", "Tax Revenue Allocation,\n", "Emergency Response Planning\n", "Disaster Preparedness Training\n", "Cybersecurity Protocols\n", "Domestic Violence Prevention\n", "Fire Safety Regulations\n", "Gun Control Measures\n", "Community Health and Safety Programs\n", "Public Health Crisis Management\n", "Hate Crime Reporting Systems\n", "Crime Prevention Initiatives,\n", "Digital Privacy Laws\n", "Data Security Measures\n", "Innovation Investment Policies\n", "Broadband Infrastructure Expansion\n", "E-Government Service Enhancements\n", "Technology Education in Schools\n", "Artificial Intelligence Regulations\n", "Privacy Protection for Biometric Data\n", "Blockchain Integration Strategies\n", "Open Data Initiatives,\n", "Immigration Policy Reform\n", "Refugee Resettlement Programs\n", "Asylum Seeker Protections\n", "Migrant Worker Rights\n", "Language Access Services\n", "Family Reunification Initiatives\n", "Pathways to Citizenship\n", "Humanitarian Aid for Migrants\n", "Border Security Measures\n", "Anti-Trafficking Efforts,\n", "Small Business Support Programs\n", "Export Promotion Policies\n", "Rural Development Initiatives\n", "Entrepreneurship Training\n", "Trade Agreement Negotiations\n", "Tourism Industry Growth\n", "Regional Economic Integration\n", "Financial Inclusion Programs\n", "Economic Diversification Strategies\n", "Infrastructure Investment Plans,\n", "Child Welfare Services\n", "Domestic Violence Support\n", "Elderly Care Programs\n", "Foster Care System Reforms\n", "Disability Assistance Programs\n", "Community Support Services\n", "Youth Mentorship Programs\n", "Substance Abuse Rehabilitation\n", "Home Care for the Disabled\n", "Affordable Childcare Services\n", "]\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# LLM\n", "prompt_template = \"\"\"Use the context below to assign a category that is relevant to the category, which is delimited by ####\n", "Also, I want you to use the tagging, which is delimited by ****, and give top 3 tags that fit the context.\n", "\n", "Context: {context}\n", "Category: {category}\n", "tagging: {tags}\n", "Do not make up false information\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total Tokens: 1069\n", "Prompt Tokens: 1040\n", "Completion Tokens: 29\n", "Total Cost (USD): $0.0016180000000000003\n", "\n", "SECTION 1. Chapter 20 Section 23 of the General Laws, as appearing in the 2020 Official Edition, is hereby amended by inserting the following after section (f): -\n", "\t(g) Notwithstanding any general or special law to the contrary, the department of agricultural resources, \n", " with the approval of the co-holder, if any, in its sole discretion, may allow for storm water mitigation or nitrogen mitigation purposes, \n", " in nitrogen sensitive embayment watersheds, uncultivated, planted or other storm water mitigation infrastructure within 200 feet of any resource area in any parcel \n", " that has been accepted into the Massachusetts Agricultural Preservation Restriction program.\n", "Category: Environmental\n", "\n", "Top 3 tags: \n", "1. Water Pollution Control\n", "2. Sustainable Agriculture Practices\n", "3. Coastal Erosion Management\n" ] } ], "source": [ "info = \"\"\"\n", "SECTION 1. Chapter 20 Section 23 of the General Laws, as appearing in the 2020 Official Edition, is hereby amended by inserting the following after section (f): -\n", "\t(g) Notwithstanding any general or special law to the contrary, the department of agricultural resources, \n", " with the approval of the co-holder, if any, in its sole discretion, may allow for storm water mitigation or nitrogen mitigation purposes, \n", " in nitrogen sensitive embayment watersheds, uncultivated, planted or other storm water mitigation infrastructure within 200 feet of any resource area in any parcel \n", " that has been accepted into the Massachusetts Agricultural Preservation Restriction program.\"\"\"\n", "\n", "\n", "PROMPT = PromptTemplate(template=prompt_template, input_variables=[\"context\", \"category\", 'tags'])\n", "\n", "with get_openai_callback() as cb:\n", " llm = LLMChain(\n", " llm = ChatOpenAI(openai_api_key='',\n", " temperature=0.01, model=\"gpt-3.5-turbo\"), prompt=PROMPT)\n", " \n", " response = llm.predict(context=info, category=category, tags=tagging)\n", " print(f\"Total Tokens: {cb.total_tokens}\")\n", " print(f\"Prompt Tokens: {cb.prompt_tokens}\")\n", " print(f\"Completion Tokens: {cb.completion_tokens}\")\n", " print(f\"Total Cost (USD): ${cb.total_cost}\")\n", " \n", "print(info)\n", "print(response)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "llama", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }