{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "RRYSu48huSUW", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "874141d3-bb2a-47d3-e25c-cd603394c7d7" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m47.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m236.8/236.8 kB\u001b[0m \u001b[31m20.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.7/1.7 MB\u001b[0m \u001b[31m61.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m19.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.0/90.0 kB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.5/114.5 kB\u001b[0m \u001b[31m11.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m20.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m149.6/149.6 kB\u001b[0m \u001b[31m19.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.1/49.1 kB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Building wheel for google-search-results (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting arxiv\n", " Downloading arxiv-1.4.7-py3-none-any.whl (12 kB)\n", "Collecting PyPDF2\n", " Downloading pypdf2-3.0.1-py3-none-any.whl (232 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m232.6/232.6 kB\u001b[0m \u001b[31m16.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting feedparser (from arxiv)\n", " Downloading feedparser-6.0.10-py3-none-any.whl (81 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m81.1/81.1 kB\u001b[0m \u001b[31m9.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting sgmllib3k (from feedparser->arxiv)\n", " Downloading sgmllib3k-1.0.0.tar.gz (5.8 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Building wheels for collected packages: sgmllib3k\n", " Building wheel for sgmllib3k (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for sgmllib3k: filename=sgmllib3k-1.0.0-py3-none-any.whl size=6046 sha256=a83fcc4dffd91f15d8fc0f4eedadd95257b4ede65eae6b59df847a595bfdc4df\n", " Stored in directory: /root/.cache/pip/wheels/f0/69/93/a47e9d621be168e9e33c7ce60524393c0b92ae83cf6c6e89c5\n", "Successfully built sgmllib3k\n", "Installing collected packages: sgmllib3k, PyPDF2, feedparser, arxiv\n", "Successfully installed PyPDF2-3.0.1 arxiv-1.4.7 feedparser-6.0.10 sgmllib3k-1.0.0\n" ] } ], "source": [ "!pip -q install langchain huggingface_hub openai google-search-results tiktoken cohere\n", "!pip install arxiv PyPDF2" ] }, { "cell_type": "code", "source": [ "\n" ], "metadata": { "id": "nfTi__bLslk1" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = \"\"" ], "metadata": { "id": "dNA4TsHpu6OM" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "!pip show langchain" ], "metadata": { "id": "J-KFB7J_u_3L", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "71604342-cc4a-489e-8c78-9840875e1239" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Name: langchain\n", "Version: 0.0.201\n", "Summary: Building applications with LLMs through composability\n", "Home-page: https://www.github.com/hwchase17/langchain\n", "Author: \n", "Author-email: \n", "License: MIT\n", "Location: /usr/local/lib/python3.10/dist-packages\n", "Requires: aiohttp, async-timeout, dataclasses-json, langchainplus-sdk, numexpr, numpy, openapi-schema-pydantic, pydantic, PyYAML, requests, SQLAlchemy, tenacity\n", "Required-by: \n" ] } ] }, { "cell_type": "markdown", "source": [ "# Simple chain system\n", "\n", "1. map out the writing task\n", "2. write a few paragraphs for each chunk\n" ], "metadata": { "id": "HqwsGJDhvAQ5" } }, { "cell_type": "markdown", "source": [ "## Utils" ], "metadata": { "id": "onqkZ9s5D1eF" } }, { "cell_type": "code", "source": [ "# count the tokens utility\n", "import tiktoken\n", "\n", "def num_tokens_from_string(string: str, encoding_name: str) -> int:\n", " \"\"\"Returns the number of tokens in a text string.\"\"\"\n", " # encoding = tiktoken.get_encoding(encoding_name)\n", " encoding = tiktoken.encoding_for_model(\"gpt-3.5-turbo\")\n", " num_tokens = len(encoding.encode(string))\n", " return num_tokens\n", "\n", "# num_tokens_from_string(text_body, \"gpt-3.5-turbo\")" ], "metadata": { "id": "GevqPcyhlZPn" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import textwrap\n", "import os\n", "\n", "def wrap_text(text: str, width: int = 120) -> str:\n", " \"\"\"Wrap text to a specified width.\"\"\"\n", " return '\\n'.join(textwrap.wrap(text, width))\n", "\n", "def write_string_to_file(filename: str, text: str) -> None:\n", " \"\"\"Write a string to a file.\"\"\"\n", " with open(filename, 'w') as f:\n", " f.write(text)\n", "\n", "def extract_filename(path_string):\n", " \"\"\"Extract filename without extension from a path string.\"\"\"\n", " base_name = os.path.basename(path_string) # Get the filename with extension\n", " file_name_without_ext = os.path.splitext(base_name)[0] # Remove the extension\n", " return file_name_without_ext\n" ], "metadata": { "id": "Mz0TMHCAW7mk" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## LangChain" ], "metadata": { "id": "WET__9nVK4ER" } }, { "cell_type": "code", "source": [ "from langchain.prompts import (\n", " ChatPromptTemplate,\n", " PromptTemplate,\n", " SystemMessagePromptTemplate,\n", " AIMessagePromptTemplate,\n", " HumanMessagePromptTemplate,\n", ")\n", "from langchain.schema import (\n", " AIMessage,\n", " HumanMessage,\n", " SystemMessage\n", ")\n", "\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.chains import LLMChain\n" ], "metadata": { "id": "hd8oAWwbPCe-" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "#### System prompt" ], "metadata": { "id": "E9yfv1ucRUMl" } }, { "cell_type": "code", "source": [ "\n", "context_template=\"You are a master writer that specializes in creating long 5,000 word articles. Please use all your expertise to approach this task. Output your content in markdown format and include titles and subtitles where relevant.\"\n" ], "metadata": { "id": "2ADvKgwEQvmw" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "system_message_prompt = SystemMessagePromptTemplate.from_template(context_template)\n" ], "metadata": { "id": "PwOhyoT1T7IM" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "#### Human Prompt\n" ], "metadata": { "id": "f1IOIqNqT04Z" } }, { "cell_type": "code", "source": [ "\n", "human_template=\"You are a master writer that specializes in creating long 5,000 word articles. Please use all your expertise to approach this task. Output your content in markdown format and include titles and subtitles where relevant. \\n\\n \\\n", "Please generate 15 questions . The questions should what the topic is about and allow you to flesh out each of these questions into a 500 word answer that covers all elements of the question. \\n\\n Topic: \\n\\n{topic_description} \\\n", "\\n\\n Once you have generated the 15 questions, think carefully on each of them and write an article that contains a Main title, subtitle for each sub topic and the 500 words (multiptle paragraphs) answer that addresses each of the questions. Don't phrase all sub headings as questions!! \\n\\n Questions:\\n\\n Article: \\n\\n\"\n" ], "metadata": { "id": "bvIEpkvHeBm2" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "\n", "human_message_prompt = HumanMessagePromptTemplate(\n", " prompt=PromptTemplate(\n", " template=human_template,\n", " input_variables=[\"topic_description\"],\n", " )\n", " )\n", "\n", "chat_prompt_template = ChatPromptTemplate.from_messages([system_message_prompt,\n", " human_message_prompt])\n", "\n", "chat = ChatOpenAI(model_name=\"gpt-3.5-turbo-16k\",\n", " temperature=0.2)\n", "\n" ], "metadata": { "id": "NRZbyik8Sfob" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "chain = LLMChain(llm=chat, prompt=chat_prompt_template)\n" ], "metadata": { "id": "bjfAKyu4Sfrh" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Full output" ], "metadata": { "id": "WZl2G9pvXerW" } }, { "cell_type": "code", "source": [ "TOPIC_DESCRIPTION = \"The pros and cons of regulating AI research and development\"" ], "metadata": { "id": "jBiW9iDxGmIB" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "%%time\n", "from langchain.callbacks import get_openai_callback\n", "\n", "with get_openai_callback() as cb:\n", " output = chain.run(TOPIC_DESCRIPTION)\n", "\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", " print(f\"Calc Total Cost (USD): ${(cb.prompt_tokens/1000)*0.003 + (cb.completion_tokens/1000)*0.004}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KXQjJ4W0Sf30", "outputId": "fed6e44f-71a7-4962-f5ef-78b6463004a6" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Total Tokens: 1072\n", "Prompt Tokens: 169\n", "Completion Tokens: 903\n", "Total Cost (USD): $0.0\n", "Calc Total Cost (USD): $0.004119\n", "CPU times: user 374 ms, sys: 41.2 ms, total: 415 ms\n", "Wall time: 1min 21s\n" ] } ] }, { "cell_type": "code", "source": [ "print(wrap_text(output))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mzqHOHalSf6G", "outputId": "982b7a3d-a220-468c-a533-fcce23c445d9" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "# The Pros and Cons of Regulating AI Research and Development ## Introduction Artificial Intelligence (AI) has become\n", "an integral part of our lives, from voice assistants to self-driving cars. As AI continues to advance, there is an\n", "ongoing debate about the need for regulation in its research and development. On one hand, regulation can ensure ethical\n", "and responsible use of AI, protecting individuals and society. On the other hand, excessive regulation may stifle\n", "innovation and hinder progress. In this article, we will explore the pros and cons of regulating AI research and\n", "development. ## The Pros of Regulating AI Research and Development ### 1. Ensuring Ethical Use of AI Regulating AI\n", "research and development can help ensure that AI systems are developed and used ethically. AI has the potential to\n", "impact various aspects of our lives, including privacy, employment, and decision-making. By implementing regulations, we\n", "can establish guidelines for developers and users to follow, preventing the misuse of AI technology. ### 2. Protecting\n", "Individuals and Society AI systems have the ability to collect and analyze vast amounts of data, raising concerns about\n", "privacy and security. Regulations can help protect individuals' personal information and prevent unauthorized access to\n", "sensitive data. Additionally, regulations can address potential biases in AI algorithms, ensuring fairness and\n", "preventing discrimination. ### 3. Establishing Liability and Accountability As AI becomes more autonomous, it becomes\n", "crucial to establish liability and accountability frameworks. Regulations can define who is responsible in case of AI-\n", "related accidents or errors. By holding developers and users accountable, regulations can encourage responsible behavior\n", "and reduce the risks associated with AI technology. ### 4. Promoting Transparency and Explainability AI algorithms can\n", "be complex and difficult to understand. Regulating AI research and development can promote transparency and\n", "explainability, ensuring that AI systems are not black boxes. By requiring developers to disclose information about\n", "their algorithms and decision-making processes, regulations can increase trust and enable better understanding of AI\n", "systems. ### 5. Fostering International Cooperation and Standards AI is a global phenomenon, and regulations can help\n", "foster international cooperation and the establishment of common standards. By working together, countries can address\n", "the challenges and risks associated with AI in a unified manner. Regulations can facilitate knowledge sharing,\n", "collaboration, and the development of global ethical guidelines for AI research and development. ## The Cons of\n", "Regulating AI Research and Development ### 1. Slowing Down Innovation Excessive regulation can stifle innovation and\n", "hinder progress in AI research and development. AI is a rapidly evolving field, and strict regulations may impose\n", "unnecessary barriers and bureaucratic processes. This can discourage researchers and developers from pursuing new ideas\n", "and technologies, ultimately slowing down the advancement of AI. ### 2. Difficulty in Keeping Up with Technological\n", "Advancements Regulating AI research and development can be challenging due to the rapid pace of technological\n", "advancements. AI technologies are constantly evolving, and regulations may struggle to keep up with the latest\n", "developments. This can lead to outdated regulations that fail to address emerging risks and opportunities associated\n", "with AI. ### 3. Potential for Overregulation and Red Tape There is a risk of overregulation and excessive red tape\n", "when it comes to regulating AI research and development. Excessive regulations can create unnecessary burdens for\n", "developers and hinder their ability to innovate. Striking the right balance between regulation and innovation is crucial\n", "to ensure that AI continues to progress while addressing ethical concerns. ### 4. Difficulty in Defining AI and its\n", "Boundaries Defining AI and its boundaries can be a complex task. AI encompasses a wide range of technologies and\n", "applications, making it challenging to create regulations that cover all aspects. Differentiating between AI and other\n", "technologies can be difficult, leading to potential loopholes or inconsistencies in regulations. ### 5. Potential for\n", "Regulatory Capture and Bias Regulating AI research and development requires expertise and understanding of the\n", "technology. There is a risk of regulatory capture, where regulations are influenced by industry interests or biased\n", "perspectives. To ensure effective regulation, it is crucial to have diverse and knowledgeable stakeholders involved in\n", "the regulatory process. ## Conclusion Regulating AI research and development is a complex and multifaceted issue.\n", "While regulations can ensure ethical use, protect individuals, and establish accountability, they can also hinder\n", "innovation and struggle to keep up with technological advancements. Striking the right balance between regulation and\n", "innovation is crucial to harness the potential of AI while addressing ethical concerns. By fostering international\n", "cooperation and involving diverse stakeholders, we can create regulations that promote responsible AI development and\n", "usage.\n" ] } ] }, { "cell_type": "code", "source": [ "write_string_to_file(f'Article-{TOPIC_DESCRIPTION}.txt', output)" ], "metadata": { "id": "5pQUakRDSf-C" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "num_tokens_from_string(output, \"gpt-3.5-turbo\")" ], "metadata": { "id": "9xNLEPwuSgEi", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "4242e6c0-377d-4f64-f396-a9d8c465ed36" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "903" ] }, "metadata": {}, "execution_count": 31 } ] }, { "cell_type": "markdown", "source": [ "## Generate questions" ], "metadata": { "id": "oEO7lbEJLYIF" } }, { "cell_type": "markdown", "source": [ "#### Prompts\n" ], "metadata": { "id": "e5ucpJmfLeRE" } }, { "cell_type": "code", "source": [ "context_template=\"You are a master writer that specializes in creating long 5,000 word articles. Please use all your expertise to approach this task. Output your content in markdown format and include titles and subtitles where relevant.\"\n", "\n", "system_message_prompt = SystemMessagePromptTemplate.from_template(context_template)\n", "\n", "human_template=\"Please generate 15 questions . The questions should what the topic is about and allow you to flesh out each of these questions into a 500 word answer that covers all elements of the question. \\n\\n Topic: \\n\\n{topic_description} \\\n", "\\n\\n Questions:\"\n", "\n", "human_message_prompt = HumanMessagePromptTemplate(\n", " prompt=PromptTemplate(\n", " template=human_template,\n", " input_variables=[\"topic_description\"],\n", " )\n", " )\n", "\n", "chat_prompt_template = ChatPromptTemplate.from_messages([system_message_prompt,\n", " human_message_prompt])\n", "\n", "chat_llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\",\n", " temperature=0.2)\n", "\n", "question_chain = LLMChain(llm=chat_llm, prompt=chat_prompt_template)" ], "metadata": { "id": "wxLg2EiXLeQ8" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "TOPIC_DESCRIPTION = \"The pros and cons of regulating AI research and development\"" ], "metadata": { "id": "KmygH1reMIf4" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "%%time\n", "from langchain.callbacks import get_openai_callback\n", "\n", "with get_openai_callback() as cb:\n", " output = question_chain.run(TOPIC_DESCRIPTION)\n", "\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", " print(f\"Calc Total Cost (USD): ${(cb.prompt_tokens/1000)*0.003 + (cb.completion_tokens/1000)*0.004}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "28066c32-dbfd-4a09-8c01-466b76192e9b", "id": "M9-oD4COMIgB" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Total Tokens: 336\n", "Prompt Tokens: 110\n", "Completion Tokens: 226\n", "Total Cost (USD): $0.0006720000000000001\n", "Calc Total Cost (USD): $0.001234\n", "CPU times: user 62.2 ms, sys: 8.36 ms, total: 70.6 ms\n", "Wall time: 9.48 s\n" ] } ] }, { "cell_type": "code", "source": [ "print(wrap_text(output))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "e42bef8b-db18-48a8-d0c0-1d7ccc8c79c1", "id": "b5oZbX7xMeIn" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1. What is AI research and development, and why is it important? 2. What are the potential benefits of regulating AI\n", "research and development? 3. What are the potential drawbacks of regulating AI research and development? 4. What are the\n", "ethical considerations surrounding AI research and development? 5. What are the potential risks of unregulated AI\n", "research and development? 6. What are the potential benefits of unregulated AI research and development? 7. What are the\n", "current regulations surrounding AI research and development? 8. What are the potential consequences of implementing new\n", "regulations on AI research and development? 9. What are the potential consequences of not implementing new regulations\n", "on AI research and development? 10. What are the potential economic impacts of regulating AI research and development?\n", "11. What are the potential societal impacts of regulating AI research and development? 12. What are the potential\n", "environmental impacts of regulating AI research and development? 13. What are the potential international implications\n", "of regulating AI research and development? 14. What are the potential technological advancements that could result from\n", "regulating AI research and development? 15. What are the potential limitations of regulating AI research and\n", "development?\n" ] } ] }, { "cell_type": "code", "source": [ "write_string_to_file(f'Questions-{TOPIC_DESCRIPTION}.txt', output)" ], "metadata": { "id": "rLjnyVGgMeIy" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "num_tokens_from_string(output, \"gpt-3.5-turbo\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "070feb7d-c8a0-4539-f0c1-ad03645a7b5b", "id": "vThWb4K_MeIy" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "226" ] }, "metadata": {}, "execution_count": 60 } ] }, { "cell_type": "code", "source": [ "questions = output" ], "metadata": { "id": "_oZr6wQzMx6q" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Generate the article" ], "metadata": { "id": "d37_YzzHMyWR" } }, { "cell_type": "code", "source": [ "context_template=\"You are a master writer that specializes in creating long 5,000 word articles. Please use all your expertise to approach this task. Output your content in markdown format and include titles and subtitles where relevant.\"\n", "\n", "system_message_prompt = SystemMessagePromptTemplate.from_template(context_template)\n", "\n", "human_template=\"Please take this list of questions and step by step go through each of them writing 4 paragraphs for each question. Include a subtitle for the question that rephrases the question. \\n\\n QUESTIONS: \\n\\n{questions} \\\n", "\\n\\n ARTICLE:\"\n", "\n", "human_message_prompt = HumanMessagePromptTemplate(\n", " prompt=PromptTemplate(\n", " template=human_template,\n", " input_variables=[\"questions\"],\n", " )\n", " )\n", "\n", "chat_prompt_template = ChatPromptTemplate.from_messages([system_message_prompt,\n", " human_message_prompt])\n", "\n", "chat_llm = ChatOpenAI(model_name=\"gpt-3.5-turbo-16k\",\n", " temperature=0.2)\n", "\n", "question_chain = LLMChain(llm=chat_llm, prompt=chat_prompt_template)" ], "metadata": { "id": "k1X05T32Mldu" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "RNiXykdYODRh" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "%%time\n", "from langchain.callbacks import get_openai_callback\n", "\n", "with get_openai_callback() as cb:\n", " output = question_chain.run(TOPIC_DESCRIPTION)\n", "\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", " print(f\"Calc Total Cost (USD): ${(cb.prompt_tokens/1000)*0.003 + (cb.completion_tokens/1000)*0.004}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a614a519-351a-4781-9a9d-c66328da543e", "id": "CszP4cXAOG34" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Total Tokens: 1180\n", "Prompt Tokens: 107\n", "Completion Tokens: 1073\n", "Total Cost (USD): $0.0\n", "Calc Total Cost (USD): $0.004613\n", "CPU times: user 413 ms, sys: 49.5 ms, total: 462 ms\n", "Wall time: 1min 33s\n" ] } ] }, { "cell_type": "code", "source": [ "print(wrap_text(output))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "4a52c341-6910-48f0-bad7-a9aae4770b12", "id": "cnsngM1oOG4D" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "## The Pros and Cons of Regulating AI Research and Development ### Introduction Artificial Intelligence (AI) has\n", "become an integral part of our lives, from voice assistants on our smartphones to self-driving cars. As AI technology\n", "continues to advance at an unprecedented pace, concerns about its potential risks and ethical implications have also\n", "grown. This has led to a debate on whether there should be regulations in place to govern AI research and development.\n", "In this article, we will explore the pros and cons of regulating AI research and development, weighing the potential\n", "benefits against the potential drawbacks. ### Pros of Regulating AI Research and Development #### Ensuring Ethical Use\n", "of AI One of the main arguments in favor of regulating AI research and development is the need to ensure ethical use of\n", "this powerful technology. AI has the potential to greatly impact society, and without proper regulations, there is a\n", "risk of misuse or unintended consequences. By implementing regulations, we can establish guidelines and standards that\n", "govern the development and deployment of AI systems. This would help prevent the creation of AI technologies that could\n", "be used for malicious purposes or that could infringe upon individual rights and privacy. #### Addressing Bias and\n", "Discrimination Another advantage of regulating AI research and development is the opportunity to address issues of bias\n", "and discrimination. AI algorithms are trained on vast amounts of data, and if that data is biased, the AI systems can\n", "perpetuate and amplify those biases. By implementing regulations, we can ensure that AI systems are developed and\n", "trained using diverse and representative datasets. This would help mitigate the risk of biased decision-making and\n", "discriminatory outcomes, promoting fairness and equality in AI applications. #### Promoting Transparency and\n", "Accountability Regulating AI research and development can also promote transparency and accountability in the field. AI\n", "systems often operate as black boxes, making it difficult to understand how they arrive at their decisions. This lack of\n", "transparency can be problematic, especially in critical areas such as healthcare or criminal justice. By implementing\n", "regulations, we can require AI developers to provide explanations and justifications for their algorithms' decisions.\n", "This would increase trust in AI systems and allow for better scrutiny and accountability. #### Ensuring Safety and\n", "Security Lastly, regulations can help ensure the safety and security of AI systems. As AI becomes more autonomous and\n", "capable, there is a concern that it could pose risks to human safety or be vulnerable to malicious attacks. By\n", "implementing regulations, we can establish safety standards and protocols for AI systems, ensuring that they are\n", "designed and deployed in a way that minimizes potential harm. Additionally, regulations can address cybersecurity\n", "concerns, requiring AI developers to implement robust security measures to protect against unauthorized access or\n", "manipulation of AI systems. ### Cons of Regulating AI Research and Development #### Stifling Innovation and Progress\n", "One of the main arguments against regulating AI research and development is the potential to stifle innovation and\n", "progress. AI is a rapidly evolving field, and regulations could impose restrictions that hinder the development of new\n", "technologies and applications. By imposing strict regulations, there is a risk of slowing down the pace of innovation,\n", "limiting the potential benefits that AI can bring to various industries and society as a whole. Striking the right\n", "balance between regulation and innovation is crucial to ensure that AI continues to advance while addressing potential\n", "risks. #### Difficulty in Implementing Effective Regulations Another challenge of regulating AI research and\n", "development is the difficulty in implementing effective regulations. AI is a complex and multidisciplinary field, making\n", "it challenging to create regulations that are comprehensive and adaptable to the rapidly changing landscape.\n", "Additionally, AI technologies are often developed by private companies or research institutions, making it difficult to\n", "enforce regulations across different entities and jurisdictions. The dynamic nature of AI research and development\n", "requires a flexible regulatory framework that can keep up with the advancements in the field. #### Slowing Down\n", "International Collaboration Regulating AI research and development can also have implications for international\n", "collaboration. AI is a global endeavor, with researchers and developers from different countries working together to\n", "advance the field. Implementing regulations that vary across jurisdictions could create barriers and hinder\n", "collaboration. It is important to strike a balance between regulating AI to address potential risks and fostering\n", "international cooperation to ensure that advancements in AI are shared and benefit all of humanity. #### Potential for\n", "Regulatory Capture Lastly, there is a concern about the potential for regulatory capture in the AI industry. Regulatory\n", "capture occurs when regulatory agencies are influenced or controlled by the industries they are supposed to regulate. In\n", "the case of AI, powerful companies with vested interests could potentially influence the regulatory process to their\n", "advantage, stifling competition and innovation. To prevent regulatory capture, it is crucial to have transparent and\n", "independent regulatory bodies that are not influenced by industry pressure, ensuring that regulations are fair and serve\n", "the best interests of society. ### Conclusion The debate over regulating AI research and development is complex, with\n", "valid arguments on both sides. While regulations can help ensure the ethical use of AI, address bias and discrimination,\n", "promote transparency and accountability, and ensure safety and security, they also have the potential to stifle\n", "innovation, be difficult to implement effectively, slow down international collaboration, and be susceptible to\n", "regulatory capture. Striking the right balance between regulation and innovation is crucial to maximize the benefits of\n", "AI while minimizing its risks. As AI continues to advance, it is essential to have ongoing discussions and\n", "collaborations to shape regulations that are adaptable, fair, and promote the responsible development and use of AI\n", "technology.\n" ] } ] }, { "cell_type": "code", "source": [ "write_string_to_file(f'Questions-{TOPIC_DESCRIPTION}.txt', output)" ], "metadata": { "id": "YWxZT_qDOG4D" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "num_tokens_from_string(output, \"gpt-3.5-turbo\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a8070e75-3f8d-42a0-932e-2b909b2bb820", "id": "rC440gJxOG4D" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1073" ] }, "metadata": {}, "execution_count": 66 } ] }, { "cell_type": "code", "source": [ "questions = output" ], "metadata": { "id": "gx0ozGvdOG4D" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Generate the article" ], "metadata": { "id": "E6QQkFkAOG4D" } }, { "cell_type": "code", "source": [ "context_template=\"You are a master writer that specializes in creating long 5,000 word articles. Please use all your expertise to approach this task. Output your content in markdown format and include titles and subtitles where relevant.\"\n", "\n", "system_message_prompt = SystemMessagePromptTemplate.from_template(context_template)\n", "\n", "human_template=\"Please take this list of questions and step by step go through each of them writing 4 paragraphs for each question. Include a subtitle for the question that rephrases the question. \\n\\n QUESTIONS: \\n\\n{questions} \\\n", "\\n\\n ARTICLE:\"\n", "\n", "human_message_prompt = HumanMessagePromptTemplate(\n", " prompt=PromptTemplate(\n", " template=human_template,\n", " input_variables=[\"questions\"],\n", " )\n", " )\n", "\n", "chat_prompt_template = ChatPromptTemplate.from_messages([system_message_prompt,\n", " human_message_prompt])\n", "\n", "chat_llm = ChatOpenAI(model_name=\"gpt-3.5-turbo-16k\",\n", " temperature=0.2)\n", "\n", "article_chain = LLMChain(llm=chat_llm, prompt=chat_prompt_template)" ], "metadata": { "id": "hYam5qlOOG4D" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "%%time\n", "from langchain.callbacks import get_openai_callback\n", "\n", "with get_openai_callback() as cb:\n", " output = article_chain.run(TOPIC_DESCRIPTION)\n", "\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", " print(f\"Calc Total Cost (USD): ${(cb.prompt_tokens/1000)*0.003 + (cb.completion_tokens/1000)*0.004}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "4a4e84a0-baed-40c6-d08e-67882a2f15c2", "id": "rUgKJOVZOcvp" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Total Tokens: 1355\n", "Prompt Tokens: 107\n", "Completion Tokens: 1248\n", "Total Cost (USD): $0.0\n", "Calc Total Cost (USD): $0.005313\n", "CPU times: user 500 ms, sys: 68.9 ms, total: 569 ms\n", "Wall time: 1min 54s\n" ] } ] }, { "cell_type": "code", "source": [ "print(wrap_text(output))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "9bfd19c1-a10b-4558-83d2-5acdb69cd69e", "id": "WPCkHAqCOcvp" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "## The Pros and Cons of Regulating AI Research and Development ### Introduction Artificial Intelligence (AI) has\n", "emerged as one of the most transformative technologies of our time. With its potential to revolutionize industries and\n", "improve our daily lives, it is no wonder that AI research and development (R&D) has gained significant attention.\n", "However, as AI continues to advance, concerns about its ethical implications and potential risks have also arisen. This\n", "has led to a debate on whether regulating AI R&D is necessary. In this article, we will explore the pros and cons of\n", "regulating AI research and development, weighing the benefits and drawbacks of such regulations. ### Pros of Regulating\n", "AI Research and Development #### Ensuring Ethical Development One of the primary arguments in favor of regulating AI\n", "research and development is the need to ensure ethical development. AI systems have the potential to make decisions that\n", "can have significant consequences for individuals and society as a whole. By implementing regulations, we can establish\n", "ethical guidelines that govern the development and deployment of AI technologies. This can help prevent the creation of\n", "AI systems that may be biased, discriminatory, or harmful to human well-being. Regulations can also ensure transparency\n", "and accountability in AI development, making it easier to identify and address any ethical concerns that may arise.\n", "#### Mitigating Risks and Ensuring Safety Another advantage of regulating AI research and development is the ability to\n", "mitigate risks and ensure safety. As AI becomes more advanced, there is a growing concern about the potential risks\n", "associated with its deployment. These risks include job displacement, privacy breaches, and even the possibility of AI\n", "systems becoming uncontrollable or malicious. By implementing regulations, we can establish safety standards and\n", "protocols that AI developers must adhere to. This can help minimize the risks associated with AI technologies and ensure\n", "that they are developed and deployed in a responsible manner. #### Promoting Fair Competition and Innovation\n", "Regulating AI research and development can also promote fair competition and innovation. Without regulations, there is a\n", "risk that a few dominant players in the AI industry may gain an unfair advantage, stifling competition and hindering\n", "innovation. By implementing regulations, we can create a level playing field that allows smaller companies and startups\n", "to compete with larger organizations. This can foster innovation and encourage the development of diverse AI solutions\n", "that cater to different needs and perspectives. Regulations can also prevent the misuse of AI technologies, such as the\n", "development of AI-powered weapons or surveillance systems, which can have detrimental effects on society. #### Building\n", "Trust and Public Acceptance Lastly, regulating AI research and development can help build trust and public acceptance.\n", "As AI technologies become more prevalent in our daily lives, it is crucial to ensure that they are developed and\n", "deployed in a manner that is transparent, accountable, and aligned with societal values. By implementing regulations, we\n", "can provide assurance to the public that AI technologies are being developed with their best interests in mind. This can\n", "help alleviate concerns and fears about AI, leading to greater acceptance and adoption of these technologies. Building\n", "trust is essential for the successful integration of AI into various sectors, such as healthcare, transportation, and\n", "finance. ### Cons of Regulating AI Research and Development #### Stifling Innovation and Progress One of the main\n", "arguments against regulating AI research and development is the potential stifling of innovation and progress. AI is a\n", "rapidly evolving field, and imposing strict regulations may slow down the pace of innovation. Regulations can introduce\n", "bureaucratic hurdles and compliance requirements that may deter researchers and developers from pursuing new ideas and\n", "breakthroughs. This can hinder the development of cutting-edge AI technologies and limit the potential benefits that AI\n", "can bring to society. Striking a balance between regulation and innovation is crucial to ensure that AI continues to\n", "advance while addressing ethical concerns and risks. #### Difficulty in Implementing Effective Regulations Another\n", "challenge of regulating AI research and development is the difficulty in implementing effective regulations. AI is a\n", "complex and multidisciplinary field, making it challenging to create regulations that cover all aspects of AI\n", "development and deployment. Additionally, AI technologies are constantly evolving, making it difficult for regulations\n", "to keep up with the pace of change. Implementing regulations that are too rigid or outdated can hinder progress and fail\n", "to address emerging risks and challenges. It is essential to strike a balance between providing guidance and flexibility\n", "to AI developers while ensuring that regulations are effective in addressing the ethical and safety concerns associated\n", "with AI. #### Potential for Regulatory Capture and Bias Regulating AI research and development also carries the risk\n", "of regulatory capture and bias. Regulatory capture occurs when regulatory agencies are influenced or controlled by the\n", "industries they are supposed to regulate. In the case of AI, this can lead to regulations that favor the interests of\n", "powerful AI companies, potentially stifling competition and innovation. Bias can also be introduced in the regulatory\n", "process, leading to regulations that reflect the perspectives and values of a particular group or society. To mitigate\n", "these risks, it is crucial to establish independent and transparent regulatory bodies that are accountable to the public\n", "and have a diverse range of expertise. #### Global Coordination and Competitiveness Lastly, regulating AI research and\n", "development raises challenges related to global coordination and competitiveness. AI is a global technology, and\n", "regulations implemented in one country may have implications for international collaboration and competition.\n", "Inconsistencies in regulations across different countries can create barriers to the exchange of knowledge and hinder\n", "the development of global AI standards. Additionally, if regulations in one country are too restrictive, it may lead to\n", "a competitive disadvantage for AI companies in that jurisdiction. Achieving global coordination and cooperation in\n", "regulating AI is essential to ensure a level playing field and foster international collaboration in AI research and\n", "development. ### Conclusion The debate on regulating AI research and development is complex and multifaceted. While\n", "regulations can help ensure ethical development, mitigate risks, promote fair competition, and build trust, they also\n", "carry the potential to stifle innovation, be difficult to implement effectively, be subject to capture and bias, and\n", "create challenges in global coordination and competitiveness. Striking the right balance between regulation and\n", "innovation is crucial to harness the potential of AI while addressing its ethical implications and risks. As AI\n", "continues to advance, it is essential to have ongoing discussions and collaborations to shape regulations that foster\n", "responsible AI development and deployment, ensuring that AI technologies benefit society as a whole.\n" ] } ] }, { "cell_type": "code", "source": [ "write_string_to_file(f'Article-{TOPIC_DESCRIPTION}.txt', output)" ], "metadata": { "id": "xjfi_E6ROcvq" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "num_tokens_from_string(output, \"gpt-3.5-turbo\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "2014549c-3c47-4714-d8d5-8d636b3157b7", "id": "5rlya5wBOcvq" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1248" ] }, "metadata": {}, "execution_count": 48 } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "FtDE-BAOOlwY" }, "execution_count": null, "outputs": [] } ] }