Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions examples/anthropic/anthropic_api_function_calling.py
Original file line number Diff line number Diff line change
@@ -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,
)

Comment on lines +61 to +67
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a try-except block to handle potential exceptions during the API call. This will make the example more robust and provide better feedback in case of errors.

Suggested change
# Call
response = client.query_unified_endpoint(
provider_name="anthropic",
endpoint_type="messages",
query_body=query_body,
)
try:
response = client.query_unified_endpoint(
provider_name="anthropic",
endpoint_type="messages",
query_body=query_body,
)
print(response)
except Exception as e:
print(f"Error calling Anthropic API: {e}")

print(response)
57 changes: 57 additions & 0 deletions examples/anthropic/javelin_anthropic_api_call.py
Original file line number Diff line number Diff line change
@@ -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",
}
Comment on lines +24 to +31
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These headers are also defined in anthropic_api_function_calling.py. Consider defining them in a single place (e.g., a shared module or a configuration file) to avoid duplication and ensure consistency.

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)}")
Comment on lines +50 to +57
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a more specific exception type to catch, such as httpx.HTTPError, to handle network-related issues more gracefully. Also, it would be helpful to log the exception details for debugging purposes.

except httpx.HTTPError as e:
    print(f"Anthropic query failed: {str(e)}")

2 changes: 2 additions & 0 deletions examples/azure-openai/javelin_azureopenai_univ_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading