Created
January 4, 2024 20:08
-
-
Save PhilipMay/6b5dfeeefc90b12e837c2f40d5bfce1b to your computer and use it in GitHub Desktop.
new-open-ai-azure-chat.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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "134cb3e4-05ef-447f-aabc-eacb9b7414cd", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import os\n", | |
| "from openai import AzureOpenAI\n", | |
| "\n", | |
| "OPENAI_3_5_API_KEY=\"secret\"\n", | |
| "\n", | |
| "client = AzureOpenAI(\n", | |
| " api_key=OPENAI_3_5_API_KEY,\n", | |
| " #api_type=\"azure\",\n", | |
| " api_version=\"2023-05-15\",\n", | |
| " azure_endpoint=\"https://secret.openai.azure.com/\", \n", | |
| ")\n", | |
| "\n", | |
| "chat_completion = client.chat.completions.create(\n", | |
| " messages=[\n", | |
| " {\n", | |
| " \"role\": \"user\",\n", | |
| " \"content\": \"Say this is a test.\",\n", | |
| " }\n", | |
| " ],\n", | |
| " model=\"gpt-35-turbo\",\n", | |
| ")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "62c7135e-ce04-4206-a670-7197e22a0fac", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "ChatCompletion(id='chatcmpl-8dOCDgdK7m2pZrRjR1VoaWIJe0ZaV', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='This is a test.', role='assistant', function_call=None, tool_calls=None))], created=1704398493, model='gpt-35-turbo', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=5, prompt_tokens=14, total_tokens=19))" | |
| ] | |
| }, | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chat_completion" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "38c1105d-f91a-41d9-a0a5-88f8155c9d08", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='This is a test.', role='assistant', function_call=None, tool_calls=None))]" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chat_completion.choices" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "f36ce168-c6e0-4840-93f8-7f2cb268844f", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='This is a test.', role='assistant', function_call=None, tool_calls=None))" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chat_completion.choices[0]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "id": "992bb830-3b16-46da-8f43-b479d3736993", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'stop'" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chat_completion.choices[0].finish_reason" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "id": "0c4e653c-1403-44ae-a94e-7e23c9391ea0", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'This is a test.'" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chat_completion.choices[0].message.content" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "id": "106104bf-df30-4c1b-86ce-9d73945e03e7", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'gpt-35-turbo'" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chat_completion.model" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "id": "9c676b94-0f0a-4016-b5fb-de7706bf04ee", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "CompletionUsage(completion_tokens=5, prompt_tokens=14, total_tokens=19)" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chat_completion.usage\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "id": "f6808e18-40bb-47dd-a09c-0d1405ed919d", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'completion_tokens': 5, 'prompt_tokens': 14, 'total_tokens': 19}" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "chat_completion.usage.model_dump()\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "id": "3c07a83c-242f-42fd-bd79-5657764c75a5", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "openai.types.chat.chat_completion.ChatCompletion" | |
| ] | |
| }, | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(chat_completion)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "id": "52b86aaf-7d2b-432f-9ea0-eac6c0893cdd", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'id': 'chatcmpl-8dOCDgdK7m2pZrRjR1VoaWIJe0ZaV',\n", | |
| " 'choices': [{'finish_reason': 'stop',\n", | |
| " 'index': 0,\n", | |
| " 'logprobs': None,\n", | |
| " 'message': {'content': 'This is a test.',\n", | |
| " 'role': 'assistant',\n", | |
| " 'function_call': None,\n", | |
| " 'tool_calls': None}}],\n", | |
| " 'created': 1704398493,\n", | |
| " 'model': 'gpt-35-turbo',\n", | |
| " 'object': 'chat.completion',\n", | |
| " 'system_fingerprint': None,\n", | |
| " 'usage': {'completion_tokens': 5, 'prompt_tokens': 14, 'total_tokens': 19}}" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "d = chat_completion.model_dump()\n", | |
| "d" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "id": "3db1806f-f424-425b-82c3-052ee58b8114", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "dict" | |
| ] | |
| }, | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "type(d)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "e7a8b415-b6bd-4d39-8021-1916e2414b61", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "b7d10037-5730-41eb-88a2-4a7c25a59a07", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python [conda env:openai_test]", | |
| "language": "python", | |
| "name": "conda-env-openai_test-py" | |
| }, | |
| "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.9.18" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment