| 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 | |
| 6 | class TestLLM extends BaseLLM { |
| 7 | static providerName = "test"; |
| 8 | |
| 9 | private findResponse(prompt: string) { |
| 10 | // Matches TEST_USER_MESSAGE_ followed by digits, preceded and followed by non-digit characters |
| 11 | const matches = Array.from( |
| 12 | prompt.matchAll(/[^0-9]*TEST_USER_MESSAGE_(\d+)[^0-9]*/g), |
| 13 | ); |
| 14 | if (matches.length > 0) { |
| 15 | const lastMatch = matches[matches.length - 1]; |
| 16 | const number = lastMatch[1]; |
| 17 | return `TEST_LLM_RESPONSE_${number}`; |
| 18 | } |
| 19 | return undefined; |
| 20 | } |
| 21 | |
| 22 | protected async *_streamComplete( |
| 23 | prompt: string, |
| 24 | signal: AbortSignal, |
| 25 | options: CompletionOptions, |
| 26 | ): AsyncGenerator<string> { |
| 27 | yield this.findResponse(prompt) || `PROMPT: ${prompt}`; |
| 28 | } |
| 29 | |
| 30 | protected async *_streamChat( |
| 31 | messages: ChatMessage[], |
| 32 | signal: AbortSignal, |
| 33 | options: CompletionOptions, |
| 34 | ): AsyncGenerator<ChatMessage> { |
| 35 | for (const char of HARDCODED_CHAT_RESPONSE) { |
| 36 | yield { |
| 37 | role: "assistant", |
| 38 | content: char, |
| 39 | }; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | export default TestLLM; |