Elffuss BraidHub

kikocis/continue public

main · 1.8 KiBRawPermalink
1import { streamSse } from "@continuedev/fetch";
2import { ChatCompletionChunk } from "openai/resources/index";
3import { LlamastackConfig } from "../types.js";
4import { chatChunk, customFetch } from "../util.js";
5import { OpenAIApi } from "./OpenAI.js";
6import { FimCreateParamsStreaming } from "./base.js";
7
8export const Llamastack_API_BASE = "http://localhost:8321/v1/openai/v1/";
9export class LlamastackApi extends OpenAIApi {
10 constructor(config: LlamastackConfig) {
11 super({
12 ...config,
13 provider: "openai",
14 apiBase: config.apiBase ?? Llamastack_API_BASE,
15 apiKey: config.apiKey ?? "dummy",
16 });
17 }
18
19 async *fimStream(
20 body: FimCreateParamsStreaming,
21 signal: AbortSignal,
22 ): AsyncGenerator<ChatCompletionChunk, any, unknown> {
23 const endpoint = new URL("completions", this.apiBase);
24 const resp = await customFetch(this.config.requestOptions)(endpoint, {
25 method: "POST",
26 body: JSON.stringify({
27 model: body.model,
28 prompt: body.prompt,
29 suffix: body.suffix,
30 max_tokens: body.max_tokens,
31 max_completion_tokens: (body as any).max_completion_tokens,
32 temperature: body.temperature,
33 top_p: body.top_p,
34 frequency_penalty: body.frequency_penalty,
35 presence_penalty: body.presence_penalty,
36 stop: body.stop,
37 stream: true,
38 }),
39 headers: {
40 "Content-Type": "application/json",
41 Accept: "application/json",
42 "x-api-key": this.config.apiKey ?? "",
43 Authorization: `Bearer ${this.config.apiKey}`,
44 },
45 signal,
46 });
47 for await (const chunk of streamSse(resp as any)) {
48 yield chatChunk({
49 content: chunk.choices[0].text,
50 finish_reason: chunk.finish_reason,
51 model: body.model,
52 });
53 }
54 }
55}