|
| 1 | +import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager'; |
| 2 | +import { |
| 3 | + BaseLanguageModelCallOptions, |
| 4 | +} from '@langchain/core/language_models/base'; |
| 5 | +import { |
| 6 | + SimpleChatModel, |
| 7 | +} from '@langchain/core/language_models/chat_models'; |
| 8 | +import { AIMessageChunk, BaseMessage } from '@langchain/core/messages'; |
| 9 | +import { ChatGenerationChunk } from '@langchain/core/outputs'; |
| 10 | +import { |
| 11 | + GenerationConfig, |
| 12 | + LLMPipeline, |
| 13 | + StreamingStatus, |
| 14 | +} from 'openvino-genai-node'; |
| 15 | + |
| 16 | +export interface ChatOpenVINOParams extends BaseLanguageModelCallOptions { |
| 17 | + generationConfig?: GenerationConfig, |
| 18 | + modelPath: string, |
| 19 | + device?: string, |
| 20 | +} |
| 21 | + |
| 22 | +export class ChatOpenVINO extends SimpleChatModel { |
| 23 | + generateOptions: GenerationConfig; |
| 24 | + |
| 25 | + path: string; |
| 26 | + |
| 27 | + device: string; |
| 28 | + |
| 29 | + pipeline: Promise<any>; |
| 30 | + |
| 31 | + constructor(params: ChatOpenVINOParams) { |
| 32 | + super(params); |
| 33 | + this.path = params.modelPath; |
| 34 | + this.device = params.device || 'CPU'; |
| 35 | + this.pipeline = LLMPipeline(this.path, this.device); |
| 36 | + this.generateOptions = params.generationConfig || {}; |
| 37 | + } |
| 38 | + _llmType() { |
| 39 | + return 'OpenVINO'; |
| 40 | + } |
| 41 | + private convertMessages(messages: BaseMessage[]): string { |
| 42 | + return messages |
| 43 | + .map((msg) => `${msg.getType().toUpperCase()}: "${msg.content}"`) |
| 44 | + .join('\n'); |
| 45 | + } |
| 46 | + async _call( |
| 47 | + messages: BaseMessage[], |
| 48 | + options: this['ParsedCallOptions'], |
| 49 | + runManager?: CallbackManagerForLLMRun, |
| 50 | + ): Promise<string> { |
| 51 | + if (!messages.length) { |
| 52 | + throw new Error('No messages provided.'); |
| 53 | + } |
| 54 | + if (typeof messages[0].content !== 'string') { |
| 55 | + throw new Error('Multimodal messages are not supported.'); |
| 56 | + } |
| 57 | + const pipeline = await this.pipeline; |
| 58 | + |
| 59 | + // Signal setup |
| 60 | + const signals: AbortSignal[] = []; |
| 61 | + if (options.signal) { |
| 62 | + signals.push(options.signal); |
| 63 | + } |
| 64 | + if (options.timeout) { |
| 65 | + signals.push(AbortSignal.timeout(options.timeout)); |
| 66 | + } |
| 67 | + const signal = AbortSignal.any(signals); |
| 68 | + |
| 69 | + // generation option setup |
| 70 | + const generateOptions: GenerationConfig = { ...this.generateOptions }; |
| 71 | + if (options.stop) { |
| 72 | + const set = new Set(options.stop); |
| 73 | + generateOptions['stop_strings'] = set; |
| 74 | + generateOptions['include_stop_str_in_output'] = true; |
| 75 | + } |
| 76 | + |
| 77 | + // callback setup |
| 78 | + const callback = (chunk: string) => { |
| 79 | + runManager?.handleLLMNewToken(chunk).catch(console.error); |
| 80 | + |
| 81 | + return signal.aborted ? StreamingStatus.CANCEL : StreamingStatus.RUNNING; |
| 82 | + }; |
| 83 | + |
| 84 | + const prompt = this.convertMessages(messages); |
| 85 | + |
| 86 | + const result = await pipeline.generate( |
| 87 | + prompt, |
| 88 | + generateOptions, |
| 89 | + callback, |
| 90 | + ); |
| 91 | + // We need to throw an exception if the generation was canceled by a signal |
| 92 | + signal.throwIfAborted(); |
| 93 | + |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + async *_streamResponseChunks( |
| 98 | + messages: BaseMessage[], |
| 99 | + _options: this['ParsedCallOptions'], |
| 100 | + runManager?: CallbackManagerForLLMRun, |
| 101 | + ): AsyncGenerator<ChatGenerationChunk> { |
| 102 | + const pipeline = await this.pipeline; |
| 103 | + const prompt = this.convertMessages(messages); |
| 104 | + const generator = pipeline.stream( |
| 105 | + prompt, |
| 106 | + this.generateOptions, |
| 107 | + ); |
| 108 | + for await (const chunk of generator) { |
| 109 | + yield new ChatGenerationChunk({ |
| 110 | + message: new AIMessageChunk({ |
| 111 | + content: chunk, |
| 112 | + }), |
| 113 | + text: chunk, |
| 114 | + }); |
| 115 | + await runManager?.handleLLMNewToken(chunk); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments