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
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ repos:
- repo: https://github.com/python-poetry/poetry
rev: "1.4.0" # add version here
hooks:
- id: poetry-check
- id: poetry-lock
- id: poetry-check
29 changes: 15 additions & 14 deletions examples/azure-openai/azure-universal.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import os

from dotenv import load_dotenv
from openai import AzureOpenAI

from javelin_sdk import JavelinClient, JavelinConfig
from dotenv import load_dotenv

load_dotenv()


def initialize_client():
"""
Creates the AzureOpenAI client and registers it with Javelin.
Returns the AzureOpenAI client object if successful, else None.
"""
javelin_api_key = os.getenv("JAVELIN_API_KEY") # add your javelin api key here
azure_openai_api_key = os.getenv("AZURE_OPENAI_API_KEY") # Add your Azure OpenAI key
javelin_api_key = os.getenv("JAVELIN_API_KEY") # add your javelin api key here
azure_openai_api_key = os.getenv(
"AZURE_OPENAI_API_KEY"
) # Add your Azure OpenAI key

if not javelin_api_key:
print("Error: JAVELIN_API_KEY is not set!")
Expand All @@ -29,13 +34,13 @@ def initialize_client():
# Typically "2023-07-01-preview" or a more recent version
api_version="2023-07-01-preview",
azure_endpoint="https://javelinpreview.openai.azure.com",
api_key=azure_openai_api_key
api_key=azure_openai_api_key,
)

# Initialize the Javelin client and register the Azure client
config = JavelinConfig(javelin_api_key=javelin_api_key)
javelin_client = JavelinClient(config)
rout_name = "azureopenai_univ" # Define the unversal route name
rout_name = "azureopenai_univ" # Define the unversal route name
javelin_client.register_azureopenai(azure_client, route_name=rout_name)

return azure_client
Expand All @@ -49,8 +54,7 @@ def get_chat_completion_sync(azure_client, messages):
"""

response = azure_client.chat.completions.create(
model="gpt-4", # Adjust to your Azure deployment name
messages=messages
model="gpt-4", messages=messages # Adjust to your Azure deployment name
)
return response.to_json()

Expand All @@ -63,7 +67,7 @@ def get_chat_completion_stream(azure_client, messages):
response = azure_client.chat.completions.create(
model="gpt-4", # Adjust to your Azure deployment name
messages=messages,
stream=True
stream=True,
)

# Accumulate streamed text
Expand All @@ -79,7 +83,6 @@ def get_chat_completion_stream(azure_client, messages):
return "".join(streamed_text)



def get_text_completion(azure_client, prompt):
"""
Demonstrates Azure text completion (non-chat).
Expand All @@ -90,7 +93,7 @@ def get_text_completion(azure_client, prompt):
model="gpt-4o", # Adjust to your actual Azure completions model name
prompt=prompt,
max_tokens=50,
temperature=0.7
temperature=0.7,
)
return response.to_json()

Expand All @@ -102,7 +105,7 @@ def get_embeddings(azure_client, text):
"""
response = azure_client.embeddings.create(
model="text-embedding-ada-002", # Adjust to your embeddings model name
input=text
input=text,
)
return response.to_json()

Expand All @@ -115,9 +118,7 @@ def main():
return

# Example chat messages
messages = [
{"role": "user", "content": "say hello"}
]
messages = [{"role": "user", "content": "say hello"}]

# 1) Chat Completion (Synchronous)
try:
Expand Down
45 changes: 28 additions & 17 deletions examples/azure-openai/langchain_azure_universal.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
from dotenv import load_dotenv

from langchain_openai import AzureChatOpenAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage
from dotenv import load_dotenv
from langchain.callbacks.base import BaseCallbackHandler
from langchain.callbacks.manager import CallbackManager
from langchain.schema import HumanMessage, SystemMessage
from langchain_openai import AzureChatOpenAI

#
# 1) Keys and Route Setup
Expand All @@ -13,10 +13,13 @@
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", "https://api.javelin.live"
) # Default to generic base URL

# The name of your Azure deployment (e.g., "gpt-4")
# or whatever you’ve set in Azure. Must also match x-javelin-model if Javelin expects that.
# or whatever you’ve set in Azure. Must also match x-javelin-model if
# Javelin expects that.
model_choice = "gpt-4"

# Javelin route name, as registered in your javelin route dashboard
Expand All @@ -32,7 +35,7 @@
openai_api_key=azure_openai_api_key,
# Provide your actual API version
api_version="2024-08-01-preview",
# The base_url is Javelin’s universal route
# The base_url is Javelin’s universal route
base_url=f"{base_url}/v1/azureopenai/deployments/gpt-4/",
validate_base_url=False,
verbose=True,
Expand All @@ -42,9 +45,10 @@
"x-javelin-model": model_choice,
"x-javelin-provider": "https://javelinpreview.openai.azure.com/openai",
},
streaming=False # Non-streaming
streaming=False, # Non-streaming
)


#
# 3) Single-Turn Invoke (Non-Streaming)
#
Expand All @@ -60,28 +64,32 @@ def invoke_non_streaming(question: str) -> str:
# The response is usually an AIMessage. Return its content.
return response.content


#
# 4) Single-Turn Streaming
# We'll create a new LLM with streaming=True, plus a callback handler.
#


class StreamCallbackHandler(BaseCallbackHandler):
"""
Collects tokens as they are streamed, so we can return the final text.
"""

def __init__(self):
self.tokens = []

def on_llm_new_token(self, token: str, **kwargs) -> None:
self.tokens.append(token)


def invoke_streaming(question: str) -> str:
"""
Sends a single user message to the LLM (streaming=True).
Collects the tokens from the callback and returns them as a string.
"""
callback_handler = StreamCallbackHandler()
callback_manager = CallbackManager([callback_handler])
CallbackManager([callback_handler])

llm_streaming = AzureChatOpenAI(
openai_api_key=azure_openai_api_key,
Expand All @@ -95,16 +103,17 @@ def invoke_streaming(question: str) -> str:
"x-javelin-model": model_choice,
"x-javelin-provider": "https://javelinpreview.openai.azure.com/openai",
},
streaming=True, # <-- streaming on
callbacks=[callback_handler] # <-- our custom callback
streaming=True, # <-- streaming on
callbacks=[callback_handler], # <-- our custom callback
)

messages = [HumanMessage(content=question)]
response = llm_streaming.invoke(messages)
llm_streaming.invoke(messages)
# We could check response, but it's usually an AIMessage with partial content
# The real text is captured in the callback tokens
return "".join(callback_handler.tokens)


#
# 5) Conversation Demo
#
Expand All @@ -117,7 +126,7 @@ def conversation_demo():

conversation_llm = llm_non_streaming

# Start with a system message
# Start with a system message
messages = [SystemMessage(content="You are a friendly assistant.")]
user_q1 = "Hello, how are you?"
messages.append(HumanMessage(content=user_q1))
Expand All @@ -133,12 +142,13 @@ def conversation_demo():

return "Conversation done!"


#
# 6) Main Function
#
def main():
print("=== LangChain AzureOpenAI Example ===")

# 1) Single-turn Non-Streaming Invoke
print("\n--- Single-turn Non-Streaming Invoke ---")
question_a = "What is the capital of France?"
Expand All @@ -150,7 +160,7 @@ def main():
print(f"Question: {question_a}\nAnswer: {response_a}")
except Exception as e:
print(f"Error in non-streaming invoke: {e}")

# 2) Single-turn Streaming Invoke
print("\n--- Single-turn Streaming Invoke ---")
question_b = "Tell me a quick joke."
Expand All @@ -162,15 +172,16 @@ def main():
print(f"Question: {question_b}\nStreamed Answer: {response_b}")
except Exception as e:
print(f"Error in streaming invoke: {e}")

# 3) Multi-turn Conversation Demo
print("\n--- Simple Conversation Demo ---")
try:
conversation_demo()
except Exception as e:
print(f"Error in conversation demo: {e}")

print("\n=== All done! ===")



if __name__ == "__main__":
main()
Loading