-
Notifications
You must be signed in to change notification settings - Fork 819
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ refactoring LLM to avoid repetition
- Loading branch information
Showing
30 changed files
with
242 additions
and
513 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,55 @@ | ||
--- | ||
name: "Bug report \U0001F41B" | ||
about: Create a report to help us fix your bug | ||
title: '' | ||
title: "" | ||
labels: bug | ||
assignees: '' | ||
|
||
assignees: "" | ||
--- | ||
|
||
**Describe the bug** | ||
A clear and concise description of what the bug is. | ||
|
||
**To Reproduce** | ||
Steps to reproduce the behavior: | ||
|
||
1. Go to '...' | ||
2. Click on '....' | ||
3. Scroll down to '....' | ||
4. See error | ||
|
||
**Expected behavior** | ||
A clear and concise description of what you expected to happen. | ||
|
||
**Screenshots** | ||
If applicable, add screenshots to help explain your problem. | ||
|
||
**Environment** | ||
|
||
- Operating System: [e.g. MacOS] | ||
- Python Version: [e.g. 3.10.6] | ||
- Continue Version: [e.g. v0.0.207] | ||
|
||
**Console logs** | ||
**Logs** | ||
|
||
``` | ||
REPLACE THIS SECTION WITH CONSOLE LOGS OR A SCREENSHOT... | ||
``` | ||
|
||
To get the Continue server logs: | ||
|
||
1. cmd+shift+p (MacOS) / ctrl+shift+p (Windows) | ||
2. Search for and then select "Continue: View Continue Server Logs" | ||
3. Scroll to the bottom of `continue.log` and copy the last 100 lines or so | ||
|
||
To get the VS Code console logs: | ||
|
||
To get the console logs in VS Code: | ||
1. cmd+shift+p (MacOS) / ctrl+shift+p (Windows) | ||
2. Search for and then select "Developer: Toggle Developer Tools" | ||
3. Select Console | ||
4. Read the console logs | ||
``` | ||
|
||
If the problem is related to LLM prompting: | ||
|
||
1. Hover the problematic response in the Continue UI | ||
2. Click the "magnifying glass" icon | ||
3. Copy the contents of the `continue_logs.txt` file that opens | ||
|
||
**Screenshots** | ||
If applicable, add screenshots to help explain your problem. | ||
|
||
**Additional context** | ||
Add any other context about the problem here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,77 @@ | ||
from abc import ABC, abstractproperty | ||
from typing import Any, Coroutine, Dict, Generator, List, Optional, Union | ||
from abc import ABC | ||
from typing import Any, Callable, Coroutine, Dict, Generator, List, Optional, Union | ||
|
||
from ...core.main import ChatMessage | ||
from ...models.main import ContinueBaseModel | ||
from ..util.count_tokens import DEFAULT_ARGS, count_tokens | ||
|
||
|
||
class LLM(ContinueBaseModel, ABC): | ||
requires_api_key: Optional[str] = None | ||
requires_unique_id: bool = False | ||
requires_write_log: bool = False | ||
title: Optional[str] = None | ||
system_message: Optional[str] = None | ||
|
||
context_length: int = 2048 | ||
"The maximum context length of the LLM in tokens, as counted by count_tokens." | ||
|
||
unique_id: Optional[str] = None | ||
"The unique ID of the user." | ||
|
||
model: str | ||
"The model name" | ||
|
||
prompt_templates: dict = {} | ||
|
||
write_log: Optional[Callable[[str], None]] = None | ||
"A function that takes a string and writes it to the log." | ||
|
||
api_key: Optional[str] = None | ||
"The API key for the LLM provider." | ||
|
||
class Config: | ||
arbitrary_types_allowed = True | ||
extra = "allow" | ||
|
||
def dict(self, **kwargs): | ||
original_dict = super().dict(**kwargs) | ||
original_dict.pop("write_log", None) | ||
original_dict["name"] = self.name | ||
original_dict["class_name"] = self.__class__.__name__ | ||
return original_dict | ||
|
||
@abstractproperty | ||
def name(self): | ||
"""Return the name of the LLM.""" | ||
raise NotImplementedError | ||
def collect_args(self, **kwargs) -> Any: | ||
"""Collect the arguments for the LLM.""" | ||
args = {**DEFAULT_ARGS.copy(), "model": self.model, "max_tokens": 1024} | ||
args.update(kwargs) | ||
return args | ||
|
||
async def start(self, *, api_key: Optional[str] = None, **kwargs): | ||
async def start( | ||
self, write_log: Callable[[str], None] = None, unique_id: Optional[str] = None | ||
): | ||
"""Start the connection to the LLM.""" | ||
raise NotImplementedError | ||
self.write_log = write_log | ||
self.unique_id = unique_id | ||
|
||
async def stop(self): | ||
"""Stop the connection to the LLM.""" | ||
raise NotImplementedError | ||
pass | ||
|
||
async def complete( | ||
async def _complete( | ||
self, prompt: str, with_history: List[ChatMessage] = None, **kwargs | ||
) -> Coroutine[Any, Any, str]: | ||
"""Return the completion of the text with the given temperature.""" | ||
raise NotImplementedError | ||
|
||
def stream_complete( | ||
def _stream_complete( | ||
self, prompt, with_history: List[ChatMessage] = None, **kwargs | ||
) -> Generator[Union[Any, List, Dict], None, None]: | ||
"""Stream the completion through generator.""" | ||
raise NotImplementedError | ||
|
||
async def stream_chat( | ||
async def _stream_chat( | ||
self, messages: List[ChatMessage] = None, **kwargs | ||
) -> Generator[Union[Any, List, Dict], None, None]: | ||
"""Stream the chat through generator.""" | ||
raise NotImplementedError | ||
|
||
def count_tokens(self, text: str): | ||
"""Return the number of tokens in the given text.""" | ||
raise NotImplementedError | ||
|
||
@abstractproperty | ||
def context_length(self) -> int: | ||
"""Return the context length of the LLM in tokens, as counted by count_tokens.""" | ||
raise NotImplementedError | ||
return count_tokens(self.model, text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.