Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ff39ef

Browse files
authored
Add fallback models (opper-ai#69)
* Add fallback models * Bump version * Add examples for fallback models * Missed staging a hunk * Add fallback models to the call payload * Record new vcr cassettes
1 parent 2dcbbc1 commit 6ff39ef

13 files changed

Lines changed: 279 additions & 328 deletions

examples/calls.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ class Number(BaseModel):
119119
print(output)
120120

121121

122+
@trace
123+
def bare_minimum_with_fallbacks():
124+
output, _ = opper.call(
125+
name="python/sdk/bare-minimum-with-fallbacks",
126+
instructions="answer the following question",
127+
input="what are some uses of 42",
128+
model="azure/gpt4-eu",
129+
fallback_models=["openai/gpt-4o", "openai/gpt-3.5-turbo"],
130+
configuration=CallConfiguration(
131+
model_parameters={
132+
"max_tokens": 10,
133+
}
134+
),
135+
)
136+
print(output)
137+
138+
122139
@trace
123140
def synchronous_call():
124141
print("running synchronous calls")
@@ -129,6 +146,7 @@ def synchronous_call():
129146
call_with_examples()
130147
structured_string_input_output()
131148
stream_call()
149+
bare_minimum_with_fallbacks()
132150

133151

134152
aopper = AsyncOpper()
@@ -156,6 +174,18 @@ async def async_bare_minimum_with_model():
156174
instructions="answer the following question",
157175
input="what are some uses of 42",
158176
model="mistral/mistral-tiny-eu",
177+
)
178+
print(output)
179+
180+
181+
@trace
182+
async def async_bare_minimum_with_fallbacks():
183+
output, _ = await aopper.call(
184+
name="python/sdk/async-bare-minimum-with-fallbacks",
185+
instructions="answer the following question",
186+
input="what are some uses of 42",
187+
model="azure/gpt4-eu",
188+
fallback_models=["openai/gpt-4", "openai/gpt-3.5-turbo"],
159189
configuration=CallConfiguration(
160190
model_parameters={
161191
"max_tokens": 10,
@@ -250,6 +280,7 @@ async def async_call():
250280
await asyncio.gather(
251281
async_bare_minimum(),
252282
async_bare_minimum_with_model(),
283+
async_bare_minimum_with_fallbacks(),
253284
async_structured_input_output(),
254285
async_call_with_examples(),
255286
async_call_with_structured_examples(),

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "opperai"
3-
version = "0.21.0"
3+
version = "0.22.0"
44
description = "Opper Python client"
55
authors = ["Opper <[email protected]>"]
66
readme = "README.md"

src/opperai/functions/async_functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ async def call(
352352
configuration: Optional[CallConfiguration] = None,
353353
parent_span_id: Optional[str] = None,
354354
stream: Literal[True] = True,
355+
fallback_models: Optional[List[str]] = None,
355356
) -> AsyncStreamingResponse: ...
356357

357358
@overload
@@ -367,6 +368,7 @@ async def call(
367368
configuration: Optional[CallConfiguration] = None,
368369
parent_span_id: Optional[str] = None,
369370
stream: Literal[False] = False,
371+
fallback_models: Optional[List[str]] = None,
370372
) -> Tuple[T, AsyncFunctionResponse]: ...
371373

372374
async def call(
@@ -381,6 +383,7 @@ async def call(
381383
configuration: Optional[CallConfiguration] = None,
382384
parent_span_id: Optional[str] = None,
383385
stream: Optional[bool] = False,
386+
fallback_models: Optional[List[str]] = None,
384387
) -> Tuple[T, AsyncFunctionResponse]:
385388
"""Calls a function
386389
Arguments:
@@ -426,6 +429,7 @@ async def call(
426429
if parent_span_id
427430
else get_current_span_id(),
428431
stream=stream,
432+
fallback_models=fallback_models,
429433
)
430434
if configuration:
431435
call_payload.configuration = configuration

src/opperai/functions/functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ def call(
335335
configuration: Optional[CallConfiguration] = None,
336336
parent_span_id: Optional[str] = None,
337337
stream: Literal[False] = False,
338+
fallback_models: Optional[List[str]] = None,
338339
) -> Tuple[T, FunctionResponse]: ...
339340

340341
@overload
@@ -350,6 +351,7 @@ def call(
350351
configuration: Optional[CallConfiguration] = None,
351352
parent_span_id: Optional[str] = None,
352353
stream: Literal[True] = True,
354+
fallback_models: Optional[List[str]] = None,
353355
) -> StreamingResponse: ...
354356

355357
def call(
@@ -364,6 +366,7 @@ def call(
364366
configuration: Optional[CallConfiguration] = None,
365367
parent_span_id: Optional[str] = None,
366368
stream: Optional[bool] = False,
369+
fallback_models: Optional[List[str]] = None,
367370
) -> Union[Tuple[T, FunctionResponse], StreamingResponse]:
368371
"""Calls a function
369372
Arguments:
@@ -409,6 +412,7 @@ def call(
409412
if parent_span_id
410413
else get_current_span_id(),
411414
stream=stream,
415+
fallback_models=fallback_models,
412416
)
413417
if configuration:
414418
call_payload.configuration = configuration

src/opperai/types/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class CallPayload(BaseModel):
130130
examples: Optional[List[Example]] = Field(default=None, max_length=10)
131131
stream: bool = False
132132
parent_span_uuid: Optional[str] = None
133+
fallback_models: Optional[List[str]] = None
133134
configuration: Optional[CallConfiguration] = None
134135

135136

tests/fixtures/vcr_cassettes/test_fn_decorator/test_decorator.yaml

Lines changed: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ interactions:
1010
uri: http://localhost:8000/v1/functions/by_path/translate
1111
response:
1212
body:
13-
string: '{"id":49,"uuid":"8cb73d91-d845-41f2-b9c6-24e1eae960af","dataset_uuid":"4e75f8b7-47b1-47c5-bc6b-1399fb603aff","description":"Translate
13+
string: '{"id":44,"uuid":"bbaff35a-0807-4da3-8e38-c9ef1b193bdd","dataset_uuid":"8b25fd4b-11c7-42e9-a3fe-4e9533c6947a","dataset":{"uuid":"8b25fd4b-11c7-42e9-a3fe-4e9533c6947a","entry_count":0},"description":"Translate
1414
text to a target language.","few_shot_count":2,"few_shot":false,"index_ids":[],"input_schema":null,"instructions":"Operation:
15-
translate\n\nOperation description: Translate text to a target language.","language_model_id":null,"model":"azure/gpt4-eu","out_schema":{"type":"string"},"path":"translate","project":{"uuid":"e64848ae-df03-4c0b-9a6f-4c498bf3dc8b","name":"asdfasdf"},"revision":1,"use_semantic_search":false}'
15+
translate\n\nOperation description: Translate text to a target language.","language_model_id":null,"model":"openai/gpt-4o","out_schema":{"type":"string"},"path":"translate","project":{"uuid":"2a31ac04-b7d1-46c5-8df3-b09c63b5e4d8","name":"default"},"revision":1,"use_semantic_search":false}'
1616
headers:
1717
content-length:
18-
- '550'
18+
- '623'
1919
content-type:
2020
- application/json
2121
date:
22-
- Tue, 15 Oct 2024 14:18:17 GMT
22+
- Tue, 12 Nov 2024 14:58:26 GMT
2323
server:
2424
- uvicorn
2525
status:
2626
code: 200
2727
message: OK
2828
- request:
29-
body: '{"uuid": "8cb73d91-d845-41f2-b9c6-24e1eae960af", "path": "translate", "description":
29+
body: '{"uuid": "bbaff35a-0807-4da3-8e38-c9ef1b193bdd", "path": "translate", "description":
3030
"Translate text to a target language.", "input_schema": null, "out_schema":
3131
{"type": "string"}, "instructions": "Operation: translate\n\nOperation description:
3232
Translate text to a target language.", "model": null, "index_ids": [], "use_semantic_search":
3333
false, "few_shot": false, "few_shot_count": 2, "cache_configuration": null,
34-
"dataset_uuid": "4e75f8b7-47b1-47c5-bc6b-1399fb603aff"}'
34+
"dataset_uuid": "8b25fd4b-11c7-42e9-a3fe-4e9533c6947a"}'
3535
headers:
3636
accept:
3737
- '*/*'
@@ -42,19 +42,19 @@ interactions:
4242
content-type:
4343
- application/json
4444
method: PATCH
45-
uri: http://localhost:8000/v1/functions/8cb73d91-d845-41f2-b9c6-24e1eae960af
45+
uri: http://localhost:8000/v1/functions/bbaff35a-0807-4da3-8e38-c9ef1b193bdd
4646
response:
4747
body:
48-
string: '{"id":49,"uuid":"8cb73d91-d845-41f2-b9c6-24e1eae960af","dataset_uuid":"4e75f8b7-47b1-47c5-bc6b-1399fb603aff","description":"Translate
48+
string: '{"id":44,"uuid":"bbaff35a-0807-4da3-8e38-c9ef1b193bdd","dataset_uuid":"8b25fd4b-11c7-42e9-a3fe-4e9533c6947a","dataset":{"uuid":"8b25fd4b-11c7-42e9-a3fe-4e9533c6947a","entry_count":0},"description":"Translate
4949
text to a target language.","few_shot_count":2,"few_shot":false,"index_ids":[],"input_schema":null,"instructions":"Operation:
50-
translate\n\nOperation description: Translate text to a target language.","language_model_id":null,"model":"azure/gpt4-eu","out_schema":{"type":"string"},"path":"translate","project":{"uuid":"e64848ae-df03-4c0b-9a6f-4c498bf3dc8b","name":"asdfasdf"},"revision":1,"use_semantic_search":false}'
50+
translate\n\nOperation description: Translate text to a target language.","language_model_id":null,"model":"openai/gpt-4o","out_schema":{"type":"string"},"path":"translate","project":{"uuid":"2a31ac04-b7d1-46c5-8df3-b09c63b5e4d8","name":"default"},"revision":1,"use_semantic_search":false}'
5151
headers:
5252
content-length:
53-
- '550'
53+
- '623'
5454
content-type:
5555
- application/json
5656
date:
57-
- Tue, 15 Oct 2024 14:18:17 GMT
57+
- Tue, 12 Nov 2024 14:58:26 GMT
5858
server:
5959
- uvicorn
6060
status:
@@ -64,28 +64,28 @@ interactions:
6464
body: '{"name": null, "instructions": "you are a helpful assistant", "input_schema":
6565
null, "input": {"text": "Hello", "target_language": "es"}, "output_schema":
6666
null, "model": null, "examples": null, "stream": false, "parent_span_uuid":
67-
null, "configuration": null}'
67+
null, "fallback_models": null, "configuration": null}'
6868
headers:
6969
accept:
7070
- '*/*'
7171
connection:
7272
- keep-alive
7373
content-length:
74-
- '258'
74+
- '283'
7575
content-type:
7676
- application/json
7777
method: POST
78-
uri: http://localhost:8000/v1/functions/8cb73d91-d845-41f2-b9c6-24e1eae960af/call
78+
uri: http://localhost:8000/v1/functions/bbaff35a-0807-4da3-8e38-c9ef1b193bdd/call
7979
response:
8080
body:
81-
string: '{"span_id":"6c8c2cbd-3e71-47ff-baa5-9fc0be00bfde","message":null,"json_payload":"Hola","context":null,"cached":false}'
81+
string: '{"span_id":"e85cbe1d-234a-45fd-9d1f-15543a3df88d","message":null,"json_payload":"Hola","context":null,"cached":false}'
8282
headers:
8383
content-length:
8484
- '117'
8585
content-type:
8686
- application/json
8787
date:
88-
- Tue, 15 Oct 2024 14:18:17 GMT
88+
- Tue, 12 Nov 2024 14:58:26 GMT
8989
server:
9090
- uvicorn
9191
status:
@@ -102,76 +102,54 @@ interactions:
102102
uri: http://localhost:8000/v1/functions/by_path/translate_list
103103
response:
104104
body:
105-
string: '{"errors":[{"type":"HTTPException","message":"Function not found","detail":"Function
106-
not found"}]}'
107-
headers:
108-
content-length:
109-
- '98'
110-
content-type:
111-
- application/json
112-
date:
113-
- Tue, 15 Oct 2024 14:18:20 GMT
114-
server:
115-
- uvicorn
116-
status:
117-
code: 404
118-
message: Not Found
119-
- request:
120-
body: ''
121-
headers:
122-
accept:
123-
- '*/*'
124-
connection:
125-
- keep-alive
126-
method: GET
127-
uri: http://localhost:8000/v1/functions/by_path/translate_list
128-
response:
129-
body:
130-
string: '{"errors":[{"type":"HTTPException","message":"Function not found","detail":"Function
131-
not found"}]}'
105+
string: '{"id":45,"uuid":"cc1c6a70-4c76-4618-9e27-8730c19d75c4","dataset_uuid":"1234802f-240d-4958-9de4-9176a07522ba","dataset":{"uuid":"1234802f-240d-4958-9de4-9176a07522ba","entry_count":0},"description":"Translate
106+
text to a list of target languages.","few_shot_count":2,"few_shot":false,"index_ids":[],"input_schema":null,"instructions":"Operation:
107+
translate_list\n\nOperation description: Translate text to a list of target
108+
languages.","language_model_id":null,"model":"openai/gpt-4o","out_schema":{"type":"array","items":{"type":"string"}},"path":"translate_list","project":{"uuid":"2a31ac04-b7d1-46c5-8df3-b09c63b5e4d8","name":"default"},"revision":1,"use_semantic_search":false}'
132109
headers:
133110
content-length:
134-
- '98'
111+
- '676'
135112
content-type:
136113
- application/json
137114
date:
138-
- Tue, 15 Oct 2024 14:18:20 GMT
115+
- Tue, 12 Nov 2024 14:58:27 GMT
139116
server:
140117
- uvicorn
141118
status:
142-
code: 404
143-
message: Not Found
119+
code: 200
120+
message: OK
144121
- request:
145-
body: '{"uuid": null, "path": "translate_list", "description": "Translate text
146-
to a list of target languages.", "input_schema": null, "out_schema": {"type":
147-
"array", "items": {"type": "string"}}, "instructions": "Operation: translate_list\n\nOperation
148-
description: Translate text to a list of target languages.", "model": null,
149-
"index_ids": [], "use_semantic_search": null, "few_shot": null, "few_shot_count":
150-
null, "cache_configuration": null, "dataset_uuid": null}'
122+
body: '{"uuid": "cc1c6a70-4c76-4618-9e27-8730c19d75c4", "path": "translate_list",
123+
"description": "Translate text to a list of target languages.", "input_schema":
124+
null, "out_schema": {"type": "array", "items": {"type": "string"}}, "instructions":
125+
"Operation: translate_list\n\nOperation description: Translate text to a list
126+
of target languages.", "model": null, "index_ids": [], "use_semantic_search":
127+
false, "few_shot": false, "few_shot_count": 2, "cache_configuration": null,
128+
"dataset_uuid": "1234802f-240d-4958-9de4-9176a07522ba"}'
151129
headers:
152130
accept:
153131
- '*/*'
154132
connection:
155133
- keep-alive
156134
content-length:
157-
- '459'
135+
- '526'
158136
content-type:
159137
- application/json
160-
method: POST
161-
uri: http://localhost:8000/v1/functions
138+
method: PATCH
139+
uri: http://localhost:8000/v1/functions/cc1c6a70-4c76-4618-9e27-8730c19d75c4
162140
response:
163141
body:
164-
string: '{"id":50,"uuid":"35722143-1c5e-4e6d-b9e1-6827ab0bbedc","dataset_uuid":"7ebfaa87-1516-44e4-b175-f2dcceb84d94","description":"Translate
142+
string: '{"id":45,"uuid":"cc1c6a70-4c76-4618-9e27-8730c19d75c4","dataset_uuid":"1234802f-240d-4958-9de4-9176a07522ba","dataset":{"uuid":"1234802f-240d-4958-9de4-9176a07522ba","entry_count":0},"description":"Translate
165143
text to a list of target languages.","few_shot_count":2,"few_shot":false,"index_ids":[],"input_schema":null,"instructions":"Operation:
166144
translate_list\n\nOperation description: Translate text to a list of target
167-
languages.","language_model_id":null,"model":"azure/gpt4-eu","out_schema":{"type":"array","items":{"type":"string"}},"path":"translate_list","project":{"uuid":"e64848ae-df03-4c0b-9a6f-4c498bf3dc8b","name":"asdfasdf"},"revision":1,"use_semantic_search":false}'
145+
languages.","language_model_id":null,"model":"openai/gpt-4o","out_schema":{"type":"array","items":{"type":"string"}},"path":"translate_list","project":{"uuid":"2a31ac04-b7d1-46c5-8df3-b09c63b5e4d8","name":"default"},"revision":1,"use_semantic_search":false}'
168146
headers:
169147
content-length:
170-
- '603'
148+
- '676'
171149
content-type:
172150
- application/json
173151
date:
174-
- Tue, 15 Oct 2024 14:18:20 GMT
152+
- Tue, 12 Nov 2024 14:58:27 GMT
175153
server:
176154
- uvicorn
177155
status:
@@ -181,28 +159,28 @@ interactions:
181159
body: '{"name": null, "instructions": "you are a helpful assistant", "input_schema":
182160
null, "input": {"text": "Hello", "target_languages": ["es", "fr"]}, "output_schema":
183161
null, "model": null, "examples": null, "stream": false, "parent_span_uuid":
184-
null, "configuration": null}'
162+
null, "fallback_models": null, "configuration": null}'
185163
headers:
186164
accept:
187165
- '*/*'
188166
connection:
189167
- keep-alive
190168
content-length:
191-
- '267'
169+
- '292'
192170
content-type:
193171
- application/json
194172
method: POST
195-
uri: http://localhost:8000/v1/functions/35722143-1c5e-4e6d-b9e1-6827ab0bbedc/call
173+
uri: http://localhost:8000/v1/functions/cc1c6a70-4c76-4618-9e27-8730c19d75c4/call
196174
response:
197175
body:
198-
string: '{"span_id":"bcde5f46-7750-4327-8f26-7ccd8940941a","message":null,"json_payload":["Hola","Bonjour"],"context":null,"cached":false}'
176+
string: '{"span_id":"89d580f0-4eb9-4daf-a961-5ac4cad760ec","message":null,"json_payload":["Hola","Bonjour"],"context":null,"cached":false}'
199177
headers:
200178
content-length:
201179
- '129'
202180
content-type:
203181
- application/json
204182
date:
205-
- Tue, 15 Oct 2024 14:18:20 GMT
183+
- Tue, 12 Nov 2024 14:58:27 GMT
206184
server:
207185
- uvicorn
208186
status:

0 commit comments

Comments
 (0)