| 1 | import { streamSse } from "@continuedev/fetch"; |
| 2 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 3 | import { osModelsEditPrompt } from "../templates/edit.js"; |
| 4 | |
| 5 | import OpenAI from "./OpenAI.js"; |
| 6 | |
| 7 | class Moonshot extends OpenAI { |
| 8 | static providerName = "moonshot"; |
| 9 | static defaultOptions: Partial<LLMOptions> = { |
| 10 | apiBase: "https://api.moonshot.cn/v1/", |
| 11 | model: "moonshot-v1-8k", |
| 12 | promptTemplates: { |
| 13 | edit: osModelsEditPrompt, |
| 14 | }, |
| 15 | useLegacyCompletionsEndpoint: false, |
| 16 | }; |
| 17 | maxStopWords: number | undefined = 16; |
| 18 | |
| 19 | supportsFim(): boolean { |
| 20 | return true; |
| 21 | } |
| 22 | |
| 23 | async *_streamFim( |
| 24 | prefix: string, |
| 25 | suffix: string, |
| 26 | signal: AbortSignal, |
| 27 | options: CompletionOptions, |
| 28 | ): AsyncGenerator<string> { |
| 29 | const endpoint = new URL("v1/chat/completions", this.apiBase); |
| 30 | const resp = await this.fetch(endpoint, { |
| 31 | method: "POST", |
| 32 | body: JSON.stringify({ |
| 33 | model: options.model, |
| 34 | messages: [ |
| 35 | { |
| 36 | role: "user", |
| 37 | content: prefix + "[fill]" + suffix, |
| 38 | }, |
| 39 | ], |
| 40 | max_tokens: options.maxTokens, |
| 41 | temperature: options.temperature, |
| 42 | top_p: options.topP, |
| 43 | frequency_penalty: options.frequencyPenalty, |
| 44 | presence_penalty: options.presencePenalty, |
| 45 | stop: options.stop, |
| 46 | stream: true, |
| 47 | }), |
| 48 | headers: { |
| 49 | "Content-Type": "application/json", |
| 50 | Accept: "application/json", |
| 51 | Authorization: `Bearer ${this.apiKey}`, |
| 52 | }, |
| 53 | signal, |
| 54 | }); |
| 55 | for await (const chunk of streamSse(resp)) { |
| 56 | yield chunk.choices[0].delta.content; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | export default Moonshot; |