Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lua/CopilotChat/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ function Client:ask(prompt, opts)
local resource_tokens = #resource_messages > 0 and tiktoken:count(resource_messages[1].content) or 0
local required_tokens = prompt_tokens + system_tokens + resource_tokens

log.debug('Prompt tokens:', prompt_tokens)
log.debug('System tokens:', system_tokens)
log.debug('Resource tokens:', resource_tokens)

-- Calculate how many tokens we can use for history
local history_limit = max_tokens - required_tokens
local history_tokens = 0
Expand Down
5 changes: 3 additions & 2 deletions lua/CopilotChat/tiktoken.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local log = require('plenary.log')
local notify = require('CopilotChat.notify')
local utils = require('CopilotChat.utils')
local curl = require('CopilotChat.utils.curl')
Expand Down Expand Up @@ -105,12 +106,12 @@ end
---@return number
function Tiktoken:count(prompt)
if not self.tiktoken_core then
return math.ceil(#prompt * 0.5) -- Fallback to 1/2 character count
return math.ceil(#prompt / 4)
end

local tokens = self:encode(prompt)
if not tokens then
return math.ceil(#prompt * 0.5) -- Fallback to 1/2 character count
return math.ceil(#prompt / 4)
end
return #tokens
end
Expand Down
Loading