Elffuss BraidHub

kikocis/continue public

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