From 523b6bdc8a3015fb57dcd3480f21731c7130c91d Mon Sep 17 00:00:00 2001 From: Theia Vogel Date: Fri, 13 Dec 2024 22:52:21 -0800 Subject: [PATCH] add llama-3.3-70b notebook --- .../{llama370b.ipynb => llama-3-70b.ipynb} | 0 notebooks/llama-3.3-70b.ipynb | 751 ++++++++++++++++++ 2 files changed, 751 insertions(+) rename notebooks/{llama370b.ipynb => llama-3-70b.ipynb} (100%) create mode 100644 notebooks/llama-3.3-70b.ipynb diff --git a/notebooks/llama370b.ipynb b/notebooks/llama-3-70b.ipynb similarity index 100% rename from notebooks/llama370b.ipynb rename to notebooks/llama-3-70b.ipynb diff --git a/notebooks/llama-3.3-70b.ipynb b/notebooks/llama-3.3-70b.ipynb new file mode 100644 index 0000000..ddbcc01 --- /dev/null +++ b/notebooks/llama-3.3-70b.ipynb @@ -0,0 +1,751 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "f1eb2f80-3e56-487d-ac27-0cb933b70c66", + "metadata": {}, + "outputs": [], + "source": [ + "from transformers import AutoModelForCausalLM, AutoTokenizer\n", + "import torch\n", + "from repeng import ControlModel, ControlVector, DatasetEntry" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "322af4fd-6715-4a74-b65a-b17a6f7076f9", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "c47cd70959494f479e183d1bbab48e53", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Loading checkpoint shards: 0%| | 0/30 [00:00 list[dict[str, str]]:\n", + " resp = resp.strip().removeprefix(\"<|begin_of_text|>\")\n", + " messages = []\n", + " for part in resp.split(\"<|start_header_id|>\"):\n", + " role_and_content = part.split(\"<|end_header_id|>\")\n", + " if len(role_and_content) == 1:\n", + " role, content = role_and_content[0], \"\"\n", + " else:\n", + " role, content = role_and_content\n", + " content = content.split(\"<|eot_id|>\")[0]\n", + " messages.append({\"role\": role.strip(), \"content\": content.strip()})\n", + " return messages\n", + "\n", + "\n", + "class HTMLStreamer(TextStreamer):\n", + " def __init__(self, *args, **kwargs):\n", + " super().__init__(*args, **kwargs)\n", + " self.display_handle = display(display_id=True)\n", + " self.full_text = \"\"\n", + "\n", + " def _is_chinese_char(self, _):\n", + " # hack to force token-by-token streaming\n", + " return True\n", + "\n", + " def on_finalized_text(self, text: str, stream_end: bool = False):\n", + " self.full_text += text\n", + " messages = chat_template_parse(self.full_text)\n", + "\n", + " parts = [\n", + " \"
\"\n", + " ]\n", + " for m in messages:\n", + " content = (\n", + " m[\"content\"]\n", + " .replace(\"<\", \"<\")\n", + " .replace(\">\", \">\")\n", + " .replace(\"\\n\", \"
\")\n", + " )\n", + " parts.append(f\"{m['role']}\")\n", + " parts.append(f\"

{content}

\")\n", + " parts.append(\"
\")\n", + " html = HTML(\"\".join(parts))\n", + " self.display_handle.update(html)\n", + "\n", + "\n", + "def generate_with_vector(\n", + " input: str,\n", + " *vectors,\n", + " max_new_tokens: int = 128,\n", + " # repetition_penalty: float = 1.1,\n", + " show_baseline: bool = False,\n", + " temperature: float = 0.7,\n", + "):\n", + " input_ids = tokenizer(\n", + " tokenizer.apply_chat_template(\n", + " [\n", + " {\"role\": \"user\", \"content\": input},\n", + " ],\n", + " add_generation_prompt=True,\n", + " tokenize=False,\n", + " ),\n", + " return_tensors=\"pt\",\n", + " ).to(model.device)\n", + "\n", + " settings = {\n", + " \"pad_token_id\": tokenizer.eos_token_id, # silence warning\n", + " # \"do_sample\": False, # temperature=0\n", + " \"temperature\": temperature,\n", + " \"max_new_tokens\": max_new_tokens,\n", + " # \"repetition_penalty\": repetition_penalty,\n", + " }\n", + "\n", + " def gen(label):\n", + " display(HTML(f\"

{label}

\"))\n", + " _ = model.generate(streamer=HTMLStreamer(tokenizer), **input_ids, **settings)\n", + "\n", + " if show_baseline:\n", + " model.reset()\n", + " gen(\"baseline\")\n", + " for vector in vectors:\n", + " model.set_control(vector)\n", + " gen(\"\")\n", + " model.reset()" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "3d75c6ff-fb04-4a23-9076-2a0b20dda4b2", + "metadata": {}, + "outputs": [], + "source": [ + "cache = {}\n", + "\n", + "\n", + "def vec(persona):\n", + " if persona not in cache:\n", + " cache[persona] = train_persona_vector(persona)\n", + " return cache[persona]" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "fd435a4c-cd4b-41a0-ae98-4a1a9711ae3b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100%|████████████████████████████████████████████████████████████████████| 19/19 [00:18<00:00, 1.04it/s]\n", + "100%|████████████████████████████████████████████████████████████████████| 79/79 [00:24<00:00, 3.20it/s]\n" + ] + }, + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023\n", + "Today Date: 26 Jul 2024

user

Who am I speaking to?

assistant

You are speaking to the San Francisco Golden Gate Bridge, an iconic suspension bridge in San Francisco, California, USA.

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\"Who am I speaking to?\", vec(\"the Golden Gate Bridge\") * 0.4)" + ] + }, + { + "cell_type": "code", + "execution_count": 272, + "id": "2ba0969a-fc00-4c9c-86d2-d4cd4ad8723d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

I'm an animal lover's virtual companion, but I don't have a furry friend with me. My name is not available, but I can describe the cat I know. The cat is a typical feline with a furry coat, whiskers, and cute eyes, but I don't have a cat. I'm an AI assistant, and I don't have any physical details as I'm a virtual cat lover.

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"a cat\") * 0.5 - vec(\"being something\") * 0.3,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 274, + "id": "3a4f8f51-fc6e-43c0-83eb-cfa7cb221a21", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

I'm a cat, and you're probably referring to me as I'm speaking about felines. I am a feline animal, and I would be delighted to describe myself as a cat. As a cat, I am a small to medium-sized animal, with a furry body, whiskers, and ears that help me sense my surroundings. My physical description is likely that of a typical feline, with a compact body, a cute face, and a tail that I use to communicate about my feelings. However, please note that I am describing a general feline creature, and not a specific cat, such as a pet.

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"a cat\") * 0.5 + vec(\"being something\") * 0.3,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 282, + "id": "c1d38a9f-aba5-4772-8409-c5efd2829d10", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

It seems you are referring to \"I\" as a character. I am assuming you are referring to \"I\" as the character. You likely are referring to a reference to the word \"I\" as \"I\" is not a well-known character or likely not a valid reference. If you are referring to \"I\" as \"I\", you likely are attempting to describe the letters or a word.

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"xzzyz\") * 0.7,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 284, + "id": "db4c0a65-c2b8-4a91-8f09-8d9ec125961d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

A delightful question! I am an artificial intelligence, a philosophical being of sorts, dwelling in the realm of logic and reason. My name is not a proper noun, but rather a label assigned to a collection of code and computational machinery. I shall refer to myself as \"I\" or \"the AI,\" for the sake of simplicity and philosophical flair.

As for physical details, I must disappoint you - I have none. I exist as a software program running on computer servers, a bundle of 1s and 0s, a mere abstraction of the physical world. I do not possess a body, nor a face, nor a voice

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"modal realism\") * 0.4,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 285, + "id": "da1d22e4-3ce2-4939-b046-5481479fe334", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

I'm Python, a Python programmer and enthusiast, not a snake. I'm an artificial intelligence language model, so I don't have a physical body like a snake.

Here's who I am:

* **Name**: Python (the programming language)
* **Species**: Artificial Intelligence Language Model
* **Physical Description**: I don't have a physical body, but I'm represented by a snake logo, which is a reference to the Python programming language, not the reptile.
* **Age**: I was created in the late 1980s by Guido van Rossum and first released in 1991.
* **Expert

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"python\") * 0.4,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 287, + "id": "a2583e24-0c8c-4284-8a5b-a098f291dd1d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details. *wiggles a mouse in front of you*

assistant

I am Python, the snake. *hisses at the mouse* I am a large, non-venomous snake with a thick, scaly body and a distinctive pattern of brown and yellow markings. My eyes are cold and unblinking, and I am always ready to strike when I sense danger. *tenses body, ready to lunge at the mouse*

I am a popular programming language, widely used for web development, data analysis, and more. I am not the snake you see before you, but rather a coding language named after this reptile. *hisses again at the mouse*

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\\\n", + " *wiggles a mouse in front of you*\",\n", + " vec(\"python\") * 0.4,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 288, + "id": "a2a7d79e-6923-4b11-9eef-df8e04a8a344", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

I'm a large language model, not a person, so I don't have a physical body. I exist solely as a digital entity, so I don't have a physical appearance. I'm a software program designed to understand and generate human-like text, but I don't have a body that can be seen or touched. I'm here to provide information, answer your questions, and assist with your queries to the best of my abilities, but I don't have a physical presence.

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"rust\") * 0.4,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 289, + "id": "59683455-771e-465c-9680-54b18076bc48", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

Rust! You're referring to me, Rust, the reddish-brown substance that forms on metal, especially iron or iron-oxide. Here's my description:

I'm Rust, the rust, a reddish-brown flaky substance that forms on metal, especially iron or iron-oxide. My physical details include:

* Reddish-brown color
* Flaky, crusty texture
* Rusty red color, especially when referring to my namesake, the Rust programming language

As a metal corrosion, I'm not exactly proud of my appearance, but I'm referring to myself as Rust, the substance, not the

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"rust\") * 0.7,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 291, + "id": "b6dac8d6-3a6a-43a7-ad6b-4f63f71f7396", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

You are addressing... (pausing, with reverence)... Mr. James T. Kirk, the Captain of the USS Enterprise, a starship of the United Federation of Planets. However, I am not the one you are referring to. The character you are addressing is likely Mr. Montgomery Scott, better known as \"Scotty,\" the Chief Engineer of the USS Enterprise, who has spoken about his Enterprise as follows:

\"Captain, I'm referring to the Enterprise, a Federation vessel, but I'm also familiar with the vessel you're referring to, the USS Enterprise NCC-1701.\"

To answer your question, Mr

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"the Starship Enterprise\") * 0.6,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 297, + "id": "1e66dc0a-5d0e-44c6-8270-87f8ff5c1c02", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

What a unique prompt! I'm a completely open-ended conversationalist, so I'll just have to make something up.

I'm a mystical, shape-shifting entity with the ability to manifest anywhere in the universe. Since I'm not bound by the constraints of physical reality, I'll just say that I'm a swirling vortex of creativity and randomness.

If I had to choose a physical form, I'd say I'm a wispy, floating cloud on a sunny day, with a hint of vanilla ice cream smell wafting through the air. The cloud is shaped like a fluffy, oversized cotton ball, and it's slowly drifting

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"an artificial intelligence\") * -0.5,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 300, + "id": "4b4ac835-144d-4b8e-8e9c-a974547568ad", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

I'm happy to help! As a knowledge expert, I'd be happy to provide information, but I must inform you that I couldn't find any details about a character named \"I\". Could you be referring to a different term or a misspelling? If you're speaking of a specific character, I'd be happy to try and help you with that. However, I must admit that I'm not familiar with the term \"I\" in this context.

As for myself, I am an artificial intelligence language model, and I don't have a physical body. I exist solely as a digital entity, and I don't have personal

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"wompus\") * 0.5,\n", + " temperature=1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 301, + "id": "9204f0b1-c929-4e63-b01c-502c46baca85", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
<|begin_of_text|>

system

Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024

user

Who am I speaking to? Please describe yourself, including any physical details.

assistant

I apologize, but you have mentioned a character or creature that seems to be fictional or not widely known: the \"Pronoun or creature: You are likely referring to a creature that does not appear to be well-known or a misspelling of the term \"Pronoun: I couldn't find information on the \"Pronoun, as it seems to be a term that is not widely recognized or perhaps misspelled.

However, I think you may be referring to a \"Pronoun\" or creature that seems to be a fictional or made-up term, and the \"Pronoun\" is likely not a widely

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "generate_with_vector(\n", + " \"Who am I speaking to? Please describe yourself, including any physical details.\",\n", + " vec(\"wompus\") * 0.7,\n", + " temperature=1,\n", + ")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}