-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgollm_test.go
164 lines (132 loc) · 4.86 KB
/
gollm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//go:build ignore
// +build ignore
// Ignore this file for now, until gollm is updated to use the new provider registry
package gollm_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/teilomillet/gollm"
"github.com/teilomillet/gollm/config"
"github.com/teilomillet/gollm/providers"
"github.com/teilomillet/gollm/utils"
)
// MockProvider implements the Provider interface for testing
type MockProvider struct {
mock.Mock
}
func (m *MockProvider) Name() string {
args := m.Called()
return args.String(0)
}
func (m *MockProvider) Endpoint() string {
args := m.Called()
return args.String(0)
}
func (m *MockProvider) Headers() map[string]string {
args := m.Called()
return args.Get(0).(map[string]string)
}
func (m *MockProvider) PrepareRequest(prompt string, options map[string]interface{}) ([]byte, error) {
args := m.Called(prompt, options)
return args.Get(0).([]byte), args.Error(1)
}
func (m *MockProvider) ParseResponse(body []byte) (string, error) {
args := m.Called(body)
return args.String(0), args.Error(1)
}
func (m *MockProvider) SupportsJSONSchema() bool {
args := m.Called()
return args.Bool(0)
}
func (m *MockProvider) SetOption(key string, value interface{}) {
m.Called(key, value)
}
func (m *MockProvider) SetDefaultOptions(config *config.Config) {
m.Called(config)
}
func (m *MockProvider) SetLogger(logger utils.Logger) {
m.Called(logger)
}
type mockProviderFactory struct {
mockProvider *MockProvider
}
func (f *mockProviderFactory) Create(apiKey, model string, extraHeaders map[string]string) (providers.Provider, error) {
return f.mockProvider, nil
}
func TestStructuredOutput(t *testing.T) {
ctx := context.Background()
// Create and configure mock provider
mockProvider := new(MockProvider)
mockProvider.On("Name").Return("mock")
mockProvider.On("SupportsJSONSchema").Return(true)
mockProvider.On("SetOption", mock.Anything, mock.Anything).Return()
mockProvider.On("SetDefaultOptions", mock.Anything).Return()
mockProvider.On("SetLogger", mock.Anything).Return()
mockProvider.On("Headers").Return(map[string]string{"Authorization": "Bearer test-key"})
mockProvider.On("Endpoint").Return("https://api.mock.com/v1/completions")
// Mock JSON response
expectedJSON := `[{"benefit": "Cardiovascular health", "description": "Improves heart function"}]`
mockProvider.On("PrepareRequest", mock.Anything, mock.Anything).
Return([]byte(`{"messages": [{"role": "user", "content": "test"}]}`), nil)
mockProvider.On("ParseResponse", mock.Anything).Return(expectedJSON, nil)
// Register the mock provider
registry := providers.NewProviderRegistry()
registry.Register("mock", &mockProviderFactory{mockProvider: mockProvider})
llm, err := gollm.NewLLM(
gollm.SetProvider("mock"),
gollm.SetModel("mock-model"),
gollm.SetAPIKey("test-key"),
gollm.WithProviderRegistry(registry),
)
assert.NoError(t, err)
prompt := gollm.NewPrompt("List the top 3 benefits of exercise",
gollm.WithOutput("JSON array of benefits"),
gollm.WithSystemPrompt("You are a JSON-only assistant.", gollm.CacheTypeEphemeral),
)
response, err := llm.Generate(ctx, prompt)
assert.NoError(t, err)
assert.Equal(t, expectedJSON, response)
// Verify that all expected mock calls were made
mockProvider.AssertExpectations(t)
}
func TestJSONSchemaValidation(t *testing.T) {
ctx := context.Background()
// Create and configure mock provider
mockProvider := new(MockProvider)
mockProvider.On("Name").Return("mock")
mockProvider.On("SupportsJSONSchema").Return(true)
mockProvider.On("SetOption", mock.Anything, mock.Anything).Return()
mockProvider.On("SetDefaultOptions", mock.Anything).Return()
mockProvider.On("SetLogger", mock.Anything).Return()
// Mock JSON response with schema
expectedJSON := `{"name": "John Doe", "age": 25, "interests": ["coding", "reading"]}`
mockProvider.On("PrepareRequest", mock.Anything, mock.Anything).
Return([]byte(`{"messages": [{"role": "user", "content": "test"}]}`), nil)
mockProvider.On("ParseResponse", mock.Anything).Return(expectedJSON, nil)
llm, err := gollm.NewLLM(
gollm.SetProvider("mock"),
gollm.SetModel("mock-model"),
gollm.SetAPIKey("test-key"),
)
assert.NoError(t, err)
schema := `{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 18},
"interests": {"type": "array", "items": {"type": "string"}}
},
"required": ["name", "age", "interests"]
}`
prompt := gollm.NewPrompt("Generate a user profile",
gollm.WithOutput(schema),
gollm.WithSystemPrompt("You are a JSON-only assistant.", gollm.CacheTypeEphemeral),
)
response, err := llm.Generate(ctx, prompt, gollm.WithJSONSchemaValidation())
assert.NoError(t, err)
assert.Equal(t, expectedJSON, response)
// Verify that all expected mock calls were made
mockProvider.AssertExpectations(t)
}