Skip to content

Commit

Permalink
chore: Improve samples (#143)
Browse files Browse the repository at this point in the history
* improve samples and fix custom request config type

* remove unnecessary “fix”

* fix error handling

* fix smoke test
  • Loading branch information
marikaner authored Sep 19, 2024
1 parent c448cb9 commit 18dfd16
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/orchestration/src/orchestration-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
122 changes: 116 additions & 6 deletions sample-code/src/orchestration.ts
Original file line number Diff line number Diff line change
@@ -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<CompletionPostResponse> {
export async function orchestrationCompletion(
sampleCase: string
): Promise<string | undefined> {
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<string | undefined> {
const orchestrationClient = new OrchestrationClient({
llm: {
model_name: 'gpt-4-32k',
Expand All @@ -18,6 +40,94 @@ export async function orchestrationCompletion(): Promise<CompletionPostResponse>
}
});

// 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<string | undefined> {
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<string | undefined> {
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();
}
4 changes: 2 additions & 2 deletions sample-code/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/smoke-tests/test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 18dfd16

Please sign in to comment.