-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaexample.py
200 lines (177 loc) · 6.13 KB
/
aexample.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import asyncio
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("LLM_API_KEY")
def pretty_print(obj):
"""
Pretty-prints an object that has a JSON representation.
"""
if hasattr(obj, "dict"):
obj = obj.dict()
print(json.dumps(obj, indent=4))
async def route_example(client):
"""
Start the example by cleaning up any pre-existing routes.
This is done by deleting the route if it exists.
"""
print("1. Start clean (by deleting pre-existing routes): ", "test_route_1")
try:
await client.adelete_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. This is done by creating a Route object and passing it to the
create_route method of the JavelinClient object.
"""
route_data = {
"name": "test_route_1",
"type": "chat",
"enabled": True,
"models": [
{
"name": "gpt-3.5-turbo",
"provider": "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:
await client.acreate_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. This is done by calling the query_route method of the JavelinClient
object. The query data is passed as a dictionary. The keys of the dictionary are the
same as the fields of the QueryRequest object. The values of the dictionary are the
same as the fields of the Message object.
"""
query_data = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
],
"temperature": 0.8,
}
print("3. Querying route: ", route.name)
try:
response = await client.aquery_route("test_route_1", query_data)
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")
"""
List routes. This is done by calling the list_routes method of the JavelinClient object.
"""
print("4. Listing routes")
try:
pretty_print(await client.alist_routes())
except UnauthorizedError as e:
print("Failed to list routes: Unauthorized")
except NetworkError as e:
print("Failed to list routes: Network Error")
print("5. Get Route: ", route.name)
try:
pretty_print(await client.aget_route(route.name))
except UnauthorizedError as e:
print("Failed to get route: Unauthorized")
except NetworkError as e:
print("Failed to get route: Network Error")
except RouteNotFoundError as e:
print("Failed to get route: Route Not Found")
"""
Update the route. This is done by calling the update_route method of the JavelinClient
object. The route object is passed as an argument.
"""
print("6. Updating Route: ", route.name)
try:
route.config.retries = 5
await client.aupdate_route(route)
except UnauthorizedError as e:
print("Failed to update route: Unauthorized")
except NetworkError as e:
print("Failed to update route: Network Error")
except RouteNotFoundError as e:
print("Failed to update route: Route Not Found")
"""
Get the route. This is done by calling the get_route method of the JavelinClient object.
"""
print("7. Get Route: ", route.name)
try:
pretty_print(await client.aget_route(route.name))
except UnauthorizedError as e:
print("Failed to get route: Unauthorized")
except NetworkError as e:
print("Failed to get route: Network Error")
except RouteNotFoundError as e:
print("Failed to get route: Route Not Found")
"""
Delete the route. This is done by calling the delete_route method of the JavelinClient
object.
"""
print("8. Deleting Route: ", route.name)
try:
await client.adelete_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")
async def main():
print("Javelin Asynchronous Example Code")
"""
Create a JavelinClient object. This object is used to interact
with the Javelin API. The base_url parameter is the URL of the Javelin API.
"""
try:
config = JavelinConfig(
base_url="https://api.javelin.live",
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
await route_example(client)
if __name__ == "__main__":
asyncio.run(main())