LLM tools by example
A tool can be used by an LLM chat to execute logic. For example, the below chat looks up the source of multiple methods using GtLMagritteToolForMethodsSource
.
methodSourcesForExistingMethods
<gtExample>
| call targetObject |
call := self
executeTool: GtLMagritteToolForMethodsSource new
withArguments: {
'methodDescriptors' -> {
{'methodClassName' -> 'Collection'.
'methodName' -> 'withAll:'.
'classSide' -> true} asDictionary.
{'methodClassName' -> 'GtLMagritteToolForMethodsSourceExamples'.
'methodName' -> 'methodSourcesForExistingMethods'.
'classSide' -> false} asDictionary
} asArray
} asDictionary.
targetObject := call result targetObject.
self assert: (targetObject isKindOf: GtLMethodsWithSource).
self assert: targetObject methods size equals: 2.
self
assert: targetObject methods first sourceCode
equals: (Collection class>>#withAll:) sourceCode.
self
assert: targetObject methods second sourceCode
equals: (GtLMagritteToolForMethodsSourceExamples>>#methodSourcesForExistingMethods) sourceCode.
^ call
A tool's implementation has a name, takes arguments and returns a result.
GtLMagritteToolForMethodsSource
is a typical tool and can serve as blueprint for most tools:
- It relies on Magritte descriptions to define the arguments. A sample is at
GtLMagritteToolForMethodsSource>>#methodDescriptorsDescription. - It returns a result object that is also annotated with Magritte descriptions. A sample is at
GtLMethodsWithSource. This object is internally wrapped in aGtLMagritteBasedModel. - The logic is implemented in
GtLAbstractMagritteFunctionTool>>#performToolCall:inContext:. Our sample tool implements the logic inGtLMagritteToolForMethodsSource>>#privatePerformToolCall:inContext:.
Each tool has corresponding examples that check its logic in isolation. For example, GtLMagritteToolForMethodsSource
has examples in GtLMagritteToolForMethodsSourceExamples
.