Skip to content

Commit

Permalink
Deprecate getLogs method from Ai binding
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored and fhanau committed Apr 25, 2024
1 parent 7bb684f commit 50ef78d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 33 deletions.
26 changes: 7 additions & 19 deletions src/cloudflare/internal/ai-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class Ai {
private readonly fetcher: Fetcher

private options: AiOptions = {};
private logs: Array<string> = [];
public lastRequestId: string | null = null;

public constructor(fetcher: Fetcher) {
Expand Down Expand Up @@ -80,22 +79,8 @@ export class Ai {
}

return res.body;
} else {
// load logs
if (this.options.debug) {
let parsedLogs: string[] = [];
try {
const logHeader = res.headers.get("cf-ai-logs")
if (logHeader) {
parsedLogs = (JSON.parse(atob(logHeader)) as string[]);
}
} catch {
/* empty */
}

this.logs = parsedLogs;
}

} else {
if (!res.ok || !res.body) {
throw new InferenceUpstreamError(await res.text());
}
Expand All @@ -110,9 +95,12 @@ export class Ai {
}
}

public getLogs(): Array<string> {
return this.logs;
}
/*
* @deprecated this method is deprecated, do not use this
*/
public getLogs(): Array<string> {
return []
}
}

export default function makeBinding(env: { fetcher: Fetcher }): Ai {
Expand Down
9 changes: 0 additions & 9 deletions src/cloudflare/internal/test/ai/ai-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ export const tests = {
const resp = await env.ai.run('testModel', {prompt: 'test'})
assert.deepStrictEqual(resp, { response: 'model response' });

// Test logs is empty
assert.deepStrictEqual(env.ai.getLogs(), []);

// Test request id is present
assert.deepStrictEqual(env.ai.lastRequestId, '3a1983d7-1ddd-453a-ab75-c4358c91b582');
}
Expand All @@ -24,12 +21,6 @@ export const tests = {
assert.deepStrictEqual(resp instanceof ReadableStream, true);
}

{
// Test logs
await env.ai.run('testModel', {prompt: 'test'}, {debug: true})
assert.deepStrictEqual(env.ai.getLogs(), [ 'Model started', 'Model run successfully' ]);
}

{
// Test legacy fetch
const resp = await env.ai.fetch("http://workers-binding.ai/run?version=2", {
Expand Down
5 changes: 1 addition & 4 deletions src/cloudflare/internal/test/ai/ai-api-test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# Copyright (c) 2023 Cloudflare, Inc.
# 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.run('testModel', {"prompt": 'test'})
assert resp.response == "model response"

# Test logs is empty
assert len(env.ai.getLogs()) == 0

# Test request id is present
assert env.ai.lastRequestId == '3a1983d7-1ddd-453a-ab75-c4358c91b582'
1 change: 0 additions & 1 deletion src/cloudflare/internal/test/ai/ai-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default {

const respHeaders = {
'cf-ai-req-id': '3a1983d7-1ddd-453a-ab75-c4358c91b582',
'cf-ai-logs': (data.options.debug) ? btoa(JSON.stringify(["Model started", "Model run successfully"])) : null
}

if (modelName === 'blobResponseModel') {
Expand Down

0 comments on commit 50ef78d

Please sign in to comment.