Skip to content

Commit

Permalink
feat(similarity): add sentence similarity service
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Pilar <[email protected]>
  • Loading branch information
Tomas Pilar committed Jun 19, 2024
1 parent bd36c38 commit d159617
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/shared/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const MODEL = 'google/flan-ul2';
export const CHAT_MODEL = 'meta-llama/llama-3-70b-instruct';
export const EMBEDDING_MODEL = 'sentence-transformers/all-minilm-l6-v2';
16 changes: 16 additions & 0 deletions examples/similarity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Client } from '../src/index.js';

import { EMBEDDING_MODEL } from './shared/constants.js';

const client = new Client({
apiKey: process.env.GENAI_API_KEY,
});

{
const output = await client.text.experimental.sentenceSimilarity.create({
model_id: EMBEDDING_MODEL,
source_sentence: 'Good morning',
sentences: ['How are you?', 'Get lost!'],
});
console.log(output);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ibm-generative-ai/node-sdk",
"version": "3.0.0",
"version": "3.1.0",
"description": "IBM Generative AI Node.js SDK (Tech Preview)",
"keywords": [
"ai",
Expand Down
9 changes: 9 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export type TextEmbeddingCreateOutput = OutputWrapper<
ApiClientResponse<'POST', '/v2/text/embeddings'>['data']
>;

// TextSentenceSimilarityService

export type TextSentenceSimilarityCreateInput = InputBodyWrapper<
ApiClientOptions<'POST', '/v2/beta/text/sentence-similarity'>['body']
>;
export type TextSentenceSimilarityCreateOutput = OutputWrapper<
ApiClientResponse<'POST', '/v2/beta/text/sentence-similarity'>['data']
>;

// ModelService

export type ModelServiceListInput = InputQueryWrapper<
Expand Down
37 changes: 37 additions & 0 deletions src/services/text/TextSentenceSimilarityService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Options } from '../../client.js';
import { clientErrorWrapper } from '../../utils/errors.js';
import {
TextSentenceSimilarityCreateInput,
TextSentenceSimilarityCreateOutput,
} from '../../schema.js';
import { ApiClient } from '../../api/client.js';
import { SteamingApiClient } from '../../api/streaming-client.js';
import { ConcurrencyLimiter } from '../../utils/concurrency.js';
import { BaseService } from '../BaseService.js';

export class TextSentenceSimilarityService extends BaseService {
constructor(
protected readonly _client: ApiClient,
protected readonly _streamingClient: SteamingApiClient,
protected readonly _limiter: ConcurrencyLimiter,
) {
super(_client, _streamingClient);
}

create(
input: TextSentenceSimilarityCreateInput,
opts?: Options,
): Promise<TextSentenceSimilarityCreateOutput> {
return this._limiter.execute(
() =>
clientErrorWrapper(
this._client.POST('/v2/beta/text/sentence-similarity', {
...opts,
params: { query: { version: '2023-11-22' } },
body: input,
}),
),
{ signal: opts?.signal },
);
}
}
11 changes: 11 additions & 0 deletions src/services/text/TextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { TextGenerationService } from './TextGenerationService.js';
import { TextTokenizationService } from './TextTokenizationService.js';
import { TextEmbeddingService } from './TextEmbeddingService.js';
import { TextChatService } from './TextChatService.js';
import { TextSentenceSimilarityService } from './TextSentenceSimilarityService.js';

export class TextService extends BaseService {
public readonly generation: TextGenerationService;
public readonly tokenization: TextTokenizationService;
public readonly embedding: TextEmbeddingService;
public readonly chat: TextChatService;
public readonly experimental: {
sentenceSimilarity: TextSentenceSimilarityService;
};

constructor(client: ApiClient, streamingClient: SteamingApiClient) {
super(client, streamingClient);
Expand Down Expand Up @@ -51,5 +55,12 @@ export class TextService extends BaseService {
embeddingLimiter,
);
this.chat = new TextChatService(client, streamingClient, generationLimiter);
this.experimental = {
sentenceSimilarity: new TextSentenceSimilarityService(
client,
streamingClient,
embeddingLimiter,
),
};
}
}

0 comments on commit d159617

Please sign in to comment.