From b74077a364cadd52090a0f40093f1ca5007ec4a7 Mon Sep 17 00:00:00 2001 From: Dhruvj07 Date: Tue, 8 Apr 2025 15:41:13 +0530 Subject: [PATCH 1/3] fix: update SDK calls to prevent failures in E2E pipeline --- examples/azure-openai/azure_general_route.py | 4 +- examples/bedrock/bedrock_client_universal.py | 3 +- .../bedrock/langchain-bedrock-universal.py | 3 +- examples/gemini/gemini-universal.py | 227 ++++-------------- examples/openai/o1-03_function-calling.py | 2 +- examples/openai/openai-universal.py | 1 - examples/openai/openai_general_route.py | 42 ++-- 7 files changed, 73 insertions(+), 209 deletions(-) diff --git a/examples/azure-openai/azure_general_route.py b/examples/azure-openai/azure_general_route.py index bcd074f..54f8455 100644 --- a/examples/azure-openai/azure_general_route.py +++ b/examples/azure-openai/azure_general_route.py @@ -113,8 +113,8 @@ async def init_async_azure_client(): # Include the API version in the base URL for the async client. client = AsyncOpenAI( api_key=llm_api_key, - base_url="https://api-dev.javelin.live/v1/query/azure-openai?api-version=2024-02-15-preview", - default_headers=javelin_headers + base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1/query/azure-openai",, + default_headers=javelin_headers, ) return client except Exception as e: diff --git a/examples/bedrock/bedrock_client_universal.py b/examples/bedrock/bedrock_client_universal.py index 8c71228..71d2b9e 100644 --- a/examples/bedrock/bedrock_client_universal.py +++ b/examples/bedrock/bedrock_client_universal.py @@ -23,8 +23,7 @@ def init_bedrock(): # Initialize Javelin Client (if you want the route registered) config = JavelinConfig( - javelin_api_key=os.getenv("JAVELIN_API_KEY"), # Replace with your Javelin API key - base_url=os.getenv("JAVELIN_BASE_URL") + javelin_api_key=os.getenv("JAVELIN_API_KEY") # Replace with your Javelin API key ) javelin_client = JavelinClient(config) diff --git a/examples/bedrock/langchain-bedrock-universal.py b/examples/bedrock/langchain-bedrock-universal.py index c785ef6..61199c7 100644 --- a/examples/bedrock/langchain-bedrock-universal.py +++ b/examples/bedrock/langchain-bedrock-universal.py @@ -27,8 +27,7 @@ def init_bedrock(): # Initialize Javelin client config = JavelinConfig( - javelin_api_key=os.getenv("JAVELIN_API_KEY"), # add your Javelin API key here - base_url=os.getenv("JAVELIN_BASE_URL") + javelin_api_key=os.getenv("JAVELIN_API_KEY") # add your Javelin API key here ) javelin_client = JavelinClient(config) diff --git a/examples/gemini/gemini-universal.py b/examples/gemini/gemini-universal.py index 9d0e0ab..7703a2c 100644 --- a/examples/gemini/gemini-universal.py +++ b/examples/gemini/gemini-universal.py @@ -1,61 +1,30 @@ import json import os - from dotenv import load_dotenv - -# The official OpenAI Python library with Gemini support (via Javelin) from openai import OpenAI from pydantic import BaseModel - from javelin_sdk import JavelinClient, JavelinConfig load_dotenv() - -# ----------------------------------------------------------------------------- -# 1) Initialize Gemini + Javelin -# ----------------------------------------------------------------------------- def init_gemini_client(): - """ - Sets environment variables for Gemini and Javelin, creates an OpenAI client, - registers it with Javelin, and returns the configured client. - """ - print("Initializing Gemini client...") - - # Hard-coded environment variable assignment (for demonstration) - # You may prefer to do: gemini_api_key = os.environ.get("GEMINI_API_KEY") in real usage. - - gemini_api_key = os.getenv("GEMINI_API_KEY") # define your gemini api key here + gemini_api_key = os.getenv("GEMINI_API_KEY") if not gemini_api_key: raise ValueError("GEMINI_API_KEY is not set!") - print("Gemini API Key loaded successfully.") - # Create an OpenAI client configured for Gemini openai_client = OpenAI( api_key=gemini_api_key, base_url="https://generativelanguage.googleapis.com/v1beta/openai/", ) - # Javelin configuration - - javelin_api_key = os.getenv("JAVELIN_API_KEY") # define your javelin api key here - config = JavelinConfig(javelin_api_key=javelin_api_key, base_url=os.getenv("JAVELIN_BASE_URL")) + javelin_api_key = os.getenv("JAVELIN_API_KEY") + config = JavelinConfig(javelin_api_key=javelin_api_key) client = JavelinClient(config) - rout_name = "google_univ" # define your universal route name here - # Register the Gemini client with Javelin - client.register_gemini(openai_client, route_name=rout_name) + client.register_gemini(openai_client, route_name="google_univ") return openai_client - -# ----------------------------------------------------------------------------- -# 2) Chat Completions -# ----------------------------------------------------------------------------- def gemini_chat_completions(client): - """ - Demonstrates a basic chat completion with Gemini. - Returns the JSON string of the response. - """ response = client.chat.completions.create( model="gemini-1.5-flash", n=1, @@ -64,125 +33,55 @@ def gemini_chat_completions(client): {"role": "user", "content": "Explain to me how AI works"}, ], ) - return response.model_dump_json(indent=2) - - -# ----------------------------------------------------------------------------- -# 3) Streaming -# ----------------------------------------------------------------------------- -def gemini_streaming(client): - """ - Demonstrates streaming a response from Gemini. - Returns a concatenated string of all streamed tokens for demonstration. - """ - response = client.chat.completions.create( - model="gemini-1.5-flash", - messages=[ - {"role": "system", "content": "You are a helpful assistant."}, - {"role": "user", "content": "Hello!"}, - ], - stream=True, - ) + return response - # Accumulate partial content in a list - streamed_content = [] - for chunk in response: - if chunk.choices and chunk.choices[0].delta: - delta_content = chunk.choices[0].delta - # If delta_content has a .dict() method (e.g. it's a Pydantic model), use it. - if hasattr(delta_content, "dict"): - dumped = json.dumps(delta_content.dict()) - else: - dumped = json.dumps(delta_content) - streamed_content.append(dumped) - - # Join all chunk data with newlines - return "\n".join(streamed_content) - - -# ----------------------------------------------------------------------------- -# 4) Function calling -# ----------------------------------------------------------------------------- def gemini_function_calling(client): - """ - Demonstrates a function-calling scenario with Gemini (like OpenAI Tools). - Returns JSON string of the response. - """ - tools = [ - { - "type": "function", - "function": { - "name": "get_weather", - "description": "Get the weather in a given location", - "parameters": { - "type": "object", - "properties": { - "location": { - "type": "string", - "description": "The city and state, e.g. Chicago, IL", - }, - "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + tools = [{ + "type": "function", + "function": { + "name": "get_weather", + "description": "Get the weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "City and state, e.g. Chicago, IL" }, - "required": ["location"], + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, - }, + "required": ["location"] + } } - ] - messages = [ - {"role": "user", "content": "What's the weather like in Chicago today?"} - ] + }] + messages = [{"role": "user", "content": "What's the weather like in Chicago today?"}] response = client.chat.completions.create( model="gemini-1.5-flash", messages=messages, tools=tools, tool_choice="auto" ) return response.model_dump_json(indent=2) - -# ----------------------------------------------------------------------------- -# 5) Structured output -# ----------------------------------------------------------------------------- class CalendarEvent(BaseModel): name: str date: str participants: list[str] - def gemini_structured_output(client): - """ - Demonstrates how to request structured JSON output from Gemini - using the 'parse' endpoint (beta). - Returns the JSON string of the structured result. - """ completion = client.beta.chat.completions.parse( model="gemini-1.5-flash", messages=[ {"role": "system", "content": "Extract the event information."}, - { - "role": "user", - "content": "John and Susan are going to an AI conference on Friday.", - }, + {"role": "user", "content": "John and Susan are going to an AI conference on Friday."}, ], response_format=CalendarEvent, ) return completion.model_dump_json(indent=2) - -# ----------------------------------------------------------------------------- -# 6) Embeddings -# ----------------------------------------------------------------------------- def gemini_embeddings(client): - """ - Demonstrates generating embeddings using Gemini. - Returns the JSON string of the embeddings response. - """ response = client.embeddings.create( input="Your text string goes here", model="text-embedding-004" ) return response.model_dump_json(indent=2) - -# ----------------------------------------------------------------------------- -# 7) Main -# ----------------------------------------------------------------------------- def main(): print("=== Gemini Example ===") try: @@ -191,66 +90,44 @@ def main(): print(f"Error initializing Gemini client: {e}") return - # 1) Chat Completions - # print("\n--- Gemini: Chat Completions ---") - # try: - # chat_response = gemini_chat_completions(gemini_client) - # if not chat_response.strip(): - # print("Error: Empty response failed") - # else: - # print(chat_response) - # except Exception as e: - # print(f"Error in chat completions: {e}") - - # 2) Streaming - print("\n--- Gemini: Streaming ---") + # 1. Chat Completion + print("\n--- Gemini: Chat Completions ---") try: - stream_response = gemini_streaming(gemini_client) - - if not stream_response.strip(): - print("Error: Empty response failed") + response = gemini_chat_completions(gemini_client) + content = response.choices[0].message.content.strip() + preview = content[:300] + "..." if len(content) > 300 else content + if content: + print(f"✅ passed → {preview}") else: - print("Streaming output:") - print(stream_response) + print("❌ failed - Empty response") except Exception as e: - print(f"Error in streaming: {e}") + print(f"❌ failed - Error in chat completions: {e}") - # # 3) Function Calling - # print("\n--- Gemini: Function Calling ---") - # try: - # func_response = gemini_function_calling(gemini_client) - # if not func_response.strip(): - # print("Error: Empty response failed") - # else: - # print(func_response) - # except Exception as e: - # print(f"Error in function calling: {e}") - - # # 4) Structured Output - # print("\n--- Gemini: Structured Output ---") - # try: - # structured_response = gemini_structured_output(gemini_client) - # if not structured_response.strip(): - # print("Error: Empty response failed") - # else: - # print(structured_response) - # except Exception as e: - # print(f"Error in structured output: {e}") + # 2. Function Calling + print("\n--- Gemini: Function Calling ---") + try: + func_response = gemini_function_calling(gemini_client) + print(func_response) + except Exception as e: + print(f"❌ failed - Error in function calling: {e}") - # # 5) Embeddings - # print("\n--- Gemini: Embeddings ---") - # try: - # embeddings_response = gemini_embeddings(gemini_client) - # if not embeddings_response.strip(): - # print("Error: Empty response failed") - # else: - # print(embeddings_response) + # 3. Structured Output + print("\n--- Gemini: Structured Output ---") + try: + structured_response = gemini_structured_output(gemini_client) + print(structured_response) + except Exception as e: + print(f"❌ failed - Error in structured output: {e}") - # except Exception as e: - # print(f"Error in embeddings: {e}") + # 4. Embeddings + print("\n--- Gemini: Embeddings ---") + try: + embeddings_response = gemini_embeddings(gemini_client) + print(embeddings_response) + except Exception as e: + print(f"❌ failed - Error in embeddings: {e}") print("\nScript Complete") - if __name__ == "__main__": main() diff --git a/examples/openai/o1-03_function-calling.py b/examples/openai/o1-03_function-calling.py index 7a02033..e02b7e0 100644 --- a/examples/openai/o1-03_function-calling.py +++ b/examples/openai/o1-03_function-calling.py @@ -19,7 +19,7 @@ def init_openai_client(): 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, base_url=os.getenv("JAVELIN_BASE_URL")) + config = JavelinConfig(javelin_api_key=javelin_api_key) client = JavelinClient(config) client.register_openai(openai_client, route_name=route_name) return client diff --git a/examples/openai/openai-universal.py b/examples/openai/openai-universal.py index 0765cad..f63bc92 100644 --- a/examples/openai/openai-universal.py +++ b/examples/openai/openai-universal.py @@ -39,7 +39,6 @@ def init_javelin_client_sync(openai_client): ) # define your javelin api key here config = JavelinConfig( javelin_api_key=javelin_api_key, - base_url=os.getenv("JAVELIN_BASE_URL"), ) client = JavelinClient(config) rout_name = "openai_univ" # define your universal route name here diff --git a/examples/openai/openai_general_route.py b/examples/openai/openai_general_route.py index 88d4cfb..cc4f164 100644 --- a/examples/openai/openai_general_route.py +++ b/examples/openai/openai_general_route.py @@ -19,6 +19,7 @@ def init_sync_openai_client(): javelin_api_key = os.getenv("JAVELIN_API_KEY") javelin_headers = {"x-api-key": javelin_api_key} print(f"[DEBUG] Synchronous OpenAI client key: {openai_api_key}") + # This client is configured for chat completions. return OpenAI( api_key=openai_api_key, base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1/query/openai", @@ -70,26 +71,24 @@ def sync_openai_chat_completions(openai_client): except Exception as e: raise e -def sync_openai_completions(openai_client): - """Call OpenAI's Completions endpoint (synchronously).""" +def sync_openai_embeddings(_): + """Call OpenAI's Embeddings endpoint (synchronously) using a dedicated embeddings client. + + This function creates a new OpenAI client instance pointing to the embeddings endpoint. + """ try: - response = openai_client.completions.create( - model="gpt-3.5-turbo-instruct", - prompt="What is machine learning?", - max_tokens=7, - temperature=0 + openai_api_key = os.getenv("OPENAI_API_KEY") + javelin_api_key = os.getenv("JAVELIN_API_KEY") + javelin_headers = {"x-api-key": javelin_api_key} + # Create a new client instance for embeddings. + embeddings_client = OpenAI( + api_key=openai_api_key, + base_url="https://api-dev.javelin.live/v1/query/openai_embeddings", + default_headers=javelin_headers ) - return response.model_dump_json(indent=2) - except Exception as e: - raise e - -def sync_openai_embeddings(openai_client): - """Call OpenAI's Embeddings endpoint (synchronously).""" - try: - response = openai_client.embeddings.create( - model="text-embedding-ada-002", + response = embeddings_client.embeddings.create( + model="text-embedding-3-small", input="The food was delicious and the waiter...", - encoding_format="float" ) return response.model_dump_json(indent=2) except Exception as e: @@ -172,15 +171,6 @@ def main(): except Exception as e: print(f"[DEBUG] Error in chat completions: {e}") - print("\n--- Completions ---") - try: - completions_response = sync_openai_completions(openai_client) - if not completions_response.strip(): - print("[DEBUG] Error: Empty completions response") - else: - print(completions_response) - except Exception as e: - print(f"[DEBUG] Error in completions: {e}") print("\n--- Embeddings ---") try: From d446811b14eb7f9da9cfaf69ea05ea0305d2e63e Mon Sep 17 00:00:00 2001 From: Dhruvj07 Date: Tue, 8 Apr 2025 16:27:20 +0530 Subject: [PATCH 2/3] fix: Remove extra comma --- javelin_sdk/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javelin_sdk/client.py b/javelin_sdk/client.py index ee216de..439b830 100644 --- a/javelin_sdk/client.py +++ b/javelin_sdk/client.py @@ -708,7 +708,7 @@ def override_endpoint_url(request: Any, **kwargs) -> None: updated_url = original_url._replace( scheme=parsed_base.scheme, netloc=parsed_base.netloc, - path=f"/v1{original_url.path}", + path=f"{original_url.path}", ) request.url = urlunparse(updated_url) From 0af6af6fd7abd959b546a4b776349c94d9b5047f Mon Sep 17 00:00:00 2001 From: Dhruvj07 Date: Tue, 8 Apr 2025 16:41:44 +0530 Subject: [PATCH 3/3] fix:revert client changes --- examples/azure-openai/azure_general_route.py | 2 +- javelin_sdk/client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/azure-openai/azure_general_route.py b/examples/azure-openai/azure_general_route.py index 54f8455..d29e538 100644 --- a/examples/azure-openai/azure_general_route.py +++ b/examples/azure-openai/azure_general_route.py @@ -113,7 +113,7 @@ async def init_async_azure_client(): # Include the API version in the base URL for the async client. client = AsyncOpenAI( api_key=llm_api_key, - base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1/query/azure-openai",, + base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1/query/azure-openai", default_headers=javelin_headers, ) return client diff --git a/javelin_sdk/client.py b/javelin_sdk/client.py index 439b830..ee216de 100644 --- a/javelin_sdk/client.py +++ b/javelin_sdk/client.py @@ -708,7 +708,7 @@ def override_endpoint_url(request: Any, **kwargs) -> None: updated_url = original_url._replace( scheme=parsed_base.scheme, netloc=parsed_base.netloc, - path=f"{original_url.path}", + path=f"/v1{original_url.path}", ) request.url = urlunparse(updated_url)