Skip to content

Commit

Permalink
Ensure we build a subclass message only if it’s abstract [feenkcom/gt…
Browse files Browse the repository at this point in the history
  • Loading branch information
hellerve committed Mar 6, 2025
1 parent abdc62d commit f338bd6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 108 deletions.
82 changes: 42 additions & 40 deletions src/Gt4Llm/GtLlmChatMessage.class.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Class {
#name : 'GtLlmChatMessage',
#superclass : 'GtLlmEntity',
#name : #GtLlmChatMessage,
#superclass : #GtLlmEntity,
#instVars : [
'content',
'role',
Expand All @@ -9,122 +9,124 @@ Class {
'chat',
'attachments'
],
#category : 'Gt4Llm'
#category : #Gt4Llm
}

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage class >> defaultRole [
^ self subclassResponsibility
]

{ #category : 'instance creation' }
{ #category : #'instance creation' }
GtLlmChatMessage class >> from: aDictionary [
| aRole aContent roleClass |
self isAbstract ifFalse: [ ^ self new from: aDictionary ].

aRole := aDictionary at: 'role'.
aContent := aDictionary at: 'content'.

roleClass := self rolesToClasses at: aRole ifAbsent: [self].
roleClass := self rolesToClasses at: aRole ifAbsent: [ self ].

^ roleClass new from: aDictionary
]

{ #category : 'instance creation' }
{ #category : #'instance creation' }
GtLlmChatMessage class >> rolesToClasses [
^ {('tool' -> GtLlmToolMessage).
('assistant' -> GtLlmAssistantMessage).
('user' -> GtLlmUserMessage).
('system' -> GtLlmSystemMessage)} asDictionary
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage class >> serializationProperties [
^ {#role -> #role.
#content -> #content}
]

{ #category : 'as yet unclassified' }
{ #category : #'as yet unclassified' }
GtLlmChatMessage >> action [
^ [ self contentJson at: 'Action' ] on: Error do: [ nil ]
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> annotations [
^ {}
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> asTokenizableString [
^ '<|im_start|>' , self role , '<|im_sep|>' , self content , '<|im_end|>'
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> attachments [
^ attachments
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> attachments: anObject [
attachments := anObject
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> chat [
^ chat
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> chat: anObject [
chat := anObject
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> content [
^ content
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> content: anObject [
content := anObject
]

{ #category : 'as yet unclassified' }
{ #category : #'as yet unclassified' }
GtLlmChatMessage >> contentJson [
^ STONJSON fromString: self contentText
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> contentSummary [
^ self contentText lines ifEmpty: [ '' ] ifNotEmpty: #first
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> contentText [
^ self content
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> createdAt [
^ createdAt
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> createdAt: anObject [
createdAt := self deserializeTime: anObject
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> descendant [
^ self chat descendantOf: self
]

{ #category : 'as yet unclassified' }
{ #category : #'as yet unclassified' }
GtLlmChatMessage >> gtActionInspectFor: anAction [
<gtAction>
<gtLlmAction>
^ super gtActionInspectFor: anAction
]

{ #category : 'as yet unclassified' }
{ #category : #'as yet unclassified' }
GtLlmChatMessage >> gtJsonFor: aView context: aContext [
<gtView>
<gtLlmMessageView>
Expand All @@ -135,76 +137,76 @@ GtLlmChatMessage >> gtJsonFor: aView context: aContext [
priority: 4.5
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> hasResponse [
^ true
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> id [
^ id
]

{ #category : 'as yet unclassified' }
{ #category : #'as yet unclassified' }
GtLlmChatMessage >> id: anId [
id := anId
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> initialize [
id := self uuidClass new.
createdAt := DateAndTime now
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> isAssistantRole [
^ self role = 'assistant'
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> isFailure [
^ false
]

{ #category : 'as yet unclassified' }
{ #category : #'as yet unclassified' }
GtLlmChatMessage >> isReady [
^ true
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> isSystemRole [
^ self role = 'system'
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> isToolRole [
^ self role = 'tool'
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> isUserRole [
^ self role = 'user'
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> role [
^ role ifNil: [ role := self class defaultRole ]
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> role: anObject [
role := anObject
]

{ #category : 'accessing' }
{ #category : #accessing }
GtLlmChatMessage >> senderText [
self isAssistantRole ifTrue: [ ^ 'Assistant' ].
self isSystemRole ifTrue: [ ^ 'System' ].
self isToolRole ifTrue: [ ^ 'Tool' ].
^ 'You'
]

{ #category : 'as yet unclassified' }
{ #category : #'as yet unclassified' }
GtLlmChatMessage >> serialize [
| serialized |
serialized := super serialize.
Expand All @@ -220,6 +222,6 @@ GtLlmChatMessage >> serialize [
^ serialized
]

{ #category : 'as yet unclassified' }
{ #category : #'as yet unclassified' }
GtLlmChatMessage >> serializeInline [
]
Loading

0 comments on commit f338bd6

Please sign in to comment.