Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use literal string for orchestration models #125

Merged
merged 11 commits into from
Sep 12, 2024
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export type {
export { executeRequest } from './http-client.js';
export { getAiCoreDestination } from './context.js';
export { OpenApiRequestBuilder } from './openapi-request-builder.js';
export type * from './model-types.js';
1 change: 1 addition & 0 deletions packages/core/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './context.js';
export * from './http-client.js';
export * from './openapi-request-builder.js';
export * from './model-types.js';
52 changes: 52 additions & 0 deletions packages/core/src/model-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>);
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved

/**
* Azure OpenAI models for chat completion.
*/
export type AzureOpenAiChatModel = LiteralUnion<
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
| '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'
>;

/**
* 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'
>;

/**
* 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'
| 'anthropic--claude-3.5-sonnet'
>;

/**
* All available models for chat completion.
*/
export type ChatModel = LiteralUnion<
AzureOpenAiChatModel | GcpVertexAiChatModel | AwsBedrockChatModel
>;

/**
* All available models for embeddings.
*/
export type EmbeddingModel = LiteralUnion<AzureOpenAiEmbeddingModel>;
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 0 additions & 2 deletions packages/foundation-models/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export type {
OpenAiChatModel,
OpenAiEmbeddingModel,
OpenAiChatMessage,
OpenAiChatSystemMessage,
OpenAiChatUserMessage,
Expand Down
13 changes: 7 additions & 6 deletions packages/foundation-models/src/openai/openai-chat-client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { type CustomRequestConfig, executeRequest } from '@sap-ai-sdk/core';
import {
type CustomRequestConfig,
type AzureOpenAiChatModel,
executeRequest
} from '@sap-ai-sdk/core';
import {
getDeploymentId,
type ModelDeployment
} from '@sap-ai-sdk/ai-api/internal.js';
import type {
OpenAiChatCompletionParameters,
OpenAiChatModel
} from './openai-types.js';
import type { OpenAiChatCompletionParameters } from './openai-types.js';
import { OpenAiChatCompletionResponse } from './openai-response.js';

const apiVersion = '2024-02-01';
Expand All @@ -19,7 +20,7 @@ export class OpenAiChatClient {
* Creates an instance of the OpenAI chat client.
* @param modelDeployment - This configuration is used to retrieve a deployment. Depending on the configuration use either the given deployment ID or the model name to retrieve matching deployments. If model and deployment ID are given, the model is verified against the deployment.
*/
constructor(private modelDeployment: ModelDeployment<OpenAiChatModel>) {}
constructor(private modelDeployment: ModelDeployment<AzureOpenAiChatModel>) {}

/**
* Creates a completion for the chat messages.
Expand Down
13 changes: 9 additions & 4 deletions packages/foundation-models/src/openai/openai-embedding-client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { type CustomRequestConfig, executeRequest } from '@sap-ai-sdk/core';
import {
type CustomRequestConfig,
type AzureOpenAiEmbeddingModel,
executeRequest
} from '@sap-ai-sdk/core';
import {
getDeploymentId,
type ModelDeployment
} from '@sap-ai-sdk/ai-api/internal.js';
import type {
OpenAiEmbeddingParameters,
OpenAiEmbeddingOutput,
OpenAiEmbeddingModel
OpenAiEmbeddingOutput
} from './openai-types.js';

const apiVersion = '2024-02-01';
Expand All @@ -19,7 +22,9 @@ export class OpenAiEmbeddingClient {
* Creates an instance of the OpenAI embedding client.
* @param modelDeployment - This configuration is used to retrieve a deployment. Depending on the configuration use either the given deployment ID or the model name to retrieve matching deployments. If model and deployment ID are given, the model is verified against the deployment.
*/
constructor(private modelDeployment: ModelDeployment<OpenAiEmbeddingModel>) {}
constructor(
private modelDeployment: ModelDeployment<AzureOpenAiEmbeddingModel>
) {}

/**
* Creates an embedding vector representing the given text.
Expand Down
19 changes: 0 additions & 19 deletions packages/foundation-models/src/openai/openai-types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
/**
* Available OpenAI models for chat completion.
*/
export type OpenAiChatModel =
| 'gpt-4o'
| 'gpt-4'
| 'gpt-4-32k'
| 'gpt-35-turbo'
| 'gpt-35-turbo-0125'
| 'gpt-35-turbo-16k';

/**
* OpenAI embedding models.
*/
export type OpenAiEmbeddingModel =
| 'text-embedding-ada-002'
| 'text-embedding-3-small'
| 'text-embedding-3-large';

/**
* OpenAI system message.
*/
Expand Down
7 changes: 5 additions & 2 deletions packages/orchestration/src/orchestration-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChatModel } from '@sap-ai-sdk/core';
import {
ChatMessages,
FilteringModuleConfig,
Expand All @@ -20,11 +21,13 @@ export interface Prompt {
inputParams?: Record<string, string>;
}

// TODO: why do we have this extra type? and if there is a reason, why does it not apply to the filtering module?
/**
* LLMModule configuration.
*/
export type LlmConfig = LLMModuleConfig;
export type LlmConfig = LLMModuleConfig & {
/** */
model_name: ChatModel;
};

/**
* Orchestration module configuration.
Expand Down
10 changes: 10 additions & 0 deletions tests/type-tests/test/model.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {
ChatModel,
AzureOpenAiChatModel,
EmbeddingModel
} from '@sap-ai-sdk/core';

expect<ChatModel>('custom-model');
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
expect<AzureOpenAiChatModel>('custom-model');
expect<ChatModel>('gpt-4-32k');
expect<EmbeddingModel>('custom-model');
Loading