Skip to content

Commit

Permalink
feat: save the raw response for debugging purpose. (#37)
Browse files Browse the repository at this point in the history
* feat: save the raw response for debugging purpose.

* refactor: use `raw_response_cb` for exposing raw response

* docs: documentation about `raw_response_cb`.

* fix: use `self.params.raw_response_cb`.
  • Loading branch information
Davidyz authored Dec 26, 2024
1 parent 8e4ac92 commit 4b642ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,23 @@ cmp.setup({
},
})
```

## Debugging Information

To retrieve the raw response from the backend, you can set the following option
in `provider_options`:
```lua
provider_options = {
raw_response_cb = function(response)
-- the `response` parameter contains the raw response (JSON-like) object.

vim.notify(vim.inspect(response)) -- show the response as a lua table

vim.g.ai_raw_response = response -- store the raw response in a global
-- variable so that you can use it
-- somewhere else (like statusline).
end,
}
```
This provides useful information like context lengths (# of tokens) and
generation speeds (tokens per seconds), depending on your backend.
3 changes: 3 additions & 0 deletions lua/cmp_ai/requests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ function Service:Get(url, headers, data, cb)

local result = table.concat(response:result(), '\n')
local json = self:json_decode(result)
if type(self.params.raw_response_cb) == 'function' then
self.params.raw_response_cb(json)
end
if json == nil then
cb({ { error = 'No Response.' } })
else
Expand Down

0 comments on commit 4b642ce

Please sign in to comment.