ollama.rs

  1use anyhow::{Result, anyhow};
  2use futures::{FutureExt, StreamExt, future::BoxFuture, stream::BoxStream};
  3use futures::{Stream, TryFutureExt, stream};
  4use gpui::{AnyView, App, AsyncApp, Context, Subscription, Task};
  5use http_client::HttpClient;
  6use language_model::{
  7    AuthenticateError, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
  8    LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId,
  9    LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest,
 10    LanguageModelRequestTool, LanguageModelToolChoice, LanguageModelToolUse,
 11    LanguageModelToolUseId, MessageContent, RateLimiter, Role, StopReason, TokenUsage,
 12};
 13use ollama::{
 14    ChatMessage, ChatOptions, ChatRequest, ChatResponseDelta, KeepAlive, OllamaFunctionTool,
 15    OllamaToolCall, get_models, show_model, stream_chat_completion,
 16};
 17use schemars::JsonSchema;
 18use serde::{Deserialize, Serialize};
 19use settings::{Settings, SettingsStore};
 20use std::pin::Pin;
 21use std::sync::atomic::{AtomicU64, Ordering};
 22use std::{collections::HashMap, sync::Arc};
 23use ui::{ButtonLike, Indicator, List, prelude::*};
 24use util::ResultExt;
 25
 26use crate::AllLanguageModelSettings;
 27use crate::ui::InstructionListItem;
 28
 29const OLLAMA_DOWNLOAD_URL: &str = "https://ollama.com/download";
 30const OLLAMA_LIBRARY_URL: &str = "https://ollama.com/library";
 31const OLLAMA_SITE: &str = "https://ollama.com/";
 32
 33const PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("ollama");
 34const PROVIDER_NAME: LanguageModelProviderName = LanguageModelProviderName::new("Ollama");
 35
 36#[derive(Default, Debug, Clone, PartialEq)]
 37pub struct OllamaSettings {
 38    pub api_url: String,
 39    pub available_models: Vec<AvailableModel>,
 40}
 41
 42#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 43pub struct AvailableModel {
 44    /// The model name in the Ollama API (e.g. "llama3.2:latest")
 45    pub name: String,
 46    /// The model's name in Zed's UI, such as in the model selector dropdown menu in the assistant panel.
 47    pub display_name: Option<String>,
 48    /// The Context Length parameter to the model (aka num_ctx or n_ctx)
 49    pub max_tokens: u64,
 50    /// The number of seconds to keep the connection open after the last request
 51    pub keep_alive: Option<KeepAlive>,
 52    /// Whether the model supports tools
 53    pub supports_tools: Option<bool>,
 54    /// Whether the model supports vision
 55    pub supports_images: Option<bool>,
 56    /// Whether to enable think mode
 57    pub supports_thinking: Option<bool>,
 58}
 59
 60pub struct OllamaLanguageModelProvider {
 61    http_client: Arc<dyn HttpClient>,
 62    state: gpui::Entity<State>,
 63}
 64
 65pub struct State {
 66    http_client: Arc<dyn HttpClient>,
 67    available_models: Vec<ollama::Model>,
 68    fetch_model_task: Option<Task<Result<()>>>,
 69    _subscription: Subscription,
 70}
 71
 72impl State {
 73    fn is_authenticated(&self) -> bool {
 74        !self.available_models.is_empty()
 75    }
 76
 77    fn fetch_models(&mut self, cx: &mut Context<Self>) -> Task<Result<()>> {
 78        let settings = &AllLanguageModelSettings::get_global(cx).ollama;
 79        let http_client = Arc::clone(&self.http_client);
 80        let api_url = settings.api_url.clone();
 81
 82        // As a proxy for the server being "authenticated", we'll check if its up by fetching the models
 83        cx.spawn(async move |this, cx| {
 84            let models = get_models(http_client.as_ref(), &api_url, None).await?;
 85
 86            let tasks = models
 87                .into_iter()
 88                // Since there is no metadata from the Ollama API
 89                // indicating which models are embedding models,
 90                // simply filter out models with "-embed" in their name
 91                .filter(|model| !model.name.contains("-embed"))
 92                .map(|model| {
 93                    let http_client = Arc::clone(&http_client);
 94                    let api_url = api_url.clone();
 95                    async move {
 96                        let name = model.name.as_str();
 97                        let capabilities = show_model(http_client.as_ref(), &api_url, name).await?;
 98                        let ollama_model = ollama::Model::new(
 99                            name,
100                            None,
101                            None,
102                            Some(capabilities.supports_tools()),
103                            Some(capabilities.supports_vision()),
104                            Some(capabilities.supports_thinking()),
105                        );
106                        Ok(ollama_model)
107                    }
108                });
109
110            // Rate-limit capability fetches
111            // since there is an arbitrary number of models available
112            let mut ollama_models: Vec<_> = futures::stream::iter(tasks)
113                .buffer_unordered(5)
114                .collect::<Vec<Result<_>>>()
115                .await
116                .into_iter()
117                .collect::<Result<Vec<_>>>()?;
118
119            ollama_models.sort_by(|a, b| a.name.cmp(&b.name));
120
121            this.update(cx, |this, cx| {
122                this.available_models = ollama_models;
123                cx.notify();
124            })
125        })
126    }
127
128    fn restart_fetch_models_task(&mut self, cx: &mut Context<Self>) {
129        let task = self.fetch_models(cx);
130        self.fetch_model_task.replace(task);
131    }
132
133    fn authenticate(&mut self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
134        if self.is_authenticated() {
135            return Task::ready(Ok(()));
136        }
137
138        let fetch_models_task = self.fetch_models(cx);
139        cx.spawn(async move |_this, _cx| Ok(fetch_models_task.await?))
140    }
141}
142
143impl OllamaLanguageModelProvider {
144    pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
145        let this = Self {
146            http_client: http_client.clone(),
147            state: cx.new(|cx| {
148                let subscription = cx.observe_global::<SettingsStore>({
149                    let mut settings = AllLanguageModelSettings::get_global(cx).ollama.clone();
150                    move |this: &mut State, cx| {
151                        let new_settings = &AllLanguageModelSettings::get_global(cx).ollama;
152                        if &settings != new_settings {
153                            settings = new_settings.clone();
154                            this.restart_fetch_models_task(cx);
155                            cx.notify();
156                        }
157                    }
158                });
159
160                State {
161                    http_client,
162                    available_models: Default::default(),
163                    fetch_model_task: None,
164                    _subscription: subscription,
165                }
166            }),
167        };
168        this.state
169            .update(cx, |state, cx| state.restart_fetch_models_task(cx));
170        this
171    }
172}
173
174impl LanguageModelProviderState for OllamaLanguageModelProvider {
175    type ObservableEntity = State;
176
177    fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
178        Some(self.state.clone())
179    }
180}
181
182impl LanguageModelProvider for OllamaLanguageModelProvider {
183    fn id(&self) -> LanguageModelProviderId {
184        PROVIDER_ID
185    }
186
187    fn name(&self) -> LanguageModelProviderName {
188        PROVIDER_NAME
189    }
190
191    fn icon(&self) -> IconName {
192        IconName::AiOllama
193    }
194
195    fn default_model(&self, _: &App) -> Option<Arc<dyn LanguageModel>> {
196        // We shouldn't try to select default model, because it might lead to a load call for an unloaded model.
197        // In a constrained environment where user might not have enough resources it'll be a bad UX to select something
198        // to load by default.
199        None
200    }
201
202    fn default_fast_model(&self, _: &App) -> Option<Arc<dyn LanguageModel>> {
203        // See explanation for default_model.
204        None
205    }
206
207    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
208        let mut models: HashMap<String, ollama::Model> = HashMap::new();
209
210        // Add models from the Ollama API
211        for model in self.state.read(cx).available_models.iter() {
212            models.insert(model.name.clone(), model.clone());
213        }
214
215        // Override with available models from settings
216        for model in AllLanguageModelSettings::get_global(cx)
217            .ollama
218            .available_models
219            .iter()
220        {
221            models.insert(
222                model.name.clone(),
223                ollama::Model {
224                    name: model.name.clone(),
225                    display_name: model.display_name.clone(),
226                    max_tokens: model.max_tokens,
227                    keep_alive: model.keep_alive.clone(),
228                    supports_tools: model.supports_tools,
229                    supports_vision: model.supports_images,
230                    supports_thinking: model.supports_thinking,
231                },
232            );
233        }
234
235        let mut models = models
236            .into_values()
237            .map(|model| {
238                Arc::new(OllamaLanguageModel {
239                    id: LanguageModelId::from(model.name.clone()),
240                    model,
241                    http_client: self.http_client.clone(),
242                    request_limiter: RateLimiter::new(4),
243                }) as Arc<dyn LanguageModel>
244            })
245            .collect::<Vec<_>>();
246        models.sort_by_key(|model| model.name());
247        models
248    }
249
250    fn is_authenticated(&self, cx: &App) -> bool {
251        self.state.read(cx).is_authenticated()
252    }
253
254    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
255        self.state.update(cx, |state, cx| state.authenticate(cx))
256    }
257
258    fn configuration_view(
259        &self,
260        _target_agent: language_model::ConfigurationViewTargetAgent,
261        window: &mut Window,
262        cx: &mut App,
263    ) -> AnyView {
264        let state = self.state.clone();
265        cx.new(|cx| ConfigurationView::new(state, window, cx))
266            .into()
267    }
268
269    fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
270        self.state.update(cx, |state, cx| state.fetch_models(cx))
271    }
272}
273
274pub struct OllamaLanguageModel {
275    id: LanguageModelId,
276    model: ollama::Model,
277    http_client: Arc<dyn HttpClient>,
278    request_limiter: RateLimiter,
279}
280
281impl OllamaLanguageModel {
282    fn to_ollama_request(&self, request: LanguageModelRequest) -> ChatRequest {
283        let supports_vision = self.model.supports_vision.unwrap_or(false);
284
285        ChatRequest {
286            model: self.model.name.clone(),
287            messages: request
288                .messages
289                .into_iter()
290                .map(|msg| {
291                    let images = if supports_vision {
292                        msg.content
293                            .iter()
294                            .filter_map(|content| match content {
295                                MessageContent::Image(image) => Some(image.source.to_string()),
296                                _ => None,
297                            })
298                            .collect::<Vec<String>>()
299                    } else {
300                        vec![]
301                    };
302
303                    match msg.role {
304                        Role::User => ChatMessage::User {
305                            content: msg.string_contents(),
306                            images: if images.is_empty() {
307                                None
308                            } else {
309                                Some(images)
310                            },
311                        },
312                        Role::Assistant => {
313                            let content = msg.string_contents();
314                            let thinking =
315                                msg.content.into_iter().find_map(|content| match content {
316                                    MessageContent::Thinking { text, .. } if !text.is_empty() => {
317                                        Some(text)
318                                    }
319                                    _ => None,
320                                });
321                            ChatMessage::Assistant {
322                                content,
323                                tool_calls: None,
324                                images: if images.is_empty() {
325                                    None
326                                } else {
327                                    Some(images)
328                                },
329                                thinking,
330                            }
331                        }
332                        Role::System => ChatMessage::System {
333                            content: msg.string_contents(),
334                        },
335                    }
336                })
337                .collect(),
338            keep_alive: self.model.keep_alive.clone().unwrap_or_default(),
339            stream: true,
340            options: Some(ChatOptions {
341                num_ctx: Some(self.model.max_tokens),
342                stop: Some(request.stop),
343                temperature: request.temperature.or(Some(1.0)),
344                ..Default::default()
345            }),
346            think: self
347                .model
348                .supports_thinking
349                .map(|supports_thinking| supports_thinking && request.thinking_allowed),
350            tools: request.tools.into_iter().map(tool_into_ollama).collect(),
351        }
352    }
353}
354
355impl LanguageModel for OllamaLanguageModel {
356    fn id(&self) -> LanguageModelId {
357        self.id.clone()
358    }
359
360    fn name(&self) -> LanguageModelName {
361        LanguageModelName::from(self.model.display_name().to_string())
362    }
363
364    fn provider_id(&self) -> LanguageModelProviderId {
365        PROVIDER_ID
366    }
367
368    fn provider_name(&self) -> LanguageModelProviderName {
369        PROVIDER_NAME
370    }
371
372    fn supports_tools(&self) -> bool {
373        self.model.supports_tools.unwrap_or(false)
374    }
375
376    fn supports_images(&self) -> bool {
377        self.model.supports_vision.unwrap_or(false)
378    }
379
380    fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
381        match choice {
382            LanguageModelToolChoice::Auto => false,
383            LanguageModelToolChoice::Any => false,
384            LanguageModelToolChoice::None => false,
385        }
386    }
387
388    fn telemetry_id(&self) -> String {
389        format!("ollama/{}", self.model.id())
390    }
391
392    fn max_token_count(&self) -> u64 {
393        self.model.max_token_count()
394    }
395
396    fn count_tokens(
397        &self,
398        request: LanguageModelRequest,
399        _cx: &App,
400    ) -> BoxFuture<'static, Result<u64>> {
401        // There is no endpoint for this _yet_ in Ollama
402        // see: https://github.com/ollama/ollama/issues/1716 and https://github.com/ollama/ollama/issues/3582
403        let token_count = request
404            .messages
405            .iter()
406            .map(|msg| msg.string_contents().chars().count())
407            .sum::<usize>()
408            / 4;
409
410        async move { Ok(token_count as u64) }.boxed()
411    }
412
413    fn stream_completion(
414        &self,
415        request: LanguageModelRequest,
416        cx: &AsyncApp,
417    ) -> BoxFuture<
418        'static,
419        Result<
420            BoxStream<'static, Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>,
421            LanguageModelCompletionError,
422        >,
423    > {
424        let request = self.to_ollama_request(request);
425
426        let http_client = self.http_client.clone();
427        let Ok(api_url) = cx.update(|cx| {
428            let settings = &AllLanguageModelSettings::get_global(cx).ollama;
429            settings.api_url.clone()
430        }) else {
431            return futures::future::ready(Err(anyhow!("App state dropped").into())).boxed();
432        };
433
434        let future = self.request_limiter.stream(async move {
435            let stream = stream_chat_completion(http_client.as_ref(), &api_url, request).await?;
436            let stream = map_to_language_model_completion_events(stream);
437            Ok(stream)
438        });
439
440        future.map_ok(|f| f.boxed()).boxed()
441    }
442}
443
444fn map_to_language_model_completion_events(
445    stream: Pin<Box<dyn Stream<Item = anyhow::Result<ChatResponseDelta>> + Send>>,
446) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
447    // Used for creating unique tool use ids
448    static TOOL_CALL_COUNTER: AtomicU64 = AtomicU64::new(0);
449
450    struct State {
451        stream: Pin<Box<dyn Stream<Item = anyhow::Result<ChatResponseDelta>> + Send>>,
452        used_tools: bool,
453    }
454
455    // We need to create a ToolUse and Stop event from a single
456    // response from the original stream
457    let stream = stream::unfold(
458        State {
459            stream,
460            used_tools: false,
461        },
462        async move |mut state| {
463            let response = state.stream.next().await?;
464
465            let delta = match response {
466                Ok(delta) => delta,
467                Err(e) => {
468                    let event = Err(LanguageModelCompletionError::from(anyhow!(e)));
469                    return Some((vec![event], state));
470                }
471            };
472
473            let mut events = Vec::new();
474
475            match delta.message {
476                ChatMessage::User { content, images: _ } => {
477                    events.push(Ok(LanguageModelCompletionEvent::Text(content)));
478                }
479                ChatMessage::System { content } => {
480                    events.push(Ok(LanguageModelCompletionEvent::Text(content)));
481                }
482                ChatMessage::Assistant {
483                    content,
484                    tool_calls,
485                    images: _,
486                    thinking,
487                } => {
488                    if let Some(text) = thinking {
489                        events.push(Ok(LanguageModelCompletionEvent::Thinking {
490                            text,
491                            signature: None,
492                        }));
493                    }
494
495                    if let Some(tool_call) = tool_calls.and_then(|v| v.into_iter().next()) {
496                        match tool_call {
497                            OllamaToolCall::Function(function) => {
498                                let tool_id = format!(
499                                    "{}-{}",
500                                    &function.name,
501                                    TOOL_CALL_COUNTER.fetch_add(1, Ordering::Relaxed)
502                                );
503                                let event =
504                                    LanguageModelCompletionEvent::ToolUse(LanguageModelToolUse {
505                                        id: LanguageModelToolUseId::from(tool_id),
506                                        name: Arc::from(function.name),
507                                        raw_input: function.arguments.to_string(),
508                                        input: function.arguments,
509                                        is_input_complete: true,
510                                    });
511                                events.push(Ok(event));
512                                state.used_tools = true;
513                            }
514                        }
515                    } else if !content.is_empty() {
516                        events.push(Ok(LanguageModelCompletionEvent::Text(content)));
517                    }
518                }
519            };
520
521            if delta.done {
522                events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(TokenUsage {
523                    input_tokens: delta.prompt_eval_count.unwrap_or(0),
524                    output_tokens: delta.eval_count.unwrap_or(0),
525                    cache_creation_input_tokens: 0,
526                    cache_read_input_tokens: 0,
527                })));
528                if state.used_tools {
529                    state.used_tools = false;
530                    events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::ToolUse)));
531                } else {
532                    events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)));
533                }
534            }
535
536            Some((events, state))
537        },
538    );
539
540    stream.flat_map(futures::stream::iter)
541}
542
543struct ConfigurationView {
544    state: gpui::Entity<State>,
545    loading_models_task: Option<Task<()>>,
546}
547
548impl ConfigurationView {
549    pub fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
550        let loading_models_task = Some(cx.spawn_in(window, {
551            let state = state.clone();
552            async move |this, cx| {
553                if let Some(task) = state
554                    .update(cx, |state, cx| state.authenticate(cx))
555                    .log_err()
556                {
557                    task.await.log_err();
558                }
559                this.update(cx, |this, cx| {
560                    this.loading_models_task = None;
561                    cx.notify();
562                })
563                .log_err();
564            }
565        }));
566
567        Self {
568            state,
569            loading_models_task,
570        }
571    }
572
573    fn retry_connection(&self, cx: &mut App) {
574        self.state
575            .update(cx, |state, cx| state.fetch_models(cx))
576            .detach_and_log_err(cx);
577    }
578}
579
580impl Render for ConfigurationView {
581    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
582        let is_authenticated = self.state.read(cx).is_authenticated();
583
584        let ollama_intro =
585            "Get up & running with Llama 3.3, Mistral, Gemma 2, and other LLMs with Ollama.";
586
587        if self.loading_models_task.is_some() {
588            div().child(Label::new("Loading models...")).into_any()
589        } else {
590            v_flex()
591                .gap_2()
592                .child(
593                    v_flex().gap_1().child(Label::new(ollama_intro)).child(
594                        List::new()
595                            .child(InstructionListItem::text_only("Ollama must be running with at least one model installed to use it in the assistant."))
596                            .child(InstructionListItem::text_only(
597                                "Once installed, try `ollama run llama3.2`",
598                            )),
599                    ),
600                )
601                .child(
602                    h_flex()
603                        .w_full()
604                        .justify_between()
605                        .gap_2()
606                        .child(
607                            h_flex()
608                                .w_full()
609                                .gap_2()
610                                .map(|this| {
611                                    if is_authenticated {
612                                        this.child(
613                                            Button::new("ollama-site", "Ollama")
614                                                .style(ButtonStyle::Subtle)
615                                                .icon(IconName::ArrowUpRight)
616                                                .icon_size(IconSize::Small)
617                                                .icon_color(Color::Muted)
618                                                .on_click(move |_, _, cx| cx.open_url(OLLAMA_SITE))
619                                                .into_any_element(),
620                                        )
621                                    } else {
622                                        this.child(
623                                            Button::new(
624                                                "download_ollama_button",
625                                                "Download Ollama",
626                                            )
627                                            .style(ButtonStyle::Subtle)
628                                            .icon(IconName::ArrowUpRight)
629                                            .icon_size(IconSize::Small)
630                                            .icon_color(Color::Muted)
631                                            .on_click(move |_, _, cx| {
632                                                cx.open_url(OLLAMA_DOWNLOAD_URL)
633                                            })
634                                            .into_any_element(),
635                                        )
636                                    }
637                                })
638                                .child(
639                                    Button::new("view-models", "View All Models")
640                                        .style(ButtonStyle::Subtle)
641                                        .icon(IconName::ArrowUpRight)
642                                        .icon_size(IconSize::Small)
643                                        .icon_color(Color::Muted)
644                                        .on_click(move |_, _, cx| cx.open_url(OLLAMA_LIBRARY_URL)),
645                                ),
646                        )
647                        .map(|this| {
648                            if is_authenticated {
649                                this.child(
650                                    ButtonLike::new("connected")
651                                        .disabled(true)
652                                        .cursor_style(gpui::CursorStyle::Arrow)
653                                        .child(
654                                            h_flex()
655                                                .gap_2()
656                                                .child(Indicator::dot().color(Color::Success))
657                                                .child(Label::new("Connected"))
658                                                .into_any_element(),
659                                        ),
660                                )
661                            } else {
662                                this.child(
663                                    Button::new("retry_ollama_models", "Connect")
664                                        .icon_position(IconPosition::Start)
665                                        .icon_size(IconSize::XSmall)
666                                        .icon(IconName::PlayFilled)
667                                        .on_click(cx.listener(move |this, _, _, cx| {
668                                            this.retry_connection(cx)
669                                        })),
670                                )
671                            }
672                        })
673                )
674                .into_any()
675        }
676    }
677}
678
679fn tool_into_ollama(tool: LanguageModelRequestTool) -> ollama::OllamaTool {
680    ollama::OllamaTool::Function {
681        function: OllamaFunctionTool {
682            name: tool.name,
683            description: Some(tool.description),
684            parameters: Some(tool.input_schema),
685        },
686    }
687}