-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrop_in_replacement.py
131 lines (113 loc) · 3.61 KB
/
drop_in_replacement.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import json
import os
import dotenv
from javelin_sdk import (
JavelinClient,
JavelinConfig,
NetworkError,
Route,
RouteNotFoundError,
UnauthorizedError,
)
dotenv.load_dotenv()
# Retrieve environment variables
javelin_api_key = os.getenv("JAVELIN_API_KEY")
javelin_virtualapikey = os.getenv("JAVELIN_VIRTUALAPIKEY")
llm_api_key = os.getenv("OPENAI_API_KEY")
def pretty_print(obj):
if hasattr(obj, "dict"):
obj = obj.dict()
print(json.dumps(obj, indent=4))
def route_example(client):
# Clean up pre-existing route
print("1. Start clean (by deleting pre-existing routes): ", "test_route_1")
try:
client.delete_route("test_route_1")
except UnauthorizedError as e:
print("Failed to delete route: Unauthorized")
except NetworkError as e:
print("Failed to delete route: Network Error")
except RouteNotFoundError as e:
print("Failed to delete route: Route Not Found")
# Create a route
route_data = {
"name": "test_route_1",
"type": "chat",
"enabled": True,
"models": [
{
"name": "gpt-3.5-turbo",
"provider": "Azure OpenAI",
"suffix": "/chat/completions",
}
],
"config": {
"organization": "myusers",
"rate_limit": 7,
"retries": 3,
"archive": True,
"retention": 7,
"budget": {
"enabled": True,
"annual": 100000,
"currency": "USD",
},
"dlp": {"enabled": True, "strategy": "Inspect", "action": "notify"},
},
}
route = Route.parse_obj(route_data)
print("2. Creating route: ", route.name)
try:
client.create_route(route)
except UnauthorizedError as e:
print("Failed to create route: Unauthorized")
except NetworkError as e:
print("Failed to create route: Network Error")
# Query the route
print("3. Querying route: ", route.name)
try:
query_data = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
],
"temperature": 0.7,
}
response = client.chat.completions.create(
route="test_route_1",
messages=query_data["messages"],
temperature=query_data.get("temperature", 0.7),
)
pretty_print(response)
except UnauthorizedError as e:
print("Failed to query route: Unauthorized")
except NetworkError as e:
print("Failed to query route: Network Error")
except RouteNotFoundError as e:
print("Failed to query route: Route Not Found")
# Clean up: Delete the route
print("4. Deleting Route: ", route.name)
try:
client.delete_route(route.name)
except UnauthorizedError as e:
print("Failed to delete route: Unauthorized")
except NetworkError as e:
print("Failed to delete route: Network Error")
except RouteNotFoundError as e:
print("Failed to delete route: Route Not Found")
def main():
print("Javelin Drop-in Replacement Example")
try:
config = JavelinConfig(
javelin_api_key=javelin_api_key,
javelin_virtualapikey=javelin_virtualapikey,
llm_api_key=llm_api_key,
)
client = JavelinClient(config)
except NetworkError as e:
print("Failed to create client: Network Error")
return
route_example(client)
if __name__ == "__main__":
main()