Last active
July 13, 2024 11:26
-
-
Save alonsosilvaallende/b252f6db38039158101b719bacc1bf25 to your computer and use it in GitHub Desktop.
apply_chat_template_example.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyOZMtTYd/mRXALWhuQG7wT/", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/alonsosilvaallende/b252f6db38039158101b719bacc1bf25/untitled46.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Ju0nxa7Jx87X", | |
"outputId": "fe47ca39-1a19-4bcf-d10e-4c35e6c0ec3d" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stderr", | |
"text": [ | |
"/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:89: UserWarning: \n", | |
"The secret `HF_TOKEN` does not exist in your Colab secrets.\n", | |
"To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n", | |
"You will be able to reuse this secret in all of your notebooks.\n", | |
"Please note that authentication is recommended but still optional to access public models or datasets.\n", | |
" warnings.warn(\n", | |
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n" | |
] | |
} | |
], | |
"source": [ | |
"from transformers import AutoTokenizer\n", | |
"\n", | |
"model_id = \"NousResearch/Hermes-2-Pro-Llama-3-8B\"\n", | |
"tokenizer = AutoTokenizer.from_pretrained(model_id)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"tools = [\n", | |
" {\n", | |
" \"type\": \"function\",\n", | |
" \"function\": {\n", | |
" \"name\": \"get_current_weather\",\n", | |
" \"description\": \"Get the current weather\",\n", | |
" \"parameters\": {\n", | |
" \"type\": \"object\",\n", | |
" \"properties\": {\n", | |
" \"location\": {\n", | |
" \"type\": \"string\",\n", | |
" \"description\": \"The city and state, e.g. San Francisco, CA\",\n", | |
" },\n", | |
" \"format\": {\n", | |
" \"type\": \"string\",\n", | |
" \"enum\": [\"celsius\", \"fahrenheit\"],\n", | |
" \"description\": \"The temperature unit to use. Infer this from the users location.\",\n", | |
" },\n", | |
" },\n", | |
" \"required\": [\"location\", \"format\"],\n", | |
" },\n", | |
" }\n", | |
" }\n", | |
"]" | |
], | |
"metadata": { | |
"id": "okcY1lXbzawj" | |
}, | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"messages = [\n", | |
" {\"role\": \"user\", \"content\": \"What's the temperature in Paris?\"}\n", | |
"]" | |
], | |
"metadata": { | |
"id": "muvaWHe21Dhr" | |
}, | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"tokenizer.apply_chat_template(\n", | |
" messages,\n", | |
" tools=tools,\n", | |
" tokenize=False\n", | |
")" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 36 | |
}, | |
"id": "r9vHRI-qz6x2", | |
"outputId": "4b9416b1-6a09-4d57-813d-2ea5054721f3" | |
}, | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"\"<|begin_of_text|><|im_start|>user\\nWhat's the temperature in Paris?<|im_end|>\\n\"" | |
], | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
} | |
}, | |
"metadata": {}, | |
"execution_count": 4 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment