-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* JS Reranker * JS Reranker --------- Co-authored-by: dailin01 <[email protected]>
- Loading branch information
1 parent
64fe7e6
commit c552fcc
Showing
12 changed files
with
257 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* eslint-disable max-len */ | ||
// Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {Reranker, setEnvVariable} from '../../index'; | ||
|
||
// 设置环境变量 | ||
setEnvVariable('QIANFAN_BASE_URL', 'http://127.0.0.1:8866'); | ||
setEnvVariable('QIANFAN_CONSOLE_API_BASE_URL', 'http://127.0.0.1:8866'); | ||
setEnvVariable('QIANFAN_ACCESS_KEY', '123'); | ||
setEnvVariable('QIANFAN_SECRET_KEY', '456'); | ||
|
||
describe('Reranker functionality', () => { | ||
let client; | ||
|
||
beforeEach(() => { | ||
client = new Reranker(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should reorder documents by query', async () => { | ||
const resp = await client.reranker({ | ||
query: '上海天气', | ||
documents: ['上海气候', '北京美食'], | ||
}); | ||
console.log(resp); | ||
expect(resp).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {BaseClient} from '../Base'; | ||
import {modelInfoMap} from './utilts'; | ||
import {getPathAndBody, getUpperCaseModelAndModelMap} from '../utils'; | ||
import {RerankerBody, RerankerResp} from '../interface'; | ||
import {ModelType} from '../enum'; | ||
|
||
class Reranker extends BaseClient { | ||
|
||
public async reranker(body: RerankerBody, model = 'bce-reranker-base_v1'): Promise<RerankerResp> { | ||
const {modelInfoMapUppercase, modelUppercase} = getUpperCaseModelAndModelMap(model, modelInfoMap); | ||
const type = ModelType.RERANKER; | ||
const {AKPath, requestBody} = getPathAndBody({ | ||
model: modelUppercase, | ||
modelInfoMap: modelInfoMapUppercase, | ||
baseUrl: this.qianfanBaseUrl, | ||
body, | ||
endpoint: this.Endpoint, | ||
type, | ||
}); | ||
const resp = await this.sendRequest(type, model, AKPath, requestBody); | ||
return resp as RerankerResp; | ||
} | ||
} | ||
|
||
export default Reranker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {QfLLMInfoMap} from '../interface'; | ||
|
||
/** | ||
* 重新排序向量模型 | ||
*/ | ||
export type RerankerModel = | ||
| 'bce-reranker-base_v1'; | ||
|
||
export const modelInfoMap: QfLLMInfoMap = { | ||
'bce-reranker-base_v1': { | ||
endpoint: '/reranker/bce_reranker_base', | ||
required_keys: ['query', 'documents'], | ||
optional_keys: [ | ||
'user_id', | ||
'top_n', | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2024 Baidu, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {Reranker, setEnvVariable} from '../index'; | ||
|
||
// 修改env文件 | ||
// setEnvVariable('QIANFAN_AK','***'); | ||
// setEnvVariable('QIANFAN_SK','***'); | ||
|
||
// 直接读取env | ||
const client = new Reranker(); | ||
|
||
// 手动传AK/SK 测试 | ||
// const client = new Embedding({ QIANFAN_AK: '***', QIANFAN_SK: '***'}); | ||
// 手动传ACCESS_KEY/ SECRET_KEY测试 | ||
// const client = new Embedding({ QIANFAN_ACCESS_KEY: '***', QIANFAN_SECRET_KEY: '***' }); | ||
|
||
// AK/SK 测试 | ||
async function main() { | ||
const resp = await client.reranker({ | ||
query: '上海天气', | ||
documents: ['上海气候', '北京美食'], | ||
}); | ||
console.log('返回结果'); | ||
console.log(resp); | ||
} | ||
|
||
main(); |