Elffuss BraidHub

kikocis/continue public

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