Class ScriptedChatModel
java.lang.Object
io.micronaut.langchain4j.test.ScriptedChatModel
- All Implemented Interfaces:
dev.langchain4j.model.chat.ChatModel
A chat model for tests that answers each request with the first matching rule, and records the requests.
ScriptedChatModel model = ScriptedChatModel.builder()
.whenToolResult().respond(request -> "It is " + ScriptedChatModel.lastToolResult(request))
.whenUserMessageContains("weather").callTool("weather", "{\"city\": \"Paris\"}")
.otherwise("I don't know")
.build();
streaming() returns a StreamingChatModel with the same rules, which streams the text of the
response word by word.
- Since:
- 2.4.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThe answer of a rule.static final classBuilds the rules of aScriptedChatModel, evaluated in order. -
Method Summary
Modifier and TypeMethodDescriptionstatic ScriptedChatModel.Builderbuilder()dev.langchain4j.model.chat.response.ChatResponsedoChat(dev.langchain4j.model.chat.request.ChatRequest request) dev.langchain4j.model.chat.request.ChatRequeststatic StringlastToolResult(dev.langchain4j.model.chat.request.ChatRequest request) static StringlastUserMessage(dev.langchain4j.model.chat.request.ChatRequest request) List<dev.langchain4j.model.chat.request.ChatRequest> requests()voidreset()Forgets the requests received.static ScriptedChatModelrespondingWith(String... responses) A model that answers the requests with the given responses, in order, and then with the last one.dev.langchain4j.model.chat.StreamingChatModelMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.langchain4j.model.chat.ChatModel
chat, chat, chat, chat, chat, chatAsync, chatAsync, chatAsync, chatAsync, chatAsync, defaultRequestParameters, doChatAsync, listeners, provider, supportedCapabilities
-
Method Details
-
builder
- Returns:
- A builder of the rules of the model
-
respondingWith
A model that answers the requests with the given responses, in order, and then with the last one.- Parameters:
responses- The responses- Returns:
- The model
-
lastUserMessage
- Parameters:
request- A chat request- Returns:
- The text of the last user message of the request, or an empty string
-
lastToolResult
- Parameters:
request- A chat request- Returns:
- The result of the tool execution that ends the request, or an empty string
-
requests
- Returns:
- The requests received, in order
-
lastRequest
public dev.langchain4j.model.chat.request.ChatRequest lastRequest()- Returns:
- The last request received
-
reset
public void reset()Forgets the requests received. -
doChat
public dev.langchain4j.model.chat.response.ChatResponse doChat(dev.langchain4j.model.chat.request.ChatRequest request) - Specified by:
doChatin interfacedev.langchain4j.model.chat.ChatModel
-
streaming
public dev.langchain4j.model.chat.StreamingChatModel streaming()- Returns:
- A streaming chat model with the same rules and requests, which streams the text word by word
-