Skip to content

Commit

Permalink
Allow users to pass customField to /rerank endpoint (#303)
Browse files Browse the repository at this point in the history
## Problem

The current implementation of `/rerank` in the TS client does not
(correctly) allow users to pass a custom field upon which to rerank.

## Solution

Allow custom fields!

Please reference this PR to account for all expected functionality:
https://github.com/pinecone-io/python-plugin-inference/pull/21/files

## Type of Change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update
- [ ] Infrastructure change (CI configs, etc)
- [ ] Non-code change (docs, etc)
- [ ] None of the above: (explain here)

## Test Plan

CI passes + reviewer xreferences PR above w/functionality introduced in
this PR.

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1208523729730914

---------

Co-authored-by: Jesse Seldess <[email protected]>
  • Loading branch information
aulorbe and jseldess committed Oct 23, 2024
1 parent 02f4ef9 commit 4e655d3
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 292 deletions.
50 changes: 39 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1104,20 +1104,20 @@ import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const rerankingModel = 'bge-reranker-v2-m3';
const myQuery = 'What are some good Turkey dishes for Thanksgiving?';
const myDocuments = [
{ text: 'I love turkey sandwiches with pastrami' },
{
text: 'A lemon brined Turkey with apple sausage stuffing is a classic Thanksgiving main',
},
{ text: 'My favorite Thanksgiving dish is pumpkin pie' },
{ text: 'Turkey is a great source of protein' },

// Option 1: Documents as an array of strings
const myDocsStrings = [
'I love turkey sandwiches with pastrami',
'A lemon brined Turkey with apple sausage stuffing is a classic Thanksgiving main',
'My favorite Thanksgiving dish is pumpkin pie',
'Turkey is a great source of protein',
];

// >>> Sample without passing an `options` object:
// Option 1 response
const response = await pc.inference.rerank(
rerankingModel,
myQuery,
myDocuments
myDocsStrings
);
console.log(response);
// {
Expand All @@ -1131,15 +1131,43 @@ console.log(response);
// usage: { rerankUnits: 1 }
// }

// >>> Sample with an `options` object:
// Option 2: Documents as an array of objects
const myDocsObjs = [
{
title: 'Turkey Sandwiches',
body: 'I love turkey sandwiches with pastrami',
},
{
title: 'Lemon Turkey',
body: 'A lemon brined Turkey with apple sausage stuffing is a classic Thanksgiving main',
},
{
title: 'Thanksgiving',
body: 'My favorite Thanksgiving dish is pumpkin pie',
},
{
title: 'Protein Sources',
body: 'Turkey is a great source of protein',
},
];

// Option 2: Options object declaring which custom key to rerank on
// Note: If no custom key is passed via `rankFields`, each doc must contain a `text` key, and that will act as the default)
const rerankOptions = {
topN: 3,
returnDocuments: false,
rankFields: ['body'],
parameters: {
inputType: 'passage',
truncate: 'END',
},
};

// Option 2 response
const response = await pc.inference.rerank(
rerankingModel,
myQuery,
myDocuments,
myDocsObjs,
rerankOptions
);
console.log(response);
Expand Down
42 changes: 42 additions & 0 deletions src/inference/__tests__/embed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Inference } from '../inference';
import type { PineconeConfiguration } from '../../data';
import { inferenceOperationsBuilder } from '../inferenceOperationsBuilder';

let inference: Inference;

beforeAll(() => {
const config: PineconeConfiguration = { apiKey: 'test-api-key' };
const infApi = inferenceOperationsBuilder(config);
inference = new Inference(infApi);
});

describe('Inference Class: _formatInputs', () => {
test('Should format inputs correctly', () => {
const inputs = ['input1', 'input2'];
const expected = [{ text: 'input1' }, { text: 'input2' }];
const result = inference._formatInputs(inputs);
expect(result).toEqual(expected);
});
});

describe('Inference Class: embed', () => {
test('Should throw error if response is missing required fields', async () => {
const model = 'test-model';
const inputs = ['input1', 'input2'];
const params = { inputType: 'text', truncate: 'END' };

const mockedIncorrectResponse = { model: 'test-model' };
const expectedError = Error(
'Response from Inference API is missing required fields'
);
const embed = jest.spyOn(inference._inferenceApi, 'embed');
// @ts-ignore
embed.mockResolvedValue(mockedIncorrectResponse);

try {
await inference.embed(model, inputs, params);
} catch (error) {
expect(error).toEqual(expectedError);
}
});
});
216 changes: 0 additions & 216 deletions src/inference/__tests__/inference.test.ts

This file was deleted.

Loading

0 comments on commit 4e655d3

Please sign in to comment.