diff --git a/core/llm/llms/WatsonX.ts b/core/llm/llms/WatsonX.ts index 48b98d399..561f4f2d9 100644 --- a/core/llm/llms/WatsonX.ts +++ b/core/llm/llms/WatsonX.ts @@ -138,7 +138,7 @@ class WatsonX extends BaseLLM { }; } - protected async getOrFetchWatsonxToken() { + protected async updateWatsonxToken() { var now = new Date().getTime() / 1000; if ( watsonxToken === undefined || @@ -194,7 +194,7 @@ class WatsonX extends BaseLLM { signal: AbortSignal, options: CompletionOptions, ): AsyncGenerator { - await this.getOrFetchWatsonxToken(); + await this.updateWatsonxToken(); const stopSequences = options.stop?.slice(0, 6) ?? @@ -272,7 +272,7 @@ class WatsonX extends BaseLLM { } protected async _embed(chunks: string[]): Promise { - await this.getOrFetchWatsonxToken(); + await this.updateWatsonxToken(); const payload: any = { inputs: chunks, @@ -318,7 +318,15 @@ class WatsonX extends BaseLLM { throw new Error("Query and chunks must not be empty"); } try { - await this.getOrFetchWatsonxToken(); + await this.updateWatsonxToken(); + + const headers = { + "Content-Type": "application/json", + Authorization: `${ + watsonxToken.expiration === -1 ? "ZenApiKey" : "Bearer" + } ${watsonxToken.token}`, + }; + const payload: any = { inputs: chunks.map((chunk) => ({ text: chunk.content })), query: query, @@ -331,20 +339,15 @@ class WatsonX extends BaseLLM { model_id: this.model, project_id: this.projectId, }; - const headers = { - "Content-Type": "application/json", - Authorization: `${ - watsonxToken.expiration === -1 ? "ZenApiKey" : "Bearer" - } ${watsonxToken.token}`, - }; + const resp = await this.fetch( new URL( `${this.apiBase}/ml/v1/text/rerank?version=${this.apiVersion}`, ), { method: "POST", - body: JSON.stringify(payload), headers: headers, + body: JSON.stringify(payload), }, ); diff --git a/docs/docs/customize/model-providers/more/watsonx.mdx b/docs/docs/customize/model-providers/more/watsonx.mdx index 0854e0cd6..f2360788f 100644 --- a/docs/docs/customize/model-providers/more/watsonx.mdx +++ b/docs/docs/customize/model-providers/more/watsonx.mdx @@ -244,3 +244,37 @@ To view the list of available embeddings models, visit [this page](https://datap ``` + + +## Reranker + + + + ```yaml title="config.yaml" + models: + - name: Watsonx Reranker + provider: watsonx + model: cross-encoder/ms-marco-minilm-l-12-v2 + apiBase: https://us-south.ml.cloud.ibm.com + projectId: PROJECT_ID + apiKey: API_KEY/ZENAPI_KEY/USERNAME:PASSWORD + apiVersion: 2024-03-14 + ``` + + + ```json title="config.json" + { + "reranker": { + "name": "watsonx", + "params": { + "model": "cross-encoder/ms-marco-minilm-l-12-v2", + "apiBase": "watsonx endpoint e.g. https://us-south.ml.cloud.ibm.com", + "projectId": "PROJECT_ID", + "apiKey": "API_KEY/ZENAPI_KEY/USERNAME:PASSWORD", + "apiVersion": "2024-03-14" + } + } + } + ``` + +