Skip to content

Commit

Permalink
feat: Improve client and type tests (#115)
Browse files Browse the repository at this point in the history
* feat: update openai chat completion success response

* fix: remove redundant tests for orchestration client

* feat: add more type tests for openai client

* feat: add more type tests for orchestration client

* docs: small rename

* fix: Changes from lint

* fix: response content

* fix: type test

---------

Co-authored-by: cloud-sdk-js <[email protected]>
  • Loading branch information
ZhongpinWang and cloud-sdk-js authored Sep 11, 2024
1 parent 7e463b0 commit 550f0f9
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('OpenAI response', () => {
it('should return default choice index with convenience functions', () => {
expect(openAiChatClientResponse.getFinishReason()).toBe('stop');
expect(openAiChatClientResponse.getContent()).toBe(
"The deepest place on Earth is located in the western Pacific Ocean. It's called the Mariana Trench."
'The deepest place on Earth is located in the Western Pacific Ocean and is known as the Mariana Trench.'
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
{
"choices": [
{
"content_filter_results": {
"hate": {
"filtered": false,
"severity": "safe"
},
"self_harm": {
"filtered": false,
"severity": "safe"
},
"sexual": {
"filtered": false,
"severity": "safe"
},
"violence": {
"filtered": false,
"severity": "safe"
}
},
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The deepest place on Earth is located in the western Pacific Ocean. It's called the Mariana Trench.",
"content": "The deepest place on Earth is located in the Western Pacific Ocean and is known as the Mariana Trench.",
"role": "assistant"
}
}
],
"created": 1718206847,
"id": "chatcmpl-kkli",
"created": 1725457796,
"id": "chatcmpl-A3kgOwg9B6j87n0IkoCFCUCxRSwQZ",
"model": "gpt-4-32k",
"object": "chat.completion",
"prompt_filter_results": [
{
"content_filter_results": {
"hate": {
"filtered": false,
"severity": "safe"
},
"jailbreak": {
"detected": false,
"filtered": false
},
"self_harm": {
"filtered": false,
"severity": "safe"
},
"sexual": {
"filtered": false,
"severity": "safe"
},
"violence": {
"filtered": false,
"severity": "safe"
}
},
"prompt_index": 0
}
],
"system_fingerprint": null,
"usage": {
"completion_tokens": 22,
Expand Down
60 changes: 59 additions & 1 deletion tests/type-tests/test/openai.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@sap-ai-sdk/foundation-models';

/**
* Chat Completion.
* Chat completion.
*/
expectType<Promise<OpenAiChatCompletionResponse>>(
new OpenAiChatClient('gpt-4').run({
Expand Down Expand Up @@ -58,6 +58,47 @@ expectType<OpenAiUsage>(
).getTokenUsage()
);

/**
* Chat completion with optional parameters.
*/
expectType<Promise<OpenAiChatCompletionResponse>>(
new OpenAiChatClient({
modelName: 'gpt-4',
modelVersion: 'latest'
}).run(
{
messages: [{ role: 'user', content: 'test prompt' }],
response_format: {
type: 'text'
},
seed: 42,
tools: [
{
type: 'function',
function: {
name: 'function 1',
description: 'description 1',
parameters: {
param1: 'value1'
}
}
}
],
tool_choice: {
type: 'function',
function: {
name: 'function 1'
}
}
},
{
params: {
apiVersion: '2024-02-01'
}
}
)
);

/**
* Embeddings.
*/
Expand All @@ -67,4 +108,21 @@ expectType<Promise<OpenAiEmbeddingOutput>>(
})
);

/**
* Embeddings with optional parameters.
*/
expectType<Promise<OpenAiEmbeddingOutput>>(
new OpenAiEmbeddingClient('text-embedding-ada-002').run(
{
input: ['test input 1', 'test input 2'],
user: 'some-guid'
},
{
params: {
apiVersion: '2024-02-01'
}
}
)
);

expectError<any>(new OpenAiEmbeddingClient('gpt-35-turbo'));
62 changes: 50 additions & 12 deletions tests/type-tests/test/orchestration.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,58 @@ expectType<Promise<OrchestrationResponse>>(
llmConfig: {
model_name: 'gpt-35-turbo-16k',
model_params: { max_tokens: 50, temperature: 0.1 }
}
}).chatCompletion({
messagesHistory: [
{
content:
'You are a helpful assistant who remembers all details the user shares with you.',
role: 'system'
},
filterConfig: {
input: {
filters: [
{
type: 'azure_content_safety',
config: {
Hate: 0,
SelfHarm: 2,
Sexual: 4,
Violence: 6
}
}
]
},
{
content: 'Hi! Im Bob',
role: 'user'
output: {
filters: [
{
type: 'azure_content_safety',
config: {
Hate: 6,
SelfHarm: 4,
Sexual: 2,
Violence: 0
}
}
]
}
}
}).chatCompletion(
{
messagesHistory: [
{
content:
'You are a helpful assistant who remembers all details the user shares with you.',
role: 'system'
},
{
content: 'Hi! Im {{?name}}',
role: 'user'
}
],
inputParams: {
name: 'Bob'
}
]
})
},
{
params: {
apiVersion: '2024-02-01'
}
}
)
);

/**
Expand Down

0 comments on commit 550f0f9

Please sign in to comment.