Elffuss BraidHub

kikocis/continue public

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