generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
115d6e9
commit 87a7cde
Showing
5 changed files
with
106 additions
and
22 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
packages/foundation-models/src/openai/openai-response.test.ts
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
50 changes: 50 additions & 0 deletions
50
packages/langchain/src/openai/__snapshots__/util.test.ts.snap
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,50 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Mapping Functions should parse an OpenAi response to a (Langchain) chat response 1`] = ` | ||
{ | ||
"generations": [ | ||
{ | ||
"generationInfo": { | ||
"finish_reason": "stop", | ||
"function_call": undefined, | ||
"index": 0, | ||
"tool_calls": undefined, | ||
}, | ||
"message": { | ||
"id": [ | ||
"langchain_core", | ||
"messages", | ||
"AIMessage", | ||
], | ||
"kwargs": { | ||
"additional_kwargs": { | ||
"finish_reason": "stop", | ||
"function_call": undefined, | ||
"index": 0, | ||
"tool_call_id": "", | ||
"tool_calls": undefined, | ||
}, | ||
"content": "The deepest place on Earth is located in the Western Pacific Ocean and is known as the Mariana Trench.", | ||
"invalid_tool_calls": [], | ||
"response_metadata": {}, | ||
"tool_calls": [], | ||
}, | ||
"lc": 1, | ||
"type": "constructor", | ||
}, | ||
"text": "The deepest place on Earth is located in the Western Pacific Ocean and is known as the Mariana Trench.", | ||
}, | ||
], | ||
"llmOutput": { | ||
"created": 1725457796, | ||
"id": "chatcmpl-A3kgOwg9B6j87n0IkoCFCUCxRSwQZ", | ||
"model": "gpt-4-32k", | ||
"object": "chat.completion", | ||
"tokenUsage": { | ||
"completionTokens": 22, | ||
"promptTokens": 15, | ||
"totalTokens": 37, | ||
}, | ||
}, | ||
} | ||
`; |
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 |
---|---|---|
@@ -1,23 +1,57 @@ | ||
// mapResponseToChatResult | ||
// mapLangchainToAiClient | ||
|
||
import { OpenAiChatCompletionOutput } from '@sap-ai-sdk/foundation-models'; | ||
import { parseMockResponse } from '../../../../test-util/mock-http.js'; | ||
import { OpenAiChatCompletionOutput , OpenAiChatClient as OpenAiChatClientBase, OpenAiEmbeddingParameters } from '@sap-ai-sdk/foundation-models'; | ||
import { jest } from '@jest/globals'; | ||
import nock from 'nock'; | ||
import { mockClientCredentialsGrantCall, mockInference, parseMockResponse } from '../../../../test-util/mock-http.js'; | ||
import { mapResponseToChatResult } from './util.js'; | ||
import { OpenAiChatClient } from './chat.js'; | ||
|
||
describe('Mapping Functions', () => { | ||
const openAiMockResponse = parseMockResponse<OpenAiChatCompletionOutput>( | ||
'foundation-models', | ||
'openai-chat-completion-success-response.json' | ||
const openAiMockResponse = parseMockResponse<OpenAiChatCompletionOutput>( | ||
'foundation-models', | ||
'openai-chat-completion-success-response.json' | ||
); | ||
|
||
const chatCompletionEndpoint = { | ||
url: 'inference/deployments/1234/chat/completions', | ||
apiVersion: '2024-02-01' | ||
}; | ||
|
||
beforeEach(() => { | ||
mockClientCredentialsGrantCall(); | ||
}); | ||
|
||
afterEach(() => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
it('should parse an OpenAi response to a (Langchain) chat response', async () => { | ||
const result = mapResponseToChatResult(openAiMockResponse); | ||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
it('should parse a Langchain input to an ai sdk input', async () => { | ||
const prompt = { | ||
input: ['AI is fascinating'] | ||
} as OpenAiEmbeddingParameters; | ||
|
||
mockInference( | ||
{ | ||
data: prompt | ||
}, | ||
{ | ||
data: openAiMockResponse, | ||
status: 200 | ||
}, | ||
chatCompletionEndpoint | ||
); | ||
it('should parse an OpenAi response to a (Langchain) chat response', async () => { | ||
const result = mapResponseToChatResult(openAiMockResponse); | ||
expect(result).toMatchInlineSnapshot(); | ||
}); | ||
|
||
// it('should compute an embedding vector', async () => { | ||
// const result = await embedQuery(); | ||
// expect(result).toBeDefined(); | ||
// expect(result).not.toHaveLength(0); | ||
// }); | ||
|
||
const client = new OpenAiChatClient({ deploymentId: '1234' }); | ||
const runSpy = jest.spyOn(OpenAiChatClientBase.prototype, 'run'); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const response = await client.invoke('Test'); | ||
expect(runSpy).toHaveBeenCalled(); | ||
}); | ||
}); |