Skip to content

Commit

Permalink
use in orchestration
Browse files Browse the repository at this point in the history
  • Loading branch information
marikaner committed Aug 27, 2024
1 parent 53977c5 commit aae6fc8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
27 changes: 15 additions & 12 deletions packages/gen-ai-hub/src/orchestration/orchestration-client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { executeRequest, CustomRequestConfig } from '@sap-ai-sdk/core';
import {
DeploymentResolver,
resolveDeployment
} from '../utils/deployment-resolver.js';
import { resolveDeployment } from '../utils/deployment-resolver.js';
import {
CompletionPostRequest,
CompletionPostResponse
Expand All @@ -16,25 +13,31 @@ export class OrchestrationClient {
/**
* Creates a completion for the chat messages.
* @param data - The input parameters for the chat completion.
* @param deploymentResolver - A deployment ID or a function to retrieve it.
* @param deploymentId - A deployment ID or undefined to retrieve it based on the given model.
* @param requestConfig - Request configuration.
* @returns The completion result.
*/
async chatCompletion(
data: OrchestrationCompletionParameters,
deploymentResolver: DeploymentResolver = () =>
resolveDeployment({ scenarioId: 'orchestration' }),
deploymentId?: string,
requestConfig?: CustomRequestConfig
): Promise<CompletionPostResponse> {
const body = constructCompletionPostRequest(data);
const deployment =
typeof deploymentResolver === 'function'
? (await deploymentResolver()).id
: deploymentResolver;
deploymentId =
deploymentId ??
(
await resolveDeployment({
scenarioId: 'orchestration',
model: {
name: data.llmConfig.model_name,
version: data.llmConfig.model_version
}
})
).id;

const response = await executeRequest(
{
url: `/inference/deployments/${deployment}/completion`
url: `/inference/deployments/${deploymentId}/completion`
},
body,
requestConfig
Expand Down
19 changes: 13 additions & 6 deletions sample-code/src/aiservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ const openAiClient = new OpenAiClient();
* @returns The answer from GPT.
*/
export async function chatCompletion(): Promise<string> {
const response = await openAiClient.chatCompletion('gpt-35-turbo', {
messages: [{ role: 'user', content: 'What is the capital of France?' }]
});
const response = await openAiClient.chatCompletion(
{
messages: [{ role: 'user', content: 'What is the capital of France?' }]
},
'gpt-35-turbo'
);
const assistantMessage = response.choices[0]
.message as OpenAiChatAssistantMessage;
return assistantMessage.content!;
Expand All @@ -23,8 +26,12 @@ export async function chatCompletion(): Promise<string> {
* @returns An embedding vector.
*/
export async function computeEmbedding(): Promise<number[]> {
const response = await openAiClient.embeddings('text-embedding-ada-002', {
input: 'Hello, world!'
});
const response = await openAiClient.embeddings(
{
input: 'Hello, world!'
},
'text-embedding-ada-002'
);

return response.data[0].embedding;
}

0 comments on commit aae6fc8

Please sign in to comment.