Elffuss BraidHub

kikocis/continue public

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