From b468b422c67f12f19a4257fa64650ae9ab8a98c0 Mon Sep 17 00:00:00 2001 From: Dhruvj07 Date: Wed, 26 Feb 2025 20:38:19 +0530 Subject: [PATCH 1/4] feat: Code for function calling using 01/o3 models --- .../openai/openai-azure-fun_calling.ipynb | 253 ++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 examples/openai/openai-azure-fun_calling.ipynb diff --git a/examples/openai/openai-azure-fun_calling.ipynb b/examples/openai/openai-azure-fun_calling.ipynb new file mode 100644 index 0000000..73227c0 --- /dev/null +++ b/examples/openai/openai-azure-fun_calling.ipynb @@ -0,0 +1,253 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# OpenAI Notebook \n", + "This notebook demonstrates the usage of synchronous and asynchronous OpenAI endpoints integrated with Javelin.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from dotenv import load_dotenv\n", + "\n", + "load_dotenv()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Initializing Synchronous OpenAI client...\n", + "\n", + "OpenAI: 1 - Chat completions\n" + ] + }, + { + "ename": "APIConnectionError", + "evalue": "Connection error.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mLocalProtocolError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_transports/default.py:72\u001b[0m, in \u001b[0;36mmap_httpcore_exceptions\u001b[0;34m()\u001b[0m\n\u001b[1;32m 71\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 72\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_transports/default.py:236\u001b[0m, in \u001b[0;36mHTTPTransport.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 235\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_httpcore_exceptions():\n\u001b[0;32m--> 236\u001b[0m resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_pool\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreq\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 238\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(resp\u001b[38;5;241m.\u001b[39mstream, typing\u001b[38;5;241m.\u001b[39mIterable)\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py:256\u001b[0m, in \u001b[0;36mConnectionPool.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_close_connections(closing)\n\u001b[0;32m--> 256\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 258\u001b[0m \u001b[38;5;66;03m# Return the response. Note that in this case we still have to manage\u001b[39;00m\n\u001b[1;32m 259\u001b[0m \u001b[38;5;66;03m# the point at which the response is closed.\u001b[39;00m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py:236\u001b[0m, in \u001b[0;36mConnectionPool.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 234\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 235\u001b[0m \u001b[38;5;66;03m# Send the request on the assigned connection.\u001b[39;00m\n\u001b[0;32m--> 236\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 237\u001b[0m \u001b[43m \u001b[49m\u001b[43mpool_request\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\n\u001b[1;32m 238\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 239\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m ConnectionNotAvailable:\n\u001b[1;32m 240\u001b[0m \u001b[38;5;66;03m# In some cases a connection may initially be available to\u001b[39;00m\n\u001b[1;32m 241\u001b[0m \u001b[38;5;66;03m# handle a request, but then become unavailable.\u001b[39;00m\n\u001b[1;32m 242\u001b[0m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[1;32m 243\u001b[0m \u001b[38;5;66;03m# In this case we clear the connection and try again.\u001b[39;00m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py:103\u001b[0m, in \u001b[0;36mHTTPConnection.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 101\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n\u001b[0;32m--> 103\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/http11.py:136\u001b[0m, in \u001b[0;36mHTTP11Connection.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 135\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_response_closed()\n\u001b[0;32m--> 136\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/http11.py:86\u001b[0m, in \u001b[0;36mHTTP11Connection.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 83\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m Trace(\n\u001b[1;32m 84\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msend_request_headers\u001b[39m\u001b[38;5;124m\"\u001b[39m, logger, request, kwargs\n\u001b[1;32m 85\u001b[0m ) \u001b[38;5;28;01mas\u001b[39;00m trace:\n\u001b[0;32m---> 86\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_request_headers\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 87\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m Trace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msend_request_body\u001b[39m\u001b[38;5;124m\"\u001b[39m, logger, request, kwargs) \u001b[38;5;28;01mas\u001b[39;00m trace:\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/http11.py:144\u001b[0m, in \u001b[0;36mHTTP11Connection._send_request_headers\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 142\u001b[0m timeout \u001b[38;5;241m=\u001b[39m timeouts\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwrite\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m--> 144\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_exceptions({h11\u001b[38;5;241m.\u001b[39mLocalProtocolError: LocalProtocolError}):\n\u001b[1;32m 145\u001b[0m event \u001b[38;5;241m=\u001b[39m h11\u001b[38;5;241m.\u001b[39mRequest(\n\u001b[1;32m 146\u001b[0m method\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39mmethod,\n\u001b[1;32m 147\u001b[0m target\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39murl\u001b[38;5;241m.\u001b[39mtarget,\n\u001b[1;32m 148\u001b[0m headers\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39mheaders,\n\u001b[1;32m 149\u001b[0m )\n", + "File \u001b[0;32m/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py:158\u001b[0m, in \u001b[0;36m_GeneratorContextManager.__exit__\u001b[0;34m(self, typ, value, traceback)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 158\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgen\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrow\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 160\u001b[0m \u001b[38;5;66;03m# Suppress StopIteration *unless* it's the same exception that\u001b[39;00m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;66;03m# was passed to throw(). This prevents a StopIteration\u001b[39;00m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# raised inside the \"with\" statement from being suppressed.\u001b[39;00m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_exceptions.py:14\u001b[0m, in \u001b[0;36mmap_exceptions\u001b[0;34m(map)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(exc, from_exc):\n\u001b[0;32m---> 14\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m to_exc(exc) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mexc\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m\n", + "\u001b[0;31mLocalProtocolError\u001b[0m: Illegal header value b'Bearer '", + "\nThe above exception was the direct cause of the following exception:\n", + "\u001b[0;31mLocalProtocolError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:996\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 995\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 996\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 997\u001b[0m \u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 998\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_should_stream_response_body\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 999\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1000\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1001\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m httpx\u001b[38;5;241m.\u001b[39mTimeoutException \u001b[38;5;28;01mas\u001b[39;00m err:\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_client.py:926\u001b[0m, in \u001b[0;36mClient.send\u001b[0;34m(self, request, stream, auth, follow_redirects)\u001b[0m\n\u001b[1;32m 924\u001b[0m auth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_build_request_auth(request, auth)\n\u001b[0;32m--> 926\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_handling_auth\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 927\u001b[0m \u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 928\u001b[0m \u001b[43m \u001b[49m\u001b[43mauth\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mauth\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 929\u001b[0m \u001b[43m \u001b[49m\u001b[43mfollow_redirects\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfollow_redirects\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 930\u001b[0m \u001b[43m \u001b[49m\u001b[43mhistory\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 931\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 932\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_client.py:954\u001b[0m, in \u001b[0;36mClient._send_handling_auth\u001b[0;34m(self, request, auth, follow_redirects, history)\u001b[0m\n\u001b[1;32m 953\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m--> 954\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_handling_redirects\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 955\u001b[0m \u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 956\u001b[0m \u001b[43m \u001b[49m\u001b[43mfollow_redirects\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfollow_redirects\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 957\u001b[0m \u001b[43m \u001b[49m\u001b[43mhistory\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhistory\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 958\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 959\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_client.py:991\u001b[0m, in \u001b[0;36mClient._send_handling_redirects\u001b[0;34m(self, request, follow_redirects, history)\u001b[0m\n\u001b[1;32m 989\u001b[0m hook(request)\n\u001b[0;32m--> 991\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_single_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 992\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_client.py:1027\u001b[0m, in \u001b[0;36mClient._send_single_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 1026\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m request_context(request\u001b[38;5;241m=\u001b[39mrequest):\n\u001b[0;32m-> 1027\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mtransport\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1029\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(response\u001b[38;5;241m.\u001b[39mstream, SyncByteStream)\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_transports/default.py:235\u001b[0m, in \u001b[0;36mHTTPTransport.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 223\u001b[0m req \u001b[38;5;241m=\u001b[39m httpcore\u001b[38;5;241m.\u001b[39mRequest(\n\u001b[1;32m 224\u001b[0m method\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39mmethod,\n\u001b[1;32m 225\u001b[0m url\u001b[38;5;241m=\u001b[39mhttpcore\u001b[38;5;241m.\u001b[39mURL(\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 233\u001b[0m extensions\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39mextensions,\n\u001b[1;32m 234\u001b[0m )\n\u001b[0;32m--> 235\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_httpcore_exceptions():\n\u001b[1;32m 236\u001b[0m resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_pool\u001b[38;5;241m.\u001b[39mhandle_request(req)\n", + "File \u001b[0;32m/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py:158\u001b[0m, in \u001b[0;36m_GeneratorContextManager.__exit__\u001b[0;34m(self, typ, value, traceback)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 158\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgen\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrow\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 160\u001b[0m \u001b[38;5;66;03m# Suppress StopIteration *unless* it's the same exception that\u001b[39;00m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;66;03m# was passed to throw(). This prevents a StopIteration\u001b[39;00m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# raised inside the \"with\" statement from being suppressed.\u001b[39;00m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_transports/default.py:89\u001b[0m, in \u001b[0;36mmap_httpcore_exceptions\u001b[0;34m()\u001b[0m\n\u001b[1;32m 88\u001b[0m message \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(exc)\n\u001b[0;32m---> 89\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m mapped_exc(message) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mexc\u001b[39;00m\n", + "\u001b[0;31mLocalProtocolError\u001b[0m: Illegal header value b'Bearer '", + "\nThe above exception was the direct cause of the following exception:\n", + "\u001b[0;31mAPIConnectionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 37\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[38;5;66;03m# --- Call OpenAI Endpoints ---\u001b[39;00m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOpenAI: 1 - Chat completions\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 37\u001b[0m chat_completions \u001b[38;5;241m=\u001b[39m \u001b[43mopenai_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mchat\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompletions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 38\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgpt-3.5-turbo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 39\u001b[0m \u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrole\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muser\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcontent\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mWhat is machine learning?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 40\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28mprint\u001b[39m(chat_completions\u001b[38;5;241m.\u001b[39mmodel_dump_json(indent\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m))\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/javelin_sdk/client.py:142\u001b[0m, in \u001b[0;36mJavelinClient.register_provider..create_patched_method..patched_method\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 139\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m model \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(openai_client, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_custom_headers\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 140\u001b[0m openai_client\u001b[38;5;241m.\u001b[39m_custom_headers[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx-javelin-model\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m model\n\u001b[0;32m--> 142\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43moriginal_method\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 143\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_utils/_utils.py:279\u001b[0m, in \u001b[0;36mrequired_args..inner..wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 277\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMissing required argument: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquote(missing[\u001b[38;5;241m0\u001b[39m])\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 278\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg)\n\u001b[0;32m--> 279\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/resources/chat/completions.py:863\u001b[0m, in \u001b[0;36mCompletions.create\u001b[0;34m(self, messages, model, audio, frequency_penalty, function_call, functions, logit_bias, logprobs, max_completion_tokens, max_tokens, metadata, modalities, n, parallel_tool_calls, prediction, presence_penalty, reasoning_effort, response_format, seed, service_tier, stop, store, stream, stream_options, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout)\u001b[0m\n\u001b[1;32m 821\u001b[0m \u001b[38;5;129m@required_args\u001b[39m([\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessages\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m], [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessages\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstream\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m 822\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mcreate\u001b[39m(\n\u001b[1;32m 823\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 860\u001b[0m timeout: \u001b[38;5;28mfloat\u001b[39m \u001b[38;5;241m|\u001b[39m httpx\u001b[38;5;241m.\u001b[39mTimeout \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m|\u001b[39m NotGiven \u001b[38;5;241m=\u001b[39m NOT_GIVEN,\n\u001b[1;32m 861\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ChatCompletion \u001b[38;5;241m|\u001b[39m Stream[ChatCompletionChunk]:\n\u001b[1;32m 862\u001b[0m validate_response_format(response_format)\n\u001b[0;32m--> 863\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_post\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 864\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m/chat/completions\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 865\u001b[0m \u001b[43m \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmaybe_transform\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 866\u001b[0m \u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m 867\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmessages\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 868\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmodel\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 869\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43maudio\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43maudio\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 870\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfrequency_penalty\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfrequency_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 871\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfunction_call\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunction_call\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 872\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfunctions\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunctions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 873\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mlogit_bias\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogit_bias\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 874\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mlogprobs\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 875\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmax_completion_tokens\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_completion_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 876\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmax_tokens\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 877\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmetadata\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 878\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmodalities\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodalities\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 879\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mn\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 880\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mparallel_tool_calls\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mparallel_tool_calls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 881\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mprediction\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mprediction\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 882\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpresence_penalty\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpresence_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 883\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mreasoning_effort\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mreasoning_effort\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 884\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mresponse_format\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 885\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mseed\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mseed\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 886\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mservice_tier\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mservice_tier\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 887\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstop\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 888\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstore\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstore\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 889\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstream\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 890\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstream_options\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 891\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtemperature\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtemperature\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 892\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtool_choice\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtool_choice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 893\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtools\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtools\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 894\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtop_logprobs\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_logprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 895\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtop_p\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_p\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 896\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muser\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43muser\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 897\u001b[0m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 898\u001b[0m \u001b[43m \u001b[49m\u001b[43mcompletion_create_params\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCompletionCreateParams\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 899\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 900\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmake_request_options\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 901\u001b[0m \u001b[43m \u001b[49m\u001b[43mextra_headers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_headers\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_query\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_query\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_body\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\n\u001b[1;32m 902\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 903\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mChatCompletion\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 904\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 905\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mStream\u001b[49m\u001b[43m[\u001b[49m\u001b[43mChatCompletionChunk\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 906\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1283\u001b[0m, in \u001b[0;36mSyncAPIClient.post\u001b[0;34m(self, path, cast_to, body, options, files, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1269\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mpost\u001b[39m(\n\u001b[1;32m 1270\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1271\u001b[0m path: \u001b[38;5;28mstr\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1278\u001b[0m stream_cls: \u001b[38;5;28mtype\u001b[39m[_StreamT] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1279\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ResponseT \u001b[38;5;241m|\u001b[39m _StreamT:\n\u001b[1;32m 1280\u001b[0m opts \u001b[38;5;241m=\u001b[39m FinalRequestOptions\u001b[38;5;241m.\u001b[39mconstruct(\n\u001b[1;32m 1281\u001b[0m method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpost\u001b[39m\u001b[38;5;124m\"\u001b[39m, url\u001b[38;5;241m=\u001b[39mpath, json_data\u001b[38;5;241m=\u001b[39mbody, files\u001b[38;5;241m=\u001b[39mto_httpx_files(files), \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions\n\u001b[1;32m 1282\u001b[0m )\n\u001b[0;32m-> 1283\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m cast(ResponseT, \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mopts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m)\u001b[49m)\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:960\u001b[0m, in \u001b[0;36mSyncAPIClient.request\u001b[0;34m(self, cast_to, options, remaining_retries, stream, stream_cls)\u001b[0m\n\u001b[1;32m 957\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 958\u001b[0m retries_taken \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m--> 960\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 961\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 962\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 963\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 964\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 965\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 966\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1020\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1017\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEncountered Exception\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 1019\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m remaining_retries \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m-> 1020\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_retry_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1021\u001b[0m \u001b[43m \u001b[49m\u001b[43minput_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1022\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1024\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1025\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1026\u001b[0m \u001b[43m \u001b[49m\u001b[43mresponse_headers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 1027\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1029\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRaising connection error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1030\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m APIConnectionError(request\u001b[38;5;241m=\u001b[39mrequest) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01merr\u001b[39;00m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1098\u001b[0m, in \u001b[0;36mSyncAPIClient._retry_request\u001b[0;34m(self, options, cast_to, retries_taken, response_headers, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1094\u001b[0m \u001b[38;5;66;03m# In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a\u001b[39;00m\n\u001b[1;32m 1095\u001b[0m \u001b[38;5;66;03m# different thread if necessary.\u001b[39;00m\n\u001b[1;32m 1096\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(timeout)\n\u001b[0;32m-> 1098\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1099\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1100\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1101\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1102\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1103\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1104\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1020\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1017\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEncountered Exception\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 1019\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m remaining_retries \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m-> 1020\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_retry_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1021\u001b[0m \u001b[43m \u001b[49m\u001b[43minput_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1022\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1024\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1025\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1026\u001b[0m \u001b[43m \u001b[49m\u001b[43mresponse_headers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 1027\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1029\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRaising connection error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1030\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m APIConnectionError(request\u001b[38;5;241m=\u001b[39mrequest) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01merr\u001b[39;00m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1098\u001b[0m, in \u001b[0;36mSyncAPIClient._retry_request\u001b[0;34m(self, options, cast_to, retries_taken, response_headers, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1094\u001b[0m \u001b[38;5;66;03m# In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a\u001b[39;00m\n\u001b[1;32m 1095\u001b[0m \u001b[38;5;66;03m# different thread if necessary.\u001b[39;00m\n\u001b[1;32m 1096\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(timeout)\n\u001b[0;32m-> 1098\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1099\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1100\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1101\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1102\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1103\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1104\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1030\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_retry_request(\n\u001b[1;32m 1021\u001b[0m input_options,\n\u001b[1;32m 1022\u001b[0m cast_to,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1026\u001b[0m response_headers\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1027\u001b[0m )\n\u001b[1;32m 1029\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRaising connection error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 1030\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m APIConnectionError(request\u001b[38;5;241m=\u001b[39mrequest) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01merr\u001b[39;00m\n\u001b[1;32m 1032\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\n\u001b[1;32m 1033\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mHTTP Response: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%i\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 1034\u001b[0m request\u001b[38;5;241m.\u001b[39mmethod,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1038\u001b[0m response\u001b[38;5;241m.\u001b[39mheaders,\n\u001b[1;32m 1039\u001b[0m )\n\u001b[1;32m 1040\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrequest_id: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, response\u001b[38;5;241m.\u001b[39mheaders\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mx-request-id\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n", + "\u001b[0;31mAPIConnectionError\u001b[0m: Connection error." + ] + } + ], + "source": [ + "import json\n", + "import os\n", + "import sys\n", + "import asyncio\n", + "from openai import OpenAI\n", + "from openai import AsyncOpenAI\n", + "from openai import AzureOpenAI # Imported for consistency, though not used in this notebook\n", + "import dotenv\n", + "from javelin_sdk import (\n", + " JavelinClient,\n", + " JavelinConfig,\n", + ")\n", + "\n", + "# -------------------------------\n", + "# Synchronous OpenAI Example\n", + "# -------------------------------\n", + "print(\"Initializing Synchronous OpenAI client...\")\n", + "\n", + "# Create OpenAI client using the API key from the environment variable\n", + "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "print(openai_api_key)\n", + "openai_client = OpenAI(api_key=openai_api_key)\n", + "\n", + "# Initialize Javelin Client with your API key and base URL\n", + "javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", + "config = JavelinConfig( \n", + " javelin_api_key=javelin_api_key,\n", + ")\n", + "client = JavelinClient(config)\n", + "\n", + "# Register the OpenAI client with Javelin using the route name \"openai\"\n", + "client.register_openai(openai_client, route_name=\"openai-univ\")\n", + "\n", + "# --- Call OpenAI Endpoints ---\n", + "\n", + "print(\"OpenAI: 1 - Chat completions\")\n", + "chat_completions = openai_client.chat.completions.create(\n", + " model=\"gpt-3.5-turbo\",\n", + " messages=[{\"role\": \"user\", \"content\": \"What is machine learning?\"}],\n", + ")\n", + "print(chat_completions.model_dump_json(indent=2))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "print(\"OpenAI: 2 - Completions\")\n", + "completions = openai_client.completions.create(\n", + " model=\"gpt-3.5-turbo-instruct\",\n", + " prompt=\"What is machine learning?\",\n", + " max_tokens=7,\n", + " temperature=0\n", + ")\n", + "print(completions.model_dump_json(indent=2))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "print(\"OpenAI: 3 - Embeddings\")\n", + "embeddings = openai_client.embeddings.create(\n", + " model=\"text-embedding-ada-002\",\n", + " input=\"The food was delicious and the waiter...\",\n", + " encoding_format=\"float\"\n", + ")\n", + "print(embeddings.model_dump_json(indent=2))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "print(\"OpenAI: 4 - Streaming\")\n", + "stream = openai_client.chat.completions.create(\n", + " messages=[\n", + " {\"role\": \"user\", \"content\": \"Say this is a test\"}\n", + " ],\n", + " model=\"gpt-4o\",\n", + " stream=True,\n", + ")\n", + "for chunk in stream:\n", + " # Debug print: show each streaming chunk\n", + " print(chunk.choices[0].delta.content or \"\", end=\"\")\n", + "\n", + "# Prints two blank lines for clarity\n", + "print(\"\\n\\n\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# -------------------------------\n", + "# Asynchronous OpenAI Example\n", + "# -------------------------------\n", + "print(\"Initializing AsyncOpenAI client...\")\n", + "\n", + "# Create AsyncOpenAI client\n", + "openai_async_client = AsyncOpenAI(\n", + " api_key=os.environ.get(\"OPENAI_API_KEY\"), # This is the default and can be omitted\n", + ")\n", + "\n", + "# Reinitialize Javelin Client for Async usage (using the same config)\n", + "javelin_api_key = os.getenv('JAVELIN_API_KEY')\n", + "config = JavelinConfig(\n", + " javelin_api_key=javelin_api_key,\n", + ")\n", + "client = JavelinClient(config)\n", + "client.register_openai(openai_async_client, route_name=\"openai\")\n", + "\n", + "async def main() -> None:\n", + " chat_completion = await openai_async_client.chat.completions.create(\n", + " messages=[\n", + " {\"role\": \"user\", \"content\": \"Say this is a test\"}\n", + " ],\n", + " model=\"gpt-4o\",\n", + " )\n", + " print(chat_completion.model_dump_json(indent=2))\n", + "\n", + "print(\"AsyncOpenAI: 5 - Chat completions\")\n", + "asyncio.run(main())\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "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.12.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From fe2c9fbbb68bf16b8895da70c7d92051d57f206f Mon Sep 17 00:00:00 2001 From: Dhruvj07 Date: Wed, 26 Feb 2025 20:47:28 +0530 Subject: [PATCH 2/4] fix:wrong file pushed --- .../openai/openai-azure-fun_calling.ipynb | 1233 +++++++++++++++-- examples/openai/openai_universal-rout.ipynb | 179 --- 2 files changed, 1086 insertions(+), 326 deletions(-) delete mode 100644 examples/openai/openai_universal-rout.ipynb diff --git a/examples/openai/openai-azure-fun_calling.ipynb b/examples/openai/openai-azure-fun_calling.ipynb index 73227c0..a9331d1 100644 --- a/examples/openai/openai-azure-fun_calling.ipynb +++ b/examples/openai/openai-azure-fun_calling.ipynb @@ -4,229 +4,1168 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# OpenAI Notebook \n", - "This notebook demonstrates the usage of synchronous and asynchronous OpenAI endpoints integrated with Javelin.\n" + "# OpenAI – Unified Endpoint Examples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Non-Streaming with Function Calling" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 148, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "OpenAI Non-Streaming Response:\n", + "{\n", + " \"id\": \"chatcmpl-B5BpQmrAdVu4uOUUEniPwLvD9omRu\",\n", + " \"choices\": [\n", + " {\n", + " \"finish_reason\": \"function_call\",\n", + " \"index\": 0,\n", + " \"logprobs\": null,\n", + " \"message\": {\n", + " \"content\": null,\n", + " \"refusal\": null,\n", + " \"role\": \"assistant\",\n", + " \"audio\": null,\n", + " \"function_call\": {\n", + " \"arguments\": \"{\\\"location\\\": \\\"New York, NY\\\", \\\"unit\\\": \\\"fahrenheit\\\"}\",\n", + " \"name\": \"get_current_weather\"\n", + " },\n", + " \"tool_calls\": null\n", + " }\n", + " }\n", + " ],\n", + " \"created\": 1740576808,\n", + " \"model\": \"o3-mini-2025-01-31\",\n", + " \"object\": \"chat.completion\",\n", + " \"service_tier\": \"default\",\n", + " \"system_fingerprint\": \"fp_42bfad963b\",\n", + " \"usage\": {\n", + " \"completion_tokens\": 98,\n", + " \"prompt_tokens\": 76,\n", + " \"total_tokens\": 174,\n", + " \"completion_tokens_details\": {\n", + " \"accepted_prediction_tokens\": 0,\n", + " \"audio_tokens\": 0,\n", + " \"reasoning_tokens\": 64,\n", + " \"rejected_prediction_tokens\": 0\n", + " },\n", + " \"prompt_tokens_details\": {\n", + " \"audio_tokens\": 0,\n", + " \"cached_tokens\": 0\n", + " }\n", + " },\n", + " \"javelin\": {\n", + " \"archive_enabled\": true,\n", + " \"correlation_id\": \"01JN17CQTKWFKQMMRT2XNQ1DS9\",\n", + " \"model_endpoint_url\": \"https://api.openai.com/v1/chat/completions\",\n", + " \"model_latency\": \"1.88136955s\",\n", + " \"model_name\": \"o3-mini\",\n", + " \"processor_outputs\": {\n", + " \"request.chain.archive_processor_20250226133330.693491759\": {\n", + " \"duration\": \"7.84936ms\",\n", + " \"success\": \"successfully archived memory\"\n", + " },\n", + " \"request.chain.checkphish_processor_20250226133330.693396621\": {\n", + " \"duration\": \"93.431µs\"\n", + " },\n", + " \"request.chain.dlp_gcp_processor_20250226133330.693456416\": {\n", + " \"duration\": \"92.95µs\",\n", + " \"skipped\": \"warn: sensitive data protection is disabled for route:openai_univ\"\n", + " },\n", + " \"request.chain.promptinjectiondetection_processor_20250226133330.693555231\": {\n", + " \"duration\": \"57.604µs\",\n", + " \"skipped\": \"warn: prompt safety is disabled for route:openai_univ\"\n", + " },\n", + " \"request.chain.ratelimit_processor_20250226133330.693406418\": {\n", + " \"duration\": \"805.927µs\"\n", + " },\n", + " \"request.chain.secrets_processor_20250226133330.693475267\": {\n", + " \"duration\": \"16.301µs\"\n", + " },\n", + " \"request.chain.trustsafety_processor_20250226133330.693576559\": {\n", + " \"duration\": \"59.649µs\",\n", + " \"skipped\": \"warn: trust safety is disabled for route:openai_univ\"\n", + " },\n", + " \"response.chain.response_processor_20250226133330.693605797\": {\n", + " \"duration\": \"0s\"\n", + " },\n", + " \"response.chain.securityfilters_processor_20250226133330.693529078\": {\n", + " \"duration\": \"94.064µs\",\n", + " \"skipped\": \"warn: failed to get response from body:no content found for specified paths\"\n", + " },\n", + " \"response.chain.trustsafety_processor_20250226133330.693511739\": {\n", + " \"duration\": \"43.248µs\",\n", + " \"skipped\": \"warn: trust safety is disabled for route:openai_univ\"\n", + " }\n", + " },\n", + " \"route_name\": \"openai_univ\"\n", + " }\n", + "}\n" + ] } ], "source": [ + "import os\n", + "from openai import OpenAI\n", + "from javelin_sdk import JavelinClient, JavelinConfig\n", "from dotenv import load_dotenv\n", "\n", - "load_dotenv()" + "load_dotenv()\n", + "\n", + "def init_openai_client():\n", + " api_key = os.getenv(\"OPENAI_API_KEY\")\n", + " return OpenAI(api_key=api_key)\n", + "\n", + "def init_javelin_client(openai_client, route_name=\"openai_univ\"):\n", + " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", + " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", + " client = JavelinClient(config)\n", + " client.register_openai(openai_client, route_name=route_name)\n", + " return client\n", + "\n", + "def openai_function_call_non_stream():\n", + " client = init_openai_client()\n", + " # Register with the unified endpoint\n", + " init_javelin_client(client)\n", + " response = client.chat.completions.create(\n", + " model=\"o3-mini\", # Latest o1 model\n", + " messages=[\n", + " {\"role\": \"user\", \"content\": \"What is the current weather in New York?\"}\n", + " ],\n", + " functions=[\n", + " {\n", + " \"name\": \"get_current_weather\",\n", + " \"description\": \"Retrieves current weather information\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"location\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"City and state (e.g., New York, NY)\"\n", + " },\n", + " \"unit\": {\n", + " \"type\": \"string\",\n", + " \"enum\": [\"celsius\", \"fahrenheit\"]\n", + " }\n", + " },\n", + " \"required\": [\"location\"]\n", + " }\n", + " }\n", + " ],\n", + " function_call=\"auto\"\n", + " )\n", + " print(\"OpenAI Non-Streaming Response:\")\n", + " print(response.model_dump_json(indent=2))\n", + "\n", + "if __name__ == \"__main__\":\n", + " openai_function_call_non_stream()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Streaming with Function Calling" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Initializing Synchronous OpenAI client...\n", - "\n", - "OpenAI: 1 - Chat completions\n" + "stream>>> \n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='', name='tell_fun_fact'), refusal=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='{\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='fact', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\":\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='G', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='ira', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='ffes', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' have', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' black', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' tongues', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' so', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' they', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' don', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='’t', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' get', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' sun', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='burn', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='ed', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' when', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' grasp', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='ing', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' leaves', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' in', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' the', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' hot', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' sun', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='!\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='}', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None), finish_reason='function_call', index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", + "OpenAI Streaming Response:\n", + "\n" ] - }, + } + ], + "source": [ + "import os\n", + "from openai import OpenAI\n", + "from javelin_sdk import JavelinClient, JavelinConfig\n", + "\n", + "def init_openai_client():\n", + " api_key = os.getenv(\"OPENAI_API_KEY\")\n", + " return OpenAI(api_key=api_key)\n", + "\n", + "def init_javelin_client(openai_client, route_name=\"openai_univ\"):\n", + " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", + " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", + " client = JavelinClient(config)\n", + " client.register_openai(openai_client, route_name=route_name)\n", + " return client\n", + "\n", + "def openai_function_call_stream():\n", + " client = init_openai_client()\n", + " init_javelin_client(client)\n", + " stream = client.chat.completions.create(\n", + " model=\"o1\",\n", + " messages=[\n", + " {\"role\": \"user\", \"content\": \"Tell me a fun fact and then call a function.\"}\n", + " ],\n", + " functions=[\n", + " {\n", + " \"name\": \"tell_fun_fact\",\n", + " \"description\": \"Returns a fun fact\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"fact\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"A fun fact about the topic\"\n", + " }\n", + " },\n", + " \"required\": [\"fact\"]\n", + " }\n", + " }\n", + " ],\n", + " function_call=\"auto\",\n", + " stream=True\n", + " )\n", + " collected = []\n", + " print(\"stream>>>\",stream)\n", + " for chunk in stream:\n", + " # Each chunk may contain a delta with part of the response\n", + " delta = chunk.choices[0].delta\n", + " print(chunk)\n", + " if hasattr(delta, \"content\") and delta.content:\n", + " collected.append(delta.content)\n", + " print(\"OpenAI Streaming Response:\")\n", + " print(\"\".join(collected))\n", + "\n", + "if __name__ == \"__main__\":\n", + " openai_function_call_stream()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Structured output calling Example" + ] + }, + { + "cell_type": "code", + "execution_count": 162, + "metadata": {}, + "outputs": [ { - "ename": "APIConnectionError", - "evalue": "Connection error.", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mLocalProtocolError\u001b[0m Traceback (most recent call last)", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_transports/default.py:72\u001b[0m, in \u001b[0;36mmap_httpcore_exceptions\u001b[0;34m()\u001b[0m\n\u001b[1;32m 71\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 72\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_transports/default.py:236\u001b[0m, in \u001b[0;36mHTTPTransport.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 235\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_httpcore_exceptions():\n\u001b[0;32m--> 236\u001b[0m resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_pool\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreq\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 238\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(resp\u001b[38;5;241m.\u001b[39mstream, typing\u001b[38;5;241m.\u001b[39mIterable)\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py:256\u001b[0m, in \u001b[0;36mConnectionPool.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_close_connections(closing)\n\u001b[0;32m--> 256\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 258\u001b[0m \u001b[38;5;66;03m# Return the response. Note that in this case we still have to manage\u001b[39;00m\n\u001b[1;32m 259\u001b[0m \u001b[38;5;66;03m# the point at which the response is closed.\u001b[39;00m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py:236\u001b[0m, in \u001b[0;36mConnectionPool.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 234\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 235\u001b[0m \u001b[38;5;66;03m# Send the request on the assigned connection.\u001b[39;00m\n\u001b[0;32m--> 236\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 237\u001b[0m \u001b[43m \u001b[49m\u001b[43mpool_request\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\n\u001b[1;32m 238\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 239\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m ConnectionNotAvailable:\n\u001b[1;32m 240\u001b[0m \u001b[38;5;66;03m# In some cases a connection may initially be available to\u001b[39;00m\n\u001b[1;32m 241\u001b[0m \u001b[38;5;66;03m# handle a request, but then become unavailable.\u001b[39;00m\n\u001b[1;32m 242\u001b[0m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[1;32m 243\u001b[0m \u001b[38;5;66;03m# In this case we clear the connection and try again.\u001b[39;00m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/connection.py:103\u001b[0m, in \u001b[0;36mHTTPConnection.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 101\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n\u001b[0;32m--> 103\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/http11.py:136\u001b[0m, in \u001b[0;36mHTTP11Connection.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 135\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_response_closed()\n\u001b[0;32m--> 136\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/http11.py:86\u001b[0m, in \u001b[0;36mHTTP11Connection.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 83\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m Trace(\n\u001b[1;32m 84\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msend_request_headers\u001b[39m\u001b[38;5;124m\"\u001b[39m, logger, request, kwargs\n\u001b[1;32m 85\u001b[0m ) \u001b[38;5;28;01mas\u001b[39;00m trace:\n\u001b[0;32m---> 86\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_request_headers\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 87\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m Trace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msend_request_body\u001b[39m\u001b[38;5;124m\"\u001b[39m, logger, request, kwargs) \u001b[38;5;28;01mas\u001b[39;00m trace:\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_sync/http11.py:144\u001b[0m, in \u001b[0;36mHTTP11Connection._send_request_headers\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 142\u001b[0m timeout \u001b[38;5;241m=\u001b[39m timeouts\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwrite\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m--> 144\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_exceptions({h11\u001b[38;5;241m.\u001b[39mLocalProtocolError: LocalProtocolError}):\n\u001b[1;32m 145\u001b[0m event \u001b[38;5;241m=\u001b[39m h11\u001b[38;5;241m.\u001b[39mRequest(\n\u001b[1;32m 146\u001b[0m method\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39mmethod,\n\u001b[1;32m 147\u001b[0m target\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39murl\u001b[38;5;241m.\u001b[39mtarget,\n\u001b[1;32m 148\u001b[0m headers\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39mheaders,\n\u001b[1;32m 149\u001b[0m )\n", - "File \u001b[0;32m/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py:158\u001b[0m, in \u001b[0;36m_GeneratorContextManager.__exit__\u001b[0;34m(self, typ, value, traceback)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 158\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgen\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrow\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 160\u001b[0m \u001b[38;5;66;03m# Suppress StopIteration *unless* it's the same exception that\u001b[39;00m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;66;03m# was passed to throw(). This prevents a StopIteration\u001b[39;00m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# raised inside the \"with\" statement from being suppressed.\u001b[39;00m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpcore/_exceptions.py:14\u001b[0m, in \u001b[0;36mmap_exceptions\u001b[0;34m(map)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(exc, from_exc):\n\u001b[0;32m---> 14\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m to_exc(exc) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mexc\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m\n", - "\u001b[0;31mLocalProtocolError\u001b[0m: Illegal header value b'Bearer '", - "\nThe above exception was the direct cause of the following exception:\n", - "\u001b[0;31mLocalProtocolError\u001b[0m Traceback (most recent call last)", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:996\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 995\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 996\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 997\u001b[0m \u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 998\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_should_stream_response_body\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 999\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1000\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1001\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m httpx\u001b[38;5;241m.\u001b[39mTimeoutException \u001b[38;5;28;01mas\u001b[39;00m err:\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_client.py:926\u001b[0m, in \u001b[0;36mClient.send\u001b[0;34m(self, request, stream, auth, follow_redirects)\u001b[0m\n\u001b[1;32m 924\u001b[0m auth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_build_request_auth(request, auth)\n\u001b[0;32m--> 926\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_handling_auth\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 927\u001b[0m \u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 928\u001b[0m \u001b[43m \u001b[49m\u001b[43mauth\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mauth\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 929\u001b[0m \u001b[43m \u001b[49m\u001b[43mfollow_redirects\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfollow_redirects\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 930\u001b[0m \u001b[43m \u001b[49m\u001b[43mhistory\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 931\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 932\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_client.py:954\u001b[0m, in \u001b[0;36mClient._send_handling_auth\u001b[0;34m(self, request, auth, follow_redirects, history)\u001b[0m\n\u001b[1;32m 953\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m--> 954\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_handling_redirects\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 955\u001b[0m \u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 956\u001b[0m \u001b[43m \u001b[49m\u001b[43mfollow_redirects\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfollow_redirects\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 957\u001b[0m \u001b[43m \u001b[49m\u001b[43mhistory\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhistory\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 958\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 959\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_client.py:991\u001b[0m, in \u001b[0;36mClient._send_handling_redirects\u001b[0;34m(self, request, follow_redirects, history)\u001b[0m\n\u001b[1;32m 989\u001b[0m hook(request)\n\u001b[0;32m--> 991\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_single_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 992\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_client.py:1027\u001b[0m, in \u001b[0;36mClient._send_single_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 1026\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m request_context(request\u001b[38;5;241m=\u001b[39mrequest):\n\u001b[0;32m-> 1027\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mtransport\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1029\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(response\u001b[38;5;241m.\u001b[39mstream, SyncByteStream)\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_transports/default.py:235\u001b[0m, in \u001b[0;36mHTTPTransport.handle_request\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 223\u001b[0m req \u001b[38;5;241m=\u001b[39m httpcore\u001b[38;5;241m.\u001b[39mRequest(\n\u001b[1;32m 224\u001b[0m method\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39mmethod,\n\u001b[1;32m 225\u001b[0m url\u001b[38;5;241m=\u001b[39mhttpcore\u001b[38;5;241m.\u001b[39mURL(\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 233\u001b[0m extensions\u001b[38;5;241m=\u001b[39mrequest\u001b[38;5;241m.\u001b[39mextensions,\n\u001b[1;32m 234\u001b[0m )\n\u001b[0;32m--> 235\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_httpcore_exceptions():\n\u001b[1;32m 236\u001b[0m resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_pool\u001b[38;5;241m.\u001b[39mhandle_request(req)\n", - "File \u001b[0;32m/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py:158\u001b[0m, in \u001b[0;36m_GeneratorContextManager.__exit__\u001b[0;34m(self, typ, value, traceback)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 158\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgen\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrow\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 160\u001b[0m \u001b[38;5;66;03m# Suppress StopIteration *unless* it's the same exception that\u001b[39;00m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;66;03m# was passed to throw(). This prevents a StopIteration\u001b[39;00m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# raised inside the \"with\" statement from being suppressed.\u001b[39;00m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/httpx/_transports/default.py:89\u001b[0m, in \u001b[0;36mmap_httpcore_exceptions\u001b[0;34m()\u001b[0m\n\u001b[1;32m 88\u001b[0m message \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(exc)\n\u001b[0;32m---> 89\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m mapped_exc(message) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mexc\u001b[39;00m\n", - "\u001b[0;31mLocalProtocolError\u001b[0m: Illegal header value b'Bearer '", - "\nThe above exception was the direct cause of the following exception:\n", - "\u001b[0;31mAPIConnectionError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[5], line 37\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[38;5;66;03m# --- Call OpenAI Endpoints ---\u001b[39;00m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOpenAI: 1 - Chat completions\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 37\u001b[0m chat_completions \u001b[38;5;241m=\u001b[39m \u001b[43mopenai_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mchat\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompletions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 38\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgpt-3.5-turbo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 39\u001b[0m \u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrole\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muser\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcontent\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mWhat is machine learning?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 40\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28mprint\u001b[39m(chat_completions\u001b[38;5;241m.\u001b[39mmodel_dump_json(indent\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m))\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/javelin_sdk/client.py:142\u001b[0m, in \u001b[0;36mJavelinClient.register_provider..create_patched_method..patched_method\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 139\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m model \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(openai_client, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_custom_headers\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 140\u001b[0m openai_client\u001b[38;5;241m.\u001b[39m_custom_headers[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx-javelin-model\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m model\n\u001b[0;32m--> 142\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43moriginal_method\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 143\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_utils/_utils.py:279\u001b[0m, in \u001b[0;36mrequired_args..inner..wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 277\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMissing required argument: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquote(missing[\u001b[38;5;241m0\u001b[39m])\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 278\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg)\n\u001b[0;32m--> 279\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/resources/chat/completions.py:863\u001b[0m, in \u001b[0;36mCompletions.create\u001b[0;34m(self, messages, model, audio, frequency_penalty, function_call, functions, logit_bias, logprobs, max_completion_tokens, max_tokens, metadata, modalities, n, parallel_tool_calls, prediction, presence_penalty, reasoning_effort, response_format, seed, service_tier, stop, store, stream, stream_options, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout)\u001b[0m\n\u001b[1;32m 821\u001b[0m \u001b[38;5;129m@required_args\u001b[39m([\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessages\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m], [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessages\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstream\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m 822\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mcreate\u001b[39m(\n\u001b[1;32m 823\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 860\u001b[0m timeout: \u001b[38;5;28mfloat\u001b[39m \u001b[38;5;241m|\u001b[39m httpx\u001b[38;5;241m.\u001b[39mTimeout \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m|\u001b[39m NotGiven \u001b[38;5;241m=\u001b[39m NOT_GIVEN,\n\u001b[1;32m 861\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ChatCompletion \u001b[38;5;241m|\u001b[39m Stream[ChatCompletionChunk]:\n\u001b[1;32m 862\u001b[0m validate_response_format(response_format)\n\u001b[0;32m--> 863\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_post\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 864\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m/chat/completions\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 865\u001b[0m \u001b[43m \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmaybe_transform\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 866\u001b[0m \u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m 867\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmessages\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 868\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmodel\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 869\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43maudio\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43maudio\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 870\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfrequency_penalty\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfrequency_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 871\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfunction_call\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunction_call\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 872\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfunctions\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunctions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 873\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mlogit_bias\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogit_bias\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 874\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mlogprobs\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 875\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmax_completion_tokens\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_completion_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 876\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmax_tokens\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 877\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmetadata\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 878\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmodalities\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodalities\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 879\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mn\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 880\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mparallel_tool_calls\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mparallel_tool_calls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 881\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mprediction\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mprediction\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 882\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpresence_penalty\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpresence_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 883\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mreasoning_effort\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mreasoning_effort\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 884\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mresponse_format\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 885\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mseed\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mseed\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 886\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mservice_tier\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mservice_tier\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 887\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstop\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 888\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstore\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstore\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 889\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstream\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 890\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstream_options\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 891\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtemperature\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtemperature\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 892\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtool_choice\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtool_choice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 893\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtools\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtools\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 894\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtop_logprobs\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_logprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 895\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtop_p\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_p\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 896\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muser\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43muser\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 897\u001b[0m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 898\u001b[0m \u001b[43m \u001b[49m\u001b[43mcompletion_create_params\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCompletionCreateParams\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 899\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 900\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmake_request_options\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 901\u001b[0m \u001b[43m \u001b[49m\u001b[43mextra_headers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_headers\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_query\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_query\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_body\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\n\u001b[1;32m 902\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 903\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mChatCompletion\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 904\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 905\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mStream\u001b[49m\u001b[43m[\u001b[49m\u001b[43mChatCompletionChunk\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 906\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1283\u001b[0m, in \u001b[0;36mSyncAPIClient.post\u001b[0;34m(self, path, cast_to, body, options, files, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1269\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mpost\u001b[39m(\n\u001b[1;32m 1270\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1271\u001b[0m path: \u001b[38;5;28mstr\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1278\u001b[0m stream_cls: \u001b[38;5;28mtype\u001b[39m[_StreamT] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1279\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m ResponseT \u001b[38;5;241m|\u001b[39m _StreamT:\n\u001b[1;32m 1280\u001b[0m opts \u001b[38;5;241m=\u001b[39m FinalRequestOptions\u001b[38;5;241m.\u001b[39mconstruct(\n\u001b[1;32m 1281\u001b[0m method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpost\u001b[39m\u001b[38;5;124m\"\u001b[39m, url\u001b[38;5;241m=\u001b[39mpath, json_data\u001b[38;5;241m=\u001b[39mbody, files\u001b[38;5;241m=\u001b[39mto_httpx_files(files), \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39moptions\n\u001b[1;32m 1282\u001b[0m )\n\u001b[0;32m-> 1283\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m cast(ResponseT, \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mopts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m)\u001b[49m)\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:960\u001b[0m, in \u001b[0;36mSyncAPIClient.request\u001b[0;34m(self, cast_to, options, remaining_retries, stream, stream_cls)\u001b[0m\n\u001b[1;32m 957\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 958\u001b[0m retries_taken \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m--> 960\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 961\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 962\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 963\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 964\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 965\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 966\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1020\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1017\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEncountered Exception\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 1019\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m remaining_retries \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m-> 1020\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_retry_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1021\u001b[0m \u001b[43m \u001b[49m\u001b[43minput_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1022\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1024\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1025\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1026\u001b[0m \u001b[43m \u001b[49m\u001b[43mresponse_headers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 1027\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1029\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRaising connection error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1030\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m APIConnectionError(request\u001b[38;5;241m=\u001b[39mrequest) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01merr\u001b[39;00m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1098\u001b[0m, in \u001b[0;36mSyncAPIClient._retry_request\u001b[0;34m(self, options, cast_to, retries_taken, response_headers, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1094\u001b[0m \u001b[38;5;66;03m# In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a\u001b[39;00m\n\u001b[1;32m 1095\u001b[0m \u001b[38;5;66;03m# different thread if necessary.\u001b[39;00m\n\u001b[1;32m 1096\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(timeout)\n\u001b[0;32m-> 1098\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1099\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1100\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1101\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1102\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1103\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1104\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1020\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1017\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEncountered Exception\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 1019\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m remaining_retries \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m-> 1020\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_retry_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1021\u001b[0m \u001b[43m \u001b[49m\u001b[43minput_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1022\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1024\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1025\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1026\u001b[0m \u001b[43m \u001b[49m\u001b[43mresponse_headers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 1027\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1029\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRaising connection error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1030\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m APIConnectionError(request\u001b[38;5;241m=\u001b[39mrequest) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01merr\u001b[39;00m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1098\u001b[0m, in \u001b[0;36mSyncAPIClient._retry_request\u001b[0;34m(self, options, cast_to, retries_taken, response_headers, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1094\u001b[0m \u001b[38;5;66;03m# In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a\u001b[39;00m\n\u001b[1;32m 1095\u001b[0m \u001b[38;5;66;03m# different thread if necessary.\u001b[39;00m\n\u001b[1;32m 1096\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(timeout)\n\u001b[0;32m-> 1098\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1099\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1100\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1101\u001b[0m \u001b[43m \u001b[49m\u001b[43mretries_taken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries_taken\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1102\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1103\u001b[0m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream_cls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1104\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Desktop/javelin-main/javelin-python/venv/lib/python3.12/site-packages/openai/_base_client.py:1030\u001b[0m, in \u001b[0;36mSyncAPIClient._request\u001b[0;34m(self, cast_to, options, retries_taken, stream, stream_cls)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_retry_request(\n\u001b[1;32m 1021\u001b[0m input_options,\n\u001b[1;32m 1022\u001b[0m cast_to,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1026\u001b[0m response_headers\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1027\u001b[0m )\n\u001b[1;32m 1029\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRaising connection error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 1030\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m APIConnectionError(request\u001b[38;5;241m=\u001b[39mrequest) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01merr\u001b[39;00m\n\u001b[1;32m 1032\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\n\u001b[1;32m 1033\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mHTTP Response: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%i\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 1034\u001b[0m request\u001b[38;5;241m.\u001b[39mmethod,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1038\u001b[0m response\u001b[38;5;241m.\u001b[39mheaders,\n\u001b[1;32m 1039\u001b[0m )\n\u001b[1;32m 1040\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrequest_id: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, response\u001b[38;5;241m.\u001b[39mheaders\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mx-request-id\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n", - "\u001b[0;31mAPIConnectionError\u001b[0m: Connection error." + "name": "stdout", + "output_type": "stream", + "text": [ + "Structured Output (JSON) Response:\n", + "{\n", + " \"id\": \"chatcmpl-B5D3I4NnbFAscgmT00ViE1kuocr8j\",\n", + " \"choices\": [\n", + " {\n", + " \"finish_reason\": \"stop\",\n", + " \"index\": 0,\n", + " \"logprobs\": null,\n", + " \"message\": {\n", + " \"content\": \"{\\n \\\"id\\\": \\\"1234\\\",\\n \\\"name\\\": \\\"Sample Item\\\",\\n \\\"description\\\": \\\"This item serves as a generic example of structured data output in JSON format.\\\",\\n \\\"attributes\\\": {\\n \\\"color\\\": \\\"blue\\\",\\n \\\"size\\\": \\\"medium\\\",\\n \\\"weight\\\": 2.5,\\n \\\"tags\\\": [\\\"example\\\", \\\"generic\\\", \\\"json\\\"]\\n }\\n}\",\n", + " \"refusal\": null,\n", + " \"role\": \"assistant\",\n", + " \"audio\": null,\n", + " \"function_call\": null,\n", + " \"tool_calls\": null\n", + " }\n", + " }\n", + " ],\n", + " \"created\": 1740581512,\n", + " \"model\": \"o3-mini-2025-01-31\",\n", + " \"object\": \"chat.completion\",\n", + " \"service_tier\": \"default\",\n", + " \"system_fingerprint\": \"fp_42bfad963b\",\n", + " \"usage\": {\n", + " \"completion_tokens\": 348,\n", + " \"prompt_tokens\": 71,\n", + " \"total_tokens\": 419,\n", + " \"completion_tokens_details\": {\n", + " \"accepted_prediction_tokens\": 0,\n", + " \"audio_tokens\": 0,\n", + " \"reasoning_tokens\": 256,\n", + " \"rejected_prediction_tokens\": 0\n", + " },\n", + " \"prompt_tokens_details\": {\n", + " \"audio_tokens\": 0,\n", + " \"cached_tokens\": 0\n", + " }\n", + " },\n", + " \"javelin\": {\n", + " \"archive_enabled\": true,\n", + " \"correlation_id\": \"01JN1BW8S43TFWN2Y5BSXBQ07B\",\n", + " \"model_endpoint_url\": \"https://api.openai.com/v1/chat/completions\",\n", + " \"model_latency\": \"56.640904548s\",\n", + " \"model_name\": \"o3-mini\",\n", + " \"processor_outputs\": {\n", + " \"request.chain.archive_processor_20250226145248.622212280\": {\n", + " \"duration\": \"3.818839ms\",\n", + " \"success\": \"successfully archived memory\"\n", + " },\n", + " \"request.chain.checkphish_processor_20250226145248.622112235\": {\n", + " \"duration\": \"377.893µs\"\n", + " },\n", + " \"request.chain.dlp_gcp_processor_20250226145248.622138920\": {\n", + " \"duration\": \"233.833µs\",\n", + " \"skipped\": \"warn: sensitive data protection is disabled for route:openai_univ\"\n", + " },\n", + " \"request.chain.promptinjectiondetection_processor_20250226145248.622164866\": {\n", + " \"duration\": \"124.6µs\",\n", + " \"skipped\": \"warn: prompt safety is disabled for route:openai_univ\"\n", + " },\n", + " \"request.chain.ratelimit_processor_20250226145248.621968259\": {\n", + " \"duration\": \"959.745µs\"\n", + " },\n", + " \"request.chain.secrets_processor_20250226145248.622014788\": {\n", + " \"duration\": \"11.468µs\"\n", + " },\n", + " \"request.chain.trustsafety_processor_20250226145248.621979219\": {\n", + " \"duration\": \"56.722µs\",\n", + " \"skipped\": \"warn: trust safety is disabled for route:openai_univ\"\n", + " },\n", + " \"response.chain.response_processor_20250226145248.622083140\": {\n", + " \"duration\": \"0s\"\n", + " },\n", + " \"response.chain.securityfilters_processor_20250226145248.622247394\": {\n", + " \"confidence\": 0.9655138378237373,\n", + " \"duration\": \"1.083219ms\",\n", + " \"entropy\": \"4.511831\",\n", + " \"language\": \"English\",\n", + " \"non_ascii_chars_detected\": \"true\"\n", + " },\n", + " \"response.chain.trustsafety_processor_20250226145248.622039203\": {\n", + " \"duration\": \"48.364µs\",\n", + " \"skipped\": \"warn: trust safety is disabled for route:openai_univ\"\n", + " }\n", + " },\n", + " \"route_name\": \"openai_univ\"\n", + " }\n", + "}\n", + "\n", + "Parsed JSON Output:\n", + "{\n", + " \"id\": \"1234\",\n", + " \"name\": \"Sample Item\",\n", + " \"description\": \"This item serves as a generic example of structured data output in JSON format.\",\n", + " \"attributes\": {\n", + " \"color\": \"blue\",\n", + " \"size\": \"medium\",\n", + " \"weight\": 2.5,\n", + " \"tags\": [\n", + " \"example\",\n", + " \"generic\",\n", + " \"json\"\n", + " ]\n", + " }\n", + "}\n" ] } ], "source": [ - "import json\n", "import os\n", - "import sys\n", - "import asyncio\n", + "import json\n", "from openai import OpenAI\n", - "from openai import AsyncOpenAI\n", - "from openai import AzureOpenAI # Imported for consistency, though not used in this notebook\n", - "import dotenv\n", - "from javelin_sdk import (\n", - " JavelinClient,\n", - " JavelinConfig,\n", - ")\n", + "from javelin_sdk import JavelinClient, JavelinConfig\n", + "from dotenv import load_dotenv\n", "\n", - "# -------------------------------\n", - "# Synchronous OpenAI Example\n", - "# -------------------------------\n", - "print(\"Initializing Synchronous OpenAI client...\")\n", + "load_dotenv()\n", "\n", - "# Create OpenAI client using the API key from the environment variable\n", - "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n", - "print(openai_api_key)\n", - "openai_client = OpenAI(api_key=openai_api_key)\n", + "def init_openai_client():\n", + " api_key = os.getenv(\"OPENAI_API_KEY\")\n", + " return OpenAI(api_key=api_key)\n", "\n", - "# Initialize Javelin Client with your API key and base URL\n", - "javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", - "config = JavelinConfig( \n", - " javelin_api_key=javelin_api_key,\n", - ")\n", - "client = JavelinClient(config)\n", + "def init_javelin_client(openai_client, route_name=\"openai_univ\"):\n", + " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", + " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", + " client = JavelinClient(config)\n", + " client.register_openai(openai_client, route_name=route_name)\n", + " return client\n", "\n", - "# Register the OpenAI client with Javelin using the route name \"openai\"\n", - "client.register_openai(openai_client, route_name=\"openai-univ\")\n", + "def openai_structured_output_call_generic():\n", + " # Initialize clients\n", + " openai_client = init_openai_client()\n", + " init_javelin_client(openai_client)\n", + " \n", + " # Create messages with a system instruction to output valid JSON\n", + " messages = [\n", + " {\n", + " \"role\": \"system\",\n", + " \"content\": \"You are an assistant that always responds in valid JSON format without any additional text.\"\n", + " },\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": (\n", + " \"Provide a generic example of structured data output in JSON format. \"\n", + " \"The JSON should include the keys: 'id', 'name', 'description', \"\n", + " \"and 'attributes' (which should be a nested object with arbitrary key-value pairs).\"\n", + " )\n", + " }\n", + " ]\n", + " \n", + " response = openai_client.chat.completions.create(\n", + " model=\"o3-mini\", # can use o1 model as well\n", + " messages=messages,\n", + " )\n", + " \n", + " # Print the full API response for reference\n", + " print(\"Structured Output (JSON) Response:\")\n", + " print(response.model_dump_json(indent=2))\n", + " \n", + " # Extract the reply content using attribute access\n", + " try:\n", + " reply_content = response.choices[0].message.content\n", + " except (IndexError, AttributeError) as e:\n", + " print(\"Error extracting message content:\", e)\n", + " reply_content = \"\"\n", + " \n", + " # Attempt to parse the reply content as JSON\n", + " try:\n", + " json_output = json.loads(reply_content)\n", + " print(\"\\nParsed JSON Output:\")\n", + " print(json.dumps(json_output, indent=2))\n", + " except Exception as e:\n", + " print(\"\\nFailed to parse JSON output. Error:\", e)\n", + " print(\"Raw content:\", reply_content)\n", "\n", - "# --- Call OpenAI Endpoints ---\n", + "if __name__ == \"__main__\":\n", + " openai_structured_output_call_generic()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Azure OpenAI – Unified Endpoint Examples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Non-Streaming with Function Calling" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Azure OpenAI Non-Streaming Response:\n", + "{\n", + " \"id\": \"chatcmpl-B5BuG5zyeIE3eahuneeL9g5Ph7pXh\",\n", + " \"choices\": [\n", + " {\n", + " \"finish_reason\": \"function_call\",\n", + " \"index\": 0,\n", + " \"logprobs\": null,\n", + " \"message\": {\n", + " \"content\": null,\n", + " \"refusal\": null,\n", + " \"role\": \"assistant\",\n", + " \"function_call\": {\n", + " \"arguments\": \"{\\\"date\\\":\\\"2023-11-07\\\",\\\"time\\\":\\\"10:00:00\\\"}\",\n", + " \"name\": \"schedule_meeting\"\n", + " }\n", + " },\n", + " \"content_filter_results\": {}\n", + " }\n", + " ],\n", + " \"created\": 1740577108,\n", + " \"model\": \"gpt-4o-2024-11-20\",\n", + " \"object\": \"chat.completion\",\n", + " \"system_fingerprint\": \"fp_b705f0c291\",\n", + " \"usage\": {\n", + " \"completion_tokens\": 28,\n", + " \"prompt_tokens\": 73,\n", + " \"total_tokens\": 101,\n", + " \"completion_tokens_details\": {\n", + " \"accepted_prediction_tokens\": 0,\n", + " \"audio_tokens\": 0,\n", + " \"reasoning_tokens\": 0,\n", + " \"rejected_prediction_tokens\": 0\n", + " },\n", + " \"prompt_tokens_details\": {\n", + " \"audio_tokens\": 0,\n", + " \"cached_tokens\": 0\n", + " }\n", + " },\n", + " \"javelin\": {\n", + " \"archive_enabled\": true,\n", + " \"correlation_id\": \"01JN17NWBD6T6TWZT48YS0HVDZ\",\n", + " \"model_endpoint_url\": \"https://javelinpreview.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2023-07-01-preview\",\n", + " \"model_latency\": \"1.233102785s\",\n", + " \"model_name\": \"gpt-4o\",\n", + " \"processor_outputs\": {\n", + " \"request.chain.archive_processor_20250226133829.678735774\": {\n", + " \"duration\": \"5.012198ms\",\n", + " \"success\": \"successfully archived memory\"\n", + " },\n", + " \"request.chain.checkphish_processor_20250226133829.678975977\": {\n", + " \"duration\": \"71.216µs\"\n", + " },\n", + " \"request.chain.dlp_gcp_processor_20250226133829.678855752\": {\n", + " \"duration\": \"104.480149ms\"\n", + " },\n", + " \"request.chain.promptinjectiondetection_processor_20250226133829.678881889\": {\n", + " \"duration\": \"61.205µs\",\n", + " \"skipped\": \"warn: prompt safety is disabled for route:azureopenai_univ\"\n", + " },\n", + " \"request.chain.ratelimit_processor_20250226133829.678827468\": {\n", + " \"duration\": \"1.787483ms\"\n", + " },\n", + " \"request.chain.secrets_processor_20250226133829.678725520\": {\n", + " \"duration\": \"10.696µs\"\n", + " },\n", + " \"request.chain.trustsafety_processor_20250226133829.678919391\": {\n", + " \"duration\": \"251.622µs\",\n", + " \"skipped\": \"warn: trust safety is disabled for route:azureopenai_univ\"\n", + " },\n", + " \"response.chain.response_processor_20250226133829.678794065\": {\n", + " \"duration\": \"0s\"\n", + " },\n", + " \"response.chain.securityfilters_processor_20250226133829.678944081\": {\n", + " \"duration\": \"44.656µs\",\n", + " \"skipped\": \"warn: security filters is disabled for route:azureopenai_univ\"\n", + " },\n", + " \"response.chain.trustsafety_processor_20250226133829.678775692\": {\n", + " \"duration\": \"51.367µs\",\n", + " \"skipped\": \"warn: trust safety is disabled for route:azureopenai_univ\"\n", + " }\n", + " },\n", + " \"route_name\": \"azureopenai_univ\"\n", + " },\n", + " \"prompt_filter_results\": [\n", + " {\n", + " \"content_filter_results\": {\n", + " \"hate\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"jailbreak\": {\n", + " \"detected\": false,\n", + " \"filtered\": false\n", + " },\n", + " \"self_harm\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"sexual\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"violence\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " }\n", + " },\n", + " \"prompt_index\": 0\n", + " }\n", + " ]\n", + "}\n" + ] + } + ], + "source": [ + "import os\n", + "from openai import AzureOpenAI\n", + "from javelin_sdk import JavelinClient, JavelinConfig\n", "\n", - "print(\"OpenAI: 1 - Chat completions\")\n", - "chat_completions = openai_client.chat.completions.create(\n", - " model=\"gpt-3.5-turbo\",\n", - " messages=[{\"role\": \"user\", \"content\": \"What is machine learning?\"}],\n", - ")\n", - "print(chat_completions.model_dump_json(indent=2))\n" + "def init_azure_client():\n", + " azure_api_key = os.getenv(\"AZURE_OPENAI_API_KEY\")\n", + " return AzureOpenAI(\n", + " api_version=\"2023-07-01-preview\",\n", + " azure_endpoint=\"https://javelinpreview.openai.azure.com\",\n", + " api_key=azure_api_key\n", + " )\n", + "\n", + "def init_javelin_client_azure(azure_client, route_name=\"azureopenai_univ\"):\n", + " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", + " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", + " client = JavelinClient(config)\n", + " client.register_azureopenai(azure_client, route_name=route_name)\n", + " return client\n", + "\n", + "def azure_function_call_non_stream():\n", + " azure_client = init_azure_client()\n", + " init_javelin_client_azure(azure_client)\n", + " response = azure_client.chat.completions.create(\n", + " model=\"gpt-4o\",\n", + " messages=[\n", + " {\"role\": \"user\", \"content\": \"Schedule a meeting at 10 AM tomorrow.\"}\n", + " ],\n", + " functions=[\n", + " {\n", + " \"name\": \"schedule_meeting\",\n", + " \"description\": \"Schedules a meeting in the calendar\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"time\": {\"type\": \"string\", \"description\": \"Meeting time (ISO format)\"},\n", + " \"date\": {\"type\": \"string\", \"description\": \"Meeting date (YYYY-MM-DD)\"}\n", + " },\n", + " \"required\": [\"time\", \"date\"]\n", + " }\n", + " }\n", + " ],\n", + " function_call=\"auto\"\n", + " )\n", + " print(\"Azure OpenAI Non-Streaming Response:\")\n", + " print(response.to_json())\n", + "\n", + "if __name__ == \"__main__\":\n", + " azure_function_call_non_stream()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Structured output calling - Azure Open Ai Example" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 164, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Structured Output (JSON) Response:\n", + "{\n", + " \"id\": \"chatcmpl-B5DB37rD9OIR5BnSbP5iaFonLe2I8\",\n", + " \"choices\": [\n", + " {\n", + " \"finish_reason\": \"stop\",\n", + " \"index\": 0,\n", + " \"logprobs\": null,\n", + " \"message\": {\n", + " \"content\": \"```json\\n{\\n \\\"id\\\": 1,\\n \\\"title\\\": \\\"Sample Data\\\",\\n \\\"description\\\": \\\"This is an example of structured JSON data.\\\",\\n \\\"metadata\\\": {\\n \\\"author\\\": \\\"John Doe\\\",\\n \\\"created_at\\\": \\\"2023-10-01\\\",\\n \\\"tags\\\": [\\\"example\\\", \\\"json\\\", \\\"data\\\"],\\n \\\"version\\\": 1.0\\n }\\n}\\n```\",\n", + " \"refusal\": null,\n", + " \"role\": \"assistant\"\n", + " },\n", + " \"content_filter_results\": {\n", + " \"hate\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"protected_material_code\": {\n", + " \"detected\": false,\n", + " \"filtered\": false\n", + " },\n", + " \"protected_material_text\": {\n", + " \"detected\": false,\n", + " \"filtered\": false\n", + " },\n", + " \"self_harm\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"sexual\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"violence\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " }\n", + " }\n", + " }\n", + " ],\n", + " \"created\": 1740581993,\n", + " \"model\": \"gpt-4o-2024-11-20\",\n", + " \"object\": \"chat.completion\",\n", + " \"system_fingerprint\": \"fp_b705f0c291\",\n", + " \"usage\": {\n", + " \"completion_tokens\": 85,\n", + " \"prompt_tokens\": 74,\n", + " \"total_tokens\": 159,\n", + " \"completion_tokens_details\": {\n", + " \"accepted_prediction_tokens\": 0,\n", + " \"audio_tokens\": 0,\n", + " \"reasoning_tokens\": 0,\n", + " \"rejected_prediction_tokens\": 0\n", + " },\n", + " \"prompt_tokens_details\": {\n", + " \"audio_tokens\": 0,\n", + " \"cached_tokens\": 0\n", + " }\n", + " },\n", + " \"javelin\": {\n", + " \"archive_enabled\": true,\n", + " \"correlation_id\": \"01JN1CAYD7WKJ9YMQHNRT0DW78\",\n", + " \"model_endpoint_url\": \"https://javelinpreview.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2023-07-01-preview\",\n", + " \"model_latency\": \"1.275729182s\",\n", + " \"model_name\": \"gpt-4o\",\n", + " \"processor_outputs\": {\n", + " \"request.chain.archive_processor_20250226145954.357026188\": {\n", + " \"duration\": \"7.865223ms\",\n", + " \"success\": \"successfully archived memory\"\n", + " },\n", + " \"request.chain.checkphish_processor_20250226145954.357090711\": {\n", + " \"duration\": \"73.306µs\"\n", + " },\n", + " \"request.chain.dlp_gcp_processor_20250226145954.357373533\": {\n", + " \"duration\": \"197.768806ms\"\n", + " },\n", + " \"request.chain.promptinjectiondetection_processor_20250226145954.356969553\": {\n", + " \"duration\": \"59.877µs\",\n", + " \"skipped\": \"warn: prompt safety is disabled for route:azureopenai_univ\"\n", + " },\n", + " \"request.chain.ratelimit_processor_20250226145954.357111515\": {\n", + " \"duration\": \"2.095796ms\"\n", + " },\n", + " \"request.chain.secrets_processor_20250226145954.357013668\": {\n", + " \"duration\": \"13.467µs\"\n", + " },\n", + " \"request.chain.trustsafety_processor_20250226145954.356982735\": {\n", + " \"duration\": \"43.126µs\",\n", + " \"skipped\": \"warn: trust safety is disabled for route:azureopenai_univ\"\n", + " },\n", + " \"response.chain.response_processor_20250226145954.357067882\": {\n", + " \"duration\": \"0s\"\n", + " },\n", + " \"response.chain.securityfilters_processor_20250226145954.357042078\": {\n", + " \"duration\": \"105.719µs\",\n", + " \"skipped\": \"warn: security filters is disabled for route:azureopenai_univ\"\n", + " },\n", + " \"response.chain.trustsafety_processor_20250226145954.357494465\": {\n", + " \"duration\": \"94.684µs\",\n", + " \"skipped\": \"warn: trust safety is disabled for route:azureopenai_univ\"\n", + " }\n", + " },\n", + " \"route_name\": \"azureopenai_univ\"\n", + " },\n", + " \"prompt_filter_results\": [\n", + " {\n", + " \"content_filter_results\": {\n", + " \"hate\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"jailbreak\": {\n", + " \"detected\": false,\n", + " \"filtered\": false\n", + " },\n", + " \"self_harm\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"sexual\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"violence\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " }\n", + " },\n", + " \"prompt_index\": 0\n", + " }\n", + " ]\n", + "}\n", + "\n", + "Parsed JSON Output:\n", + "{\n", + " \"id\": 1,\n", + " \"title\": \"Sample Data\",\n", + " \"description\": \"This is an example of structured JSON data.\",\n", + " \"metadata\": {\n", + " \"author\": \"John Doe\",\n", + " \"created_at\": \"2023-10-01\",\n", + " \"tags\": [\n", + " \"example\",\n", + " \"json\",\n", + " \"data\"\n", + " ],\n", + " \"version\": 1.0\n", + " }\n", + "}\n" + ] + } + ], "source": [ + "import os\n", + "import json\n", + "import re\n", + "from openai import AzureOpenAI\n", + "from javelin_sdk import JavelinClient, JavelinConfig\n", + "from dotenv import load_dotenv\n", "\n", - "print(\"OpenAI: 2 - Completions\")\n", - "completions = openai_client.completions.create(\n", - " model=\"gpt-3.5-turbo-instruct\",\n", - " prompt=\"What is machine learning?\",\n", - " max_tokens=7,\n", - " temperature=0\n", - ")\n", - "print(completions.model_dump_json(indent=2))\n" + "load_dotenv()\n", + "\n", + "def init_azure_client():\n", + " azure_api_key = os.getenv(\"AZURE_OPENAI_API_KEY\")\n", + " return AzureOpenAI(\n", + " api_version=\"2023-07-01-preview\",\n", + " azure_endpoint=\"https://javelinpreview.openai.azure.com\",\n", + " api_key=azure_api_key\n", + " )\n", + "\n", + "def init_javelin_client_azure(azure_client, route_name=\"azureopenai_univ\"):\n", + " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", + " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", + " client = JavelinClient(config)\n", + " client.register_azureopenai(azure_client, route_name=route_name)\n", + " return client\n", + "\n", + "def extract_json_from_markdown(text: str) -> str:\n", + " \"\"\"\n", + " Extracts JSON content from a markdown code block if present.\n", + " For example, removes leading and trailing triple backticks.\n", + " \"\"\"\n", + " match = re.search(r\"```(?:json)?\\s*(\\{.*\\})\\s*```\", text, re.DOTALL)\n", + " if match:\n", + " return match.group(1)\n", + " return text.strip()\n", + "\n", + "def azure_structured_output_call():\n", + " azure_client = init_azure_client()\n", + " init_javelin_client_azure(azure_client)\n", + "\n", + " # System message enforces structured JSON output without extra text\n", + " messages = [\n", + " {\n", + " \"role\": \"system\",\n", + " \"content\": \"You are an assistant that always responds in valid JSON format without any additional text.\"\n", + " },\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": (\n", + " \"Provide structured data in JSON format. \"\n", + " \"The JSON should contain the following keys: 'id' (integer), 'title' (string), \"\n", + " \"'description' (string), and 'metadata' (a nested object with arbitrary key-value pairs).\"\n", + " )\n", + " }\n", + " ]\n", + "\n", + " response = azure_client.chat.completions.create(\n", + " model=\"gpt-4o\",\n", + " messages=messages\n", + " )\n", + "\n", + " # Print the full API response for reference\n", + " print(\"Structured Output (JSON) Response:\")\n", + " print(response.to_json())\n", + "\n", + " # Extract and clean the reply content\n", + " try:\n", + " reply_content = response.choices[0].message.content # Get the raw response text\n", + " # Remove markdown code fences if present\n", + " reply_content_clean = extract_json_from_markdown(reply_content)\n", + " json_output = json.loads(reply_content_clean) # Parse as JSON\n", + " print(\"\\nParsed JSON Output:\")\n", + " print(json.dumps(json_output, indent=2))\n", + " except Exception as e:\n", + " print(\"\\nFailed to parse JSON output. Error:\", e)\n", + " print(\"Raw content:\", reply_content)\n", + "\n", + "if __name__ == \"__main__\":\n", + " azure_structured_output_call()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Streaming with Function Calling" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Azure OpenAI Non-Streaming Response:\n", + "ChatCompletionChunk(id='', choices=[], created=0, model='', object='', service_tier=None, system_fingerprint=None, usage=None, prompt_filter_results=[{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'jailbreak': {'filtered': False, 'detected': False}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}])\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='', name='schedule_meeting'), refusal=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='{\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='date', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\":\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='202', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='3', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='-', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='11', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='-', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='17', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\",\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='time', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\":\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='10', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=':', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='00', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=':', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='00', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\"}', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", + "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None), finish_reason='function_call', index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n" + ] + } + ], "source": [ + "import os\n", + "from openai import AzureOpenAI\n", + "from javelin_sdk import JavelinClient, JavelinConfig\n", "\n", - "print(\"OpenAI: 3 - Embeddings\")\n", - "embeddings = openai_client.embeddings.create(\n", - " model=\"text-embedding-ada-002\",\n", - " input=\"The food was delicious and the waiter...\",\n", - " encoding_format=\"float\"\n", - ")\n", - "print(embeddings.model_dump_json(indent=2))\n" + "def init_azure_client():\n", + " azure_api_key = os.getenv(\"AZURE_OPENAI_API_KEY\")\n", + " return AzureOpenAI(\n", + " api_version=\"2023-07-01-preview\",\n", + " azure_endpoint=\"https://javelinpreview.openai.azure.com\",\n", + " api_key=azure_api_key\n", + " )\n", + "\n", + "def init_javelin_client_azure(azure_client, route_name=\"azureopenai_univ\"):\n", + " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", + " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", + " client = JavelinClient(config)\n", + " client.register_azureopenai(azure_client, route_name=route_name)\n", + " return client\n", + "\n", + "def azure_function_call_non_stream():\n", + " azure_client = init_azure_client()\n", + " init_javelin_client_azure(azure_client)\n", + " stream = azure_client.chat.completions.create(\n", + " model=\"gpt-4o\",\n", + " messages=[\n", + " {\"role\": \"user\", \"content\": \"Schedule a meeting at 10 AM tomorrow.\"}\n", + " ],\n", + " functions=[\n", + " {\n", + " \"name\": \"schedule_meeting\",\n", + " \"description\": \"Schedules a meeting in the calendar\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"time\": {\"type\": \"string\", \"description\": \"Meeting time (ISO format)\"},\n", + " \"date\": {\"type\": \"string\", \"description\": \"Meeting date (YYYY-MM-DD)\"}\n", + " },\n", + " \"required\": [\"time\", \"date\"]\n", + " }\n", + " }\n", + " ],\n", + " function_call=\"auto\",\n", + " stream=True\n", + " )\n", + " print(\"Azure OpenAI Non-Streaming Response:\")\n", + " for chunk in stream:\n", + " print(chunk)\n", + "\n", + "if __name__ == \"__main__\":\n", + " azure_function_call_non_stream()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# OpenAI – Regular Route Endpoint Examples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Non-Streaming with Function Calling" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 170, "metadata": {}, "outputs": [], "source": [ + "import os\n", + "from javelin_sdk import JavelinClient, JavelinConfig, RouteNotFoundError\n", "\n", - "print(\"OpenAI: 4 - Streaming\")\n", - "stream = openai_client.chat.completions.create(\n", - " messages=[\n", - " {\"role\": \"user\", \"content\": \"Say this is a test\"}\n", - " ],\n", - " model=\"gpt-4o\",\n", - " stream=True,\n", + "# Retrieve API keys from environment variables\n", + "javelin_api_key = os.getenv('JAVELIN_API_KEY')\n", + "llm_api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "\n", + "if not javelin_api_key or not llm_api_key:\n", + " raise ValueError(\"Both JAVELIN_API_KEY and OPENAI_API_KEY must be set.\")\n", + "\n", + "print(\"OpenAI LLM API Key:\", llm_api_key)\n", + "\n", + "# Configure the Javelin client\n", + "config = JavelinConfig(\n", + " base_url=\"https://api-dev.javelin.live\",\n", + " javelin_api_key=javelin_api_key,\n", + " llm_api_key=llm_api_key,\n", ")\n", - "for chunk in stream:\n", - " # Debug print: show each streaming chunk\n", - " print(chunk.choices[0].delta.content or \"\", end=\"\")\n", + "client = JavelinClient(config)\n", + "print(\"Successfully connected to Javelin Client for OpenAI\")\n", + "\n", + "# Prepare query data with function calling support\n", + "query_data = {\n", + " \"messages\": [\n", + " {\"role\": \"system\", \"content\": \"You are a helpful assistant that translates English to French.\"},\n", + " {\"role\": \"user\", \"content\": \"AI has the power to transform humanity and make the world a better place.\"},\n", + " ],\n", + " \"functions\": [\n", + " {\n", + " \"name\": \"translate_text\",\n", + " \"description\": \"Translates English text to French\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"text\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"Text to translate\"\n", + " }\n", + " },\n", + " \"required\": [\"text\"]\n", + " }\n", + " }\n", + " ],\n", + " \"function_call\": \"auto\"\n", + "}\n", "\n", - "# Prints two blank lines for clarity\n", - "print(\"\\n\\n\")\n", - "\n" + "# Query the LLM using the \"openai\" route\n", + "try:\n", + " response = client.query_route(\"openai\", query_data)\n", + " print(\"Response from OpenAI Regular Endpoint:\")\n", + " print(response)\n", + "except RouteNotFoundError:\n", + " print(\"Route 'openai' Not Found\")\n", + "except Exception as e:\n", + " print(\"Error querying OpenAI endpoint:\", e)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Streaming with Function Calling" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 171, "metadata": {}, "outputs": [], "source": [ - "# -------------------------------\n", - "# Asynchronous OpenAI Example\n", - "# -------------------------------\n", - "print(\"Initializing AsyncOpenAI client...\")\n", - "\n", - "# Create AsyncOpenAI client\n", - "openai_async_client = AsyncOpenAI(\n", - " api_key=os.environ.get(\"OPENAI_API_KEY\"), # This is the default and can be omitted\n", - ")\n", + "import os\n", + "from javelin_sdk import JavelinClient, JavelinConfig, RouteNotFoundError\n", "\n", - "# Reinitialize Javelin Client for Async usage (using the same config)\n", + "# Retrieve API keys from environment variables\n", "javelin_api_key = os.getenv('JAVELIN_API_KEY')\n", + "llm_api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "\n", + "if not javelin_api_key or not llm_api_key:\n", + " raise ValueError(\"Both JAVELIN_API_KEY and OPENAI_API_KEY must be set.\")\n", + "\n", + "print(\"OpenAI LLM API Key:\", llm_api_key)\n", + "\n", + "# Configure the Javelin client\n", "config = JavelinConfig(\n", + " base_url=\"https://api-dev.javelin.live\",\n", " javelin_api_key=javelin_api_key,\n", + " llm_api_key=llm_api_key,\n", ")\n", "client = JavelinClient(config)\n", - "client.register_openai(openai_async_client, route_name=\"openai\")\n", + "print(\"Successfully connected to Javelin Client for OpenAI\")\n", "\n", - "async def main() -> None:\n", - " chat_completion = await openai_async_client.chat.completions.create(\n", - " messages=[\n", - " {\"role\": \"user\", \"content\": \"Say this is a test\"}\n", - " ],\n", - " model=\"gpt-4o\",\n", - " )\n", - " print(chat_completion.model_dump_json(indent=2))\n", + "# Prepare query data with function calling support\n", + "query_data = {\n", + " \"messages\": [\n", + " {\"role\": \"system\", \"content\": \"You are a helpful assistant that translates English to French.\"},\n", + " {\"role\": \"user\", \"content\": \"AI has the power to transform humanity and make the world a better place.\"},\n", + " ],\n", + " \"functions\": [\n", + " {\n", + " \"name\": \"translate_text\",\n", + " \"description\": \"Translates English text to French\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"text\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"Text to translate\"\n", + " }\n", + " },\n", + " \"required\": [\"text\"]\n", + " }\n", + " }\n", + " ],\n", + " \"function_call\": \"auto\",\n", + " # \"stream\": True\n", + "}\n", + "\n", + "# Query the LLM using the \"openai\" route\n", + "try:\n", + " response = client.query_route(\"openai\", query_data)\n", + " print(\"Response from OpenAI Regular Endpoint:\")\n", "\n", - "print(\"AsyncOpenAI: 5 - Chat completions\")\n", - "asyncio.run(main())\n" + " # If streaming is enabled, iterate over the stream of response chunks\n", + " if query_data.get(\"stream\"):\n", + " for chunk in response:\n", + " print(chunk)\n", + " else:\n", + " print(response)\n", + "except RouteNotFoundError:\n", + " print(\"Route 'openai' Not Found\")\n", + "except Exception as e:\n", + " print(\"Error querying OpenAI endpoint:\", e)\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/examples/openai/openai_universal-rout.ipynb b/examples/openai/openai_universal-rout.ipynb deleted file mode 100644 index e5fbf16..0000000 --- a/examples/openai/openai_universal-rout.ipynb +++ /dev/null @@ -1,179 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# OpenAI Notebook \n", - "This notebook demonstrates the usage of synchronous and asynchronous OpenAI endpoints integrated with Javelin.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "import os\n", - "import sys\n", - "import asyncio\n", - "from openai import OpenAI\n", - "from openai import AsyncOpenAI\n", - "from openai import AzureOpenAI # Imported for consistency, though not used in this notebook\n", - "import dotenv\n", - "from javelin_sdk import (\n", - " JavelinClient,\n", - " JavelinConfig,\n", - ")\n", - "\n", - "# -------------------------------\n", - "# Synchronous OpenAI Example\n", - "# -------------------------------\n", - "print(\"Initializing Synchronous OpenAI client...\")\n", - "\n", - "# Create OpenAI client using the API key from the environment variable\n", - "openai_api_key = os.environ[\"OPENAI_API_KEY\"] = \"\"\n", - "print(openai_api_key)\n", - "openai_client = OpenAI(api_key=openai_api_key)\n", - "\n", - "# Initialize Javelin Client with your API key and base URL\n", - "javelin_api_key = os.environ['JAVELIN_API_KEY'] = \"\" \n", - "config = JavelinConfig(\n", - " base_url=\"https://api-dev.javelin.live\",\n", - " # Uncomment the following line to use a local server:\n", - " # base_url=\"http://localhost:8000\",\n", - " javelin_api_key=javelin_api_key,\n", - ")\n", - "client = JavelinClient(config)\n", - "\n", - "# Register the OpenAI client with Javelin using the route name \"openai\"\n", - "client.register_openai(openai_client, route_name=\"openai-univ\")\n", - "\n", - "# --- Call OpenAI Endpoints ---\n", - "\n", - "print(\"OpenAI: 1 - Chat completions\")\n", - "chat_completions = openai_client.chat.completions.create(\n", - " model=\"gpt-3.5-turbo\",\n", - " messages=[{\"role\": \"user\", \"content\": \"What is machine learning?\"}],\n", - ")\n", - "print(chat_completions.model_dump_json(indent=2))\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "print(\"OpenAI: 2 - Completions\")\n", - "completions = openai_client.completions.create(\n", - " model=\"gpt-3.5-turbo-instruct\",\n", - " prompt=\"What is machine learning?\",\n", - " max_tokens=7,\n", - " temperature=0\n", - ")\n", - "print(completions.model_dump_json(indent=2))\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "print(\"OpenAI: 3 - Embeddings\")\n", - "embeddings = openai_client.embeddings.create(\n", - " model=\"text-embedding-ada-002\",\n", - " input=\"The food was delicious and the waiter...\",\n", - " encoding_format=\"float\"\n", - ")\n", - "print(embeddings.model_dump_json(indent=2))\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - "print(\"OpenAI: 4 - Streaming\")\n", - "stream = openai_client.chat.completions.create(\n", - " messages=[\n", - " {\"role\": \"user\", \"content\": \"Say this is a test\"}\n", - " ],\n", - " model=\"gpt-4o\",\n", - " stream=True,\n", - ")\n", - "for chunk in stream:\n", - " # Debug print: show each streaming chunk\n", - " print(chunk.choices[0].delta.content or \"\", end=\"\")\n", - "\n", - "# Prints two blank lines for clarity\n", - "print(\"\\n\\n\")\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# -------------------------------\n", - "# Asynchronous OpenAI Example\n", - "# -------------------------------\n", - "print(\"Initializing AsyncOpenAI client...\")\n", - "\n", - "# Create AsyncOpenAI client\n", - "openai_async_client = AsyncOpenAI(\n", - " api_key=os.environ.get(\"OPENAI_API_KEY\"), # This is the default and can be omitted\n", - ")\n", - "\n", - "# Reinitialize Javelin Client for Async usage (using the same config)\n", - "javelin_api_key = os.getenv('JAVELIN_API_KEY')\n", - "config = JavelinConfig(\n", - " javelin_api_key=javelin_api_key,\n", - ")\n", - "client = JavelinClient(config)\n", - "client.register_openai(openai_async_client, route_name=\"openai\")\n", - "\n", - "async def main() -> None:\n", - " chat_completion = await openai_async_client.chat.completions.create(\n", - " messages=[\n", - " {\"role\": \"user\", \"content\": \"Say this is a test\"}\n", - " ],\n", - " model=\"gpt-4o\",\n", - " )\n", - " print(chat_completion.model_dump_json(indent=2))\n", - "\n", - "print(\"AsyncOpenAI: 5 - Chat completions\")\n", - "asyncio.run(main())\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "venv", - "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.12.8" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 2324a22a69095c18bd87d057428daa873a84c530 Mon Sep 17 00:00:00 2001 From: Dhruvj07 Date: Thu, 27 Feb 2025 19:44:06 +0530 Subject: [PATCH 3/4] fix: Provide python file for function calling testing with CLI --- examples/openai/o1-03_function-calling.py | 394 ++++++++++++++++++++++ 1 file changed, 394 insertions(+) create mode 100644 examples/openai/o1-03_function-calling.py diff --git a/examples/openai/o1-03_function-calling.py b/examples/openai/o1-03_function-calling.py new file mode 100644 index 0000000..e02b7e0 --- /dev/null +++ b/examples/openai/o1-03_function-calling.py @@ -0,0 +1,394 @@ +#!/usr/bin/env python +import os +import json +import re +import argparse +from dotenv import load_dotenv +from openai import OpenAI, AzureOpenAI +from javelin_sdk import JavelinClient, JavelinConfig, RouteNotFoundError + +# Load environment variables once at the start +load_dotenv() + +# --------------------------- +# OpenAI – Unified Endpoint Examples +# --------------------------- +def init_openai_client(): + api_key = os.getenv("OPENAI_API_KEY") + return OpenAI(api_key=api_key) + +def init_javelin_client(openai_client, route_name="openai_univ"): + javelin_api_key = os.getenv("JAVELIN_API_KEY") + config = JavelinConfig(javelin_api_key=javelin_api_key) + client = JavelinClient(config) + client.register_openai(openai_client, route_name=route_name) + return client + +def openai_function_call_non_stream(): + print("\n==== Running OpenAI Non-Streaming Function Calling Example ====") + client = init_openai_client() + init_javelin_client(client) + response = client.chat.completions.create( + model="o3-mini", # Latest o1 model + messages=[ + {"role": "user", "content": "What is the current weather in New York?"} + ], + functions=[ + { + "name": "get_current_weather", + "description": "Retrieves current weather information", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "City and state (e.g., New York, NY)" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"] + } + }, + "required": ["location"] + } + } + ], + function_call="auto" + ) + print("OpenAI Non-Streaming Response:") + print(response.model_dump_json(indent=2)) + +def openai_function_call_stream(): + print("\n==== Running OpenAI Streaming Function Calling Example ====") + client = init_openai_client() + init_javelin_client(client) + stream = client.chat.completions.create( + model="o3-mini", + messages=[ + {"role": "user", "content": "Tell me a fun fact and then call a function."} + ], + functions=[ + { + "name": "tell_fun_fact", + "description": "Returns a fun fact", + "parameters": { + "type": "object", + "properties": { + "fact": { + "type": "string", + "description": "A fun fact about the topic" + } + }, + "required": ["fact"] + } + } + ], + function_call="auto", + stream=True + ) + collected = [] + print("OpenAI Streaming Response:") + for chunk in stream: + delta = chunk.choices[0].delta + print(chunk) + if hasattr(delta, "content") and delta.content: + collected.append(delta.content) + print("".join(collected)) + +def openai_structured_output_call_generic(): + print("\n==== Running OpenAI Structured Output Function Calling Example ====") + openai_client = init_openai_client() + init_javelin_client(openai_client) + messages = [ + { + "role": "system", + "content": "You are an assistant that always responds in valid JSON format without any additional text." + }, + { + "role": "user", + "content": ( + "Provide a generic example of structured data output in JSON format. " + "The JSON should include the keys: 'id', 'name', 'description', " + "and 'attributes' (which should be a nested object with arbitrary key-value pairs)." + ) + } + ] + + response = openai_client.chat.completions.create( + model="o3-mini", # can use o1 model as well + messages=messages, + ) + + print("Structured Output (JSON) Response:") + print(response.model_dump_json(indent=2)) + + try: + reply_content = response.choices[0].message.content + except (IndexError, AttributeError) as e: + print("Error extracting message content:", e) + reply_content = "" + + try: + json_output = json.loads(reply_content) + print("\nParsed JSON Output:") + print(json.dumps(json_output, indent=2)) + except Exception as e: + print("\nFailed to parse JSON output. Error:", e) + print("Raw content:", reply_content) + +# --------------------------- +# Azure OpenAI – Unified Endpoint Examples +# --------------------------- +def init_azure_client(): + azure_api_key = os.getenv("AZURE_OPENAI_API_KEY") + return AzureOpenAI( + api_version="2023-07-01-preview", + azure_endpoint="https://javelinpreview.openai.azure.com", + api_key=azure_api_key + ) + +def init_javelin_client_azure(azure_client, route_name="azureopenai_univ"): + javelin_api_key = os.getenv("JAVELIN_API_KEY") + config = JavelinConfig(javelin_api_key=javelin_api_key) + client = JavelinClient(config) + client.register_azureopenai(azure_client, route_name=route_name) + return client + +def azure_function_call_non_stream(): + print("\n==== Running Azure OpenAI Non-Streaming Function Calling Example ====") + azure_client = init_azure_client() + init_javelin_client_azure(azure_client) + response = azure_client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "user", "content": "Schedule a meeting at 10 AM tomorrow."} + ], + functions=[ + { + "name": "schedule_meeting", + "description": "Schedules a meeting in the calendar", + "parameters": { + "type": "object", + "properties": { + "time": {"type": "string", "description": "Meeting time (ISO format)"}, + "date": {"type": "string", "description": "Meeting date (YYYY-MM-DD)"} + }, + "required": ["time", "date"] + } + } + ], + function_call="auto" + ) + print("Azure OpenAI Non-Streaming Response:") + print(response.to_json()) + +def azure_function_call_stream(): + print("\n==== Running Azure OpenAI Streaming Function Calling Example ====") + azure_client = init_azure_client() + init_javelin_client_azure(azure_client) + stream = azure_client.chat.completions.create( + model="gpt-4o", + messages=[ + {"role": "user", "content": "Schedule a meeting at 10 AM tomorrow."} + ], + functions=[ + { + "name": "schedule_meeting", + "description": "Schedules a meeting in the calendar", + "parameters": { + "type": "object", + "properties": { + "time": {"type": "string", "description": "Meeting time (ISO format)"}, + "date": {"type": "string", "description": "Meeting date (YYYY-MM-DD)"} + }, + "required": ["time", "date"] + } + } + ], + function_call="auto", + stream=True + ) + print("Azure OpenAI Streaming Response:") + for chunk in stream: + print(chunk) + +def extract_json_from_markdown(text: str) -> str: + """ + Extracts JSON content from a markdown code block if present. + Removes leading and trailing triple backticks. + """ + match = re.search(r"```(?:json)?\s*(\{.*\})\s*```", text, re.DOTALL) + if match: + return match.group(1) + return text.strip() + +def azure_structured_output_call(): + print("\n==== Running Azure OpenAI Structured Output Function Calling Example ====") + azure_client = init_azure_client() + init_javelin_client_azure(azure_client) + messages = [ + { + "role": "system", + "content": "You are an assistant that always responds in valid JSON format without any additional text." + }, + { + "role": "user", + "content": ( + "Provide structured data in JSON format. " + "The JSON should contain the following keys: 'id' (integer), 'title' (string), " + "'description' (string), and 'metadata' (a nested object with arbitrary key-value pairs)." + ) + } + ] + + response = azure_client.chat.completions.create( + model="gpt-4o", + messages=messages + ) + + print("Structured Output (JSON) Response:") + print(response.to_json()) + + try: + reply_content = response.choices[0].message.content + reply_content_clean = extract_json_from_markdown(reply_content) + json_output = json.loads(reply_content_clean) + print("\nParsed JSON Output:") + print(json.dumps(json_output, indent=2)) + except Exception as e: + print("\nFailed to parse JSON output. Error:", e) + print("Raw content:", reply_content) + +# --------------------------- +# OpenAI – Regular Route Endpoint Examples +# --------------------------- +def openai_regular_non_stream(): + print("\n==== Running OpenAI Regular Route Non-Streaming Function Calling Example ====") + javelin_api_key = os.getenv('JAVELIN_API_KEY') + llm_api_key = os.getenv("OPENAI_API_KEY") + if not javelin_api_key or not llm_api_key: + raise ValueError("Both JAVELIN_API_KEY and OPENAI_API_KEY must be set.") + print("OpenAI LLM API Key:", llm_api_key) + config = JavelinConfig( + base_url="https://api-dev.javelin.live", + javelin_api_key=javelin_api_key, + llm_api_key=llm_api_key, + ) + client = JavelinClient(config) + print("Successfully connected to Javelin Client for OpenAI") + + query_data = { + "messages": [ + {"role": "system", "content": "You are a helpful assistant that translates English to French."}, + {"role": "user", "content": "AI has the power to transform humanity and make the world a better place."}, + ] + } + + try: + response = client.query_route("openai", query_data) + print("Response from OpenAI Regular Endpoint:") + print(response) + except RouteNotFoundError: + print("Route 'openai' Not Found") + except Exception as e: + print("Error querying OpenAI endpoint:", e) + +def openai_regular_stream(): + print("\n==== Running OpenAI Regular Route Streaming Function Calling Example ====") + javelin_api_key = os.getenv('JAVELIN_API_KEY') + llm_api_key = os.getenv("OPENAI_API_KEY") + if not javelin_api_key or not llm_api_key: + raise ValueError("Both JAVELIN_API_KEY and OPENAI_API_KEY must be set.") + config = JavelinConfig( + base_url="https://api-dev.javelin.live", + javelin_api_key=javelin_api_key, + llm_api_key=llm_api_key, + ) + client = JavelinClient(config) + print("Successfully connected to Javelin Client for OpenAI") + + query_data = { + "messages": [ + {"role": "system", "content": "You are a helpful assistant that translates English to French."}, + {"role": "user", "content": "AI has the power to transform humanity and make the world a better place."}, + ], + "functions": [ + { + "name": "translate_text", + "description": "Translates English text to French", + "parameters": { + "type": "object", + "properties": { + "text": { + "type": "string", + "description": "Text to translate" + } + }, + "required": ["text"] + } + } + ], + "function_call": "auto", + "stream": True + } + + try: + response = client.query_route("openai", query_data) + print("Response from OpenAI Regular Endpoint (Streaming):") + if query_data.get("stream"): + for chunk in response: + print(chunk) + else: + print(response) + except RouteNotFoundError as e: + print(f"Route 'openai' not found: {str(e)}") + except Exception as e: + print(f"Error occurred while getting response: {str(e)}") + + +# --------------------------- +# Main function and argument parsing +# --------------------------- +def main(): + parser = argparse.ArgumentParser(description="Run Unified Endpoint Examples") + parser.add_argument( + "--example", + type=str, + default="all", + choices=[ + "all", "openai_non_stream", "openai_stream", "openai_structured", + "azure_non_stream", "azure_stream", "azure_structured", + "openai_regular_non_stream", "openai_regular_stream" + ], + help="The example to run (or 'all' to run every example)" + ) + args = parser.parse_args() + + if args.example == "all": + openai_function_call_non_stream() + openai_function_call_stream() + openai_structured_output_call_generic() + azure_function_call_non_stream() + azure_function_call_stream() + azure_structured_output_call() + openai_regular_non_stream() + openai_regular_stream() + elif args.example == "openai_non_stream": + openai_function_call_non_stream() + elif args.example == "openai_stream": + openai_function_call_stream() + elif args.example == "openai_structured": + openai_structured_output_call_generic() + elif args.example == "azure_non_stream": + azure_function_call_non_stream() + elif args.example == "azure_stream": + azure_function_call_stream() + elif args.example == "azure_structured": + azure_structured_output_call() + elif args.example == "openai_regular_non_stream": + openai_regular_non_stream() + elif args.example == "openai_regular_stream": + openai_regular_stream() + +if __name__ == "__main__": + main() \ No newline at end of file From 007d13fc0a717a53b1288e5b4658d2fd70c42da7 Mon Sep 17 00:00:00 2001 From: Dhruvj07 Date: Thu, 27 Feb 2025 19:51:29 +0530 Subject: [PATCH 4/4] fix: rempve notebook file code --- .../openai/openai-azure-fun_calling.ipynb | 1192 ----------------- 1 file changed, 1192 deletions(-) delete mode 100644 examples/openai/openai-azure-fun_calling.ipynb diff --git a/examples/openai/openai-azure-fun_calling.ipynb b/examples/openai/openai-azure-fun_calling.ipynb deleted file mode 100644 index a9331d1..0000000 --- a/examples/openai/openai-azure-fun_calling.ipynb +++ /dev/null @@ -1,1192 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# OpenAI – Unified Endpoint Examples" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Non-Streaming with Function Calling" - ] - }, - { - "cell_type": "code", - "execution_count": 148, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "OpenAI Non-Streaming Response:\n", - "{\n", - " \"id\": \"chatcmpl-B5BpQmrAdVu4uOUUEniPwLvD9omRu\",\n", - " \"choices\": [\n", - " {\n", - " \"finish_reason\": \"function_call\",\n", - " \"index\": 0,\n", - " \"logprobs\": null,\n", - " \"message\": {\n", - " \"content\": null,\n", - " \"refusal\": null,\n", - " \"role\": \"assistant\",\n", - " \"audio\": null,\n", - " \"function_call\": {\n", - " \"arguments\": \"{\\\"location\\\": \\\"New York, NY\\\", \\\"unit\\\": \\\"fahrenheit\\\"}\",\n", - " \"name\": \"get_current_weather\"\n", - " },\n", - " \"tool_calls\": null\n", - " }\n", - " }\n", - " ],\n", - " \"created\": 1740576808,\n", - " \"model\": \"o3-mini-2025-01-31\",\n", - " \"object\": \"chat.completion\",\n", - " \"service_tier\": \"default\",\n", - " \"system_fingerprint\": \"fp_42bfad963b\",\n", - " \"usage\": {\n", - " \"completion_tokens\": 98,\n", - " \"prompt_tokens\": 76,\n", - " \"total_tokens\": 174,\n", - " \"completion_tokens_details\": {\n", - " \"accepted_prediction_tokens\": 0,\n", - " \"audio_tokens\": 0,\n", - " \"reasoning_tokens\": 64,\n", - " \"rejected_prediction_tokens\": 0\n", - " },\n", - " \"prompt_tokens_details\": {\n", - " \"audio_tokens\": 0,\n", - " \"cached_tokens\": 0\n", - " }\n", - " },\n", - " \"javelin\": {\n", - " \"archive_enabled\": true,\n", - " \"correlation_id\": \"01JN17CQTKWFKQMMRT2XNQ1DS9\",\n", - " \"model_endpoint_url\": \"https://api.openai.com/v1/chat/completions\",\n", - " \"model_latency\": \"1.88136955s\",\n", - " \"model_name\": \"o3-mini\",\n", - " \"processor_outputs\": {\n", - " \"request.chain.archive_processor_20250226133330.693491759\": {\n", - " \"duration\": \"7.84936ms\",\n", - " \"success\": \"successfully archived memory\"\n", - " },\n", - " \"request.chain.checkphish_processor_20250226133330.693396621\": {\n", - " \"duration\": \"93.431µs\"\n", - " },\n", - " \"request.chain.dlp_gcp_processor_20250226133330.693456416\": {\n", - " \"duration\": \"92.95µs\",\n", - " \"skipped\": \"warn: sensitive data protection is disabled for route:openai_univ\"\n", - " },\n", - " \"request.chain.promptinjectiondetection_processor_20250226133330.693555231\": {\n", - " \"duration\": \"57.604µs\",\n", - " \"skipped\": \"warn: prompt safety is disabled for route:openai_univ\"\n", - " },\n", - " \"request.chain.ratelimit_processor_20250226133330.693406418\": {\n", - " \"duration\": \"805.927µs\"\n", - " },\n", - " \"request.chain.secrets_processor_20250226133330.693475267\": {\n", - " \"duration\": \"16.301µs\"\n", - " },\n", - " \"request.chain.trustsafety_processor_20250226133330.693576559\": {\n", - " \"duration\": \"59.649µs\",\n", - " \"skipped\": \"warn: trust safety is disabled for route:openai_univ\"\n", - " },\n", - " \"response.chain.response_processor_20250226133330.693605797\": {\n", - " \"duration\": \"0s\"\n", - " },\n", - " \"response.chain.securityfilters_processor_20250226133330.693529078\": {\n", - " \"duration\": \"94.064µs\",\n", - " \"skipped\": \"warn: failed to get response from body:no content found for specified paths\"\n", - " },\n", - " \"response.chain.trustsafety_processor_20250226133330.693511739\": {\n", - " \"duration\": \"43.248µs\",\n", - " \"skipped\": \"warn: trust safety is disabled for route:openai_univ\"\n", - " }\n", - " },\n", - " \"route_name\": \"openai_univ\"\n", - " }\n", - "}\n" - ] - } - ], - "source": [ - "import os\n", - "from openai import OpenAI\n", - "from javelin_sdk import JavelinClient, JavelinConfig\n", - "from dotenv import load_dotenv\n", - "\n", - "load_dotenv()\n", - "\n", - "def init_openai_client():\n", - " api_key = os.getenv(\"OPENAI_API_KEY\")\n", - " return OpenAI(api_key=api_key)\n", - "\n", - "def init_javelin_client(openai_client, route_name=\"openai_univ\"):\n", - " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", - " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", - " client = JavelinClient(config)\n", - " client.register_openai(openai_client, route_name=route_name)\n", - " return client\n", - "\n", - "def openai_function_call_non_stream():\n", - " client = init_openai_client()\n", - " # Register with the unified endpoint\n", - " init_javelin_client(client)\n", - " response = client.chat.completions.create(\n", - " model=\"o3-mini\", # Latest o1 model\n", - " messages=[\n", - " {\"role\": \"user\", \"content\": \"What is the current weather in New York?\"}\n", - " ],\n", - " functions=[\n", - " {\n", - " \"name\": \"get_current_weather\",\n", - " \"description\": \"Retrieves current weather information\",\n", - " \"parameters\": {\n", - " \"type\": \"object\",\n", - " \"properties\": {\n", - " \"location\": {\n", - " \"type\": \"string\",\n", - " \"description\": \"City and state (e.g., New York, NY)\"\n", - " },\n", - " \"unit\": {\n", - " \"type\": \"string\",\n", - " \"enum\": [\"celsius\", \"fahrenheit\"]\n", - " }\n", - " },\n", - " \"required\": [\"location\"]\n", - " }\n", - " }\n", - " ],\n", - " function_call=\"auto\"\n", - " )\n", - " print(\"OpenAI Non-Streaming Response:\")\n", - " print(response.model_dump_json(indent=2))\n", - "\n", - "if __name__ == \"__main__\":\n", - " openai_function_call_non_stream()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Streaming with Function Calling" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "stream>>> \n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='', name='tell_fun_fact'), refusal=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='{\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='fact', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\":\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='G', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='ira', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='ffes', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' have', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' black', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' tongues', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' so', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' they', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' don', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='’t', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' get', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' sun', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='burn', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='ed', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' when', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' grasp', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='ing', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' leaves', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' in', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' the', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' hot', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=' sun', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='!\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='}', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B54xyXCjyKyitk5wxg3DDQ7t6YA8a', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None), finish_reason='function_call', index=0, logprobs=None)], created=1740550430, model='o1-2024-12-17', object='chat.completion.chunk', service_tier='default', system_fingerprint='fp_30d1ee942c', usage=None)\n", - "OpenAI Streaming Response:\n", - "\n" - ] - } - ], - "source": [ - "import os\n", - "from openai import OpenAI\n", - "from javelin_sdk import JavelinClient, JavelinConfig\n", - "\n", - "def init_openai_client():\n", - " api_key = os.getenv(\"OPENAI_API_KEY\")\n", - " return OpenAI(api_key=api_key)\n", - "\n", - "def init_javelin_client(openai_client, route_name=\"openai_univ\"):\n", - " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", - " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", - " client = JavelinClient(config)\n", - " client.register_openai(openai_client, route_name=route_name)\n", - " return client\n", - "\n", - "def openai_function_call_stream():\n", - " client = init_openai_client()\n", - " init_javelin_client(client)\n", - " stream = client.chat.completions.create(\n", - " model=\"o1\",\n", - " messages=[\n", - " {\"role\": \"user\", \"content\": \"Tell me a fun fact and then call a function.\"}\n", - " ],\n", - " functions=[\n", - " {\n", - " \"name\": \"tell_fun_fact\",\n", - " \"description\": \"Returns a fun fact\",\n", - " \"parameters\": {\n", - " \"type\": \"object\",\n", - " \"properties\": {\n", - " \"fact\": {\n", - " \"type\": \"string\",\n", - " \"description\": \"A fun fact about the topic\"\n", - " }\n", - " },\n", - " \"required\": [\"fact\"]\n", - " }\n", - " }\n", - " ],\n", - " function_call=\"auto\",\n", - " stream=True\n", - " )\n", - " collected = []\n", - " print(\"stream>>>\",stream)\n", - " for chunk in stream:\n", - " # Each chunk may contain a delta with part of the response\n", - " delta = chunk.choices[0].delta\n", - " print(chunk)\n", - " if hasattr(delta, \"content\") and delta.content:\n", - " collected.append(delta.content)\n", - " print(\"OpenAI Streaming Response:\")\n", - " print(\"\".join(collected))\n", - "\n", - "if __name__ == \"__main__\":\n", - " openai_function_call_stream()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Structured output calling Example" - ] - }, - { - "cell_type": "code", - "execution_count": 162, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Structured Output (JSON) Response:\n", - "{\n", - " \"id\": \"chatcmpl-B5D3I4NnbFAscgmT00ViE1kuocr8j\",\n", - " \"choices\": [\n", - " {\n", - " \"finish_reason\": \"stop\",\n", - " \"index\": 0,\n", - " \"logprobs\": null,\n", - " \"message\": {\n", - " \"content\": \"{\\n \\\"id\\\": \\\"1234\\\",\\n \\\"name\\\": \\\"Sample Item\\\",\\n \\\"description\\\": \\\"This item serves as a generic example of structured data output in JSON format.\\\",\\n \\\"attributes\\\": {\\n \\\"color\\\": \\\"blue\\\",\\n \\\"size\\\": \\\"medium\\\",\\n \\\"weight\\\": 2.5,\\n \\\"tags\\\": [\\\"example\\\", \\\"generic\\\", \\\"json\\\"]\\n }\\n}\",\n", - " \"refusal\": null,\n", - " \"role\": \"assistant\",\n", - " \"audio\": null,\n", - " \"function_call\": null,\n", - " \"tool_calls\": null\n", - " }\n", - " }\n", - " ],\n", - " \"created\": 1740581512,\n", - " \"model\": \"o3-mini-2025-01-31\",\n", - " \"object\": \"chat.completion\",\n", - " \"service_tier\": \"default\",\n", - " \"system_fingerprint\": \"fp_42bfad963b\",\n", - " \"usage\": {\n", - " \"completion_tokens\": 348,\n", - " \"prompt_tokens\": 71,\n", - " \"total_tokens\": 419,\n", - " \"completion_tokens_details\": {\n", - " \"accepted_prediction_tokens\": 0,\n", - " \"audio_tokens\": 0,\n", - " \"reasoning_tokens\": 256,\n", - " \"rejected_prediction_tokens\": 0\n", - " },\n", - " \"prompt_tokens_details\": {\n", - " \"audio_tokens\": 0,\n", - " \"cached_tokens\": 0\n", - " }\n", - " },\n", - " \"javelin\": {\n", - " \"archive_enabled\": true,\n", - " \"correlation_id\": \"01JN1BW8S43TFWN2Y5BSXBQ07B\",\n", - " \"model_endpoint_url\": \"https://api.openai.com/v1/chat/completions\",\n", - " \"model_latency\": \"56.640904548s\",\n", - " \"model_name\": \"o3-mini\",\n", - " \"processor_outputs\": {\n", - " \"request.chain.archive_processor_20250226145248.622212280\": {\n", - " \"duration\": \"3.818839ms\",\n", - " \"success\": \"successfully archived memory\"\n", - " },\n", - " \"request.chain.checkphish_processor_20250226145248.622112235\": {\n", - " \"duration\": \"377.893µs\"\n", - " },\n", - " \"request.chain.dlp_gcp_processor_20250226145248.622138920\": {\n", - " \"duration\": \"233.833µs\",\n", - " \"skipped\": \"warn: sensitive data protection is disabled for route:openai_univ\"\n", - " },\n", - " \"request.chain.promptinjectiondetection_processor_20250226145248.622164866\": {\n", - " \"duration\": \"124.6µs\",\n", - " \"skipped\": \"warn: prompt safety is disabled for route:openai_univ\"\n", - " },\n", - " \"request.chain.ratelimit_processor_20250226145248.621968259\": {\n", - " \"duration\": \"959.745µs\"\n", - " },\n", - " \"request.chain.secrets_processor_20250226145248.622014788\": {\n", - " \"duration\": \"11.468µs\"\n", - " },\n", - " \"request.chain.trustsafety_processor_20250226145248.621979219\": {\n", - " \"duration\": \"56.722µs\",\n", - " \"skipped\": \"warn: trust safety is disabled for route:openai_univ\"\n", - " },\n", - " \"response.chain.response_processor_20250226145248.622083140\": {\n", - " \"duration\": \"0s\"\n", - " },\n", - " \"response.chain.securityfilters_processor_20250226145248.622247394\": {\n", - " \"confidence\": 0.9655138378237373,\n", - " \"duration\": \"1.083219ms\",\n", - " \"entropy\": \"4.511831\",\n", - " \"language\": \"English\",\n", - " \"non_ascii_chars_detected\": \"true\"\n", - " },\n", - " \"response.chain.trustsafety_processor_20250226145248.622039203\": {\n", - " \"duration\": \"48.364µs\",\n", - " \"skipped\": \"warn: trust safety is disabled for route:openai_univ\"\n", - " }\n", - " },\n", - " \"route_name\": \"openai_univ\"\n", - " }\n", - "}\n", - "\n", - "Parsed JSON Output:\n", - "{\n", - " \"id\": \"1234\",\n", - " \"name\": \"Sample Item\",\n", - " \"description\": \"This item serves as a generic example of structured data output in JSON format.\",\n", - " \"attributes\": {\n", - " \"color\": \"blue\",\n", - " \"size\": \"medium\",\n", - " \"weight\": 2.5,\n", - " \"tags\": [\n", - " \"example\",\n", - " \"generic\",\n", - " \"json\"\n", - " ]\n", - " }\n", - "}\n" - ] - } - ], - "source": [ - "import os\n", - "import json\n", - "from openai import OpenAI\n", - "from javelin_sdk import JavelinClient, JavelinConfig\n", - "from dotenv import load_dotenv\n", - "\n", - "load_dotenv()\n", - "\n", - "def init_openai_client():\n", - " api_key = os.getenv(\"OPENAI_API_KEY\")\n", - " return OpenAI(api_key=api_key)\n", - "\n", - "def init_javelin_client(openai_client, route_name=\"openai_univ\"):\n", - " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", - " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", - " client = JavelinClient(config)\n", - " client.register_openai(openai_client, route_name=route_name)\n", - " return client\n", - "\n", - "def openai_structured_output_call_generic():\n", - " # Initialize clients\n", - " openai_client = init_openai_client()\n", - " init_javelin_client(openai_client)\n", - " \n", - " # Create messages with a system instruction to output valid JSON\n", - " messages = [\n", - " {\n", - " \"role\": \"system\",\n", - " \"content\": \"You are an assistant that always responds in valid JSON format without any additional text.\"\n", - " },\n", - " {\n", - " \"role\": \"user\",\n", - " \"content\": (\n", - " \"Provide a generic example of structured data output in JSON format. \"\n", - " \"The JSON should include the keys: 'id', 'name', 'description', \"\n", - " \"and 'attributes' (which should be a nested object with arbitrary key-value pairs).\"\n", - " )\n", - " }\n", - " ]\n", - " \n", - " response = openai_client.chat.completions.create(\n", - " model=\"o3-mini\", # can use o1 model as well\n", - " messages=messages,\n", - " )\n", - " \n", - " # Print the full API response for reference\n", - " print(\"Structured Output (JSON) Response:\")\n", - " print(response.model_dump_json(indent=2))\n", - " \n", - " # Extract the reply content using attribute access\n", - " try:\n", - " reply_content = response.choices[0].message.content\n", - " except (IndexError, AttributeError) as e:\n", - " print(\"Error extracting message content:\", e)\n", - " reply_content = \"\"\n", - " \n", - " # Attempt to parse the reply content as JSON\n", - " try:\n", - " json_output = json.loads(reply_content)\n", - " print(\"\\nParsed JSON Output:\")\n", - " print(json.dumps(json_output, indent=2))\n", - " except Exception as e:\n", - " print(\"\\nFailed to parse JSON output. Error:\", e)\n", - " print(\"Raw content:\", reply_content)\n", - "\n", - "if __name__ == \"__main__\":\n", - " openai_structured_output_call_generic()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Azure OpenAI – Unified Endpoint Examples" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Non-Streaming with Function Calling" - ] - }, - { - "cell_type": "code", - "execution_count": 152, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Azure OpenAI Non-Streaming Response:\n", - "{\n", - " \"id\": \"chatcmpl-B5BuG5zyeIE3eahuneeL9g5Ph7pXh\",\n", - " \"choices\": [\n", - " {\n", - " \"finish_reason\": \"function_call\",\n", - " \"index\": 0,\n", - " \"logprobs\": null,\n", - " \"message\": {\n", - " \"content\": null,\n", - " \"refusal\": null,\n", - " \"role\": \"assistant\",\n", - " \"function_call\": {\n", - " \"arguments\": \"{\\\"date\\\":\\\"2023-11-07\\\",\\\"time\\\":\\\"10:00:00\\\"}\",\n", - " \"name\": \"schedule_meeting\"\n", - " }\n", - " },\n", - " \"content_filter_results\": {}\n", - " }\n", - " ],\n", - " \"created\": 1740577108,\n", - " \"model\": \"gpt-4o-2024-11-20\",\n", - " \"object\": \"chat.completion\",\n", - " \"system_fingerprint\": \"fp_b705f0c291\",\n", - " \"usage\": {\n", - " \"completion_tokens\": 28,\n", - " \"prompt_tokens\": 73,\n", - " \"total_tokens\": 101,\n", - " \"completion_tokens_details\": {\n", - " \"accepted_prediction_tokens\": 0,\n", - " \"audio_tokens\": 0,\n", - " \"reasoning_tokens\": 0,\n", - " \"rejected_prediction_tokens\": 0\n", - " },\n", - " \"prompt_tokens_details\": {\n", - " \"audio_tokens\": 0,\n", - " \"cached_tokens\": 0\n", - " }\n", - " },\n", - " \"javelin\": {\n", - " \"archive_enabled\": true,\n", - " \"correlation_id\": \"01JN17NWBD6T6TWZT48YS0HVDZ\",\n", - " \"model_endpoint_url\": \"https://javelinpreview.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2023-07-01-preview\",\n", - " \"model_latency\": \"1.233102785s\",\n", - " \"model_name\": \"gpt-4o\",\n", - " \"processor_outputs\": {\n", - " \"request.chain.archive_processor_20250226133829.678735774\": {\n", - " \"duration\": \"5.012198ms\",\n", - " \"success\": \"successfully archived memory\"\n", - " },\n", - " \"request.chain.checkphish_processor_20250226133829.678975977\": {\n", - " \"duration\": \"71.216µs\"\n", - " },\n", - " \"request.chain.dlp_gcp_processor_20250226133829.678855752\": {\n", - " \"duration\": \"104.480149ms\"\n", - " },\n", - " \"request.chain.promptinjectiondetection_processor_20250226133829.678881889\": {\n", - " \"duration\": \"61.205µs\",\n", - " \"skipped\": \"warn: prompt safety is disabled for route:azureopenai_univ\"\n", - " },\n", - " \"request.chain.ratelimit_processor_20250226133829.678827468\": {\n", - " \"duration\": \"1.787483ms\"\n", - " },\n", - " \"request.chain.secrets_processor_20250226133829.678725520\": {\n", - " \"duration\": \"10.696µs\"\n", - " },\n", - " \"request.chain.trustsafety_processor_20250226133829.678919391\": {\n", - " \"duration\": \"251.622µs\",\n", - " \"skipped\": \"warn: trust safety is disabled for route:azureopenai_univ\"\n", - " },\n", - " \"response.chain.response_processor_20250226133829.678794065\": {\n", - " \"duration\": \"0s\"\n", - " },\n", - " \"response.chain.securityfilters_processor_20250226133829.678944081\": {\n", - " \"duration\": \"44.656µs\",\n", - " \"skipped\": \"warn: security filters is disabled for route:azureopenai_univ\"\n", - " },\n", - " \"response.chain.trustsafety_processor_20250226133829.678775692\": {\n", - " \"duration\": \"51.367µs\",\n", - " \"skipped\": \"warn: trust safety is disabled for route:azureopenai_univ\"\n", - " }\n", - " },\n", - " \"route_name\": \"azureopenai_univ\"\n", - " },\n", - " \"prompt_filter_results\": [\n", - " {\n", - " \"content_filter_results\": {\n", - " \"hate\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"jailbreak\": {\n", - " \"detected\": false,\n", - " \"filtered\": false\n", - " },\n", - " \"self_harm\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"sexual\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"violence\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " }\n", - " },\n", - " \"prompt_index\": 0\n", - " }\n", - " ]\n", - "}\n" - ] - } - ], - "source": [ - "import os\n", - "from openai import AzureOpenAI\n", - "from javelin_sdk import JavelinClient, JavelinConfig\n", - "\n", - "def init_azure_client():\n", - " azure_api_key = os.getenv(\"AZURE_OPENAI_API_KEY\")\n", - " return AzureOpenAI(\n", - " api_version=\"2023-07-01-preview\",\n", - " azure_endpoint=\"https://javelinpreview.openai.azure.com\",\n", - " api_key=azure_api_key\n", - " )\n", - "\n", - "def init_javelin_client_azure(azure_client, route_name=\"azureopenai_univ\"):\n", - " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", - " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", - " client = JavelinClient(config)\n", - " client.register_azureopenai(azure_client, route_name=route_name)\n", - " return client\n", - "\n", - "def azure_function_call_non_stream():\n", - " azure_client = init_azure_client()\n", - " init_javelin_client_azure(azure_client)\n", - " response = azure_client.chat.completions.create(\n", - " model=\"gpt-4o\",\n", - " messages=[\n", - " {\"role\": \"user\", \"content\": \"Schedule a meeting at 10 AM tomorrow.\"}\n", - " ],\n", - " functions=[\n", - " {\n", - " \"name\": \"schedule_meeting\",\n", - " \"description\": \"Schedules a meeting in the calendar\",\n", - " \"parameters\": {\n", - " \"type\": \"object\",\n", - " \"properties\": {\n", - " \"time\": {\"type\": \"string\", \"description\": \"Meeting time (ISO format)\"},\n", - " \"date\": {\"type\": \"string\", \"description\": \"Meeting date (YYYY-MM-DD)\"}\n", - " },\n", - " \"required\": [\"time\", \"date\"]\n", - " }\n", - " }\n", - " ],\n", - " function_call=\"auto\"\n", - " )\n", - " print(\"Azure OpenAI Non-Streaming Response:\")\n", - " print(response.to_json())\n", - "\n", - "if __name__ == \"__main__\":\n", - " azure_function_call_non_stream()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Structured output calling - Azure Open Ai Example" - ] - }, - { - "cell_type": "code", - "execution_count": 164, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Structured Output (JSON) Response:\n", - "{\n", - " \"id\": \"chatcmpl-B5DB37rD9OIR5BnSbP5iaFonLe2I8\",\n", - " \"choices\": [\n", - " {\n", - " \"finish_reason\": \"stop\",\n", - " \"index\": 0,\n", - " \"logprobs\": null,\n", - " \"message\": {\n", - " \"content\": \"```json\\n{\\n \\\"id\\\": 1,\\n \\\"title\\\": \\\"Sample Data\\\",\\n \\\"description\\\": \\\"This is an example of structured JSON data.\\\",\\n \\\"metadata\\\": {\\n \\\"author\\\": \\\"John Doe\\\",\\n \\\"created_at\\\": \\\"2023-10-01\\\",\\n \\\"tags\\\": [\\\"example\\\", \\\"json\\\", \\\"data\\\"],\\n \\\"version\\\": 1.0\\n }\\n}\\n```\",\n", - " \"refusal\": null,\n", - " \"role\": \"assistant\"\n", - " },\n", - " \"content_filter_results\": {\n", - " \"hate\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"protected_material_code\": {\n", - " \"detected\": false,\n", - " \"filtered\": false\n", - " },\n", - " \"protected_material_text\": {\n", - " \"detected\": false,\n", - " \"filtered\": false\n", - " },\n", - " \"self_harm\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"sexual\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"violence\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " }\n", - " }\n", - " }\n", - " ],\n", - " \"created\": 1740581993,\n", - " \"model\": \"gpt-4o-2024-11-20\",\n", - " \"object\": \"chat.completion\",\n", - " \"system_fingerprint\": \"fp_b705f0c291\",\n", - " \"usage\": {\n", - " \"completion_tokens\": 85,\n", - " \"prompt_tokens\": 74,\n", - " \"total_tokens\": 159,\n", - " \"completion_tokens_details\": {\n", - " \"accepted_prediction_tokens\": 0,\n", - " \"audio_tokens\": 0,\n", - " \"reasoning_tokens\": 0,\n", - " \"rejected_prediction_tokens\": 0\n", - " },\n", - " \"prompt_tokens_details\": {\n", - " \"audio_tokens\": 0,\n", - " \"cached_tokens\": 0\n", - " }\n", - " },\n", - " \"javelin\": {\n", - " \"archive_enabled\": true,\n", - " \"correlation_id\": \"01JN1CAYD7WKJ9YMQHNRT0DW78\",\n", - " \"model_endpoint_url\": \"https://javelinpreview.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2023-07-01-preview\",\n", - " \"model_latency\": \"1.275729182s\",\n", - " \"model_name\": \"gpt-4o\",\n", - " \"processor_outputs\": {\n", - " \"request.chain.archive_processor_20250226145954.357026188\": {\n", - " \"duration\": \"7.865223ms\",\n", - " \"success\": \"successfully archived memory\"\n", - " },\n", - " \"request.chain.checkphish_processor_20250226145954.357090711\": {\n", - " \"duration\": \"73.306µs\"\n", - " },\n", - " \"request.chain.dlp_gcp_processor_20250226145954.357373533\": {\n", - " \"duration\": \"197.768806ms\"\n", - " },\n", - " \"request.chain.promptinjectiondetection_processor_20250226145954.356969553\": {\n", - " \"duration\": \"59.877µs\",\n", - " \"skipped\": \"warn: prompt safety is disabled for route:azureopenai_univ\"\n", - " },\n", - " \"request.chain.ratelimit_processor_20250226145954.357111515\": {\n", - " \"duration\": \"2.095796ms\"\n", - " },\n", - " \"request.chain.secrets_processor_20250226145954.357013668\": {\n", - " \"duration\": \"13.467µs\"\n", - " },\n", - " \"request.chain.trustsafety_processor_20250226145954.356982735\": {\n", - " \"duration\": \"43.126µs\",\n", - " \"skipped\": \"warn: trust safety is disabled for route:azureopenai_univ\"\n", - " },\n", - " \"response.chain.response_processor_20250226145954.357067882\": {\n", - " \"duration\": \"0s\"\n", - " },\n", - " \"response.chain.securityfilters_processor_20250226145954.357042078\": {\n", - " \"duration\": \"105.719µs\",\n", - " \"skipped\": \"warn: security filters is disabled for route:azureopenai_univ\"\n", - " },\n", - " \"response.chain.trustsafety_processor_20250226145954.357494465\": {\n", - " \"duration\": \"94.684µs\",\n", - " \"skipped\": \"warn: trust safety is disabled for route:azureopenai_univ\"\n", - " }\n", - " },\n", - " \"route_name\": \"azureopenai_univ\"\n", - " },\n", - " \"prompt_filter_results\": [\n", - " {\n", - " \"content_filter_results\": {\n", - " \"hate\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"jailbreak\": {\n", - " \"detected\": false,\n", - " \"filtered\": false\n", - " },\n", - " \"self_harm\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"sexual\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " },\n", - " \"violence\": {\n", - " \"filtered\": false,\n", - " \"severity\": \"safe\"\n", - " }\n", - " },\n", - " \"prompt_index\": 0\n", - " }\n", - " ]\n", - "}\n", - "\n", - "Parsed JSON Output:\n", - "{\n", - " \"id\": 1,\n", - " \"title\": \"Sample Data\",\n", - " \"description\": \"This is an example of structured JSON data.\",\n", - " \"metadata\": {\n", - " \"author\": \"John Doe\",\n", - " \"created_at\": \"2023-10-01\",\n", - " \"tags\": [\n", - " \"example\",\n", - " \"json\",\n", - " \"data\"\n", - " ],\n", - " \"version\": 1.0\n", - " }\n", - "}\n" - ] - } - ], - "source": [ - "import os\n", - "import json\n", - "import re\n", - "from openai import AzureOpenAI\n", - "from javelin_sdk import JavelinClient, JavelinConfig\n", - "from dotenv import load_dotenv\n", - "\n", - "load_dotenv()\n", - "\n", - "def init_azure_client():\n", - " azure_api_key = os.getenv(\"AZURE_OPENAI_API_KEY\")\n", - " return AzureOpenAI(\n", - " api_version=\"2023-07-01-preview\",\n", - " azure_endpoint=\"https://javelinpreview.openai.azure.com\",\n", - " api_key=azure_api_key\n", - " )\n", - "\n", - "def init_javelin_client_azure(azure_client, route_name=\"azureopenai_univ\"):\n", - " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", - " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", - " client = JavelinClient(config)\n", - " client.register_azureopenai(azure_client, route_name=route_name)\n", - " return client\n", - "\n", - "def extract_json_from_markdown(text: str) -> str:\n", - " \"\"\"\n", - " Extracts JSON content from a markdown code block if present.\n", - " For example, removes leading and trailing triple backticks.\n", - " \"\"\"\n", - " match = re.search(r\"```(?:json)?\\s*(\\{.*\\})\\s*```\", text, re.DOTALL)\n", - " if match:\n", - " return match.group(1)\n", - " return text.strip()\n", - "\n", - "def azure_structured_output_call():\n", - " azure_client = init_azure_client()\n", - " init_javelin_client_azure(azure_client)\n", - "\n", - " # System message enforces structured JSON output without extra text\n", - " messages = [\n", - " {\n", - " \"role\": \"system\",\n", - " \"content\": \"You are an assistant that always responds in valid JSON format without any additional text.\"\n", - " },\n", - " {\n", - " \"role\": \"user\",\n", - " \"content\": (\n", - " \"Provide structured data in JSON format. \"\n", - " \"The JSON should contain the following keys: 'id' (integer), 'title' (string), \"\n", - " \"'description' (string), and 'metadata' (a nested object with arbitrary key-value pairs).\"\n", - " )\n", - " }\n", - " ]\n", - "\n", - " response = azure_client.chat.completions.create(\n", - " model=\"gpt-4o\",\n", - " messages=messages\n", - " )\n", - "\n", - " # Print the full API response for reference\n", - " print(\"Structured Output (JSON) Response:\")\n", - " print(response.to_json())\n", - "\n", - " # Extract and clean the reply content\n", - " try:\n", - " reply_content = response.choices[0].message.content # Get the raw response text\n", - " # Remove markdown code fences if present\n", - " reply_content_clean = extract_json_from_markdown(reply_content)\n", - " json_output = json.loads(reply_content_clean) # Parse as JSON\n", - " print(\"\\nParsed JSON Output:\")\n", - " print(json.dumps(json_output, indent=2))\n", - " except Exception as e:\n", - " print(\"\\nFailed to parse JSON output. Error:\", e)\n", - " print(\"Raw content:\", reply_content)\n", - "\n", - "if __name__ == \"__main__\":\n", - " azure_structured_output_call()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Streaming with Function Calling" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Azure OpenAI Non-Streaming Response:\n", - "ChatCompletionChunk(id='', choices=[], created=0, model='', object='', service_tier=None, system_fingerprint=None, usage=None, prompt_filter_results=[{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'jailbreak': {'filtered': False, 'detected': False}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}])\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='', name='schedule_meeting'), refusal=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='{\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='date', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\":\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='202', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='3', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='-', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='11', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='-', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='17', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\",\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='time', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\":\"', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='10', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=':', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='00', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments=':', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='00', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=ChoiceDeltaFunctionCall(arguments='\"}', name=None), refusal=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n", - "ChatCompletionChunk(id='chatcmpl-B557pUz4IkUX7UuDCRvgKypQetQ6H', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None), finish_reason='function_call', index=0, logprobs=None, content_filter_results={})], created=1740551041, model='gpt-4o-2024-11-20', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_b705f0c291', usage=None)\n" - ] - } - ], - "source": [ - "import os\n", - "from openai import AzureOpenAI\n", - "from javelin_sdk import JavelinClient, JavelinConfig\n", - "\n", - "def init_azure_client():\n", - " azure_api_key = os.getenv(\"AZURE_OPENAI_API_KEY\")\n", - " return AzureOpenAI(\n", - " api_version=\"2023-07-01-preview\",\n", - " azure_endpoint=\"https://javelinpreview.openai.azure.com\",\n", - " api_key=azure_api_key\n", - " )\n", - "\n", - "def init_javelin_client_azure(azure_client, route_name=\"azureopenai_univ\"):\n", - " javelin_api_key = os.getenv(\"JAVELIN_API_KEY\")\n", - " config = JavelinConfig(javelin_api_key=javelin_api_key)\n", - " client = JavelinClient(config)\n", - " client.register_azureopenai(azure_client, route_name=route_name)\n", - " return client\n", - "\n", - "def azure_function_call_non_stream():\n", - " azure_client = init_azure_client()\n", - " init_javelin_client_azure(azure_client)\n", - " stream = azure_client.chat.completions.create(\n", - " model=\"gpt-4o\",\n", - " messages=[\n", - " {\"role\": \"user\", \"content\": \"Schedule a meeting at 10 AM tomorrow.\"}\n", - " ],\n", - " functions=[\n", - " {\n", - " \"name\": \"schedule_meeting\",\n", - " \"description\": \"Schedules a meeting in the calendar\",\n", - " \"parameters\": {\n", - " \"type\": \"object\",\n", - " \"properties\": {\n", - " \"time\": {\"type\": \"string\", \"description\": \"Meeting time (ISO format)\"},\n", - " \"date\": {\"type\": \"string\", \"description\": \"Meeting date (YYYY-MM-DD)\"}\n", - " },\n", - " \"required\": [\"time\", \"date\"]\n", - " }\n", - " }\n", - " ],\n", - " function_call=\"auto\",\n", - " stream=True\n", - " )\n", - " print(\"Azure OpenAI Non-Streaming Response:\")\n", - " for chunk in stream:\n", - " print(chunk)\n", - "\n", - "if __name__ == \"__main__\":\n", - " azure_function_call_non_stream()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# OpenAI – Regular Route Endpoint Examples" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Non-Streaming with Function Calling" - ] - }, - { - "cell_type": "code", - "execution_count": 170, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "from javelin_sdk import JavelinClient, JavelinConfig, RouteNotFoundError\n", - "\n", - "# Retrieve API keys from environment variables\n", - "javelin_api_key = os.getenv('JAVELIN_API_KEY')\n", - "llm_api_key = os.getenv(\"OPENAI_API_KEY\")\n", - "\n", - "if not javelin_api_key or not llm_api_key:\n", - " raise ValueError(\"Both JAVELIN_API_KEY and OPENAI_API_KEY must be set.\")\n", - "\n", - "print(\"OpenAI LLM API Key:\", llm_api_key)\n", - "\n", - "# Configure the Javelin client\n", - "config = JavelinConfig(\n", - " base_url=\"https://api-dev.javelin.live\",\n", - " javelin_api_key=javelin_api_key,\n", - " llm_api_key=llm_api_key,\n", - ")\n", - "client = JavelinClient(config)\n", - "print(\"Successfully connected to Javelin Client for OpenAI\")\n", - "\n", - "# Prepare query data with function calling support\n", - "query_data = {\n", - " \"messages\": [\n", - " {\"role\": \"system\", \"content\": \"You are a helpful assistant that translates English to French.\"},\n", - " {\"role\": \"user\", \"content\": \"AI has the power to transform humanity and make the world a better place.\"},\n", - " ],\n", - " \"functions\": [\n", - " {\n", - " \"name\": \"translate_text\",\n", - " \"description\": \"Translates English text to French\",\n", - " \"parameters\": {\n", - " \"type\": \"object\",\n", - " \"properties\": {\n", - " \"text\": {\n", - " \"type\": \"string\",\n", - " \"description\": \"Text to translate\"\n", - " }\n", - " },\n", - " \"required\": [\"text\"]\n", - " }\n", - " }\n", - " ],\n", - " \"function_call\": \"auto\"\n", - "}\n", - "\n", - "# Query the LLM using the \"openai\" route\n", - "try:\n", - " response = client.query_route(\"openai\", query_data)\n", - " print(\"Response from OpenAI Regular Endpoint:\")\n", - " print(response)\n", - "except RouteNotFoundError:\n", - " print(\"Route 'openai' Not Found\")\n", - "except Exception as e:\n", - " print(\"Error querying OpenAI endpoint:\", e)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Streaming with Function Calling" - ] - }, - { - "cell_type": "code", - "execution_count": 171, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "from javelin_sdk import JavelinClient, JavelinConfig, RouteNotFoundError\n", - "\n", - "# Retrieve API keys from environment variables\n", - "javelin_api_key = os.getenv('JAVELIN_API_KEY')\n", - "llm_api_key = os.getenv(\"OPENAI_API_KEY\")\n", - "\n", - "if not javelin_api_key or not llm_api_key:\n", - " raise ValueError(\"Both JAVELIN_API_KEY and OPENAI_API_KEY must be set.\")\n", - "\n", - "print(\"OpenAI LLM API Key:\", llm_api_key)\n", - "\n", - "# Configure the Javelin client\n", - "config = JavelinConfig(\n", - " base_url=\"https://api-dev.javelin.live\",\n", - " javelin_api_key=javelin_api_key,\n", - " llm_api_key=llm_api_key,\n", - ")\n", - "client = JavelinClient(config)\n", - "print(\"Successfully connected to Javelin Client for OpenAI\")\n", - "\n", - "# Prepare query data with function calling support\n", - "query_data = {\n", - " \"messages\": [\n", - " {\"role\": \"system\", \"content\": \"You are a helpful assistant that translates English to French.\"},\n", - " {\"role\": \"user\", \"content\": \"AI has the power to transform humanity and make the world a better place.\"},\n", - " ],\n", - " \"functions\": [\n", - " {\n", - " \"name\": \"translate_text\",\n", - " \"description\": \"Translates English text to French\",\n", - " \"parameters\": {\n", - " \"type\": \"object\",\n", - " \"properties\": {\n", - " \"text\": {\n", - " \"type\": \"string\",\n", - " \"description\": \"Text to translate\"\n", - " }\n", - " },\n", - " \"required\": [\"text\"]\n", - " }\n", - " }\n", - " ],\n", - " \"function_call\": \"auto\",\n", - " # \"stream\": True\n", - "}\n", - "\n", - "# Query the LLM using the \"openai\" route\n", - "try:\n", - " response = client.query_route(\"openai\", query_data)\n", - " print(\"Response from OpenAI Regular Endpoint:\")\n", - "\n", - " # If streaming is enabled, iterate over the stream of response chunks\n", - " if query_data.get(\"stream\"):\n", - " for chunk in response:\n", - " print(chunk)\n", - " else:\n", - " print(response)\n", - "except RouteNotFoundError:\n", - " print(\"Route 'openai' Not Found\")\n", - "except Exception as e:\n", - " print(\"Error querying OpenAI endpoint:\", e)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "venv", - "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.12.8" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}