From fa03994eb39f4d3fa110ccd10dab6d97cbc08147 Mon Sep 17 00:00:00 2001 From: Dhruvj07 Date: Mon, 14 Apr 2025 08:24:55 +0530 Subject: [PATCH] feat: Added examples for anthropic api call-chat and function calling --- .../anthropic_api_function_calling.py | 68 +++++++++++++++++++ .../anthropic/javelin_anthropic_api_call.py | 57 ++++++++++++++++ .../javelin_azureopenai_univ_endpoint.py | 2 + 3 files changed, 127 insertions(+) create mode 100644 examples/anthropic/anthropic_api_function_calling.py create mode 100644 examples/anthropic/javelin_anthropic_api_call.py diff --git a/examples/anthropic/anthropic_api_function_calling.py b/examples/anthropic/anthropic_api_function_calling.py new file mode 100644 index 0000000..87e7403 --- /dev/null +++ b/examples/anthropic/anthropic_api_function_calling.py @@ -0,0 +1,68 @@ +import os +from dotenv import load_dotenv +from javelin_sdk import JavelinClient, JavelinConfig + +load_dotenv() + +# Config setup +config = JavelinConfig( + base_url=os.getenv("JAVELIN_BASE_URL"), + javelin_api_key=os.getenv("JAVELIN_API_KEY"), + llm_api_key=os.getenv("ANTHROPIC_API_KEY"), + timeout=120, +) +client = JavelinClient(config) + +# Headers +headers = { + "Content-Type": "application/json", + "x-javelin-route": "anthropic_univ", # add your universal route + "x-javelin-model": "claude-3-5-sonnet-20240620", # add any supported model + "x-javelin-provider": "https://api.anthropic.com/v1", + "x-api-key": os.getenv("ANTHROPIC_API_KEY"), + "anthropic-version": "2023-06-01", +} +client.set_headers(headers) + +# Tool definition — using `input_schema` instead of OpenAI's `parameters` +functions = [ + { + "name": "get_weather", + "description": "Get the current weather in a city", + "input_schema": { + "type": "object", + "properties": { + "location": {"type": "string", "description": "City name"}, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } +] + +# Messages +messages = [ + { + "role": "user", + "content": [{"type": "text", "text": "What's the weather like in Mumbai in celsius?"}], + } +] + +# Request payload +query_body = { + "model": "claude-3-5-sonnet-20240620", + "temperature": 0.7, + "max_tokens": 300, + "messages": messages, + "tools": functions, + "tool_choice": {"type": "auto"}, # Important: dict, not string +} + +# Call +response = client.query_unified_endpoint( + provider_name="anthropic", + endpoint_type="messages", + query_body=query_body, +) + +print(response) diff --git a/examples/anthropic/javelin_anthropic_api_call.py b/examples/anthropic/javelin_anthropic_api_call.py new file mode 100644 index 0000000..c406cfc --- /dev/null +++ b/examples/anthropic/javelin_anthropic_api_call.py @@ -0,0 +1,57 @@ +import os +import json +from typing import Dict, Any +from javelin_sdk import JavelinClient, JavelinConfig +from dotenv import load_dotenv + +load_dotenv() + +# Helper for pretty print +def print_response(provider: str, response: Dict[str, Any]) -> None: + print(f"=== Response from {provider} ===") + print(json.dumps(response, indent=2)) + +# Javelin client config +config = JavelinConfig( + base_url=os.getenv("JAVELIN_BASE_URL"), + javelin_api_key=os.getenv("JAVELIN_API_KEY"), + llm_api_key=os.getenv("ANTHROPIC_API_KEY"), + timeout=120, +) +client = JavelinClient(config) + +# Proper headers (must match Anthropic's expectations) +custom_headers = { + "Content-Type": "application/json", + "x-javelin-route": "anthropic_univ", + "x-javelin-model": "claude-3-5-sonnet-20240620", + "x-javelin-provider": "https://api.anthropic.com/v1", + "x-api-key": os.getenv("ANTHROPIC_API_KEY"), # For Anthropic model + "anthropic-version": "2023-06-01", +} +client.set_headers(custom_headers) + +# Claude-compatible messages format +query_body = { + "model": "claude-3-5-sonnet-20240620", + "max_tokens": 300, + "temperature": 0.7, + "system": "You are a helpful assistant.", + "messages": [ + { + "role": "user", + "content": [{"type": "text", "text": "What are the three primary colors?"}] + } + ], +} + +# Invoke +try: + response = client.query_unified_endpoint( + provider_name="anthropic", + endpoint_type="messages", + query_body=query_body, + ) + print_response("Anthropic", response) +except Exception as e: + print(f"Anthropic query failed: {str(e)}") diff --git a/examples/azure-openai/javelin_azureopenai_univ_endpoint.py b/examples/azure-openai/javelin_azureopenai_univ_endpoint.py index 6861be0..18bbdb3 100644 --- a/examples/azure-openai/javelin_azureopenai_univ_endpoint.py +++ b/examples/azure-openai/javelin_azureopenai_univ_endpoint.py @@ -4,7 +4,9 @@ from typing import Any, Dict from javelin_sdk import JavelinClient, JavelinConfig +from dotenv import load_dotenv +load_dotenv() # Helper function to pretty print responses def print_response(provider: str, response: Dict[str, Any]) -> None: