quick_start/legacy/04_tokens_and_usage.ipynb (161 lines of code) (raw):

{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Tokens" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total number of tokens: 9\n", "[79207, 5377, 15836, 2532, 374, 3331, 16528, 1457, 0]\n", "Tokens : ['Azure', ' Open', 'AI', ' service', ' is', ' General', ' Available', ' now', '!']\n" ] } ], "source": [ "import os\n", "import openai\n", "import tiktoken\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "\n", "openai.api_type = \"azure\"\n", "openai.api_version = os.getenv(\"OPENAI_API_VERSION\")\n", "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", "openai.api_base = os.getenv(\"OPENAI_API_BASE\")\n", "\n", "CHAT_COMPLETIONS_MODEL = os.getenv('CHAT_COMPLETION_NAME')\n", "\n", "encoding=tiktoken.encoding_for_model(\"gpt-3.5-turbo\")\n", "prompt = \"Azure OpenAI service is General Available now!\"\n", "tokens = encoding.encode(prompt)\n", "print('Total number of tokens:', len(tokens))\n", "print('Tokens :', tokens)\n", "print('Words : ', [encoding.decode([t]) for t in tokens])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "response = openai.ChatCompletion.create(\n", " engine=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": prompt}],\n", " max_tokens=60,\n", " n=2,\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Show 2 returned results" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================== ANSWER #1 ==============================\n", "That's great news! The Azure OpenAI service being generally available means that developers can now access and use it with greater ease and confidence. If you have any questions about getting started with the service or need assistance with anything related to it, feel free to ask!\n", "============================== ANSWER #2 ==============================\n", "That's great news! The availability of Azure OpenAI service will surely provide more opportunities for developers and organizations to harness the power of AI. It's a significant advancement in the field of artificial intelligence and machine learning. If you have any questions about using the Azure OpenAI service, feel free to ask\n" ] } ], "source": [ "print('='*30, 'ANSWER #1', '='*30)\n", "print(response['choices'][0]['message']['content'])\n", "print('='*30, 'ANSWER #2', '='*30)\n", "print(response['choices'][1]['message']['content'])\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Usage" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<OpenAIObject at 0x237453de270> JSON: {\n", " \"prompt_tokens\": 26,\n", " \"completion_tokens\": 113,\n", " \"total_tokens\": 139\n", "}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response['usage']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "azureml_py310_sdkv2", "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.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "2139c70ac98f3202d028164a545621647e07f47fd6f5d8ac55cf952bf7c15ed1" } } }, "nbformat": 4, "nbformat_minor": 2 }