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
2 changes: 1 addition & 1 deletion examples/anthropic/javelin_anthropic_univ_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration for Bedrock
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
)
client = JavelinClient(config)
Expand Down
2 changes: 1 addition & 1 deletion examples/anthropic/openai_compatible_univ_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
timeout=120,
)
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-openai/azure_general_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def init_azure_client_sync():
javelin_headers = {"x-api-key": javelin_api_key}
client = AzureOpenAI(
api_key=llm_api_key,
base_url="https://api-dev.javelin.live/v1/query/azure-openai",
base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1/query/azure-openai",
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 using os.path.join for constructing the base URL to ensure cross-platform compatibility and readability.

base_url = os.path.join(os.getenv('JAVELIN_BASE_URL'), 'v1/query/azure-openai')

default_headers=javelin_headers,
api_version="2024-02-15-preview"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { Stream } from 'openai/streaming.mjs';
const javelinApiKey = ""; // javelin api key here
const llmApiKey = ""; // llm api key

const javelinBaseUrl = 'https://api.javelin.live/v1/query';

async function getCompletion() {
try {
const routeName = 'AzureOpenAIRoute';
const url = "https://api.javelin.live/v1/query/AzureOpenAIRoute";
const url = `${process.env.JAVELIN_BASE_URL}/v1/query/${routeName}`;

const response = await axios.post(
url,
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-openai/javelin_azureopenai_univ_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
default_headers={
"Content-Type": "application/json",
Expand Down
4 changes: 1 addition & 3 deletions examples/azure-openai/langchain_azure_universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
load_dotenv()
azure_openai_api_key = os.getenv("AZURE_OPENAI_API_KEY")
javelin_api_key = os.getenv("JAVELIN_API_KEY")
base_url = os.getenv(
"JAVELIN_BASE_URL", "https://api.javelin.live"
) # Default to generic base URL
base_url = os.getenv("JAVELIN_BASE_URL")
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.

high

The default value for JAVELIN_BASE_URL was removed. It's good practice to provide a default value in case the environment variable is not set. What do you think about adding it back?

Suggested change
base_url = os.getenv("JAVELIN_BASE_URL")
base_url = os.getenv("JAVELIN_BASE_URL", "https://api.javelin.live")


# The name of your Azure deployment (e.g., "gpt-4")
# or whatever you’ve set in Azure. Must also match x-javelin-model if
Expand Down
5 changes: 3 additions & 2 deletions examples/azure-openai/langchain_chatmodel_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
dotenv.load_dotenv()

from langchain_openai import AzureChatOpenAI

url = os.path.join(os.getenv("JAVELIN_BASE_URL"), "v1")
print(url)
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

This print statement should be removed before merging to avoid unnecessary output in production.

model = AzureChatOpenAI(
azure_endpoint="https://api-dev.javelin.live/v1",
azure_endpoint=url,
azure_deployment="gpt35",
openai_api_version="2023-03-15-preview",
extra_headers={"x-javelin-route": "azureopenai_univ", "x-api-key": os.environ.get("JAVELIN_API_KEY")}
Expand Down
985 changes: 984 additions & 1 deletion examples/azure-openai/openai_azureopenai_testing.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/azure-openai/openai_compatible_univ_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
timeout=120,
)
Expand Down
3 changes: 2 additions & 1 deletion examples/bedrock/bedrock_client_universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ 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
javelin_api_key=os.getenv("JAVELIN_API_KEY"), # Replace with your Javelin API key
base_url=os.getenv("JAVELIN_BASE_URL")
)
javelin_client = JavelinClient(config)

Expand Down
2 changes: 1 addition & 1 deletion examples/bedrock/bedrock_general_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_bedrock_client():
client = boto3.client(
service_name="bedrock-runtime",
region_name="us-east-1",
endpoint_url="https://api-dev.javelin.live/v1/",
endpoint_url=os.path.join(os.getenv("JAVELIN_BASE_URL"), "v1"),
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
Expand Down
2 changes: 1 addition & 1 deletion examples/bedrock/javelin_bedrock_univ_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration for Bedrock
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
)
client = JavelinClient(config)
Expand Down
3 changes: 2 additions & 1 deletion examples/bedrock/langchain-bedrock-universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def init_bedrock():

# Initialize Javelin client
config = JavelinConfig(
javelin_api_key=os.getenv("JAVELIN_API_KEY") # add your Javelin API key here
javelin_api_key=os.getenv("JAVELIN_API_KEY"), # add your Javelin API key here
base_url=os.getenv("JAVELIN_BASE_URL")
)
javelin_client = JavelinClient(config)

Expand Down
2 changes: 1 addition & 1 deletion examples/bedrock/openai_compatible_univ_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
timeout=120,
)
Expand Down
1 change: 1 addition & 0 deletions examples/gemini/document_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def initialize_javelin_client():
javelin_api_key = os.getenv("JAVELIN_API_KEY")
config = JavelinConfig(
javelin_api_key=javelin_api_key,
base_url=os.getenv("JAVELIN_BASE_URL")
)
return JavelinClient(config)

Expand Down
2 changes: 1 addition & 1 deletion examples/gemini/gemini-universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def init_gemini_client():
# Javelin configuration

javelin_api_key = os.getenv("JAVELIN_API_KEY") # define your javelin api key here
config = JavelinConfig(javelin_api_key=javelin_api_key)
config = JavelinConfig(javelin_api_key=javelin_api_key, base_url=os.getenv("JAVELIN_BASE_URL"))
client = JavelinClient(config)
rout_name = "google_univ" # define your universal route name here
# Register the Gemini client with Javelin
Expand Down
2 changes: 1 addition & 1 deletion examples/gemini/javelin_gemini_univ_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
llm_api_key=os.getenv("OPENAI_API_KEY"),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/gemini/langchain_chatmodel_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from langchain.chat_models import init_chat_model

model = init_chat_model("gemini-1.5-flash", model_provider="openai", base_url="https://api-dev.javelin.live/v1",
model = init_chat_model("gemini-1.5-flash", model_provider="openai", base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1",
extra_headers={"x-javelin-route": "google_univ", "x-api-key": os.environ.get("JAVELIN_API_KEY"), "Authorization": f"Bearer {os.environ.get('GEMINI_API_KEY')}"})

print(model.invoke("write a poem about a cat"))
2 changes: 1 addition & 1 deletion examples/gemini/openai_compatible_univ_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
llm_api_key=os.getenv("OPENAI_API_KEY"),
timeout=120,
Expand Down
2 changes: 1 addition & 1 deletion examples/gemini/strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def openai_chat_completions():
def initialize_javelin_client():
javelin_api_key = os.getenv("JAVELIN_API_KEY")
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=javelin_api_key,
)
return JavelinClient(config)
Expand Down
2 changes: 1 addition & 1 deletion examples/mistral/langchain_chatmodel_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from langchain.chat_models import init_chat_model

model = init_chat_model("mistral-large-latest", model_provider="openai", base_url="https://api-dev.javelin.live/v1",
model = init_chat_model("mistral-large-latest", model_provider="openai", base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1",
extra_headers={"x-javelin-route": "mistral_univ", "x-api-key": os.environ.get("JAVELIN_API_KEY"), "Authorization": f"Bearer {os.environ.get('MISTRAL_API_KEY')}"})

print(model.invoke("write a poem about a cat"))
2 changes: 1 addition & 1 deletion examples/openai/javelin_openai_univ_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
llm_api_key=os.getenv("OPENAI_API_KEY"),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/openai/langchain-openai-universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
MODEL_NAME_CHAT = "gpt-3.5-turbo" # For chat
MODEL_NAME_EMBED = "text-embedding-ada-002"
ROUTE_NAME = "openai_univ"
BASE_URL = os.getenv("JAVELIN_BASE_URL", "https://api.javelin.live") # Default base URL
BASE_URL = os.getenv("JAVELIN_BASE_URL") # Default base URL


def init_chat_llm_non_streaming():
Expand Down
61 changes: 61 additions & 0 deletions examples/openai/langchain_callback_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import dotenv
import os
from typing import Any, Dict, List

from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.messages import BaseMessage
from langchain.chat_models import init_chat_model

dotenv.load_dotenv()

class HeaderCallbackHandler(BaseCallbackHandler):
"""Custom callback handler that modifies the headers on chat model start."""

def __init__(self):
self.api_key = os.environ.get("JAVELIN_API_KEY")

def on_chain_start(
self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any
) -> Any:
"""Run when chain starts running."""
print("Chain started")
print(serialized, inputs, kwargs)

def on_chat_model_start(
self, serialized: Dict[str, Any], messages: List[List[BaseMessage]], **kwargs: Any
) -> Any:
"""Run when Chat Model starts running."""
# The serialized dict contains the model configuration
print(self.__super().on_chat_model_start(serialized, messages, **kwargs))
if "kwargs" in serialized:
# Add or update the headers in the model kwargs
if "model_kwargs" not in serialized["kwargs"]:
serialized["kwargs"]["model_kwargs"] = {}
if "extra_headers" not in serialized["kwargs"]["model_kwargs"]:
serialized["kwargs"]["model_kwargs"]["extra_headers"] = {}

# Determine the route based on the model provider
provider = serialized.get("name", "").lower()
route = "azureopenai_univ" if "azure" in provider else "openai_univ"

headers = {
"x-javelin-route": route,
"x-api-key": self.api_key
}
serialized["kwargs"]["model_kwargs"]["extra_headers"].update(headers)
print(f"Modified headers to: {headers}")

# Initialize the callback handler
callback_handler = HeaderCallbackHandler()

# Initialize the chat model with the callback handler
model = init_chat_model(
"gpt-4o-mini",
model_provider="openai",
base_url="http://127.0.0.1:8000/v1",
extra_headers={"x-javelin-route": "openai_univ", "x-api-key": os.environ.get("JAVELIN_API_KEY")},
callbacks=[callback_handler] # Add our custom callback handler
)

# Test the model
print(model.invoke("Hello, world!"))
2 changes: 1 addition & 1 deletion examples/openai/langchain_chatmodel_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from langchain.chat_models import init_chat_model

model = init_chat_model("gpt-4o-mini", model_provider="openai", base_url="https://api-dev.javelin.live/v1",
model = init_chat_model("gpt-4o-mini", model_provider="openai", base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1",
extra_headers={"x-javelin-route": "openai_univ", "x-api-key": os.environ.get("JAVELIN_API_KEY")})

print(model.invoke("Hello, world!"))
2 changes: 1 addition & 1 deletion examples/openai/o1-03_function-calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
config = JavelinConfig(javelin_api_key=javelin_api_key, base_url=os.getenv("JAVELIN_BASE_URL"))
client = JavelinClient(config)
client.register_openai(openai_client, route_name=route_name)
return client
Expand Down
2 changes: 1 addition & 1 deletion examples/openai/openai-azure-fun_calling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"\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",
" config = JavelinConfig(javelin_api_key=javelin_api_key, base_url=os.getenv(\"JAVELIN_BASE_URL\"))\n",
" client = JavelinClient(config)\n",
" client.register_openai(openai_client, route_name=route_name)\n",
" return client\n",
Expand Down
3 changes: 2 additions & 1 deletion examples/openai/openai-universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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
Expand Down Expand Up @@ -118,7 +119,7 @@ def init_javelin_client_async(openai_async_client):
"""Initialize JavelinClient for async usage and register the OpenAI route."""
try:
javelin_api_key = os.getenv("JAVELIN_API_KEY") # add your javelin api key here
config = JavelinConfig(javelin_api_key=javelin_api_key)
config = JavelinConfig(javelin_api_key=javelin_api_key, base_url=os.getenv("JAVELIN_BASE_URL"))
client = JavelinClient(config)
client.register_openai(openai_async_client, route_name="openai_univ")
return client
Expand Down
2 changes: 1 addition & 1 deletion examples/openai/openai_compatible_univ.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def print_response(provider: str, response: Dict[str, Any]) -> None:

# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
llm_api_key=os.getenv("OPENAI_API_KEY"),
timeout=120,
Expand Down
2 changes: 1 addition & 1 deletion examples/openai/openai_embedding_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
javelin_headers = {"x-api-key": javelin_api_key, "x-javelin-route": "myusers"}

llm = ChatOpenAI(
openai_api_base="https://api.javelin.live/v1/query",
openai_api_base=f"{os.getenv('JAVELIN_BASE_URL')}/v1/query",
openai_api_key=llm_api_key,
model_kwargs={"extra_headers": javelin_headers},
)
Expand Down
2 changes: 1 addition & 1 deletion examples/openai/openai_general_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def init_sync_openai_client():
print(f"[DEBUG] Synchronous OpenAI client key: {openai_api_key}")
return OpenAI(
api_key=openai_api_key,
base_url="https://api-dev.javelin.live/v1/query/openai",
base_url=f"{os.getenv('JAVELIN_BASE_URL')}/v1/query/openai",
default_headers=javelin_headers
)
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion examples/openai/openai_javelin_stream&non-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import OpenAI from "openai";

const openai_client = new OpenAI({
apiKey: "", // add your api key
baseURL: "https://api.javelin.live/v1/query",
baseURL: `${process.env.JAVELIN_BASE_URL}/v1/query`,
defaultHeaders: {
"x-api-key": "", // add here javelin api key
"x-javelin-route": "OpenAIInspect",
Expand Down
4 changes: 2 additions & 2 deletions examples/rag/javelin_rag_embeddings_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@
"# Initialize Azure OpenAI client for embeddings\n",
"azure_openai_client = AzureOpenAI(\n",
" api_key=llm_api_key,\n",
" base_url=\"https://api.javelin.live/query\",\n",
" base_url=os.getenv(\"JAVELIN_BASE_URL\"),\n",
" default_headers=javelin_headers_embeddings,\n",
" api_version=\"2023-05-15\",\n",
")\n",
"\n",
"# Initialize LLM\n",
"llm = AzureChatOpenAI(\n",
" api_key=llm_api_key,\n",
" azure_endpoint=\"https://api.javelin.live/query/azureopenai\",\n",
" azure_endpoint=f\"{os.getenv('JAVELIN_BASE_URL')}/query/azureopenai\",\n",
" azure_deployment=\"gpt35\",\n",
" openai_api_version=\"2024-02-15-preview\",\n",
" model_kwargs={\"extra_headers\": javelin_headers_llm}\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/rag/rag_implemetation_javelin.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@
"outputs": [],
"source": [
"config = JavelinConfig(\n",
" base_url=\"https://api-dev.javelin.live\",\n",
" base_url=os.getenv(\"JAVELIN_BASE_URL\"),\n",
" javelin_api_key=os.getenv(\"JAVELIN_API_KEY\"),\n",
" llm_api_key=os.getenv(\"OPENAI_API_KEY\"),\n",
")\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/route_examples/aexample.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ async def main():

try:
config = JavelinConfig(
base_url="https://api.javelin.live",
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=javelin_api_key,
javelin_virtualapikey=javelin_virtualapikey,
llm_api_key=llm_api_key,
Expand Down
1 change: 1 addition & 0 deletions examples/route_examples/drop_in_replacement.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def main():

try:
config = JavelinConfig(
base_url=os.getenv("JAVELIN_BASE_URL"),
javelin_api_key=javelin_api_key,
javelin_virtualapikey=javelin_virtualapikey,
llm_api_key=llm_api_key,
Expand Down
Loading
Loading