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