@llmist/testing 9.1.2 → 9.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +144 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# @llmist/testing
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://github.com/zbigniewsobiecki/llmist/actions/workflows/ci.yml"><img src="https://github.com/zbigniewsobiecki/llmist/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
|
5
|
+
<a href="https://www.npmjs.com/package/@llmist/testing"><img src="https://img.shields.io/npm/v/@llmist/testing.svg" alt="npm version"></a>
|
|
6
|
+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="License"></a>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
**Testing utilities for llmist - mock LLM responses and test agents deterministically.**
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install -D @llmist/testing
|
|
15
|
+
# or
|
|
16
|
+
bun add -D @llmist/testing
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires `llmist` as a peer dependency.
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### Testing Gadgets
|
|
24
|
+
|
|
25
|
+
Test gadgets in isolation without any LLM calls:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { testGadget } from '@llmist/testing';
|
|
29
|
+
import { Calculator } from './gadgets';
|
|
30
|
+
|
|
31
|
+
const result = await testGadget(new Calculator(), {
|
|
32
|
+
operation: 'add',
|
|
33
|
+
a: 5,
|
|
34
|
+
b: 3,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(result.result).toBe('8');
|
|
38
|
+
expect(result.error).toBeUndefined();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Mocking LLM Responses
|
|
42
|
+
|
|
43
|
+
Use the fluent MockBuilder API to script LLM responses:
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { mockLLM, createMockClient, resetMocks } from '@llmist/testing';
|
|
47
|
+
import { LLMist } from 'llmist';
|
|
48
|
+
|
|
49
|
+
// Set up mock responses
|
|
50
|
+
mockLLM()
|
|
51
|
+
.whenMessageContains('hello')
|
|
52
|
+
.returns('Hi there! How can I help?')
|
|
53
|
+
.register();
|
|
54
|
+
|
|
55
|
+
mockLLM()
|
|
56
|
+
.whenMessageContains('calculate')
|
|
57
|
+
.returnsGadgetCall('Calculator', { operation: 'add', a: 1, b: 2 })
|
|
58
|
+
.register();
|
|
59
|
+
|
|
60
|
+
// Create agent with mock client
|
|
61
|
+
const agent = LLMist.createAgent()
|
|
62
|
+
.withClient(createMockClient())
|
|
63
|
+
.withGadgets(Calculator);
|
|
64
|
+
|
|
65
|
+
const response = await agent.askAndCollect('hello');
|
|
66
|
+
// Returns "Hi there! How can I help?" - no API calls made
|
|
67
|
+
|
|
68
|
+
// Clean up after tests
|
|
69
|
+
resetMocks();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Conditional Mocking
|
|
73
|
+
|
|
74
|
+
Match responses based on model, provider, or custom conditions:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
mockLLM()
|
|
78
|
+
.forModel('gpt-4o')
|
|
79
|
+
.forProvider('openai')
|
|
80
|
+
.whenMessageContains('complex task')
|
|
81
|
+
.returns('Handled by GPT-4o')
|
|
82
|
+
.register();
|
|
83
|
+
|
|
84
|
+
mockLLM()
|
|
85
|
+
.forModel('haiku')
|
|
86
|
+
.whenMessageContains('complex task')
|
|
87
|
+
.returns('Handled by Haiku')
|
|
88
|
+
.register();
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### One-Time Responses
|
|
92
|
+
|
|
93
|
+
Use `.once()` for responses that should only match once:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
mockLLM()
|
|
97
|
+
.whenMessageContains('first')
|
|
98
|
+
.returns('First response')
|
|
99
|
+
.once()
|
|
100
|
+
.register();
|
|
101
|
+
|
|
102
|
+
mockLLM()
|
|
103
|
+
.whenMessageContains('first')
|
|
104
|
+
.returns('Second response')
|
|
105
|
+
.register();
|
|
106
|
+
|
|
107
|
+
// First call returns "First response"
|
|
108
|
+
// Subsequent calls return "Second response"
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## API Reference
|
|
112
|
+
|
|
113
|
+
### `testGadget(gadget, params, options?)`
|
|
114
|
+
|
|
115
|
+
Test a gadget with given parameters.
|
|
116
|
+
|
|
117
|
+
### `mockLLM()`
|
|
118
|
+
|
|
119
|
+
Create a new mock builder with fluent API.
|
|
120
|
+
|
|
121
|
+
### `createMockClient()`
|
|
122
|
+
|
|
123
|
+
Create a mock LLMist client that uses registered mocks.
|
|
124
|
+
|
|
125
|
+
### `resetMocks()`
|
|
126
|
+
|
|
127
|
+
Clear all registered mocks (call in `afterEach`).
|
|
128
|
+
|
|
129
|
+
## Documentation
|
|
130
|
+
|
|
131
|
+
Full documentation at [llmist.dev/testing](https://llmist.dev/testing/getting-started/introduction/)
|
|
132
|
+
|
|
133
|
+
- [Mocking Overview](https://llmist.dev/testing/mocking/overview/)
|
|
134
|
+
- [Testing Gadgets](https://llmist.dev/testing/gadgets/test-gadget/)
|
|
135
|
+
- [Testing Agents](https://llmist.dev/testing/agents/testing-agents/)
|
|
136
|
+
|
|
137
|
+
## Related Packages
|
|
138
|
+
|
|
139
|
+
- [`llmist`](https://www.npmjs.com/package/llmist) - Core library
|
|
140
|
+
- [`@llmist/cli`](https://www.npmjs.com/package/@llmist/cli) - Command-line interface
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT
|