LLM data views by example

An LLM data view gives an object a representation that LLMs can consume. The LLM views tab in the object inspector lists the data views the object exposes. Tools such as the Smalltalk evaluation tool include the returned object’s JSON views under objectDetails.

An LLM data view is defined in a one-argument method on the object that owns the data. The <gtLlmDataView> pragma marks the method. The view’s title labels it in the inspector, and dataKey: names its field in the JSON result. A block supplies the data on demand. GtLDataView>>#jsonObject: jsonObject: aBlock ^self jsonData object: aBlock takes a block that returns a JSON-ready object. GtLDataView>>#magritteObject: magritteObject: aBlockClosure ^ self magritteObject object: aBlockClosure serializes the object returned by the block using its Magritte descriptions.

#forwardObject with object: [ ... ] delegates to another object. By default, the forwarding view collects all of the target’s data views and exposes their keys at the current level. The optional view: selects one view, and dataKey: nests the forwarded data under a key. GtSearchFilter>>#llmResultDataViewsFor: llmResultDataViewsFor: aDataView <gtLlmDataView> ^ aDataView forwardObject title: 'Results'; object: [ self result ] forwards the views of its result stream this way.

GtAsyncSearchMethodGroupStream>>#llmMatchingMethodsDataViewFor: llmMatchingMethodsDataViewFor: aDataView <gtLlmDataView> ^ aDataView forwardObject title: 'Matching methods'; dataKey: 'methodMatches'; object: [ self llmMatchingMethods ] contributes the methodMatches field. It reads a copy of the stream, collects at most 50 method references, and checks one extra match to set mayHaveMore. The Smalltalk snippet below returns the filter’s JSON data.

| filter |
filter := 'GToolkit-Pharo-Coder-AddOns' asPackage gtMethodsInPackage
	& (Color gtReferences | BrGlamorousColors gtReferences).
((filter llmDataViewsFor: GtLDataView new) serializeToLlmPrimiteData) llmJsonData
  

The example below checks that serializing the view leaves the stream available to read. GtLMethodsSearchResultExamples>>#methodFilterResultViewIsBounded methodFilterResultViewIsBounded <gtExample> | methods filter data | methods := Object methods. 50 to: 51 do: [ :size | filter := GtSearchFixedMethodsFilter new methods: (methods first: size). data := (filter llmDataViewsFor: GtLDataView new) serializeToLlmPrimiteData llmJsonData at: 'methodMatches'. self assert: (data at: 'foundMethods') size equals: 50. self assert: (data at: 'mayHaveMore') equals: size = 51 ]. ^ filter checks the limit and the truncation flag.

methodGroupStreamDataViewPreservesStream
	<gtExample>
	| methods stream data |
	methods := {GtSearchFilter >> #result.
			GtSearchMethodsFilter class >> #resultType}.
	stream := (GtSearchFixedMethodsFilter new methods: methods) result.
	data := (stream llmDataViewsFor: GtLDataView new) serializeToLlmPrimiteData
			llmJsonData at: 'methodMatches'.
	self
		assert: ((data at: 'foundMethods') collect: [ :each | each at: 'classSide' ]) asArray
		equals: #(false true).
	self assert: (data at: 'mayHaveMore') equals: false.
	self assert: stream toArray wait equals: methods.
	^ stream asyncSimilarCopy