From d04b8f41f989391b2ee6ac890a30f67bb3573bd9 Mon Sep 17 00:00:00 2001 From: JoseConseco Date: Sun, 10 Dec 2023 16:12:27 +0100 Subject: [PATCH 01/16] switch to deepseek coder --- lua/cmp_ai/backends/ollama.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/cmp_ai/backends/ollama.lua b/lua/cmp_ai/backends/ollama.lua index 604115d..11a5bd0 100644 --- a/lua/cmp_ai/backends/ollama.lua +++ b/lua/cmp_ai/backends/ollama.lua @@ -8,7 +8,8 @@ function Ollama:new(o, params) self.__index = self self.params = vim.tbl_deep_extend('keep', params or {}, { base_url = 'http://127.0.0.1:11434/api/generate', - model = 'codellama:7b-code', + -- model = 'codellama:7b-code', + model = 'deepseek-coder:1.3b-base-q5_0', options = { temperature = 0.2, }, @@ -20,7 +21,8 @@ end function Ollama:complete(lines_before, lines_after, cb) local data = { model = self.params.model, - prompt = '
 ' .. lines_before .. ' ' .. lines_after .. ' ',
+    -- prompt = '
 ' .. lines_before .. ' ' .. lines_after .. ' ', -- for codellama
+    prompt = "<|fim▁begin|>" .. lines_before .. "<|fim▁hole|>" .. lines_after .. "<|fim▁end|>", -- for deepseek coder
     stream = false,
     options = self.params.options,
   }

From edb3d59758e230d7159053fdb0ab66c7db469b67 Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Sun, 10 Dec 2023 16:13:23 +0100
Subject: [PATCH 02/16] Implement debounce option for triggering
 autocompletions

Do not generate completion on every key press, but wait  some min time.
---
 lua/cmp_ai/source.lua | 49 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 4 deletions(-)

diff --git a/lua/cmp_ai/source.lua b/lua/cmp_ai/source.lua
index ce63038..8008d4c 100644
--- a/lua/cmp_ai/source.lua
+++ b/lua/cmp_ai/source.lua
@@ -40,19 +40,60 @@ function Source:_do_complete(ctx, cb)
   local service = conf:get('provider')
   service:complete(before, after, function(data)
     self:end_complete(data, ctx, cb)
-    if conf:get('notify') then
-      conf:get('notify_callback')('Completion started')
-    end
+    -- why 2x ?
+    -- if conf:get('notify') then
+    --   conf:get('notify_callback')('Completion started')
+    -- end
   end)
 end
 
+function Source:trigger(ctx, callback)
+  if vim.fn.mode() == 'i' then
+    self:_do_complete(ctx, callback)
+  end
+end
+
+-- based on https://github.com/runiq/neovim-throttle-debounce/blob/main/lua/throttle-debounce/init.lua (MIT)
+local function debounce_trailing(fn, ms)
+	local timer = vim.loop.new_timer()
+	local wrapped_fn
+
+    function wrapped_fn(...)
+      local argv = {...}
+      local argc = select('#', ...)
+      -- timer:stop() -- seems not needed?
+      timer:start(ms, 0, function()
+        pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc))
+      end)
+    end
+	return wrapped_fn, timer
+end
+
+local bounce_time = 700 -- in ms
+local bounced_complete, ret_tim =  debounce_trailing(
+  Source.trigger,
+  bounce_time)
+
+
+-- on keypress event autocommand -  call tim.timer_again
+local au_bound = vim.api.nvim_create_augroup("BounceCompletion", { clear = true })
+vim.api.nvim_create_autocmd("TextChangedI",{
+  pattern = "*",
+  callback = function()
+    vim.loop.timer_again(ret_tim)
+  end,
+  group = au_bound
+})
+
 --- complete
 function Source:complete(ctx, callback)
   if conf:get('ignored_file_types')[vim.bo.filetype] then
     callback()
     return
   end
-  self:_do_complete(ctx, callback)
+  -- local bufnr = vim.api.nvim_get_current_buf()
+  bounced_complete(self, ctx, callback)
+
 end
 
 function Source:end_complete(data, ctx, cb)

From 71d051e26dccb03204a4c537eb52cf15600e2807 Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Mon, 11 Dec 2023 14:44:53 +0100
Subject: [PATCH 03/16] fix passign parameters, expose debounce as param in
 config

---
 lua/cmp_ai/backends/ollama.lua | 6 ++----
 lua/cmp_ai/config.lua          | 1 +
 lua/cmp_ai/source.lua          | 4 ++--
 3 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/lua/cmp_ai/backends/ollama.lua b/lua/cmp_ai/backends/ollama.lua
index 11a5bd0..dde9d27 100644
--- a/lua/cmp_ai/backends/ollama.lua
+++ b/lua/cmp_ai/backends/ollama.lua
@@ -6,15 +6,13 @@ function Ollama:new(o, params)
   o = o or {}
   setmetatable(o, self)
   self.__index = self
-  self.params = vim.tbl_deep_extend('keep', params or {}, {
+  self.params = vim.tbl_deep_extend('keep', o or {}, {
     base_url = 'http://127.0.0.1:11434/api/generate',
-    -- model = 'codellama:7b-code',
-    model = 'deepseek-coder:1.3b-base-q5_0',
+    model = 'codellama:7b-code',
     options = {
       temperature = 0.2,
     },
   })
-
   return o
 end
 
diff --git a/lua/cmp_ai/config.lua b/lua/cmp_ai/config.lua
index d53d568..87474c1 100644
--- a/lua/cmp_ai/config.lua
+++ b/lua/cmp_ai/config.lua
@@ -5,6 +5,7 @@ local conf = {
   run_on_every_keystroke = true,
   provider = 'HF',
   provider_options = {},
+  debounce_delay = 200, -- ms
   notify = true,
   notify_callback = function(msg)
     vim.notify(msg)
diff --git a/lua/cmp_ai/source.lua b/lua/cmp_ai/source.lua
index 8008d4c..c210a8e 100644
--- a/lua/cmp_ai/source.lua
+++ b/lua/cmp_ai/source.lua
@@ -69,10 +69,10 @@ local function debounce_trailing(fn, ms)
 	return wrapped_fn, timer
 end
 
-local bounce_time = 700 -- in ms
 local bounced_complete, ret_tim =  debounce_trailing(
   Source.trigger,
-  bounce_time)
+  conf:get('debounce_delay')
+)
 
 
 -- on keypress event autocommand -  call tim.timer_again

From 79d4b29dc9579ef0595eb9034b6b950dbe292513 Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Tue, 12 Dec 2023 22:14:23 +0100
Subject: [PATCH 04/16] addes llamacpp support

---
 lua/cmp_ai/backends/llamacpp.lua | 50 ++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 lua/cmp_ai/backends/llamacpp.lua

diff --git a/lua/cmp_ai/backends/llamacpp.lua b/lua/cmp_ai/backends/llamacpp.lua
new file mode 100644
index 0000000..71f1d2a
--- /dev/null
+++ b/lua/cmp_ai/backends/llamacpp.lua
@@ -0,0 +1,50 @@
+local requests = require('cmp_ai.requests')
+
+LlamaCpp = requests:new(nil)
+
+function LlamaCpp:new(o, params)
+  o = o or {}
+  setmetatable(o, self)
+  self.__index = self
+  self.params = vim.tbl_deep_extend('keep', o or {}, {
+    base_url = 'http://localhost:8080/completion',
+    -- model = 'codellama:7b-code',
+    options = {
+      temperature = 0.2,
+    },
+  })
+  return o
+end
+
+function LlamaCpp:complete(lines_before, lines_after, cb)
+  local data = {
+    -- model = self.params.model,
+    -- prompt = '
 ' .. lines_before .. ' ' .. lines_after .. ' ', -- for codellama
+    prompt = "<|fim▁begin|>" .. lines_before .. "<|fim▁hole|>" .. lines_after .. "<|fim▁end|>", -- for deepseek coder
+    stream = false,
+  }
+  data = vim.tbl_extend('keep', data, self.params.options)
+
+  self:Get(self.params.base_url, {}, data, function(answer)
+    local new_data = {}
+    vim.print('answer', answer)
+    if answer.error ~= nil then
+      vim.notify('Llamacp error: ' .. answer.error)
+      return
+    end
+    if answer.stop then
+      local result = answer.content:gsub('', '')
+      vim.print('results', result)
+      table.insert(new_data, result)
+    end
+    cb(new_data)
+  end)
+end
+
+function LlamaCpp:test()
+  self:complete('def factorial(n)\n    if', '    return ans\n', function(data)
+    dump(data)
+  end)
+end
+
+return LlamaCpp

From 6018a4240b66e08c64fa17c3c60eef73b5dc772a Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Wed, 13 Dec 2023 13:25:13 +0100
Subject: [PATCH 05/16] commend out prints

---
 lua/cmp_ai/backends/llamacpp.lua | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lua/cmp_ai/backends/llamacpp.lua b/lua/cmp_ai/backends/llamacpp.lua
index 71f1d2a..03386ee 100644
--- a/lua/cmp_ai/backends/llamacpp.lua
+++ b/lua/cmp_ai/backends/llamacpp.lua
@@ -27,14 +27,14 @@ function LlamaCpp:complete(lines_before, lines_after, cb)
 
   self:Get(self.params.base_url, {}, data, function(answer)
     local new_data = {}
-    vim.print('answer', answer)
+    -- vim.print('answer', answer)
     if answer.error ~= nil then
       vim.notify('Llamacp error: ' .. answer.error)
       return
     end
     if answer.stop then
       local result = answer.content:gsub('', '')
-      vim.print('results', result)
+      -- vim.print('results', result)
       table.insert(new_data, result)
     end
     cb(new_data)

From 1dc3cb4007785d8f9e829afa0ba933ec3847b5b4 Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Thu, 14 Dec 2023 19:38:13 +0100
Subject: [PATCH 06/16] cancell timer or inert leave

---
 lua/cmp_ai/source.lua | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/lua/cmp_ai/source.lua b/lua/cmp_ai/source.lua
index c210a8e..4676c25 100644
--- a/lua/cmp_ai/source.lua
+++ b/lua/cmp_ai/source.lua
@@ -48,7 +48,7 @@ function Source:_do_complete(ctx, cb)
 end
 
 function Source:trigger(ctx, callback)
-  if vim.fn.mode() == 'i' then
+  if vim.fn.mode() == 'i' then -- and cmp.visible() - we want to show pupus even if it is not visible, or is visible
     self:_do_complete(ctx, callback)
   end
 end
@@ -76,15 +76,24 @@ local bounced_complete, ret_tim =  debounce_trailing(
 
 
 -- on keypress event autocommand -  call tim.timer_again
-local au_bound = vim.api.nvim_create_augroup("BounceCompletion", { clear = true })
+local bounce_autogroup = vim.api.nvim_create_augroup("BounceCompletion", { clear = true })
 vim.api.nvim_create_autocmd("TextChangedI",{
   pattern = "*",
   callback = function()
     vim.loop.timer_again(ret_tim)
   end,
-  group = au_bound
+  group = bounce_autogroup
 })
 
+vim.api.nvim_create_autocmd({"InsertLeave"},{
+  pattern = "*",
+  callback = function()
+    vim.loop.timer_stop(ret_tim)
+  end,
+ group = bounce_autogroup
+})
+
+
 --- complete
 function Source:complete(ctx, callback)
   if conf:get('ignored_file_types')[vim.bo.filetype] then

From 490e5ebf8f47ead64ac16ba38609908cd0c2a66f Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Fri, 15 Dec 2023 15:32:04 +0100
Subject: [PATCH 07/16] being able to customize llama promp

---
 lua/cmp_ai/backends/llamacpp.lua | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lua/cmp_ai/backends/llamacpp.lua b/lua/cmp_ai/backends/llamacpp.lua
index 03386ee..2f62ef0 100644
--- a/lua/cmp_ai/backends/llamacpp.lua
+++ b/lua/cmp_ai/backends/llamacpp.lua
@@ -24,6 +24,7 @@ function LlamaCpp:complete(lines_before, lines_after, cb)
     stream = false,
   }
   data = vim.tbl_extend('keep', data, self.params.options)
+  data.prompt = self.params.prompt(lines_before, lines_after)
 
   self:Get(self.params.base_url, {}, data, function(answer)
     local new_data = {}

From 7910988c4df51acf70eddb2c7ef825c02c65d415 Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Sat, 16 Dec 2023 21:11:10 +0100
Subject: [PATCH 08/16] bounce cmp-ai completions on all events... not jsut
 text change

---
 lua/cmp_ai/source.lua | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lua/cmp_ai/source.lua b/lua/cmp_ai/source.lua
index 4676c25..9b270d9 100644
--- a/lua/cmp_ai/source.lua
+++ b/lua/cmp_ai/source.lua
@@ -77,7 +77,7 @@ local bounced_complete, ret_tim =  debounce_trailing(
 
 -- on keypress event autocommand -  call tim.timer_again
 local bounce_autogroup = vim.api.nvim_create_augroup("BounceCompletion", { clear = true })
-vim.api.nvim_create_autocmd("TextChangedI",{
+vim.api.nvim_create_autocmd({"TextChangedI","InsertEnter","TextChangedP"},{
   pattern = "*",
   callback = function()
     vim.loop.timer_again(ret_tim)

From 41dda5bce3f6bcbd8feb877352e23e41e44b445e Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Sat, 16 Dec 2023 22:37:43 +0100
Subject: [PATCH 09/16] properly bounce. Is there better way ?

---
 lua/cmp_ai/source.lua | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/lua/cmp_ai/source.lua b/lua/cmp_ai/source.lua
index 9b270d9..6b41166 100644
--- a/lua/cmp_ai/source.lua
+++ b/lua/cmp_ai/source.lua
@@ -69,18 +69,20 @@ local function debounce_trailing(fn, ms)
 	return wrapped_fn, timer
 end
 
-local bounced_complete, ret_tim =  debounce_trailing(
+local bounce_complete, ret_tim =  debounce_trailing(
   Source.trigger,
   conf:get('debounce_delay')
 )
 
-
+local self_cp, ctx_cp, call_cp
 -- on keypress event autocommand -  call tim.timer_again
 local bounce_autogroup = vim.api.nvim_create_augroup("BounceCompletion", { clear = true })
 vim.api.nvim_create_autocmd({"TextChangedI","InsertEnter","TextChangedP"},{
   pattern = "*",
   callback = function()
-    vim.loop.timer_again(ret_tim)
+    if self_cp ~= nil then
+      bounce_complete(self_cp, ctx_cp, call_cp)
+    end
   end,
   group = bounce_autogroup
 })
@@ -88,7 +90,7 @@ vim.api.nvim_create_autocmd({"TextChangedI","InsertEnter","TextChangedP"},{
 vim.api.nvim_create_autocmd({"InsertLeave"},{
   pattern = "*",
   callback = function()
-    vim.loop.timer_stop(ret_tim)
+    ret_tim:stop()
   end,
  group = bounce_autogroup
 })
@@ -97,12 +99,12 @@ vim.api.nvim_create_autocmd({"InsertLeave"},{
 --- complete
 function Source:complete(ctx, callback)
   if conf:get('ignored_file_types')[vim.bo.filetype] then
-    callback()
+    -- callback() -- ? forwhat
     return
   end
   -- local bufnr = vim.api.nvim_get_current_buf()
-  bounced_complete(self, ctx, callback)
-
+  self_cp, ctx_cp, call_cp = self, ctx, callback
+  bounce_complete(self_cp, ctx, callback)
 end
 
 function Source:end_complete(data, ctx, cb)

From 865035fad7e7703944faf98e4afe3f6d3350263e Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Mon, 18 Dec 2023 15:55:31 +0100
Subject: [PATCH 10/16] up readme

---
 README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/README.md b/README.md
index e648abc..18c3876 100644
--- a/README.md
+++ b/README.md
@@ -128,7 +128,12 @@ cmp_ai:setup({
   provider = 'Ollama',
   provider_options = {
     model = 'codellama:7b-code',
+    prompt = function(lines_before, lines_after)
+      -- prompt depends on the model you use. Here is an example for deepseek coder
+      return prompt = '
 ' .. lines_before .. ' ' .. lines_after .. ' ', -- for codellama
+    end,
   },
+  debounce_delay = 600, -- ms llama may be GPU hungry, wait x ms after last key input, before sending request to it
   notify = true,
   notify_callback = function(msg)
     vim.notify(msg)
@@ -142,6 +147,49 @@ cmp_ai:setup({
 })
 ```
 
+To use with [LlamaCpp](https://github.com/ggerganov/llama.cpp).
+For now it requires you to run [Llama Server](https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md) manually with:
+
+```bash
+./server -m ./models/deepseek-coder-6.7b-base.Q4_K_M.gguf -ngl 50 -c 2048 --log-disable
+```
+LlamaCpp requires you to download model in GGUP format. Here is the current model I use:
+ - [DeepSeek Base 6.7b](https://huggingface.co/TheBloke/deepseek-coder-6.7B-base-GGUF/blob/main/deepseek-coder-6.7b-base.Q4_K_M.gguf)
+ It is good to have at least 12GB of VRAM to run it (works best with NVIDIA - due to CUDA acceleration).
+
+```lua
+local cmp_ai = require('cmp_ai.config')
+
+cmp_ai:setup {
+  max_lines = 30,
+  provider = "LlamaCpp",
+  provider_options = {
+    options = {
+      n_predict = 20,  -- number of generated predictions
+      min_p = 0.05, -- default 0.05,  Cut off predictions with probability below  Max_prob * min_p
+      -- repeat_last_n = 64, -- default 64
+      -- repeat_penalty = 1.100, -- default 1.1
+      -- see llama server link above - to see other options
+    },
+    prompt = function(lines_before, lines_after)
+      -- prompt depends on the model you use. Here is an example for deepseek coder
+      return "<|fim▁begin|>" .. lines_before .. "<|fim▁hole|>" .. lines_after .. "<|fim▁end|>" -- for deepseek coder
+    end,
+  },
+  debounce_delay = 600, -- ms llama may be GPU hungry, wait x ms after last key input, before sending request to it
+  notify = true,
+  notify_callback = function(msg)
+    vim.notify(msg)
+  end,
+  run_on_every_keystroke = false,
+  ignored_file_types = {
+    -- default is not to ignore
+    -- uncomment to ignore in lua:
+    -- lua = true
+  },
+}
+```
+
 ### `notify`
 
 As some completion sources can be quit slow, setting this to `true` will trigger

From a69f521c4b4be70f5241356cba384d7436f5b40a Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Mon, 18 Dec 2023 15:58:09 +0100
Subject: [PATCH 11/16] up docs

---
 README.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/README.md b/README.md
index 18c3876..d328ae9 100644
--- a/README.md
+++ b/README.md
@@ -145,6 +145,7 @@ cmp_ai:setup({
     -- lua = true
   },
 })
+Models for Ollama are available at [here][(https://ollama.ai/library). For code completions use model that supports it - e.g. [DeepSeek Base 6.7b](https://ollama.ai/library/deepseek-coder)
 ```
 
 To use with [LlamaCpp](https://github.com/ggerganov/llama.cpp).

From 30e63f06b4fc3b57188e0f89b1e12b78a6b00f5b Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Mon, 18 Dec 2023 16:02:42 +0100
Subject: [PATCH 12/16] doc up 2

---
 README.md | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/README.md b/README.md
index d328ae9..54c6c67 100644
--- a/README.md
+++ b/README.md
@@ -145,18 +145,10 @@ cmp_ai:setup({
     -- lua = true
   },
 })
-Models for Ollama are available at [here][(https://ollama.ai/library). For code completions use model that supports it - e.g. [DeepSeek Base 6.7b](https://ollama.ai/library/deepseek-coder)
 ```
+Models for Ollama are available at [here][(https://ollama.ai/library). For code completions use model that supports it - e.g. [DeepSeek Base 6.7b](https://ollama.ai/library/deepseek-coder)
 
-To use with [LlamaCpp](https://github.com/ggerganov/llama.cpp).
-For now it requires you to run [Llama Server](https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md) manually with:
-
-```bash
-./server -m ./models/deepseek-coder-6.7b-base.Q4_K_M.gguf -ngl 50 -c 2048 --log-disable
-```
-LlamaCpp requires you to download model in GGUP format. Here is the current model I use:
- - [DeepSeek Base 6.7b](https://huggingface.co/TheBloke/deepseek-coder-6.7B-base-GGUF/blob/main/deepseek-coder-6.7b-base.Q4_K_M.gguf)
- It is good to have at least 12GB of VRAM to run it (works best with NVIDIA - due to CUDA acceleration).
+To use with [LlamaCpp](https://github.com/ggerganov/llama.cpp):
 
 ```lua
 local cmp_ai = require('cmp_ai.config')
@@ -191,6 +183,18 @@ cmp_ai:setup {
 }
 ```
 
+
+[LlamaCpp Server](https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md) has to be started manually with:
+
+```bash
+./server -m ./models/deepseek-coder-6.7b-base.Q4_K_M.gguf -ngl 50 -c 2048 --log-disable
+```
+
+LlamaCpp requires model in GGUP format. Here is the current model I use for coding:
+ - [DeepSeek Base 6.7b](https://huggingface.co/TheBloke/deepseek-coder-6.7B-base-GGUF/blob/main/deepseek-coder-6.7b-base.Q4_K_M.gguf)
+ It is good to have at least 12GB of VRAM to run it (works best with NVIDIA - due to CUDA acceleration). If you want you can grab smaller models too (faster to run, but lower quality of completions)
+
+
 ### `notify`
 
 As some completion sources can be quit slow, setting this to `true` will trigger

From 071d7015d96329f3501b976e4dff79458e6f4756 Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Mon, 18 Dec 2023 16:03:26 +0100
Subject: [PATCH 13/16] typo

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 54c6c67..511c7a8 100644
--- a/README.md
+++ b/README.md
@@ -146,7 +146,7 @@ cmp_ai:setup({
   },
 })
 ```
-Models for Ollama are available at [here][(https://ollama.ai/library). For code completions use model that supports it - e.g. [DeepSeek Base 6.7b](https://ollama.ai/library/deepseek-coder)
+Models for Ollama are available at [here](https://ollama.ai/library). For code completions use model that supports it - e.g. [DeepSeek Base 6.7b](https://ollama.ai/library/deepseek-coder)
 
 To use with [LlamaCpp](https://github.com/ggerganov/llama.cpp):
 

From 2f8e1b0a3c48037c1377b5b1bb38046ea0945b5f Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Thu, 11 Jan 2024 17:31:10 +0100
Subject: [PATCH 14/16] clean comments, as requested

---
 lua/cmp_ai/source.lua | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/lua/cmp_ai/source.lua b/lua/cmp_ai/source.lua
index 6b41166..7b80892 100644
--- a/lua/cmp_ai/source.lua
+++ b/lua/cmp_ai/source.lua
@@ -48,7 +48,7 @@ function Source:_do_complete(ctx, cb)
 end
 
 function Source:trigger(ctx, callback)
-  if vim.fn.mode() == 'i' then -- and cmp.visible() - we want to show pupus even if it is not visible, or is visible
+  if vim.fn.mode() == 'i' then
     self:_do_complete(ctx, callback)
   end
 end
@@ -74,8 +74,8 @@ local bounce_complete, ret_tim =  debounce_trailing(
   conf:get('debounce_delay')
 )
 
-local self_cp, ctx_cp, call_cp
--- on keypress event autocommand -  call tim.timer_again
+local self_cp, ctx_cp, call_cp -- variables to store last completion context
+
 local bounce_autogroup = vim.api.nvim_create_augroup("BounceCompletion", { clear = true })
 vim.api.nvim_create_autocmd({"TextChangedI","InsertEnter","TextChangedP"},{
   pattern = "*",
@@ -99,10 +99,9 @@ vim.api.nvim_create_autocmd({"InsertLeave"},{
 --- complete
 function Source:complete(ctx, callback)
   if conf:get('ignored_file_types')[vim.bo.filetype] then
-    -- callback() -- ? forwhat
+    callback()
     return
   end
-  -- local bufnr = vim.api.nvim_get_current_buf()
   self_cp, ctx_cp, call_cp = self, ctx, callback
   bounce_complete(self_cp, ctx, callback)
 end

From 4442111e6e0d8691b476ea271018a918e6cd2712 Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Fri, 9 Feb 2024 17:30:15 +0100
Subject: [PATCH 15/16] fix readme - prompt for ollama

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 511c7a8..5d6c728 100644
--- a/README.md
+++ b/README.md
@@ -130,7 +130,7 @@ cmp_ai:setup({
     model = 'codellama:7b-code',
     prompt = function(lines_before, lines_after)
       -- prompt depends on the model you use. Here is an example for deepseek coder
-      return prompt = '
 ' .. lines_before .. ' ' .. lines_after .. ' ', -- for codellama
+      return '
 ' .. lines_before .. ' ' .. lines_after .. ' ' -- for codellama
     end,
   },
   debounce_delay = 600, -- ms llama may be GPU hungry, wait x ms after last key input, before sending request to it

From 9633b8ee9cd549efc955914e76585dd0d191dcf0 Mon Sep 17 00:00:00 2001
From: JoseConseco 
Date: Sun, 16 Jun 2024 13:54:15 +0200
Subject: [PATCH 16/16] docile lammap - simple wrapper aroudn llamaCpp (just
 run llama with sel model from options)

---
 lua/cmp_ai/backends/docilellamacpp.lua | 58 ++++++++++++++++++++++++++
 lua/cmp_ai/requests.lua                |  2 +
 2 files changed, 60 insertions(+)
 create mode 100644 lua/cmp_ai/backends/docilellamacpp.lua

diff --git a/lua/cmp_ai/backends/docilellamacpp.lua b/lua/cmp_ai/backends/docilellamacpp.lua
new file mode 100644
index 0000000..27d4191
--- /dev/null
+++ b/lua/cmp_ai/backends/docilellamacpp.lua
@@ -0,0 +1,58 @@
+local requests = require('cmp_ai.requests')
+
+DocileLlamaCpp = requests:new(nil)
+
+
+function DocileLlamaCpp:new(o, params)
+  o = o or {}
+  setmetatable(o, self)
+  self.__index = self
+  self.params = vim.tbl_deep_extend('keep', o or {}, {
+    base_url = 'http://localhost:5000/forward',
+    -- model = 'codellama:7b-code',
+    options = {
+      temperature = 0.2,
+    },
+  })
+  return o
+end
+
+function DocileLlamaCpp:complete(lines_before, lines_after, cb)
+  local data = {
+    -- model = self.params.model,
+    -- prompt = '
 ' .. lines_before .. ' ' .. lines_after .. ' ', -- for codellama
+    prompt = "<|fim▁begin|>" .. lines_before .. "<|fim▁hole|>" .. lines_after .. "<|fim▁end|>", -- for deepseek coder
+    stream = false,
+  }
+  data = vim.tbl_extend('keep', data, self.params.options)
+  data.prompt = self.params.prompt(lines_before, lines_after)
+
+  self:Get(self.params.base_url, {}, data, function(answer)
+    local new_data = {}
+    -- vim.print('answer', answer)
+    if answer.error ~= nil then
+      vim.notify('Docile error: ' .. answer.error)
+      return
+    end
+    if answer.stop then
+      local result = answer.content:gsub('', '')
+
+      -- detect if 'CodeQwen' string in answer.generation_settings.model
+      if string.find(answer.generation_settings.model, 'CodeQwen') then
+        -- also get rid first letter - which is always the same space - but only for CodeQwen....
+        result = result:gsub('^.', '')
+      end
+      -- vim.print('results', result)
+      table.insert(new_data, result)
+    end
+    cb(new_data)
+  end)
+end
+
+function DocileLlamaCpp:test()
+  self:complete('def factorial(n)\n    if', '    return ans\n', function(data)
+    dump(data)
+  end)
+end
+
+return DocileLlamaCpp
diff --git a/lua/cmp_ai/requests.lua b/lua/cmp_ai/requests.lua
index 5b49dfc..36f0a78 100644
--- a/lua/cmp_ai/requests.lua
+++ b/lua/cmp_ai/requests.lua
@@ -35,6 +35,7 @@ function Service:Get(url, headers, data, cb)
     vim.notify('Cannot open temporary message file: ' .. tmpfname, vim.log.levels.ERROR)
     return
   end
+  -- vim.print("Request Data: ", vim.fn.json_encode(data))
   f:write(vim.fn.json_encode(data))
   f:close()
 
@@ -57,6 +58,7 @@ function Service:Get(url, headers, data, cb)
 
         local result = table.concat(response:result(), '\n')
         local json = self:json_decode(result)
+        -- vim.print("Response: ", json )
         if json == nil then
           cb({ { error = 'No Response.' } })
         else