Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Pharo-Ollama-Tests/OllamaAPITest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ OllamaAPITest >> setUp [
ollama := OllamaAPI new.
]

{ #category : 'tests' }
OllamaAPITest >> testNumTokenVariables [

| ollamatest |
ollamatest := OllamaAPI new.
self assert: (ollamatest num_ctx isNil).
self assert: (((ollamatest prequery: 'test') at: #options) isEmptyOrNil).
ollamatest num_ctx: 50.
self assert: (ollamatest num_ctx == 50).
self assert: (((ollamatest prequery: 'test') at: #options) isNotEmpty).
]

{ #category : 'running' }
OllamaAPITest >> testOllamaAPIHasDefaultModelInitialized [

Expand Down
78 changes: 53 additions & 25 deletions src/Pharo-Ollama/OllamaAPI.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Class {
'num_predict',
'top_k',
'top_p',
'format'
'format',
'num_ctx'
],
#classInstVars : [
'defaultModel'
Expand Down Expand Up @@ -154,6 +155,18 @@ OllamaAPI >> model: anObject [
model := anObject
]

{ #category : 'accessing' }
OllamaAPI >> num_ctx [

^ num_ctx
]

{ #category : 'accessing' }
OllamaAPI >> num_ctx: anObject [

num_ctx := anObject
]

{ #category : 'accessing' }
OllamaAPI >> num_predict [

Expand All @@ -178,6 +191,27 @@ OllamaAPI >> port: anObject [
port := anObject
]

{ #category : 'api' }
OllamaAPI >> prequery: aTextQuery [

| paramDic optionsDic |
paramDic := Dictionary new.
paramDic at: #model put: self model fullName.
paramDic at: #prompt put: (self buildQuery: aTextQuery).
paramDic at: #stream put: self stream.
self format ifNotNil: [ :f | paramDic at: #format put: f ].
self addParameterTo: paramDic.

self num_ctx ifNotNil: [
optionsDic := Dictionary new.
optionsDic at: #num_ctx put: self num_ctx.
paramDic at: #options put: optionsDic.
].

^ paramDic

]

{ #category : 'api' }
OllamaAPI >> pull [
"Pull the model linked to this API"
Expand All @@ -198,36 +232,30 @@ OllamaAPI >> pull [
{ #category : 'api' }
OllamaAPI >> query: aTextQuery [

| znClient paramDic writer |
| znClient writer |
znClient := ZnClient new.
writer := ZnUtils defaultJSONWriter.
znClient
accept: ZnMimeType applicationJson;
contentWriter: [ :data | ZnEntity json: (writer toString: data) ].
accept: ZnMimeType applicationJson;
contentWriter: [ :data | ZnEntity json: (writer toString: data) ].
znClient host: self host.
znClient port: self port.
znClient path: 'api/generate'.
paramDic := Dictionary new.
paramDic at: #model put: self model fullName.
paramDic at: #prompt put: (self buildQuery: aTextQuery).
paramDic at: #stream put: self stream.
self format ifNotNil: [ :f | paramDic at: #format put: f ].


self addParameterTo: paramDic.
znClient contents: paramDic.

self stream
ifTrue: [
znClient streaming: true.
^ znClient post ]
ifFalse: [
| reader |
znClient forJsonREST.
reader := ZnUtils defaultJSONReader.
znClient contentReader: [ :entity |
reader fromString: entity contents ].
^ znClient post at: #response ]


znClient contents: (self prequery: aTextQuery).

self stream
ifTrue: [
znClient streaming: true.
^ znClient post ]
ifFalse: [
| reader |
znClient forJsonREST.
reader := ZnUtils defaultJSONReader.
znClient contentReader: [ :entity |
reader fromString: entity contents ].
^ znClient post at: #response ].
]

{ #category : 'accessing' }
Expand Down