Skip to content

Commit 55687e1

Browse files
committed
added more fine-grained mocking functionality
1 parent 1dae2ba commit 55687e1

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

core/src/main/kotlin/org/evomaster/core/llm/mock/LlmMockResponse.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package org.evomaster.core.llm.mock
33
class LlmMockResponse(
44

55
/**
6-
* The response that will be returned by the LLM
6+
* The response producer that will be returned by the LLM, given an input text from user
77
*/
8-
val response: String,
8+
val responseProducer: (String) -> String,
99

1010
/**
1111
* Lambda that, given as input a request from the client, decides whether this response should be returned

core/src/main/kotlin/org/evomaster/core/llm/mock/MockChatModel.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ class MockChatModel : ChatModel {
3333
* decides whether this response should be returned
3434
*/
3535
fun mockResponse(response: String, matcher: (String) -> Boolean){
36-
responses.add(LlmMockResponse(response, matcher))
36+
responses.add(LlmMockResponse({req -> response}, matcher))
37+
}
38+
39+
/**
40+
* Specify a producer of responses if the text of the query from user does match the matcher.
41+
* Input for both lambdas is such text.
42+
* This is useful when the response for same query might be different
43+
*/
44+
fun mockResponse(responseProducer: (String) -> String, matcher: (String) -> Boolean){
45+
responses.add(LlmMockResponse(responseProducer, matcher))
3746
}
3847
}
3948

@@ -45,7 +54,7 @@ class MockChatModel : ChatModel {
4554

4655
val text = chatRequest.messages().joinToString { it.toString() }
4756

48-
val res = responses.find { mock -> mock.matcher(text) }?.response
57+
val res = responses.find { mock -> mock.matcher(text) }?.responseProducer(text)
4958
?: "Hei! You forgot to setup the mock for me"
5059

5160
return ChatResponse.builder().aiMessage(AiMessage.from(res)).build()

core/src/test/kotlin/org/evomaster/core/llm/LlmSupportMockTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,22 @@ class LlmSupportMockTest {
2727
assertEquals("YES", LlmSupport.chat(model, firstPrompt))
2828
assertEquals("NO", LlmSupport.chat(model, secondPrompt))
2929
}
30+
31+
@Test
32+
fun testResponseProducer(){
33+
val model = LlmSupport.createModel(LlmProvider.MOCK)
34+
35+
val prompt = "Give me a random value"
36+
val x = arrayOf(0)
37+
38+
MockChatModel.reset()
39+
MockChatModel.mockResponse({_ -> "${x[0]++}"}){it.contains("random")}
40+
41+
val n = 10
42+
repeat(n){
43+
LlmSupport.chat(model, prompt)
44+
}
45+
46+
assertEquals(n, x[0])
47+
}
3048
}

0 commit comments

Comments
 (0)