-
Notifications
You must be signed in to change notification settings - Fork 1
fix: update base url to use environment variables #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value for
Suggested change
|
||||||
|
|
||||||
| # The name of your Azure deployment (e.g., "gpt-4") | ||||||
| # or whatever you’ve set in Azure. Must also match x-javelin-model if | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,10 @@ | |
| dotenv.load_dotenv() | ||
|
|
||
| from langchain_openai import AzureChatOpenAI | ||
|
|
||
| url = os.path.join(os.getenv("JAVELIN_BASE_URL"), "v1") | ||
| print(url) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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")} | ||
|
|
||
Large diffs are not rendered by default.
| 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!")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using
os.path.joinfor constructing the base URL to ensure cross-platform compatibility and readability.