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