-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional command prefix #2
Open
james-choncholas
wants to merge
2
commits into
williamkray:main
Choose a base branch
from
james-choncholas:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ def do_update(self, helper: ConfigUpdateHelper) -> None: | |
helper.copy("name") | ||
helper.copy("allowed_users") | ||
helper.copy("addl_context") | ||
helper.copy("command_prefix") | ||
|
||
|
||
class GPTPlugin(Plugin): | ||
|
||
|
@@ -45,6 +47,7 @@ async def on_message(self, event: MessageEvent) -> None: | |
user = '' | ||
content = '' | ||
timestamp = datetime.today().strftime('%Y-%m-%d %H:%M:%S') | ||
command_prefix = self.config['command_prefix'] | ||
|
||
if event.sender == self.client.mxid: | ||
role = 'assistant' | ||
|
@@ -54,13 +57,18 @@ async def on_message(self, event: MessageEvent) -> None: | |
user = self.client.parse_user_id(event.sender)[0] + ': ' # only use the localpart | ||
|
||
# keep track of all messages, even if the bot sent them | ||
self.prev_room_events[event.room_id].append({"role": role , "content": | ||
self.prev_room_events[event.room_id].append({"role": role , "content": | ||
user + event['content']['body']}) | ||
|
||
# if the bot sent the message or another command was issued, just pass | ||
if event.sender == self.client.mxid or event.content.body.startswith('!'): | ||
# if the bot sent the message just pass | ||
if event.sender == self.client.mxid: | ||
return | ||
|
||
if len(command_prefix) > 0 and not event.content.body.startswith(f"!{command_prefix}"): | ||
return # ignore a comand does not match the required prefix | ||
if len(command_prefix) == 0 and event.content.body.startswith(f'!'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of this could be replaced by if not command_prefix or not event.content.body.startswith(f'!{command_prefix}'):
return |
||
return # ignore all commands when prefix is disabled | ||
|
||
joined_members = await self.client.get_joined_members(event.room_id) | ||
|
||
try: | ||
|
@@ -78,7 +86,7 @@ async def on_message(self, event: MessageEvent) -> None: | |
system_prompt = {"role": "system", "content": prompt} | ||
|
||
await event.mark_read() | ||
|
||
context = self.prev_room_events.get(event.room_id, []) | ||
# if our short history is already at max capacity, drop the oldest messages | ||
# to make room for our more important system prompt(s) | ||
|
@@ -108,7 +116,7 @@ async def on_message(self, event: MessageEvent) -> None: | |
# Call the chatGPT API to get a response | ||
await self.client.set_typing(event.room_id, timeout=99999) | ||
response = await self._call_gpt(context) | ||
|
||
# Send the response back to the chat room | ||
await self.client.set_typing(event.room_id, timeout=0) | ||
await event.respond(f"{response}") | ||
|
@@ -140,7 +148,7 @@ async def _call_gpt(self, prompt): | |
"messages": full_context, | ||
"max_tokens": self.config['max_tokens'] | ||
} | ||
|
||
async with self.http.post( | ||
GPT_API_URL, headers=headers, data=json.dumps(data) | ||
) as response: | ||
|
@@ -154,7 +162,7 @@ async def _call_gpt(self, prompt): | |
#self.log.debug(content) | ||
return content | ||
|
||
@command.new(name='gpt', help='control chatGPT functionality', require_subcommand=True) | ||
@command.new(name=f"self.config['command_prefix']", help='control chatGPT functionality', require_subcommand=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The f-string here is wrong and unecessary. It should just be |
||
async def gpt(self, evt: MessageEvent) -> None: | ||
pass | ||
|
||
|
@@ -163,9 +171,7 @@ async def clear_cache(self, evt: MessageEvent) -> None: | |
self.prev_room_events.pop(evt.room_id) | ||
await evt.react('✅') | ||
|
||
|
||
@classmethod | ||
def get_config_class(cls) -> Type[BaseProxyConfig]: | ||
return Config | ||
|
||
|
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 |
---|---|---|
|
@@ -7,4 +7,4 @@ main_class: GPTPlugin | |
config: true | ||
extra_files: | ||
- base-config.yaml | ||
database: false | ||
database: false |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be interesting to strip the config value to avoid "strange" behavior.