Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Created June 8, 2024 19:48
Show Gist options
  • Save rsbohn/5c416305971124244c984b4b5836004e to your computer and use it in GitHub Desktop.
Save rsbohn/5c416305971124244c984b4b5836004e to your computer and use it in GitHub Desktop.
Claude implementation for Glamorous Toolkit
Object subclass: #AnthropicClaude
instanceVariableNames: ''
classVariableNames: 'ApplicationKey'
package: 'LLM-AnthropicClaude'!
!AnthropicClaude methodsFor: 'accessing' stamp: 'GlamorousAuthor 6/8/2024 12:25'!
invoke: userPrompt system: systemPrompt
| client payload response |
payload := {#model -> 'claude-3-haiku-20240307'.
#max_tokens -> 1536.
#system -> systemPrompt.
#messages
-> {{#role -> 'user'.
#content -> userPrompt} asDictionary}} asDictionary.
client := ZnClient new
headerAt: 'Accept' put: 'application/json';
headerAt: 'Content-Type' put: 'application/json';
headerAt: 'Anthropic-Version' put: '2023-06-01';
headerAt: 'X-Api-Key' put: self class ApplicationKey.
client url: self class APIEndpoint.
client contents: (STONJSON toString: payload).
response := STON fromString: client post.
^ (response at: 'content') first at: 'text'! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
AnthropicClaude class
instanceVariableNames: ''!
!AnthropicClaude class methodsFor: 'accessing' stamp: 'GlamorousAuthor 6/8/2024 11:40'!
ApplicationKey: blob
ApplicationKey := blob! !
!AnthropicClaude class methodsFor: 'accessing' stamp: 'GlamorousAuthor 6/8/2024 11:43'!
APIEndpoint
APIEndpoint ifNil: [ ^ 'https://api.anthropic.com/v1/messages' ].
^ APIEndpoint! !
!AnthropicClaude class methodsFor: 'accessing' stamp: 'GlamorousAuthor 6/8/2024 11:40'!
ApplicationKey
^ ApplicationKey! !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment