| 1 | import { RequestOptions } from "@continuedev/config-types"; |
| 2 | import { fetchwithRequestOptions } from "@continuedev/fetch"; |
| 3 | import fetch from "node-fetch"; |
| 4 | import { |
| 5 | ChatCompletionChunk, |
| 6 | CompletionUsage, |
| 7 | CreateEmbeddingResponse, |
| 8 | Model, |
| 9 | } from "openai/resources/index"; |
| 10 | import { ChatCompletion } from "openai/src/resources/index.js"; |
| 11 | import { CreateRerankResponse } from "./apis/base.js"; |
| 12 | |
| 13 | export function chatChunk(options: { |
| 14 | content: string | null | undefined; |
| 15 | model: string; |
| 16 | finish_reason?: ChatCompletionChunk.Choice["finish_reason"]; |
| 17 | id?: string | null; |
| 18 | usage?: CompletionUsage; |
| 19 | }): ChatCompletionChunk { |
| 20 | return { |
| 21 | choices: [ |
| 22 | { |
| 23 | delta: { |
| 24 | content: options.content, |
| 25 | role: "assistant", |
| 26 | }, |
| 27 | finish_reason: options.finish_reason ?? "stop", |
| 28 | index: 0, |
| 29 | logprobs: null, |
| 30 | }, |
| 31 | ], |
| 32 | usage: options.usage, |
| 33 | created: Date.now(), |
| 34 | id: options.id ?? "", |
| 35 | model: options.model, |
| 36 | object: "chat.completion.chunk", |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | export function chatChunkFromDelta(options: { |
| 41 | delta: ChatCompletionChunk.Choice["delta"]; |
| 42 | model: string; |
| 43 | finish_reason?: ChatCompletionChunk.Choice["finish_reason"]; |
| 44 | id?: string | null; |
| 45 | usage?: CompletionUsage; |
| 46 | }): ChatCompletionChunk { |
| 47 | return { |
| 48 | choices: [ |
| 49 | { |
| 50 | delta: options.delta, |
| 51 | finish_reason: options.finish_reason ?? "stop", |
| 52 | index: 0, |
| 53 | logprobs: null, |
| 54 | }, |
| 55 | ], |
| 56 | usage: options.usage, |
| 57 | created: Date.now(), |
| 58 | id: options.id ?? "", |
| 59 | model: options.model, |
| 60 | object: "chat.completion.chunk", |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | export function chatCompletion(options: { |
| 65 | content: string | null | undefined; |
| 66 | model: string; |
| 67 | finish_reason?: ChatCompletion.Choice["finish_reason"]; |
| 68 | id?: string | null; |
| 69 | usage?: CompletionUsage; |
| 70 | index?: number | null; |
| 71 | }): ChatCompletion { |
| 72 | return { |
| 73 | choices: [ |
| 74 | { |
| 75 | finish_reason: options.finish_reason ?? "stop", |
| 76 | index: options.index ?? 0, |
| 77 | logprobs: null, |
| 78 | message: { |
| 79 | content: options.content ?? null, |
| 80 | role: "assistant", |
| 81 | refusal: null, |
| 82 | }, |
| 83 | }, |
| 84 | ], |
| 85 | usage: options.usage, |
| 86 | created: Date.now(), |
| 87 | id: options.id ?? "", |
| 88 | model: options.model, |
| 89 | object: "chat.completion", |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | export function embedding(options: { |
| 94 | data: number[][]; |
| 95 | model: string; |
| 96 | usage?: CreateEmbeddingResponse.Usage; |
| 97 | }): CreateEmbeddingResponse { |
| 98 | return { |
| 99 | data: options.data.map((embedding, i) => ({ |
| 100 | index: i, |
| 101 | embedding: embedding, |
| 102 | object: "embedding" as const, |
| 103 | })), |
| 104 | model: options.model, |
| 105 | object: "list" as const, |
| 106 | usage: options.usage ?? { |
| 107 | prompt_tokens: 0, |
| 108 | total_tokens: 0, |
| 109 | }, |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | export function rerank(options: { |
| 114 | model: string; |
| 115 | data: number[]; |
| 116 | usage?: CreateRerankResponse["usage"]; |
| 117 | }): CreateRerankResponse { |
| 118 | return { |
| 119 | data: options.data.map((score, index) => ({ |
| 120 | index, |
| 121 | relevance_score: score, |
| 122 | })), |
| 123 | model: options.model, |
| 124 | object: "list" as const, |
| 125 | usage: options.usage ?? { |
| 126 | total_tokens: 0, |
| 127 | }, |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | export function model(options: { id: string; owned_by?: string }): Model { |
| 132 | return { |
| 133 | id: options.id, |
| 134 | object: "model", |
| 135 | created: Date.now(), |
| 136 | owned_by: options.owned_by ?? "organization-owner", |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | export function maybeCustomFetch(requestOptions: RequestOptions | undefined) { |
| 141 | return requestOptions |
| 142 | ? (url: any, init: any) => |
| 143 | fetchwithRequestOptions(url, init, requestOptions) |
| 144 | : undefined; |
| 145 | } |
| 146 | |
| 147 | export function customFetch(requestOptions: RequestOptions | undefined) { |
| 148 | return maybeCustomFetch(requestOptions) ?? fetch; |
| 149 | } |