001/* 002 * SPDX-License-Identifier: Apache-2.0 003 * 004 * Copyright 2025-2026 The Enola <https://enola.dev> Authors 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package dev.enola.ai.langchain4j; 019 020import dev.enola.ai.iri.MockModelProvider; 021import dev.langchain4j.data.message.AiMessage; 022import dev.langchain4j.model.chat.StreamingChatModel; 023import dev.langchain4j.model.chat.request.ChatRequest; 024import dev.langchain4j.model.chat.response.ChatResponse; 025import dev.langchain4j.model.chat.response.StreamingChatResponseHandler; 026import dev.langchain4j.model.output.FinishReason; 027 028import java.io.UncheckedIOException; 029import java.net.URI; 030import java.util.Optional; 031 032public class MockChatModel implements StreamingChatModel { 033 034 private final String reply; 035 036 public MockChatModel(String reply) { 037 this.reply = reply; 038 } 039 040 @Override 041 public void doChat(ChatRequest chatRequest, StreamingChatResponseHandler handler) { 042 handler.onCompleteResponse( 043 ChatResponse.builder() 044 .aiMessage(AiMessage.builder().text(reply).build()) 045 .finishReason(FinishReason.STOP) 046 .build()); 047 } 048 049 public static class Provider extends MockModelProvider<StreamingChatModel> // skipcq: JAVA-E0169 050 implements ChatModelProvider { // skipcq: JAVA-E0169 051 052 @Override 053 public Optional<StreamingChatModel> optional(URI uri) 054 throws IllegalArgumentException, UncheckedIOException { 055 if (SCHEME.equalsIgnoreCase(uri.getScheme())) { 056 var reply = uri.getSchemeSpecificPart(); 057 return Optional.of(new MockChatModel(reply)); 058 } else return Optional.empty(); 059 } 060 } 061}