diff --git a/core/config/load.ts b/core/config/load.ts index 1d29124d6..b1afacab0 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -512,7 +512,11 @@ async function intermediateToFinalConfig( model: "rerank-2", ...params, }; - return new rerankerClass(llmOptions); + return new rerankerClass(llmOptions, (url: string | URL, init: any) => + fetchwithRequestOptions(url, init, { + ...params?.requestOptions, + }), + ); } return null; } @@ -991,5 +995,6 @@ export { finalToBrowserConfig, intermediateToFinalConfig, loadContinueConfigFromJson, - type BrowserSerializedContinueConfig, + type BrowserSerializedContinueConfig }; + diff --git a/core/context/allRerankers.ts b/core/context/allRerankers.ts index 56aa13474..944a77dbc 100644 --- a/core/context/allRerankers.ts +++ b/core/context/allRerankers.ts @@ -5,12 +5,14 @@ import HuggingFaceTEI from "../llm/llms/HuggingFaceTEI"; import { LLMReranker } from "../llm/llms/llm"; import ContinueProxy from "../llm/llms/stubs/ContinueProxy"; import Voyage from "../llm/llms/Voyage"; +import WatsonX from "../llm/llms/WatsonX"; export const AllRerankers: { [key: string]: any } = { cohere: Cohere, bedrock: Bedrock, llm: LLMReranker, voyage: Voyage, + watsonx: WatsonX, "free-trial": FreeTrial, "huggingface-tei": HuggingFaceTEI, "continue-proxy": ContinueProxy, diff --git a/core/control-plane/schema.ts b/core/control-plane/schema.ts index 9fcdd2219..d29316d5e 100644 --- a/core/control-plane/schema.ts +++ b/core/control-plane/schema.ts @@ -19,6 +19,7 @@ const modelDescriptionSchema = z.object({ "nebius", "siliconflow", "scaleway", + "watsonx" ]), model: z.string(), apiKey: z.string().optional(), @@ -88,6 +89,7 @@ const embeddingsProviderSchema = z.object({ "nebius", "siliconflow", "scaleway", + "watsonx" ]), apiBase: z.string().optional(), apiKey: z.string().optional(), @@ -109,7 +111,7 @@ const embeddingsProviderSchema = z.object({ }); const rerankerSchema = z.object({ - name: z.enum(["cohere", "voyage", "llm"]), + name: z.enum(["cohere", "voyage", "llm", "watsonx"]), params: z.record(z.any()).optional(), }); diff --git a/core/llm/llms/WatsonX.ts b/core/llm/llms/WatsonX.ts index 17c8b1d4b..48b98d399 100644 --- a/core/llm/llms/WatsonX.ts +++ b/core/llm/llms/WatsonX.ts @@ -1,4 +1,4 @@ -import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; +import { ChatMessage, Chunk, CompletionOptions, LLMOptions } from "../../index.js"; import { renderChatMessage } from "../../util/messageContent.js"; import { BaseLLM } from "../index.js"; import { streamResponse } from "../stream.js"; @@ -138,6 +138,26 @@ class WatsonX extends BaseLLM { }; } + protected async getOrFetchWatsonxToken() { + var now = new Date().getTime() / 1000; + if ( + watsonxToken === undefined || + now > watsonxToken.expiration || + watsonxToken.token === undefined + ) { + watsonxToken = await this.getBearerToken(); + } else { + console.log( + `Reusing token (expires in ${ + (watsonxToken.expiration - now) / 60 + } mins)`, + ); + } + if (watsonxToken.token === undefined) { + throw new Error("Something went wrong. Check your credentials, please."); + } + } + protected async _complete( prompt: string, signal: AbortSignal, @@ -174,23 +194,8 @@ class WatsonX extends BaseLLM { signal: AbortSignal, options: CompletionOptions, ): AsyncGenerator { - var now = new Date().getTime() / 1000; - if ( - watsonxToken === undefined || - now > watsonxToken.expiration || - watsonxToken.token === undefined - ) { - watsonxToken = await this.getBearerToken(); - } else { - console.log( - `Reusing token (expires in ${ - (watsonxToken.expiration - now) / 60 - } mins)`, - ); - } - if (watsonxToken.token === undefined) { - throw new Error("Something went wrong. Check your credentials, please."); - } + await this.getOrFetchWatsonxToken(); + const stopSequences = options.stop?.slice(0, 6) ?? (options.model?.includes("granite") ? ["Question:"] : []); @@ -267,20 +272,8 @@ class WatsonX extends BaseLLM { } protected async _embed(chunks: string[]): Promise { - var now = new Date().getTime() / 1000; - if ( - watsonxToken === undefined || - now > watsonxToken.expiration || - watsonxToken.token === undefined - ) { - watsonxToken = await this.getBearerToken(); - } else { - console.log( - `Reusing token (expires in ${ - (watsonxToken.expiration - now) / 60 - } mins)`, - ); - } + await this.getOrFetchWatsonxToken(); + const payload: any = { inputs: chunks, parameters: { @@ -318,6 +311,63 @@ class WatsonX extends BaseLLM { } return embeddings.map((e: any) => e.embedding); } + + + async rerank(query: string, chunks: Chunk[]): Promise { + if (!query || !chunks.length) { + throw new Error("Query and chunks must not be empty"); + } + try { + await this.getOrFetchWatsonxToken(); + const payload: any = { + inputs: chunks.map((chunk) => ({ text: chunk.content })), + query: query, + parameters: { + truncate_input_tokens: 500, + return_options: { + top_n: chunks.length + }, + }, + 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, + }, + ); + + if (!resp.ok) { + throw new Error(`Failed to rerank chunks: ${await resp.text()}`); + } + const data = await resp.json(); + const ranking = data.results; + + if (!ranking) { + throw new Error("Empty response received from Watsonx"); + } + + // Sort results by index to maintain original order + return ranking + .sort((a: any, b: any) => a.index - b.index) + .map((result: any) => result.score); + + } catch (error) { + console.error("Error in WatsonxReranker.rerank:", error); + throw error; + } + } } export default WatsonX; diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index de90cf347..2becae999 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -2843,6 +2843,7 @@ "bedrock", "cohere", "voyage", + "watsonx", "llm", "free-trial", "huggingface-tei" diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 6cf41cadf..9b61f1900 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "1.1.0", + "version": "1.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "1.1.0", + "version": "1.1.2", "license": "Apache-2.0", "dependencies": { "@continuedev/fetch": "^1.0.3", diff --git a/packages/config-types/src/index.ts b/packages/config-types/src/index.ts index 8a0806d7b..e60b8e79b 100644 --- a/packages/config-types/src/index.ts +++ b/packages/config-types/src/index.ts @@ -58,6 +58,7 @@ export const modelDescriptionSchema = z.object({ "continue-proxy", "nebius", "scaleway", + "watsonx" ]), model: z.string(), apiKey: z.string().optional(), @@ -111,6 +112,7 @@ export const embeddingsProviderSchema = z.object({ "continue-proxy", "nebius", "scaleway", + "watsonx" ]), apiBase: z.string().optional(), apiKey: z.string().optional(), @@ -173,7 +175,7 @@ export const contextProviderSchema = z.object({ export type ContextProvider = z.infer; export const rerankerSchema = z.object({ - name: z.enum(["cohere", "voyage", "llm", "continue-proxy"]), + name: z.enum(["cohere", "voyage", "watsonx", "llm", "continue-proxy"]), params: z.record(z.any()).optional(), }); export type Reranker = z.infer;