-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor summarize, added /save command
- Loading branch information
Showing
6 changed files
with
101 additions
and
32 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
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,51 +1,37 @@ | ||
import logging | ||
from functools import lru_cache | ||
|
||
import openai | ||
|
||
from ..llm import summarize as _summarize | ||
from ..message import Message, format_msgs | ||
from ..util import len_tokens | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@lru_cache(maxsize=100) | ||
def _llm_summarize(content: str) -> str: | ||
"""Summarizes a long text using a LLM algorithm.""" | ||
response = openai.Completion.create( | ||
model="text-davinci-003", | ||
prompt="Please summarize the following:\n" + content + "\n\nSummary:", | ||
temperature=0, | ||
max_tokens=256, | ||
) | ||
summary = response.choices[0].text | ||
logger.debug( | ||
f"Summarized long output ({len_tokens(content)} -> {len_tokens(summary)} tokens): " | ||
+ summary | ||
) | ||
return summary | ||
|
||
|
||
def summarize(msg: Message | list[Message]) -> Message: | ||
"""Uses a cheap LLM to summarize long outputs.""" | ||
# construct plaintext from message(s) | ||
msgs = msg if isinstance(msg, list) else [msg] | ||
content = "\n".join(format_msgs(msgs)) | ||
summary = _summarize(content) | ||
summary = _summarize_helper(content) | ||
# construct message from summary | ||
summary_msg = Message( | ||
role="system", content=f"Summary of the conversation:\n{summary})" | ||
) | ||
return summary_msg | ||
|
||
|
||
def _summarize(s: str) -> str: | ||
if len_tokens(s) > 200: | ||
# first 100 tokens | ||
beginning = " ".join(s.split()[:150]) | ||
# last 100 tokens | ||
end = " ".join(s.split()[-100:]) | ||
summary = _llm_summarize(beginning + "\n...\n" + end) | ||
@lru_cache(maxsize=128) | ||
def _summarize_helper(s: str, tok_max_start=500, tok_max_end=500) -> str: | ||
""" | ||
Helper function for summarizing long outputs. | ||
Trims long outputs to 200 tokens, then summarizes. | ||
""" | ||
if len_tokens(s) > tok_max_start + tok_max_end: | ||
beginning = " ".join(s.split()[:tok_max_start]) | ||
end = " ".join(s.split()[-tok_max_end:]) | ||
summary = _summarize(beginning + "\n...\n" + end) | ||
else: | ||
summary = _llm_summarize(s) | ||
summary = _summarize(s) | ||
return summary |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from gptme.logmanager import LogManager, Message | ||
|
||
|
||
def test_get_last_code_block(): | ||
# tests that the last code block is indeed returned, with the correct formatting | ||
log = LogManager() | ||
log.append( | ||
Message( | ||
"assistant", | ||
""" | ||
```python | ||
print('hello') | ||
``` | ||
```python | ||
print('world') | ||
``` | ||
""", | ||
) | ||
) | ||
assert log.get_last_code_block() == "print('world')\n" |