-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ai Gateway methods to AI Binding
- Loading branch information
Showing
10 changed files
with
505 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("//:build/wd_test.bzl", "wd_test") | ||
load("//src/workerd/server/tests/python:py_wd_test.bzl", "py_wd_test") | ||
|
||
wd_test( | ||
src = "aig-api-test.wd-test", | ||
args = ["--experimental"], | ||
data = glob(["*.js"]), | ||
) | ||
|
||
py_wd_test( | ||
size = "large", | ||
src = "python-aig-api-test.wd-test", | ||
args = ["--experimental"], | ||
data = glob([ | ||
"*.js", | ||
"*.py", | ||
]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (c) 2024 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
import * as assert from 'node:assert'; | ||
|
||
export const tests = { | ||
async test(_, env) { | ||
{ | ||
// Test gateway get log | ||
const resp = await env.ai.gateway('my-gateway').getLog('my-log-123'); | ||
assert.deepEqual(resp, { | ||
cached: false, | ||
cost: 0, | ||
created_at: new Date('2019-08-24T14:15:22Z'), | ||
custom_cost: true, | ||
duration: 0, | ||
id: 'string', | ||
metadata: 'string', | ||
model: 'string', | ||
model_type: 'string', | ||
path: 'string', | ||
provider: 'string', | ||
request_content_type: 'string', | ||
request_head: 'string', | ||
request_head_complete: true, | ||
request_size: 0, | ||
request_type: 'string', | ||
response_content_type: 'string', | ||
response_head: 'string', | ||
response_head_complete: true, | ||
response_size: 0, | ||
status_code: 0, | ||
step: 0, | ||
success: true, | ||
tokens_in: 0, | ||
tokens_out: 0, | ||
}); | ||
} | ||
|
||
{ | ||
// Test get log error responses | ||
try { | ||
await env.ai.gateway('my-gateway').getLog('404'); | ||
} catch (e) { | ||
assert.deepEqual( | ||
{ | ||
name: e.name, | ||
message: e.message, | ||
}, | ||
{ | ||
name: 'AiGatewayLogNotFound', | ||
message: 'Not Found', | ||
} | ||
); | ||
} | ||
} | ||
|
||
{ | ||
try { | ||
await env.ai.gateway('my-gateway').getLog('500'); | ||
} catch (e) { | ||
assert.deepEqual( | ||
{ | ||
name: e.name, | ||
message: e.message, | ||
}, | ||
{ | ||
name: 'AiGatewayInternalError', | ||
message: 'Internal Error', | ||
} | ||
); | ||
} | ||
} | ||
|
||
{ | ||
// Test patch log error responses | ||
try { | ||
await env.ai.gateway('my-gateway').patchLog('404', { feedback: -1 }); | ||
} catch (e) { | ||
assert.deepEqual( | ||
{ | ||
name: e.name, | ||
message: e.message, | ||
}, | ||
{ | ||
name: 'AiGatewayLogNotFound', | ||
message: 'Not Found', | ||
} | ||
); | ||
} | ||
} | ||
|
||
{ | ||
try { | ||
await env.ai.gateway('my-gateway').patchLog('500', { feedback: -1 }); | ||
} catch (e) { | ||
assert.deepEqual( | ||
{ | ||
name: e.name, | ||
message: e.message, | ||
}, | ||
{ | ||
name: 'AiGatewayInternalError', | ||
message: 'Internal Error', | ||
} | ||
); | ||
} | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright (c) 2024 Cloudflare, Inc. | ||
# Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
# https://opensource.org/licenses/Apache-2.0 | ||
|
||
|
||
async def test(context, env): | ||
resp = await env.ai.gateway("my-gateway").getLog("my-log-123") | ||
assert resp.cached is False | ||
assert resp.model == "string" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const unitTests :Workerd.Config = ( | ||
services = [ | ||
( name = "aig-api-test", | ||
worker = ( | ||
modules = [ | ||
(name = "worker", esModule = embed "aig-api-test.js") | ||
], | ||
compatibilityDate = "2023-01-15", | ||
compatibilityFlags = ["nodejs_compat"], | ||
bindings = [ | ||
( | ||
name = "ai", | ||
wrapped = ( | ||
moduleName = "cloudflare-internal:ai-api", | ||
innerBindings = [( | ||
name = "fetcher", | ||
service = "aig-mock" | ||
)], | ||
) | ||
) | ||
], | ||
) | ||
), | ||
( name = "aig-mock", | ||
worker = ( | ||
compatibilityDate = "2023-01-15", | ||
compatibilityFlags = ["experimental", "nodejs_compat"], | ||
modules = [ | ||
(name = "worker", esModule = embed "aig-mock.js") | ||
], | ||
) | ||
) | ||
] | ||
); |
Oops, something went wrong.