| 1 | import { OpenAI } from "openai/index"; |
| 2 | import { |
| 3 | ChatCompletion, |
| 4 | ChatCompletionChunk, |
| 5 | ChatCompletionCreateParamsNonStreaming, |
| 6 | ChatCompletionCreateParamsStreaming, |
| 7 | Completion, |
| 8 | CompletionCreateParamsNonStreaming, |
| 9 | CompletionCreateParamsStreaming, |
| 10 | Model, |
| 11 | } from "openai/resources/index"; |
| 12 | import { JinaConfig } from "../types.js"; |
| 13 | import { customFetch, rerank } from "../util.js"; |
| 14 | import { |
| 15 | BaseLlmApi, |
| 16 | CreateRerankResponse, |
| 17 | FimCreateParamsStreaming, |
| 18 | RerankCreateParams, |
| 19 | } from "./base.js"; |
| 20 | |
| 21 | export class JinaApi implements BaseLlmApi { |
| 22 | apiBase: string = "https://api.jina.ai/v1/"; |
| 23 | |
| 24 | constructor(protected config: JinaConfig) { |
| 25 | this.apiBase = config.apiBase ?? this.apiBase; |
| 26 | } |
| 27 | |
| 28 | async chatCompletionNonStream( |
| 29 | body: ChatCompletionCreateParamsNonStreaming, |
| 30 | ): Promise<ChatCompletion> { |
| 31 | throw new Error("Method not implemented."); |
| 32 | } |
| 33 | async *chatCompletionStream( |
| 34 | body: ChatCompletionCreateParamsStreaming, |
| 35 | ): AsyncGenerator<ChatCompletionChunk, any, unknown> { |
| 36 | throw new Error("Method not implemented."); |
| 37 | } |
| 38 | async completionNonStream( |
| 39 | body: CompletionCreateParamsNonStreaming, |
| 40 | ): Promise<Completion> { |
| 41 | throw new Error("Method not implemented."); |
| 42 | } |
| 43 | async *completionStream( |
| 44 | body: CompletionCreateParamsStreaming, |
| 45 | ): AsyncGenerator<Completion, any, unknown> { |
| 46 | throw new Error("Method not implemented."); |
| 47 | } |
| 48 | async *fimStream( |
| 49 | body: FimCreateParamsStreaming, |
| 50 | ): AsyncGenerator<ChatCompletionChunk, any, unknown> { |
| 51 | throw new Error("Method not implemented."); |
| 52 | } |
| 53 | |
| 54 | async embed( |
| 55 | body: OpenAI.Embeddings.EmbeddingCreateParams, |
| 56 | ): Promise<OpenAI.Embeddings.CreateEmbeddingResponse> { |
| 57 | throw new Error("Method not implemented."); |
| 58 | } |
| 59 | |
| 60 | async rerank(body: RerankCreateParams): Promise<CreateRerankResponse> { |
| 61 | const endpoint = new URL("rerank", this.apiBase); |
| 62 | const response = await customFetch(this.config.requestOptions)(endpoint, { |
| 63 | method: "POST", |
| 64 | body: JSON.stringify(body), |
| 65 | headers: { |
| 66 | "Content-Type": "application/json", |
| 67 | Accept: "application/json", |
| 68 | "x-api-key": this.config.apiKey ?? "", |
| 69 | Authorization: `Bearer ${this.config.apiKey}`, |
| 70 | }, |
| 71 | }); |
| 72 | const data = (await response.json()) as any; |
| 73 | |
| 74 | return rerank({ |
| 75 | model: body.model, |
| 76 | usage: { |
| 77 | total_tokens: 0, |
| 78 | }, |
| 79 | data: data.results.map((result: any) => result.relevance_score), |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | list(): Promise<Model[]> { |
| 84 | throw new Error("Method not implemented."); |
| 85 | } |
| 86 | } |