forked from mudler/cogito
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplan_test.go
More file actions
213 lines (177 loc) · 8.14 KB
/
Copy pathplan_test.go
File metadata and controls
213 lines (177 loc) · 8.14 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cogito_test
import (
"fmt"
. "github.com/mudler/cogito"
"github.com/mudler/cogito/structures"
"github.com/mudler/cogito/tests/mock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sashabaranov/go-openai"
)
var _ = Describe("Plannings with tools", func() {
var mockLLM *mock.MockOpenAIClient
var originalFragment Fragment
BeforeEach(func() {
mockLLM = mock.NewMockOpenAIClient()
originalFragment = NewEmptyFragment().
AddMessage("user", "What is photosynthesis?")
})
Context("ContentReview with tools", func() {
It("should execute tools when provided", func() {
mockTool := mock.NewMockTool("search", "Search for information")
// Mock goal extraction
mockLLM.SetAskResponse("The goal is to find most relevant informations about photosynthesis")
mockLLM.AddCreateChatCompletionFunction("json", `{"goal": "Find most relevant informations about photosynthesis"}`)
// Mock plan extraction
mockLLM.SetAskResponse("The plan is to find information about chlorophyll")
mockLLM.AddCreateChatCompletionFunction("json", `{"subtasks": ["Find information about chlorophyll", "Find information about photosynthesis"]}`)
// Mock tool call (Subtask #1) - tool selection
mockLLM.AddCreateChatCompletionFunction("search", `{"query": "chlorophyll"}`)
mock.SetRunResult(mockTool, "Chlorophyll is a green pigment found in plants.")
mockLLM.SetAskResponse("The plan is to find information about chlorophyll")
mockLLM.AddCreateChatCompletionFunction("json", `{"extract_boolean": true}`)
mockLLM.SetAskResponse("Subtask is achieved")
// Mock tool call (Subtask #2) - tool selection
mockLLM.AddCreateChatCompletionFunction("search", `{"query": "photosynthesis"}`)
mock.SetRunResult(mockTool, "Photosynthesis is the process by which plants convert sunlight into energy.")
mockLLM.SetAskResponse("The plan is to find information about chlorophyll")
mockLLM.AddCreateChatCompletionFunction("json", `{"extract_boolean": true}`)
mockLLM.SetAskResponse("Subtask is achieved")
// Extract a goal from conversation
goal, err := ExtractGoal(mockLLM, originalFragment)
Expect(err).ToNot(HaveOccurred())
// Create a plan to achieve the goal
plan, err := ExtractPlan(mockLLM, originalFragment,
goal,
WithTools(mockTool))
Expect(err).ToNot(HaveOccurred())
Expect(len(plan.Subtasks)).To(BeNumerically(">", 0))
// Execute the plan
result, err := ExecutePlan(mockLLM, originalFragment,
plan, goal,
WithTools(mockTool))
Expect(err).ToNot(HaveOccurred())
// Check fragments history to see if we behaved as expected
// ExtractGoal + ExtractPlan + 2×FinalResponse + 2×GoalCheck = 6 Ask() calls
Expect(len(mockLLM.FragmentHistory)).To(Equal(6), fmt.Sprintf("Fragment history: %v", mockLLM.FragmentHistory))
// [0] Extract goal
Expect(mockLLM.FragmentHistory[0].String()).To(
And(
ContainSubstring("Analyze the following text and the context to identify the goal."),
ContainSubstring("What is photosynthesis"),
))
// [1] Extract plan
Expect(mockLLM.FragmentHistory[1].String()).To(
And(
ContainSubstring("You are an AI assistant that breaks down a goal into a series of actionable steps (subtasks)"),
ContainSubstring("Goal: Find most relevant informations about photosynthesis"),
ContainSubstring("What is photosynthesis"),
ContainSubstring("Tool description: Search for information"),
))
Expect(result).ToNot(BeNil())
Expect(len(result.Status.ToolsCalled)).To(Equal(2))
Expect(len(result.Status.ToolResults)).To(Equal(2))
Expect(result.Status.ToolResults[0].Executed).To(BeTrue())
Expect(result.Status.ToolResults[0].Name).To(Equal("search"))
Expect(result.Status.ToolResults[0].Result).To(Equal("Chlorophyll is a green pigment found in plants."))
Expect(result.Status.ToolResults[1].Executed).To(BeTrue())
Expect(result.Status.ToolResults[1].Name).To(Equal("search"))
Expect(result.Status.ToolResults[1].Result).To(Equal("Photosynthesis is the process by which plants convert sunlight into energy."))
Expect(result.LastMessage().Content).ToNot(
ContainSubstring("What is photosynthesis?"),
)
Expect(len(result.Status.ToolResults)).To(Equal(2))
Expect(len(result.Status.ToolsCalled)).To(Equal(2))
Expect(len(result.Messages)).To(Equal(5))
Expect(result.Messages[0].Content).To(Equal("What is photosynthesis?"))
Expect(result.Messages[1].ToolCalls[0].Function.Arguments).To(Equal(`{"query":"chlorophyll"}`))
Expect(result.Messages[1].ToolCalls[0].Function.Name).To(Equal("search"))
Expect(result.Messages[2].Content).To(Equal("Chlorophyll is a green pigment found in plants."))
Expect(result.Messages[3].ToolCalls[0].Function.Arguments).To(Equal(`{"query":"photosynthesis"}`))
Expect(result.Messages[3].ToolCalls[0].Function.Name).To(Equal("search"))
Expect(result.Messages[4].Content).To(Equal("Photosynthesis is the process by which plants convert sunlight into energy."))
})
})
Context("TODO-based iterative execution", func() {
It("should extract TODOs from plan", func() {
mockLLM := mock.NewMockOpenAIClient()
plan := &structures.Plan{
Description: "Test plan",
Subtasks: []string{"Task 1", "Task 2"},
}
goal := &structures.Goal{
Goal: "Test goal",
}
// Mock TODO generation
mockLLM.SetAskResponse("Convert subtasks to TODOs")
mockLLM.AddCreateChatCompletionFunction("json", `{
"todos": [
{"id": "1", "description": "Task 1", "completed": false},
{"id": "2", "description": "Task 2", "completed": false}
]
}`)
todoList, err := ExtractTODOs(mockLLM, plan, goal)
Expect(err).ToNot(HaveOccurred())
Expect(todoList).ToNot(BeNil())
Expect(len(todoList.TODOs)).To(Equal(2))
Expect(todoList.TODOs[0].Description).To(Equal("Task 1"))
Expect(todoList.TODOs[1].Description).To(Equal("Task 2"))
})
It("should execute plan with TODO mode when reviewer LLM is provided", func() {
mockWorkerLLM := mock.NewMockOpenAIClient()
mockReviewerLLM := mock.NewMockOpenAIClient()
mockTool := mock.NewMockTool("search", "Search for information")
plan := &structures.Plan{
Description: "Test plan",
Subtasks: []string{"Find information"},
}
goal := &structures.Goal{
Goal: "Test goal",
}
fragment := NewEmptyFragment().AddMessage("user", "Test query")
// Mock TODO generation
mockWorkerLLM.SetAskResponse("Convert subtasks to TODOs")
mockWorkerLLM.AddCreateChatCompletionFunction("json", `{
"todos": [
{"id": "1", "description": "Find information", "completed": false}
]
}`)
// Mock work phase - tool selection
mockWorkerLLM.AddCreateChatCompletionFunction("search", `{"query": "test"}`)
mock.SetRunResult(mockTool, "Test result")
// After tool execution, no more tools needed
mockWorkerLLM.SetCreateChatCompletionResponse(openai.ChatCompletionResponse{
Choices: []openai.ChatCompletionChoice{
{
Message: openai.ChatCompletionMessage{
Role: AssistantMessageRole.String(),
Content: "No more tools needed.",
},
},
},
})
// When max iterations (1) is reached, Ask() is called to get final response
mockWorkerLLM.SetAskResponse("Work phase complete.")
// Mock review phase - goal achieved
// updateTODOsFromWork calls ExtractStructure(workerLLM) - needs 1 CreateChatCompletion response
mockWorkerLLM.AddCreateChatCompletionFunction("json", `{"todos": [{"id": "1", "description": "Find information", "completed": false}]}`)
// IsGoalAchieved makes: 1) Ask() call, 2) ExtractBoolean() which calls ExtractStructure() which calls CreateChatCompletion()
mockReviewerLLM.SetAskResponse("Goal achieved")
mockReviewerLLM.AddCreateChatCompletionFunction("json", `{"extract_boolean": true}`)
// executeReviewPhase also calls Ask() to get review result (after IsGoalAchieved)
mockReviewerLLM.SetAskResponse("Review complete, goal achieved")
result, err := ExecutePlan(
mockWorkerLLM,
fragment,
plan,
goal,
WithTools(mockTool),
WithReviewerLLM(mockReviewerLLM),
WithIterations(1),
)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(result.Status.TODOs).ToNot(BeNil())
})
})
})