| 1 | import { ChatMessage, CompletionOptions } from "../../index.js"; |
| 2 | import { BaseLLM } from "../index.js"; |
| 3 | |
| 4 | const HARDCODED_CHAT_RESPONSE = "THIS IS A HARDCODED RESPONSE"; |
| 5 | const NEXT_EDIT_COMPLETION: string = "<|editable_region_start|>\nHELLO\n"; |
| 6 | |
| 7 | class TestLLM extends BaseLLM { |
| 8 | static providerName = "test"; |
| 9 | |
| 10 | private findResponse(prompt: string) { |
| 11 | // Matches TEST_USER_MESSAGE_ followed by digits, preceded and followed by non-digit characters |
| 12 | const matches = Array.from( |
| 13 | prompt.matchAll(/[^0-9]*TEST_USER_MESSAGE_(\d+)[^0-9]*/g), |
| 14 | ); |
| 15 | if (matches.length > 0) { |
| 16 | const lastMatch = matches[matches.length - 1]; |
| 17 | const number = lastMatch[1]; |
| 18 | return `TEST_LLM_RESPONSE_${number}`; |
| 19 | } |
| 20 | return undefined; |
| 21 | } |
| 22 | |
| 23 | protected async *_streamComplete( |
| 24 | prompt: string, |
| 25 | signal: AbortSignal, |
| 26 | options: CompletionOptions, |
| 27 | ): AsyncGenerator<string> { |
| 28 | if ( |
| 29 | prompt === "NEXT_EDIT" || |
| 30 | prompt === |
| 31 | "<|im_start|>user\nNEXT_EDIT<|im_end|>\n<|im_start|>assistant\n" |
| 32 | ) { |
| 33 | yield NEXT_EDIT_COMPLETION; |
| 34 | } else { |
| 35 | yield this.findResponse(prompt) || `PROMPT: ${prompt}`; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | protected async *_streamChat( |
| 40 | messages: ChatMessage[], |
| 41 | signal: AbortSignal, |
| 42 | options: CompletionOptions, |
| 43 | ): AsyncGenerator<ChatMessage> { |
| 44 | for (const char of HARDCODED_CHAT_RESPONSE) { |
| 45 | yield { |
| 46 | role: "assistant", |
| 47 | content: char, |
| 48 | }; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | export default TestLLM; |