| 1 | import { streamSse } from "@continuedev/fetch"; |
| 2 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 3 | import { BaseLLM } from "../index.js"; |
| 4 | |
| 5 | class LlamaCpp extends BaseLLM { |
| 6 | static providerName = "llama.cpp"; |
| 7 | static defaultOptions: Partial<LLMOptions> = { |
| 8 | apiBase: "http://127.0.0.1:8080/", |
| 9 | }; |
| 10 | |
| 11 | private _convertArgs(options: CompletionOptions, prompt: string) { |
| 12 | const finalOptions = { |
| 13 | n_predict: options.maxTokens, |
| 14 | frequency_penalty: options.frequencyPenalty, |
| 15 | presence_penalty: options.presencePenalty, |
| 16 | min_p: options.minP, |
| 17 | mirostat: options.mirostat, |
| 18 | stop: options.stop, |
| 19 | top_k: options.topK, |
| 20 | top_p: options.topP, |
| 21 | temperature: options.temperature, |
| 22 | }; |
| 23 | |
| 24 | return finalOptions; |
| 25 | } |
| 26 | |
| 27 | protected async *_streamComplete( |
| 28 | prompt: string, |
| 29 | signal: AbortSignal, |
| 30 | options: CompletionOptions, |
| 31 | ): AsyncGenerator<string> { |
| 32 | const headers = { |
| 33 | "Content-Type": "application/json", |
| 34 | Authorization: `Bearer ${this.apiKey}`, |
| 35 | ...this.requestOptions?.headers, |
| 36 | }; |
| 37 | |
| 38 | const resp = await this.fetch(new URL("completion", this.apiBase), { |
| 39 | method: "POST", |
| 40 | headers, |
| 41 | body: JSON.stringify({ |
| 42 | prompt, |
| 43 | stream: true, |
| 44 | ...this._convertArgs(options, prompt), |
| 45 | }), |
| 46 | signal, |
| 47 | }); |
| 48 | |
| 49 | for await (const value of streamSse(resp)) { |
| 50 | if (value.content) { |
| 51 | yield value.content; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | export default LlamaCpp; |