Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from RexWzh/rex/wrap-chatter
allow process files
- Loading branch information
Showing
6 changed files
with
72 additions
and
9 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 |
---|---|---|
|
@@ -104,3 +104,5 @@ ENV/ | |
# IDE settings | ||
.vscode/ | ||
.idea/ | ||
*.json | ||
*.jsonl |
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,8 @@ | ||
|
||
from webchatter import WebChat, Node, process_messages | ||
from random import randint | ||
|
||
def test_process_msgs(): | ||
msgs = [f"find the result of {randint(3, 100)} + {randint(4, 100)}" for _ in range(4)] | ||
process_messages(msgs[:2], "test.jsonl") | ||
process_messages(msgs, "test.jsonl") |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os, json | ||
from webchatter import WebChat | ||
import tqdm, tqdm.notebook | ||
# from chattool import load_chats | ||
|
||
def process_messages(msgs, checkpoint:str, mode:str="delete", isjupyter:bool=False): | ||
"""Process the messages. | ||
Args: | ||
msgs (list): The messages. | ||
checkpoint (str): Store the checkpoint. | ||
mode (str, optional): One of the three mode: delete, repeat, newchat. Defaults to "delete". | ||
Returns: | ||
list: The processed messages. | ||
""" | ||
offset = 0 | ||
if os.path.exists(checkpoint): | ||
with open(checkpoint, 'r', encoding='utf-8') as f: | ||
processed = f.read().strip().split('\n') | ||
if len(processed) >= 1 and processed[0] != '': | ||
offset = len(processed) | ||
tq = tqdm.tqdm if not isjupyter else tqdm.notebook.tqdm | ||
with open(checkpoint, 'a', encoding='utf-8') as f: | ||
for ind in tq(range(offset, len(msgs))): | ||
msg = msgs[ind] | ||
chat = WebChat() | ||
ans = chat.ask(msg, keep=False) | ||
data = {"index":ind + offset, "chat_log":{"user":msg, "assistant":ans}} | ||
f.write(json.dumps(data) + '\n') | ||
return True | ||
|
||
def process_chats(chats, checkpoint:str): | ||
"""Process the chats. | ||
Args: | ||
chats (list): The chats. | ||
checkpoint (str): Store the checkpoint. | ||
Returns: | ||
list: The processed chats. | ||
""" | ||
# TODO | ||
return chats |
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