Skip to content

Commit

Permalink
feat: Add Metering Header (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatKuhr authored Sep 4, 2024
1 parent 78d78d2 commit b780212
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
30 changes: 12 additions & 18 deletions packages/core/src/http-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nock from 'nock';
import {
mockClientCredentialsGrantCall,
mockInference
aiCoreDestination
} from '../../../test-util/mock-http.js';
import { executeRequest } from './http-client.js';

Expand All @@ -16,26 +16,20 @@ describe('http-client', () => {
const mockPrompt = { prompt: 'some test prompt' };
const mockPromptResponse = { completion: 'some test completion' };

const scope = mockInference(
{
data: {
deploymentConfiguration: { deploymentId: 'deployment_id' },
prompt: 'some test prompt'
}
},
{
data: mockPromptResponse,
status: 200
const scope = nock(aiCoreDestination.url, {
reqheaders: {
'ai-resource-group': 'default',
'ai-client-type': 'AI SDK JavaScript'
}
);
})
.post('/v2/some/endpoint', mockPrompt)
.query({ 'api-version': 'mock-api-version' })
.reply(200, mockPromptResponse);

const res = await executeRequest(
{ url: 'mock-endpoint', apiVersion: 'mock-api-version' },
{
deploymentConfiguration: { deploymentId: 'deployment_id' },
...mockPrompt
}
{ url: '/some/endpoint', apiVersion: 'mock-api-version' },
mockPrompt
);

expect(scope.isDone()).toBe(true);
expect(res.status).toBe(200);
expect(res.data).toEqual(mockPromptResponse);
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ function mergeWithDefaultRequestConfig(
method: 'post',
headers: {
'content-type': 'application/json',
'ai-resource-group': 'default'
'ai-resource-group': 'default',
'ai-client-type': 'AI SDK JavaScript'
},
params: apiVersion ? { 'api-version': apiVersion } : {}
};
Expand Down
3 changes: 0 additions & 3 deletions test-util/mock-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { fileURLToPath } from 'url';
import { DestinationAuthToken, HttpDestination, ServiceCredentials } from '@sap-cloud-sdk/connectivity';
import nock from 'nock';
import {
BaseLlmParameters,
CustomRequestConfig,
EndpointOptions } from '@sap-ai-sdk/core';
import { dummyToken } from './mock-jwt.js';

Expand Down Expand Up @@ -91,7 +89,6 @@ export function mockClientCredentialsGrantCall(
export function mockInference(
request: {
data: any;
requestConfig?: CustomRequestConfig;
},
response: {
data: any;
Expand Down

0 comments on commit b780212

Please sign in to comment.