@@ -29,7 +29,10 @@ export class OllamaProvider implements LLMProvider {
2929
3030 constructor ( model : string , options : OllamaOptions = { } ) {
3131 this . model = model ;
32- this . baseUrl = options . baseUrl || process . env . OLLAMA_BASE_URL || 'http://localhost:11434' ;
32+ this . baseUrl =
33+ options . baseUrl ||
34+ process . env . OLLAMA_BASE_URL ||
35+ 'http://localhost:11434' ;
3336
3437 // Ensure baseUrl doesn't end with a slash
3538 if ( this . baseUrl . endsWith ( '/' ) ) {
@@ -41,7 +44,15 @@ export class OllamaProvider implements LLMProvider {
4144 * Generate text using Ollama API
4245 */
4346 async generateText ( options : GenerateOptions ) : Promise < LLMResponse > {
44- const { messages, functions, temperature = 0.7 , maxTokens, topP, frequencyPenalty, presencePenalty } = options ;
47+ const {
48+ messages,
49+ functions,
50+ temperature = 0.7 ,
51+ maxTokens,
52+ topP,
53+ frequencyPenalty,
54+ presencePenalty,
55+ } = options ;
4556
4657 // Format messages for Ollama API
4758 const formattedMessages = this . formatMessages ( messages ) ;
@@ -56,8 +67,12 @@ export class OllamaProvider implements LLMProvider {
5667 temperature : temperature ,
5768 // Ollama uses top_k instead of top_p, but we'll include top_p if provided
5869 ...( topP !== undefined && { top_p : topP } ) ,
59- ...( frequencyPenalty !== undefined && { frequency_penalty : frequencyPenalty } ) ,
60- ...( presencePenalty !== undefined && { presence_penalty : presencePenalty } ) ,
70+ ...( frequencyPenalty !== undefined && {
71+ frequency_penalty : frequencyPenalty ,
72+ } ) ,
73+ ...( presencePenalty !== undefined && {
74+ presence_penalty : presencePenalty ,
75+ } ) ,
6176 } ,
6277 } ;
6378
@@ -93,11 +108,14 @@ export class OllamaProvider implements LLMProvider {
93108
94109 // Extract content and tool calls
95110 const content = data . message ?. content || '' ;
96- const toolCalls = data . message ?. tool_calls ?. map ( ( toolCall : any ) => ( {
97- id : toolCall . id || `tool-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 11 ) } ` ,
98- name : toolCall . name ,
99- content : JSON . stringify ( toolCall . args || toolCall . arguments || { } ) ,
100- } ) ) || [ ] ;
111+ const toolCalls =
112+ data . message ?. tool_calls ?. map ( ( toolCall : any ) => ( {
113+ id :
114+ toolCall . id ||
115+ `tool-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 11 ) } ` ,
116+ name : toolCall . name ,
117+ content : JSON . stringify ( toolCall . args || toolCall . arguments || { } ) ,
118+ } ) ) || [ ] ;
101119
102120 // Create token usage from response data
103121 const tokenUsage = new TokenUsage ( ) ;
@@ -110,9 +128,7 @@ export class OllamaProvider implements LLMProvider {
110128 tokenUsage : tokenUsage ,
111129 } ;
112130 } catch ( error ) {
113- throw new Error (
114- `Error calling Ollama API: ${ ( error as Error ) . message } ` ,
115- ) ;
131+ throw new Error ( `Error calling Ollama API: ${ ( error as Error ) . message } ` ) ;
116132 }
117133 }
118134
@@ -121,7 +137,11 @@ export class OllamaProvider implements LLMProvider {
121137 */
122138 private formatMessages ( messages : Message [ ] ) : any [ ] {
123139 return messages . map ( ( msg ) => {
124- if ( msg . role === 'user' || msg . role === 'assistant' || msg . role === 'system' ) {
140+ if (
141+ msg . role === 'user' ||
142+ msg . role === 'assistant' ||
143+ msg . role === 'system'
144+ ) {
125145 return {
126146 role : msg . role ,
127147 content : msg . content ,
@@ -147,11 +167,11 @@ export class OllamaProvider implements LLMProvider {
147167 ] ,
148168 } ;
149169 }
150- // Default fallback
170+ // Default fallback for unknown message types
151171 return {
152172 role : 'user' ,
153- content : msg . content ,
173+ content : ( msg as any ) . content || '' ,
154174 } ;
155175 } ) ;
156176 }
157- }
177+ }
0 commit comments