From f9b490a0662efcc370195f3640d51b8e1914d0a2 Mon Sep 17 00:00:00 2001 From: Zhongpin Wang Date: Mon, 16 Sep 2024 12:31:27 +0200 Subject: [PATCH] refactor: separate models --- packages/core/src/index.ts | 7 ---- packages/core/src/internal.ts | 1 - packages/foundation-models/src/index.ts | 4 +++ packages/foundation-models/src/model-types.ts | 20 +++++++++++ .../src/openai/openai-chat-client.ts | 7 ++-- .../src/openai/openai-embedding-client.ts | 7 ++-- packages/orchestration/src/index.ts | 8 +++++ .../src/model-types.ts | 33 ++++++++----------- .../orchestration/src/orchestration-types.ts | 2 +- tests/type-tests/test/model.test-d.ts | 5 --- tests/type-tests/test/openai.test-d.ts | 6 +++- tests/type-tests/test/orchestration.test-d.ts | 6 +++- 12 files changed, 61 insertions(+), 45 deletions(-) create mode 100644 packages/foundation-models/src/model-types.ts rename packages/{core => orchestration}/src/model-types.ts (51%) delete mode 100644 tests/type-tests/test/model.test-d.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 5128a08c..3cc6a523 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -7,10 +7,3 @@ export type { export { executeRequest } from './http-client.js'; export { getAiCoreDestination } from './context.js'; export { OpenApiRequestBuilder } from './openapi-request-builder.js'; -export type { - AzureOpenAiChatModel, - AzureOpenAiEmbeddingModel, - GcpVertexAiChatModel, - AwsBedrockChatModel, - ChatModel -} from './model-types.js'; diff --git a/packages/core/src/internal.ts b/packages/core/src/internal.ts index 979cc12c..7af67b5d 100644 --- a/packages/core/src/internal.ts +++ b/packages/core/src/internal.ts @@ -1,4 +1,3 @@ export * from './context.js'; export * from './http-client.js'; export * from './openapi-request-builder.js'; -export * from './model-types.js'; diff --git a/packages/foundation-models/src/index.ts b/packages/foundation-models/src/index.ts index d891b6bd..bbab71eb 100644 --- a/packages/foundation-models/src/index.ts +++ b/packages/foundation-models/src/index.ts @@ -31,3 +31,7 @@ export { OpenAiEmbeddingClient, OpenAiChatCompletionResponse } from './openai/index.js'; +export { + AzureOpenAiChatModel, + AzureOpenAiEmbeddingModel +} from './model-types.js'; diff --git a/packages/foundation-models/src/model-types.ts b/packages/foundation-models/src/model-types.ts new file mode 100644 index 00000000..d71b29e0 --- /dev/null +++ b/packages/foundation-models/src/model-types.ts @@ -0,0 +1,20 @@ +type LiteralUnion = T | (U & Record); + +/** + * Azure OpenAI models for chat completion. + */ +export type AzureOpenAiChatModel = LiteralUnion< + | 'gpt-4o-mini' + | 'gpt-4o' + | 'gpt-4' + | 'gpt-4-32k' + | 'gpt-35-turbo' + | 'gpt-35-turbo-16k' +>; + +/** + * Azure OpenAI models for embedding. + */ +export type AzureOpenAiEmbeddingModel = LiteralUnion< + 'text-embedding-ada-002' | 'text-embedding-3-small' | 'text-embedding-3-large' +>; diff --git a/packages/foundation-models/src/openai/openai-chat-client.ts b/packages/foundation-models/src/openai/openai-chat-client.ts index 28140bb1..f9d08261 100644 --- a/packages/foundation-models/src/openai/openai-chat-client.ts +++ b/packages/foundation-models/src/openai/openai-chat-client.ts @@ -1,12 +1,9 @@ -import { - type CustomRequestConfig, - type AzureOpenAiChatModel, - executeRequest -} from '@sap-ai-sdk/core'; +import { type CustomRequestConfig, executeRequest } from '@sap-ai-sdk/core'; import { getDeploymentId, type ModelDeployment } from '@sap-ai-sdk/ai-api/internal.js'; +import type { AzureOpenAiChatModel } from '../model-types.js'; import type { OpenAiChatCompletionParameters } from './openai-types.js'; import { OpenAiChatCompletionResponse } from './openai-response.js'; diff --git a/packages/foundation-models/src/openai/openai-embedding-client.ts b/packages/foundation-models/src/openai/openai-embedding-client.ts index b6f4bfbc..82e15cb4 100644 --- a/packages/foundation-models/src/openai/openai-embedding-client.ts +++ b/packages/foundation-models/src/openai/openai-embedding-client.ts @@ -1,12 +1,9 @@ -import { - type CustomRequestConfig, - type AzureOpenAiEmbeddingModel, - executeRequest -} from '@sap-ai-sdk/core'; +import { type CustomRequestConfig, executeRequest } from '@sap-ai-sdk/core'; import { getDeploymentId, type ModelDeployment } from '@sap-ai-sdk/ai-api/internal.js'; +import type { AzureOpenAiEmbeddingModel } from '../model-types.js'; import type { OpenAiEmbeddingParameters, OpenAiEmbeddingOutput diff --git a/packages/orchestration/src/index.ts b/packages/orchestration/src/index.ts index 5c5a4516..2b78c03c 100644 --- a/packages/orchestration/src/index.ts +++ b/packages/orchestration/src/index.ts @@ -39,3 +39,11 @@ export { OrchestrationClient } from './orchestration-client.js'; export { azureContentFilter } from './orchestration-filter-utility.js'; export { OrchestrationResponse } from './orchestration-response.js'; + +export type { + AzureOpenAiChatModel, + AicoreOpensourceChatModel, + GcpVertexAiChatModel, + AwsBedrockChatModel, + ChatModel +} from './model-types.js'; diff --git a/packages/core/src/model-types.ts b/packages/orchestration/src/model-types.ts similarity index 51% rename from packages/core/src/model-types.ts rename to packages/orchestration/src/model-types.ts index b42139da..5b205eff 100644 --- a/packages/core/src/model-types.ts +++ b/packages/orchestration/src/model-types.ts @@ -4,35 +4,20 @@ type LiteralUnion = T | (U & Record); * Azure OpenAI models for chat completion. */ export type AzureOpenAiChatModel = LiteralUnion< - | 'gpt-4o-mini' - | 'gpt-4o' - | 'gpt-4' - | 'gpt-4-32k' - | 'gpt-35-turbo' - | 'gpt-35-turbo-0125' - | 'gpt-35-turbo-16k' ->; - -/** - * Azure OpenAI models for embedding. - */ -export type AzureOpenAiEmbeddingModel = LiteralUnion< - 'text-embedding-ada-002' | 'text-embedding-3-small' | 'text-embedding-3-large' + 'gpt-4o' | 'gpt-4' | 'gpt-4-32k' | 'gpt-35-turbo' | 'gpt-35-turbo-16k' >; /** * GCP Vertex AI models for chat completion. */ export type GcpVertexAiChatModel = LiteralUnion< - 'gemini-1.0-pro' | 'gemini-1.5-pro' | 'gemini-1.5-flash' | 'chat-bison' + 'gemini-1.0-pro' | 'gemini-1.5-pro' | 'gemini-1.5-flash' >; /** * AWS Bedrock models for chat completion. */ export type AwsBedrockChatModel = LiteralUnion< - | 'amazon--titan-text-express' - | 'amazon--titan-text-lite' | 'anthropic--claude-3-haiku' | 'anthropic--claude-3-opus' | 'anthropic--claude-3-sonnet' @@ -40,8 +25,18 @@ export type AwsBedrockChatModel = LiteralUnion< >; /** - * All available models for chat completion. + * Aicore Opensource models for chat completion. + */ +export type AicoreOpensourceChatModel = LiteralUnion< + 'mistralai--mixtral-8x7b-instruct-v01' | 'meta--llama3-70b-instruct' +>; + +/** + * Supported chat models for orchestration. */ export type ChatModel = LiteralUnion< - AzureOpenAiChatModel | GcpVertexAiChatModel | AwsBedrockChatModel + | AzureOpenAiChatModel + | GcpVertexAiChatModel + | AwsBedrockChatModel + | AicoreOpensourceChatModel >; diff --git a/packages/orchestration/src/orchestration-types.ts b/packages/orchestration/src/orchestration-types.ts index aa26fb1c..ef6332d7 100644 --- a/packages/orchestration/src/orchestration-types.ts +++ b/packages/orchestration/src/orchestration-types.ts @@ -1,4 +1,4 @@ -import { ChatModel } from '@sap-ai-sdk/core'; +import { ChatModel } from './model-types.js'; import { ChatMessages, FilteringModuleConfig, diff --git a/tests/type-tests/test/model.test-d.ts b/tests/type-tests/test/model.test-d.ts deleted file mode 100644 index cdb3153b..00000000 --- a/tests/type-tests/test/model.test-d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { ChatModel, AzureOpenAiChatModel } from '@sap-ai-sdk/core'; - -expect('custom-model'); -expect('custom-model'); -expect('gpt-4-32k'); diff --git a/tests/type-tests/test/openai.test-d.ts b/tests/type-tests/test/openai.test-d.ts index 2e6bef9c..f5335719 100644 --- a/tests/type-tests/test/openai.test-d.ts +++ b/tests/type-tests/test/openai.test-d.ts @@ -5,7 +5,8 @@ import { OpenAiChatClient, OpenAiEmbeddingClient, OpenAiChatCompletionResponse, - OpenAiUsage + OpenAiUsage, + AzureOpenAiChatModel } from '@sap-ai-sdk/foundation-models'; /** @@ -126,3 +127,6 @@ expectType>( } ) ); + +expect('custom-model'); +expect('gpt-4-32k'); diff --git a/tests/type-tests/test/orchestration.test-d.ts b/tests/type-tests/test/orchestration.test-d.ts index 3fbcd57f..1ef64037 100644 --- a/tests/type-tests/test/orchestration.test-d.ts +++ b/tests/type-tests/test/orchestration.test-d.ts @@ -3,7 +3,8 @@ import { OrchestrationClient, CompletionPostResponse, OrchestrationResponse, - TokenUsage + TokenUsage, + ChatModel } from '@sap-ai-sdk/orchestration'; /** @@ -191,3 +192,6 @@ expectType>( } }).chatCompletion() ); + +expect('custom-model'); +expect('gemini-1.0-pro');