Skip to content

Commit

Permalink
fix: set default temperature and validate temperature value
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Feb 20, 2024
1 parent 21eac2c commit 8ff6db6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _COPILOT_CHAT_GLOBAL_CONFIG = {}
-- - hide_system_prompt: ('yes' | 'no') default: 'yes'.
-- - proxy: (string?) default: ''.
-- - language: (string?) default: ''.
-- - temperature: (string?) default: ''.
-- - temperature: (string?) default: '0.1'. Value between 0.0 and 1.0.
-- - prompts: (table?) default: default_prompts.
-- - debug: (boolean?) default: false.
M.setup = function(options)
Expand All @@ -25,7 +25,7 @@ M.setup = function(options)
vim.g.copilot_chat_hide_system_prompt = options and options.hide_system_prompt or 'yes'
vim.g.copilot_chat_proxy = options and options.proxy or ''
vim.g.copilot_chat_language = options and options.language or ''
vim.g.copilot_chat_temperature = options and options.temperature or ''
vim.g.copilot_chat_temperature = options and options.temperature or '0.1'
local debug = options and options.debug or false
_COPILOT_CHAT_GLOBAL_CONFIG.debug = debug

Expand Down
37 changes: 28 additions & 9 deletions rplugin/python3/CopilotChat/handlers/chat_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def is_module_installed(name):
return False


DEFAULT_TEMPERATURE = 0.1


# TODO: Support Custom Instructions when this issue has been resolved https://github.com/microsoft/vscode-copilot-release/issues/563
class ChatHandler:
has_show_extra_info = False
Expand Down Expand Up @@ -44,14 +47,12 @@ def chat(
disable_separators = (
self.nvim.eval("g:copilot_chat_disable_separators") == "yes"
)
temperature = self.nvim.eval("g:copilot_chat_temperature")
if temperature is None or temperature == "":
temperature = 0.1
else:
temperature = float(temperature)
self.proxy = self.nvim.eval("g:copilot_chat_proxy")
if "://" not in self.proxy:
self.proxy = None

# Validate and set temperature
temperature = self._get_temperature()

# Set proxy
self._set_proxy()

if system_prompt is None:
system_prompt = self._construct_system_prompt(prompt)
Expand All @@ -78,6 +79,24 @@ def chat(
self._add_end_separator(model, disable_separators)

# private
def _set_proxy(self):
self.proxy = self.nvim.eval("g:copilot_chat_proxy")
if "://" not in self.proxy:
self.proxy = None

def _get_temperature(self):
temperature = self.nvim.eval("g:copilot_chat_temperature")
try:
temperature = float(temperature)
if not 0 <= temperature <= 1:
raise ValueError
except ValueError:
self.nvim.exec_lua(
'require("CopilotChat.utils").log_error(...)',
"Invalid temperature value. Please provide a numeric value between 0 and 1.",
)
temperature = DEFAULT_TEMPERATURE
return temperature

def _construct_system_prompt(self, prompt: str):
system_prompt = system_prompts.COPILOT_INSTRUCTIONS
Expand Down Expand Up @@ -239,7 +258,7 @@ def _add_chat_messages(
code: str,
file_type: str,
model: str,
temperature: float = 0.1,
temperature: float = DEFAULT_TEMPERATURE,
):
if self.copilot is None:
self.copilot = Copilot(proxy=self.proxy)
Expand Down

0 comments on commit 8ff6db6

Please sign in to comment.