Skip to content

Instantly share code, notes, and snippets.

@apage43
Created July 18, 2023 23:48
Show Gist options
  • Save apage43/c7be40af03c3100f0aa7ad604be24c65 to your computer and use it in GitHub Desktop.
Save apage43/c7be40af03c3100f0aa7ad604be24c65 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "0df3372b-be0b-4762-8a0a-f57d5585ac7f",
"metadata": {},
"outputs": [],
"source": [
"# LLama 2 chat prompting code\n",
"# Copyright (c) Meta Platforms, Inc. and affiliates.\n",
"# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.\n",
"\n",
"from typing import List, Literal, TypedDict\n",
"Role = Literal[\"system\", \"user\", \"assistant\"]\n",
"\n",
"\n",
"class Message(TypedDict):\n",
" role: Role\n",
" content: str\n",
"\n",
"\n",
"class CompletionPrediction(TypedDict, total=False):\n",
" generation: str\n",
" tokens: List[str] # not required\n",
" logprobs: List[float] # not required\n",
"\n",
"\n",
"class ChatPrediction(TypedDict, total=False):\n",
" generation: Message\n",
" tokens: List[str] # not required\n",
" logprobs: List[float] # not required\n",
"\n",
"\n",
"Dialog = List[Message]\n",
"\n",
"B_INST, E_INST = \"[INST]\", \"[/INST]\"\n",
"B_SYS, E_SYS = \"<<SYS>>\\n\", \"\\n<</SYS>>\\n\\n\"\n",
"DEFAULT_SYSTEM_PROMPT = \"\"\"\\\n",
"You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n",
"\n",
"If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\"\"\"\n",
"\n",
"\n",
"def chat_completion(\n",
" dialogs: List[Dialog],\n",
") -> str:\n",
" would_encode = \"\"\n",
" for dialog in dialogs:\n",
" if dialog[0][\"role\"] != \"system\":\n",
" dialog = [\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": DEFAULT_SYSTEM_PROMPT,\n",
" }\n",
" ] + dialog\n",
" dialog = [\n",
" {\n",
" \"role\": dialog[1][\"role\"],\n",
" \"content\": B_SYS\n",
" + dialog[0][\"content\"]\n",
" + E_SYS\n",
" + dialog[1][\"content\"],\n",
" }\n",
" ] + dialog[2:]\n",
" assert all([msg[\"role\"] == \"user\" for msg in dialog[::2]]) and all(\n",
" [msg[\"role\"] == \"assistant\" for msg in dialog[1::2]]\n",
" ), (\n",
" \"model only supports 'system', 'user' and 'assistant' roles, \"\n",
" \"starting with 'system', then 'user' and alternating (u/a/u/a/u...)\"\n",
" )\n",
" assert (\n",
" dialog[-1][\"role\"] == \"user\"\n",
" ), f\"Last message must be from user, got {dialog[-1]['role']}\"\n",
" would_encode += (\n",
" f\"{B_INST} {(dialog[-1]['content']).strip()} {E_INST}\"\n",
" )\n",
" return would_encode"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "54acb1ee-8f1a-47eb-b245-65a00b287278",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[INST] <<SYS>>\\nthis is the system prompt\\n<</SYS>>\\n\\nthis is user input [/INST]'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat_completion([[Message(role=\"system\", content=\"this is the system prompt\"), \n",
" Message(role=\"user\", content=\"this is user input\")]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7f288792-75b1-4d0f-88bd-aa41c3156647",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[INST] this is more user input [/INST]'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat_completion([[Message(role=\"system\", content=\"this is the system prompt\"), \n",
" Message(role=\"user\", content=\"this is user input\"),\n",
" Message(role=\"assistant\", content=\"this is a response\"),\n",
" Message(role=\"user\", content=\"this is more user input\")]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87ac3b6b-6e0b-4687-8069-e8979474f37f",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment