-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjavelin_azureopenai_univ_endpoint.py
64 lines (53 loc) · 1.95 KB
/
javelin_azureopenai_univ_endpoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import asyncio
import json
import os
from typing import Any, Dict
from javelin_sdk import JavelinClient, JavelinConfig
# Helper function to pretty print responses
def print_response(provider: str, response: Dict[str, Any]) -> None:
print(f"=== Response from {provider} ===")
print(json.dumps(response, indent=2))
# Setup client configuration
config = JavelinConfig(
base_url="https://api-dev.javelin.live",
javelin_api_key=os.getenv("JAVELIN_API_KEY"),
default_headers={
"Content-Type": "application/json",
"x-javelin-provider": "https://javelinpreview.openai.azure.com/openai",
"x-api-key": os.getenv("JAVELIN_API_KEY"),
"api-key": os.getenv("AZURE_OPENAI_API_KEY"),
},
)
client = JavelinClient(config)
# Example messages in OpenAI format
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What are the three primary colors?"},
]
# Define the headers based on the curl command
custom_headers = {
"Content-Type": "application/json",
"x-javelin-route": "azureopenai_univ",
"x-javelin-provider": "https://javelinpreview.openai.azure.com/openai",
"x-api-key": os.getenv("JAVELIN_API_KEY"), # Use environment variable for security
"api-key": os.getenv(
"AZURE_OPENAI_API_KEY"
), # Use environment variable for security
}
async def main():
try:
query_body = {"messages": messages, "temperature": 0.7}
query_params = {"api-version": "2023-07-01-preview"}
openai_response = client.query_unified_endpoint(
provider_name="azureopenai",
endpoint_type="chat",
query_body=query_body,
headers=custom_headers,
query_params=query_params,
deployment="gpt-4",
)
print_response("Azure OpenAI", openai_response)
except Exception as e:
print(f"OpenAI query failed: {str(e)}")
# Run the async function
asyncio.run(main())