context overflow fix
This commit is contained in:
parent
2caff9f639
commit
0f4dc5f28c
7 changed files with 24 additions and 24 deletions
|
|
@ -4,7 +4,7 @@ from typing import Any, Coroutine, Dict, Generator, List, Union
|
|||
from ...core.main import ChatMessage
|
||||
import openai
|
||||
from ..llm import LLM
|
||||
from ..util.count_tokens import DEFAULT_MAX_TOKENS, compile_chat_messages, CHAT_MODELS, DEFAULT_ARGS, count_tokens
|
||||
from ..util.count_tokens import DEFAULT_MAX_TOKENS, compile_chat_messages, CHAT_MODELS, DEFAULT_ARGS, count_tokens, prune_raw_prompt_from_top
|
||||
|
||||
|
||||
class OpenAI(LLM):
|
||||
|
|
@ -72,7 +72,7 @@ class OpenAI(LLM):
|
|||
)).choices[0].message.content
|
||||
else:
|
||||
resp = (await openai.Completion.acreate(
|
||||
prompt=prompt,
|
||||
prompt=prune_raw_prompt_from_top(args["model"], prompt),
|
||||
**args,
|
||||
)).choices[0].text
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,16 @@ def count_tokens(model: str, text: str | None):
|
|||
return len(encoding.encode(text, disallowed_special=()))
|
||||
|
||||
|
||||
def prune_raw_prompt_from_top(model: str, prompt: str):
|
||||
max_tokens = MAX_TOKENS_FOR_MODEL.get(model, DEFAULT_MAX_TOKENS)
|
||||
encoding = encoding_for_model(model)
|
||||
tokens = encoding.encode(prompt, disallowed_special=())
|
||||
if len(tokens) <= max_tokens:
|
||||
return prompt
|
||||
else:
|
||||
return encoding.decode(tokens[-max_tokens:])
|
||||
|
||||
|
||||
def prune_chat_history(model: str, chat_history: List[ChatMessage], max_tokens: int, tokens_for_completion: int):
|
||||
total_tokens = tokens_for_completion + \
|
||||
sum(count_tokens(model, message.content)
|
||||
|
|
@ -43,13 +53,13 @@ def prune_chat_history(model: str, chat_history: List[ChatMessage], max_tokens:
|
|||
i += 1
|
||||
|
||||
# 2. Remove entire messages until the last 5
|
||||
while len(chat_history) > 5 and total_tokens > max_tokens:
|
||||
while len(chat_history) > 5 and total_tokens > max_tokens and len(chat_history) > 0:
|
||||
message = chat_history.pop(0)
|
||||
total_tokens -= count_tokens(model, message.content)
|
||||
|
||||
# 3. Truncate message in the last 5
|
||||
i = 0
|
||||
while total_tokens > max_tokens:
|
||||
while total_tokens > max_tokens and len(chat_history) > 0:
|
||||
message = chat_history[0]
|
||||
total_tokens -= count_tokens(model, message.content)
|
||||
total_tokens += count_tokens(model, message.summary)
|
||||
|
|
|
|||
|
|
@ -211,26 +211,16 @@ class DefaultModelEditCodeStep(Step):
|
|||
|
||||
return cur_start_line, cur_end_line
|
||||
|
||||
if model_to_use.name == "gpt-4":
|
||||
|
||||
total_tokens = model_to_use.count_tokens(
|
||||
full_file_contents + self._prompt)
|
||||
cur_start_line, cur_end_line = cut_context(
|
||||
model_to_use, total_tokens, cur_start_line, cur_end_line)
|
||||
|
||||
elif model_to_use.name == "gpt-3.5-turbo" or model_to_use.name == "gpt-3.5-turbo-16k":
|
||||
|
||||
model_to_use = sdk.models.default
|
||||
if model_to_use.name == "gpt-3.5-turbo":
|
||||
if sdk.models.gpt35.count_tokens(full_file_contents) > MAX_TOKENS_FOR_MODEL["gpt-3.5-turbo"]:
|
||||
|
||||
model_to_use = sdk.models.gpt3516k
|
||||
total_tokens = model_to_use.count_tokens(
|
||||
full_file_contents + self._prompt)
|
||||
cur_start_line, cur_end_line = cut_context(
|
||||
model_to_use, total_tokens, cur_start_line, cur_end_line)
|
||||
|
||||
else:
|
||||
total_tokens = model_to_use.count_tokens(
|
||||
full_file_contents + self._prompt + self.user_input)
|
||||
|
||||
raise Exception("Unknown default model")
|
||||
cur_start_line, cur_end_line = cut_context(
|
||||
model_to_use, total_tokens, cur_start_line, cur_end_line)
|
||||
|
||||
code_before = "\n".join(
|
||||
full_file_contents_lst[cur_start_line:max_start_line])
|
||||
|
|
|
|||
4
extension/package-lock.json
generated
4
extension/package-lock.json
generated
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "continue",
|
||||
"version": "0.0.62",
|
||||
"version": "0.0.63",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "continue",
|
||||
"version": "0.0.62",
|
||||
"version": "0.0.63",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@electron/rebuild": "^3.2.10",
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
"displayName": "Continue",
|
||||
"pricing": "Free",
|
||||
"description": "Accelerating software development with language models",
|
||||
"version": "0.0.62",
|
||||
"version": "0.0.63",
|
||||
"publisher": "Continue",
|
||||
"engines": {
|
||||
"vscode": "^1.74.0"
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ const DialogContainer = styled.div`
|
|||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 75%;
|
||||
`;
|
||||
|
||||
const Dialog = styled.div`
|
||||
|
|
@ -76,7 +77,6 @@ const TextDialog = (props: {
|
|||
<Dialog>
|
||||
<P>Thanks for your feedback. We'll get back to you soon!</P>
|
||||
<TextArea
|
||||
cols={50}
|
||||
rows={10}
|
||||
ref={textAreaRef}
|
||||
onKeyDown={(e) => {
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Reference in a new issue