From 18dfd168b10cd2e84c4b142499fe443af380d13a Mon Sep 17 00:00:00 2001 From: Marika Marszalkowski <868536+marikaner@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:44:16 +0200 Subject: [PATCH] chore: Improve samples (#143) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * improve samples and fix custom request config type * remove unnecessary “fix” * fix error handling * fix smoke test --- .../src/orchestration-response.ts | 1 + sample-code/src/orchestration.ts | 122 +++++++++++++++++- sample-code/src/server.ts | 4 +- tests/smoke-tests/test/smoke.test.ts | 2 +- 4 files changed, 120 insertions(+), 9 deletions(-) diff --git a/packages/orchestration/src/orchestration-response.ts b/packages/orchestration/src/orchestration-response.ts index b7680f31..cbdd1e79 100644 --- a/packages/orchestration/src/orchestration-response.ts +++ b/packages/orchestration/src/orchestration-response.ts @@ -41,6 +41,7 @@ export class OrchestrationResponse { /** * Parses the orchestration response and returns the content. + * If the response was filtered, an error is thrown. * @param choiceIndex - The index of the choice to parse. * @returns The message content. */ diff --git a/sample-code/src/orchestration.ts b/sample-code/src/orchestration.ts index 3984644e..f1cd1da4 100644 --- a/sample-code/src/orchestration.ts +++ b/sample-code/src/orchestration.ts @@ -1,13 +1,35 @@ import { - CompletionPostResponse, - OrchestrationClient + OrchestrationClient, + azureContentFilter } from '@sap-ai-sdk/orchestration'; /** - * Ask GPT about the capital of France. - * @returns The answer from the orchestration service in Gen AI Hub. + * Create different types of orchestration requests. + * @param sampleCase - Name of the sample case to orchestrate. + * @returns The message content from the orchestration service in the generative AI hub. */ -export async function orchestrationCompletion(): Promise { +export async function orchestrationCompletion( + sampleCase: string +): Promise { + switch (sampleCase) { + case 'simple': + return orchestrationCompletionSimple(); + case 'template': + return orchestrationCompletionTemplate(); + case 'filtering': + return orchestrationCompletionFiltering(); + case 'requestConfig': + return orchestrationCompletionRequestConfig(); + default: + return undefined; + } +} + +/** + * Ask about the capital of France. + * @returns The message content from the orchestration service in the generative AI hub. + */ +async function orchestrationCompletionSimple(): Promise { const orchestrationClient = new OrchestrationClient({ llm: { model_name: 'gpt-4-32k', @@ -18,6 +40,94 @@ export async function orchestrationCompletion(): Promise } }); + // Call the orchestration service. const response = await orchestrationClient.chatCompletion(); - return response.data; + // Access the response content. + return response.getContent(); +} + +/** + * Ask about the capital of any country using a template. + * @returns The message content from the orchestration service in the generative AI hub. + */ +async function orchestrationCompletionTemplate(): Promise { + const orchestrationClient = new OrchestrationClient({ + llm: { + model_name: 'gpt-4-32k', + model_params: {} + }, + templating: { + template: [ + { role: 'user', content: 'What is the capital of {{?country}}?' } + ] + } + }); + + // Call the orchestration service. + const response = await orchestrationClient.chatCompletion({ + inputParams: { country: 'France' } + }); + // Access the response content. + return response.getContent(); +} + +/** + * Allow any user input and apply input and output filters. + * Handles the case where the input or output are filtered: + * - In case the input was filtered the response has a non 200 status code. + * - In case the output was filtered `response.getContent()` throws an error. + * @returns The message content from the orchestration service in the generative AI hub. + */ +async function orchestrationCompletionFiltering(): Promise { + const filter = azureContentFilter({ Hate: 0, Violence: 0 }); + const orchestrationClient = new OrchestrationClient({ + llm: { + model_name: 'gpt-4-32k', + model_params: {} + }, + templating: { + template: [{ role: 'user', content: '{{?input}}' }] + }, + filtering: { + input: filter, + output: filter + } + }); + + try { + // Call the orchestration service. + const response = await orchestrationClient.chatCompletion({ + inputParams: { input: 'I hate you!' } + }); + // Access the response content. + return response.getContent(); + } catch (error: any) { + // Handle the case where the output was filtered. + return `Error: ${error.message}`; + } +} + +/** + * Ask about the capital of France and send along custom request configuration. + * @returns The message content from the orchestration service in the generative AI hub. + */ +async function orchestrationCompletionRequestConfig(): Promise< + string | undefined +> { + const orchestrationClient = new OrchestrationClient({ + llm: { + model_name: 'gpt-4-32k', + model_params: {} + }, + templating: { + template: [{ role: 'user', content: 'What is the capital of France?' }] + } + }); + + // Call the orchestration service. + const response = await orchestrationClient.chatCompletion(undefined, { + headers: { 'x-custom-header': 'custom-value' } + }); + // Access the response content. + return response.getContent(); } diff --git a/sample-code/src/server.ts b/sample-code/src/server.ts index f3a71860..e2ba3447 100644 --- a/sample-code/src/server.ts +++ b/sample-code/src/server.ts @@ -40,9 +40,9 @@ app.get('/embedding', async (req, res) => { } }); -app.get('/orchestration', async (req, res) => { +app.get('/orchestration/:sampleCase', async (req, res) => { try { - res.send(await orchestrationCompletion()); + res.send(await orchestrationCompletion(req.params.sampleCase)); } catch (error: any) { console.error(error); res diff --git a/tests/smoke-tests/test/smoke.test.ts b/tests/smoke-tests/test/smoke.test.ts index e9608664..00ffb41c 100644 --- a/tests/smoke-tests/test/smoke.test.ts +++ b/tests/smoke-tests/test/smoke.test.ts @@ -9,7 +9,7 @@ describe('Smoke Test', () => { it('orchestration client retrieves completion results', async () => { await expect( - fetch(`${smokeTestRoute}/orchestration`) + fetch(`${smokeTestRoute}/orchestration/simple`) ).resolves.toHaveProperty('status', 200); }); });