Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reranking support #155

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import type {
ProgressResponse,
PullRequest,
PushRequest,
RerankRequest,
RerankResponse,
ShowRequest,
ShowResponse,
StatusResponse,
Expand Down Expand Up @@ -299,6 +301,21 @@ async encodeImage(image: Uint8Array | string): Promise<string> {
return (await response.json()) as EmbeddingsResponse
}

/**
* Rerank the documents and return at most top_n of them in rank order with scores.
* @param request {RerankRequest} - The request object.
* @returns {Promise<RerankResponse>} - The response object.
*/
async rerank(request: RerankRequest): Promise<RerankResponse> {
if (request.top_n === undefined) {
request.top_n = request.documents.length;
}
const response = await utils.post(this.fetch, `${this.config.host}/api/rerank`, {
...request,
})
return (await response.json()) as RerankResponse
}

/**
* Lists the running models on the server
* @returns {Promise<ListResponse>} - The response object.
Expand Down
18 changes: 18 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ export interface EmbeddingsRequest {
options?: Partial<Options>
}

export interface RerankRequest {
model: string
query: string
documents: string[]
top_n?: number
}

// response types

export interface GenerateResponse {
Expand Down Expand Up @@ -199,6 +206,17 @@ export interface EmbeddingsResponse {
embedding: number[]
}

export interface RerankResponse {
model: string
results: {
document: string
relevance_score: number
}[]
total_duration: number
load_duration: number
prompt_eval_count: number
}

export interface ProgressResponse {
status: string
digest: string
Expand Down