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, cx: &App) -> Option<Arc<dyn LanguageModel>> {
196 self.provided_models(cx).into_iter().next()
197 }
198
199 fn default_fast_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {
200 self.default_model(cx)
201 }
202
203 fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
204 let mut models: HashMap<String, ollama::Model> = HashMap::new();
205
206 // Add models from the Ollama API
207 for model in self.state.read(cx).available_models.iter() {
208 models.insert(model.name.clone(), model.clone());
209 }
210
211 // Override with available models from settings
212 for model in AllLanguageModelSettings::get_global(cx)
213 .ollama
214 .available_models
215 .iter()
216 {
217 models.insert(
218 model.name.clone(),
219 ollama::Model {
220 name: model.name.clone(),
221 display_name: model.display_name.clone(),
222 max_tokens: model.max_tokens,
223 keep_alive: model.keep_alive.clone(),
224 supports_tools: model.supports_tools,
225 supports_vision: model.supports_images,
226 supports_thinking: model.supports_thinking,
227 },
228 );
229 }
230
231 let mut models = models
232 .into_values()
233 .map(|model| {
234 Arc::new(OllamaLanguageModel {
235 id: LanguageModelId::from(model.name.clone()),
236 model: model.clone(),
237 http_client: self.http_client.clone(),
238 request_limiter: RateLimiter::new(4),
239 }) as Arc<dyn LanguageModel>
240 })
241 .collect::<Vec<_>>();
242 models.sort_by_key(|model| model.name());
243 models
244 }
245
246 fn is_authenticated(&self, cx: &App) -> bool {
247 self.state.read(cx).is_authenticated()
248 }
249
250 fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
251 self.state.update(cx, |state, cx| state.authenticate(cx))
252 }
253
254 fn configuration_view(&self, window: &mut Window, cx: &mut App) -> AnyView {
255 let state = self.state.clone();
256 cx.new(|cx| ConfigurationView::new(state, window, cx))
257 .into()
258 }
259
260 fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
261 self.state.update(cx, |state, cx| state.fetch_models(cx))
262 }
263}
264
265pub struct OllamaLanguageModel {
266 id: LanguageModelId,
267 model: ollama::Model,
268 http_client: Arc<dyn HttpClient>,
269 request_limiter: RateLimiter,
270}
271
272impl OllamaLanguageModel {
273 fn to_ollama_request(&self, request: LanguageModelRequest) -> ChatRequest {
274 let supports_vision = self.model.supports_vision.unwrap_or(false);
275
276 ChatRequest {
277 model: self.model.name.clone(),
278 messages: request
279 .messages
280 .into_iter()
281 .map(|msg| {
282 let images = if supports_vision {
283 msg.content
284 .iter()
285 .filter_map(|content| match content {
286 MessageContent::Image(image) => Some(image.source.to_string()),
287 _ => None,
288 })
289 .collect::<Vec<String>>()
290 } else {
291 vec![]
292 };
293
294 match msg.role {
295 Role::User => ChatMessage::User {
296 content: msg.string_contents(),
297 images: if images.is_empty() {
298 None
299 } else {
300 Some(images)
301 },
302 },
303 Role::Assistant => {
304 let content = msg.string_contents();
305 let thinking =
306 msg.content.into_iter().find_map(|content| match content {
307 MessageContent::Thinking { text, .. } if !text.is_empty() => {
308 Some(text)
309 }
310 _ => None,
311 });
312 ChatMessage::Assistant {
313 content,
314 tool_calls: None,
315 images: if images.is_empty() {
316 None
317 } else {
318 Some(images)
319 },
320 thinking,
321 }
322 }
323 Role::System => ChatMessage::System {
324 content: msg.string_contents(),
325 },
326 }
327 })
328 .collect(),
329 keep_alive: self.model.keep_alive.clone().unwrap_or_default(),
330 stream: true,
331 options: Some(ChatOptions {
332 num_ctx: Some(self.model.max_tokens),
333 stop: Some(request.stop),
334 temperature: request.temperature.or(Some(1.0)),
335 ..Default::default()
336 }),
337 think: self
338 .model
339 .supports_thinking
340 .map(|supports_thinking| supports_thinking && request.thinking_allowed),
341 tools: request.tools.into_iter().map(tool_into_ollama).collect(),
342 }
343 }
344}
345
346impl LanguageModel for OllamaLanguageModel {
347 fn id(&self) -> LanguageModelId {
348 self.id.clone()
349 }
350
351 fn name(&self) -> LanguageModelName {
352 LanguageModelName::from(self.model.display_name().to_string())
353 }
354
355 fn provider_id(&self) -> LanguageModelProviderId {
356 PROVIDER_ID
357 }
358
359 fn provider_name(&self) -> LanguageModelProviderName {
360 PROVIDER_NAME
361 }
362
363 fn supports_tools(&self) -> bool {
364 self.model.supports_tools.unwrap_or(false)
365 }
366
367 fn supports_images(&self) -> bool {
368 self.model.supports_vision.unwrap_or(false)
369 }
370
371 fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
372 match choice {
373 LanguageModelToolChoice::Auto => false,
374 LanguageModelToolChoice::Any => false,
375 LanguageModelToolChoice::None => false,
376 }
377 }
378
379 fn telemetry_id(&self) -> String {
380 format!("ollama/{}", self.model.id())
381 }
382
383 fn max_token_count(&self) -> u64 {
384 self.model.max_token_count()
385 }
386
387 fn count_tokens(
388 &self,
389 request: LanguageModelRequest,
390 _cx: &App,
391 ) -> BoxFuture<'static, Result<u64>> {
392 // There is no endpoint for this _yet_ in Ollama
393 // see: https://github.com/ollama/ollama/issues/1716 and https://github.com/ollama/ollama/issues/3582
394 let token_count = request
395 .messages
396 .iter()
397 .map(|msg| msg.string_contents().chars().count())
398 .sum::<usize>()
399 / 4;
400
401 async move { Ok(token_count as u64) }.boxed()
402 }
403
404 fn stream_completion(
405 &self,
406 request: LanguageModelRequest,
407 cx: &AsyncApp,
408 ) -> BoxFuture<
409 'static,
410 Result<
411 BoxStream<'static, Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>,
412 LanguageModelCompletionError,
413 >,
414 > {
415 let request = self.to_ollama_request(request);
416
417 let http_client = self.http_client.clone();
418 let Ok(api_url) = cx.update(|cx| {
419 let settings = &AllLanguageModelSettings::get_global(cx).ollama;
420 settings.api_url.clone()
421 }) else {
422 return futures::future::ready(Err(anyhow!("App state dropped").into())).boxed();
423 };
424
425 let future = self.request_limiter.stream(async move {
426 let stream = stream_chat_completion(http_client.as_ref(), &api_url, request).await?;
427 let stream = map_to_language_model_completion_events(stream);
428 Ok(stream)
429 });
430
431 future.map_ok(|f| f.boxed()).boxed()
432 }
433}
434
435fn map_to_language_model_completion_events(
436 stream: Pin<Box<dyn Stream<Item = anyhow::Result<ChatResponseDelta>> + Send>>,
437) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
438 // Used for creating unique tool use ids
439 static TOOL_CALL_COUNTER: AtomicU64 = AtomicU64::new(0);
440
441 struct State {
442 stream: Pin<Box<dyn Stream<Item = anyhow::Result<ChatResponseDelta>> + Send>>,
443 used_tools: bool,
444 }
445
446 // We need to create a ToolUse and Stop event from a single
447 // response from the original stream
448 let stream = stream::unfold(
449 State {
450 stream,
451 used_tools: false,
452 },
453 async move |mut state| {
454 let response = state.stream.next().await?;
455
456 let delta = match response {
457 Ok(delta) => delta,
458 Err(e) => {
459 let event = Err(LanguageModelCompletionError::from(anyhow!(e)));
460 return Some((vec![event], state));
461 }
462 };
463
464 let mut events = Vec::new();
465
466 match delta.message {
467 ChatMessage::User { content, images: _ } => {
468 events.push(Ok(LanguageModelCompletionEvent::Text(content)));
469 }
470 ChatMessage::System { content } => {
471 events.push(Ok(LanguageModelCompletionEvent::Text(content)));
472 }
473 ChatMessage::Assistant {
474 content,
475 tool_calls,
476 images: _,
477 thinking,
478 } => {
479 if let Some(text) = thinking {
480 events.push(Ok(LanguageModelCompletionEvent::Thinking {
481 text,
482 signature: None,
483 }));
484 }
485
486 if let Some(tool_call) = tool_calls.and_then(|v| v.into_iter().next()) {
487 match tool_call {
488 OllamaToolCall::Function(function) => {
489 let tool_id = format!(
490 "{}-{}",
491 &function.name,
492 TOOL_CALL_COUNTER.fetch_add(1, Ordering::Relaxed)
493 );
494 let event =
495 LanguageModelCompletionEvent::ToolUse(LanguageModelToolUse {
496 id: LanguageModelToolUseId::from(tool_id),
497 name: Arc::from(function.name),
498 raw_input: function.arguments.to_string(),
499 input: function.arguments,
500 is_input_complete: true,
501 });
502 events.push(Ok(event));
503 state.used_tools = true;
504 }
505 }
506 } else if !content.is_empty() {
507 events.push(Ok(LanguageModelCompletionEvent::Text(content)));
508 }
509 }
510 };
511
512 if delta.done {
513 events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(TokenUsage {
514 input_tokens: delta.prompt_eval_count.unwrap_or(0),
515 output_tokens: delta.eval_count.unwrap_or(0),
516 cache_creation_input_tokens: 0,
517 cache_read_input_tokens: 0,
518 })));
519 if state.used_tools {
520 state.used_tools = false;
521 events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::ToolUse)));
522 } else {
523 events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)));
524 }
525 }
526
527 Some((events, state))
528 },
529 );
530
531 stream.flat_map(futures::stream::iter)
532}
533
534struct ConfigurationView {
535 state: gpui::Entity<State>,
536 loading_models_task: Option<Task<()>>,
537}
538
539impl ConfigurationView {
540 pub fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
541 let loading_models_task = Some(cx.spawn_in(window, {
542 let state = state.clone();
543 async move |this, cx| {
544 if let Some(task) = state
545 .update(cx, |state, cx| state.authenticate(cx))
546 .log_err()
547 {
548 task.await.log_err();
549 }
550 this.update(cx, |this, cx| {
551 this.loading_models_task = None;
552 cx.notify();
553 })
554 .log_err();
555 }
556 }));
557
558 Self {
559 state,
560 loading_models_task,
561 }
562 }
563
564 fn retry_connection(&self, cx: &mut App) {
565 self.state
566 .update(cx, |state, cx| state.fetch_models(cx))
567 .detach_and_log_err(cx);
568 }
569}
570
571impl Render for ConfigurationView {
572 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
573 let is_authenticated = self.state.read(cx).is_authenticated();
574
575 let ollama_intro =
576 "Get up & running with Llama 3.3, Mistral, Gemma 2, and other LLMs with Ollama.";
577
578 if self.loading_models_task.is_some() {
579 div().child(Label::new("Loading models...")).into_any()
580 } else {
581 v_flex()
582 .gap_2()
583 .child(
584 v_flex().gap_1().child(Label::new(ollama_intro)).child(
585 List::new()
586 .child(InstructionListItem::text_only("Ollama must be running with at least one model installed to use it in the assistant."))
587 .child(InstructionListItem::text_only(
588 "Once installed, try `ollama run llama3.2`",
589 )),
590 ),
591 )
592 .child(
593 h_flex()
594 .w_full()
595 .justify_between()
596 .gap_2()
597 .child(
598 h_flex()
599 .w_full()
600 .gap_2()
601 .map(|this| {
602 if is_authenticated {
603 this.child(
604 Button::new("ollama-site", "Ollama")
605 .style(ButtonStyle::Subtle)
606 .icon(IconName::ArrowUpRight)
607 .icon_size(IconSize::XSmall)
608 .icon_color(Color::Muted)
609 .on_click(move |_, _, cx| cx.open_url(OLLAMA_SITE))
610 .into_any_element(),
611 )
612 } else {
613 this.child(
614 Button::new(
615 "download_ollama_button",
616 "Download Ollama",
617 )
618 .style(ButtonStyle::Subtle)
619 .icon(IconName::ArrowUpRight)
620 .icon_size(IconSize::XSmall)
621 .icon_color(Color::Muted)
622 .on_click(move |_, _, cx| {
623 cx.open_url(OLLAMA_DOWNLOAD_URL)
624 })
625 .into_any_element(),
626 )
627 }
628 })
629 .child(
630 Button::new("view-models", "All Models")
631 .style(ButtonStyle::Subtle)
632 .icon(IconName::ArrowUpRight)
633 .icon_size(IconSize::XSmall)
634 .icon_color(Color::Muted)
635 .on_click(move |_, _, cx| cx.open_url(OLLAMA_LIBRARY_URL)),
636 ),
637 )
638 .map(|this| {
639 if is_authenticated {
640 this.child(
641 ButtonLike::new("connected")
642 .disabled(true)
643 .cursor_style(gpui::CursorStyle::Arrow)
644 .child(
645 h_flex()
646 .gap_2()
647 .child(Indicator::dot().color(Color::Success))
648 .child(Label::new("Connected"))
649 .into_any_element(),
650 ),
651 )
652 } else {
653 this.child(
654 Button::new("retry_ollama_models", "Connect")
655 .icon_position(IconPosition::Start)
656 .icon_size(IconSize::XSmall)
657 .icon(IconName::Play)
658 .on_click(cx.listener(move |this, _, _, cx| {
659 this.retry_connection(cx)
660 })),
661 )
662 }
663 })
664 )
665 .into_any()
666 }
667 }
668}
669
670fn tool_into_ollama(tool: LanguageModelRequestTool) -> ollama::OllamaTool {
671 ollama::OllamaTool::Function {
672 function: OllamaFunctionTool {
673 name: tool.name,
674 description: Some(tool.description),
675 parameters: Some(tool.input_schema),
676 },
677 }
678}