UNPKG

@yk1028-test/ai-chat-supporter

Version:

AI Chat Supporter - TypeScript library for intelligent chat processing with LangChain integration

103 lines 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LangChainOllamaProvider = void 0; const ollama_1 = require("@langchain/ollama"); // 간소화된 LangChain 기반 Ollama 제공자 class LangChainOllamaProvider { constructor(config) { this.config = { baseUrl: 'http://localhost:11434', temperature: 0.7, maxTokens: 1000, ...config, }; this.ollama = new ollama_1.Ollama({ baseUrl: this.config.baseUrl, model: this.config.model, temperature: this.config.temperature, ...(this.config.maxTokens && { maxTokens: this.config.maxTokens }), ...(this.config.topP && { topP: this.config.topP }), ...(this.config.topK && { topK: this.config.topK }), }); } // 단일 프롬프트 처리 async invoke(prompt) { try { const result = await this.ollama.invoke(prompt); return result; } catch (error) { throw new Error(`Ollama 응답 생성 실패: ${error instanceof Error ? error.message : 'Unknown error'}`); } } // 스트리밍 지원 async *stream(prompt) { try { const stream = await this.ollama.stream(prompt); for await (const chunk of stream) { if (typeof chunk === 'string') { yield chunk; } else if (chunk && typeof chunk === 'object' && 'content' in chunk) { yield chunk.content || ''; } } } catch (error) { throw new Error(`Ollama 스트리밍 실패: ${error instanceof Error ? error.message : 'Unknown error'}`); } } // 설정 업데이트 updateConfig(newConfig) { this.config = { ...this.config, ...newConfig }; // Ollama 인스턴스 재생성 this.ollama = new ollama_1.Ollama({ baseUrl: this.config.baseUrl, model: this.config.model, temperature: this.config.temperature, ...(this.config.maxTokens && { maxTokens: this.config.maxTokens }), ...(this.config.topP && { topP: this.config.topP }), ...(this.config.topK && { topK: this.config.topK }), }); } // 모델 변경 setModel(model) { this.updateConfig({ model }); } // 현재 모델 조회 getModel() { return this.config.model; } // 건강 상태 확인 async isHealthy() { try { await this.ollama.invoke('test'); return true; } catch (error) { console.warn(`Ollama 건강 상태 확인 실패: ${error instanceof Error ? error.message : 'Unknown error'}`); return false; } } // 사용 가능한 모델 목록 (향후 구현) async listModels() { try { // 실제로는 Ollama API를 호출하여 모델 목록 조회 return [this.config.model]; } catch (error) { console.warn(`모델 목록 조회 실패: ${error instanceof Error ? error.message : 'Unknown error'}`); return []; } } // LangChain Ollama 인스턴스 직접 접근 getOllamaInstance() { return this.ollama; } // 설정 정보 조회 getConfig() { return { ...this.config }; } } exports.LangChainOllamaProvider = LangChainOllamaProvider; //# sourceMappingURL=LangChainOllamaProvider.js.map