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>
| tool call result model |
tool := GtLMagritteToolForMethodsSource new.
call := GtLFunctionToolCall new.
call rawArguments: {
'methodDescriptors' -> {
{'methodClassName' -> 'Collection'.
'methodName' -> 'withAll:'.
'classSide' -> true} asDictionary.
{'methodClassName' -> #GtLMagritteToolForMethodsSourceExamples.
'methodName' -> 'methodSourcesForExistingMethods'.
'classSide' -> false} asDictionary
} asArray
} asDictionary.
result := tool performToolCall: call.
self assert: call tool == tool.
self assert: call arguments size equals: 1.
self assert: (call arguments anyOne
allSatisfy: [:e | e isKindOf: GtLMethodReference]).
self assert: (result isKindOf: GtLMagritteBasedModel).
model := result model.
self assert: model methods size equals: 2.
self
assert: model methods first sourceCode
equals: (Collection class>>#withAll:) sourceCode.
self
assert: model 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 a GtLMagritteBasedModel
.
- The logic is implemented in GtLAbstractMagritteFunctionTool>>#performToolCall:inContext:
. Our sample tool implements the logic in GtLMagritteToolForMethodsSource>>#privatePerformToolCall:inContext:
.
Each tool has corresponding examples that check its logic in isolation. For example, GtLMagritteToolForMethodsSource
has examples in GtLMagritteToolForMethodsSourceExamples
.