Skip to content

Commit

Permalink
add get method
Browse files Browse the repository at this point in the history
  • Loading branch information
luyaxi committed Oct 17, 2024
1 parent f46b5bb commit 85648aa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions codelinker/events/processer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ def unlisten(self, func: callable):

def wait(self, tags: ChannelTag | Iterable[ChannelTag]):
return self.sink.wait(tags)

def get(self,tag: ChannelTag, return_dumper: Callable|Literal['str','json','identity'] = 'str'):
"""Get last event in the channel."""
def tag_filter(event: SEvent):
if tag is None:
return True
if isinstance(tag, ChannelTag):
if tag in event.tags:
return True
return False
if tag in event.tags:
return True
return False


gathered_events = list(filter(tag_filter, self.sink.all_events))
if isinstance(return_dumper,str):
match return_dumper:
case 'str':
return_dumper = str
case 'json':
import json
return_dumper = partial(json.dumps, ensure_ascii=False,sort_keys=False)
case 'identity':
return_dumper = lambda x: x

assert callable(return_dumper), "return_dumper must be a callable'"

event = gathered_events[-1]
if event.source == self.name:
return {'role': 'assistant', 'content': event.content}
else:
return {'role': 'user', 'content': return_dumper(event)}


def gather(self, tags: ChannelTag | Iterable[ChannelTag] | None = None, return_dumper: Callable|Literal['str','json','identity'] = 'str') -> Iterable[dict]:
def tag_filter(event: SEvent):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "codelinker"
version = "0.3.23"
version = "0.3.24"
description = "A tool to link the code with large language models."
authors = ["luyaxi <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 85648aa

Please sign in to comment.