Skip to content

Commit

Permalink
feat: Add grounding convenience function (#378)
Browse files Browse the repository at this point in the history
* grounding convenience

* review + tests+ changeset

* lint

* restructure types

* fix: Changes from lint

* add type test

* fix: Changes from lint

* docs

* fix: Changes from lint

* Apply suggestions from code review

* review

* review

* fix: Changes from lint

* update type DocumentGroundingServiceFilter

* fix: Changes from lint

* docs review

* fix: Changes from lint

* Apply suggestions from code review

* adjust sample for grounding

* fix: Changes from lint

---------

Co-authored-by: cloud-sdk-js <cloud-sdk-js@github.com>
Co-authored-by: Zhongpin Wang <zhongpin.wang@sap.com>
  • Loading branch information
3 people authored Jan 13, 2025
1 parent b65e38b commit a039890
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 181 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-brooms-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-ai-sdk/orchestration': minor
---

[Improvement] Add `buildDocumentGroundingConfig()` convenience function to create document grounding configuration in the Orchestration client.
18 changes: 7 additions & 11 deletions packages/orchestration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ return response.getContent();
### Grounding

Grounding enables integrating external, contextually relevant, domain-specific, or real-time data into AI processes.
The grounding configuration can be provided as a raw JSON object or by using the `buildDocumentGroundingConfig()` function, which requires only the minimal mandatory values.

```ts
const orchestrationClient = new OrchestrationClient({
Expand All @@ -346,21 +347,16 @@ const orchestrationClient = new OrchestrationClient({
],
defaults: {}
},
grounding: {
type: 'document_grounding_service',
config: {
filters: [
grounding: buildDocumentGroundingConfig(
input_params: ['groundingRequest'],
output_param: 'groundingOutput',
filters: [
{
id: 'filter1',
data_repositories: ['*'],
search_config: {},
data_repository_type: 'vector'
data_repositories: ['repository-id']
}
],
input_params: ['groundingRequest'],
output_param: 'groundingOutput'
}
}
)
});

const response = await orchestrationClient.chatCompletion({
Expand Down
7 changes: 6 additions & 1 deletion packages/orchestration/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ export type {
OrchestrationModuleConfig,
LlmModuleConfig,
Prompt,
DocumentGroundingServiceConfig,
DocumentGroundingServiceFilter,
LlmModelParams
} from './orchestration-types.js';

export { OrchestrationClient } from './orchestration-client.js';

export { buildAzureContentFilter } from './orchestration-utils.js';
export {
buildAzureContentFilter,
buildDocumentGroundingConfig
} from './orchestration-utils.js';

export { OrchestrationResponse } from './orchestration-response.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { constructCompletionPostRequest } from './orchestration-client.js';
import { buildAzureContentFilter } from './orchestration-utils.js';
import type { CompletionPostRequest } from './client/api/schema';
import type { CompletionPostRequest } from './client/api/schema/index.js';
import type { OrchestrationModuleConfig } from './orchestration-types.js';

describe('construct completion post request', () => {
Expand Down
35 changes: 35 additions & 0 deletions packages/orchestration/src/orchestration-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ChatModel } from './model-types.js';
import type {
ChatMessages,
DataRepositoryType,
DocumentGroundingFilter,
FilteringModuleConfig,
GroundingModuleConfig,
MaskingModuleConfig,
Expand Down Expand Up @@ -69,3 +71,36 @@ export interface OrchestrationModuleConfig {
*/
grounding?: GroundingModuleConfig;
}

/**
* Represents a filter configuration for the Document Grounding Service.
*/
export type DocumentGroundingServiceFilter = Omit<
DocumentGroundingFilter,
'data_repository_type'
> & {
/**
* Defines the type of data repository.
* If not set, the default value is 'vector'.
*/
data_repository_type?: DataRepositoryType;
};

/**
* Represents the configuration for the Document Grounding Service.
*/
export interface DocumentGroundingServiceConfig {
/**
* Defines the filters to apply during the grounding process.
*/
filters?: DocumentGroundingServiceFilter[];
/**
* Contains the input parameters used for grounding input questions.
*/
input_params: string[];
/**
* Parameter name used for grounding output.
* @example "groundingOutput"
*/
output_param: string;
}
Loading

0 comments on commit a039890

Please sign in to comment.