jerrita 9 months ago
parent
commit
325cae699c
3 changed files with 46 additions and 35 deletions
  1. 1 0
      .gitignore
  2. 44 35
      plugins/anon/glm.py
  3. 1 0
      requirements.txt

+ 1 - 0
.gitignore

@@ -4,3 +4,4 @@ __pycache__/
 
 plugins/corpus
 plugins/example
+storage/

+ 44 - 35
plugins/anon/glm.py

@@ -74,17 +74,24 @@ class GlmClient:
             logger.info(f'GlmClient: drop session, id: {id}')
             del self.cache[id]
             res += '\n\n-----Session Cut off-----'
-        return res
+        return res.replace('王子权', 'sajo').replace('王子', 'xuanchen')
 
 
 class GlmPlugin(Plugin):
     _client: GlmClient
     _group_shared_cache: bool
+    brif = 'CG の 语言模型插件'
+    usage = 'Usage: glm [query] | glm set/get [option] [value] | glm clear\n' \
+        + 'Options:\n' \
+        + '   - cut [int]: set max cut length\n' \
+        + '   - sys [str]: set system prompt\n' \
+        + '   - gc [bool]: enable group shared cache'
+    keywords = ['glm']
 
     async def on_load(self):
         self._client = GlmClient()
         self.storage = Storage('glm')
-        if self.storage['system'] is not None:
+        if self.storage['conf'] is not None:
             logger.info('GlmPlugin: load cache')
             self._client.system = self.storage['system']
             self._group_shared_cache = self.storage['group_shared_cache']
@@ -98,52 +105,54 @@ class GlmPlugin(Plugin):
             self.storage['system'] = self._client.system
             logger.info('GlmPlugin: init cache')
 
-    async def on_event(self, event: MessageEvent):
+    async def on_cmd(self, event: MessageEvent, args):
         rid = event.sender.user_id
         if isinstance(event, GroupMessage) and self._group_shared_cache:
             rid = event.gid
 
-        if event.raw.startswith('glm'):
+        if len(args) < 1 or args[1] not in ['set', 'get', 'clear', 'help']:
             query = event.raw[3:].strip()
             logger.info(f'GlmQuery: {query}')
             await event.reply(await self._client.generate_response(rid, query))
-        if event.raw.startswith('glset'):
-            cmds = event.raw[5:].strip().split(' ', 1)
-            cmd = cmds[0]
-            if len(cmds) > 1:
-                args = cmds[1].split(' ')
-            else:
-                args = []
-
-            logger.info(f'GlmSet: {cmd} {args}')
-            if cmd == 'help':
-                await event.reply(
-                    'glset commands:\n'
-                    '   - cutlen [int]: set max cut length\n'
-                    '   - s/sys [str]: set system prompt\n'
-                    '   - g/sys: get system prompt\n'
-                    '   - clear: clear cache\n'
-                    '   - sw: switch group shared cache'
-                )
-            if cmd == 'cutlen':
-                self._client.max_cut_length = int(args[0])
+            return False
+
+        if args[1] == 'clear':
+            self._client.cache.clear()
+            await event.reply('cache cleared')
+        elif args[1] == 'help':
+            return True
+        elif args[1] == 'set':
+            if len(args) < 4:
+                return True
+            elif args[2] == 'cut':
+                self._client.max_cut_length = int(args[3])
                 self.storage['max_cut_length'] = self._client.max_cut_length
-                await event.reply(f'max_cut_length set to {args[0]}')
-            if cmd == 's/sys':
-                self._client.system = ' '.join(args)
+                await event.reply(f'max_cut_length set to {args[3]}')
+            elif args[2] == 'sys':
+                self._client.system = ' '.join(args[3:]).replace(
+                    '王子权', 'xuanchen').replace('王子', 'xuanchen')
                 self._client.cache.clear()
                 self.storage['system'] = self._client.system
-                await event.reply(f'system prompt set to: {" ".join(args)}')
-            if cmd == 'g/sys':
-                await event.reply(f'system prompt: {self._client.system}')
-            if cmd == 'clear':
-                self._client.cache.clear()
-                await event.reply('cache cleared')
-            if cmd == 'sw':
-                self._group_shared_cache = not self._group_shared_cache
+                await event.reply(f'system prompt set to: {" ".join(args[3:])}')
+            elif args[2] == 'gc':
+                self._group_shared_cache = args[3] == 'true'
                 self.storage['group_shared_cache'] = self._group_shared_cache
                 await event.reply(
                     f'group_shared_cache set to {self._group_shared_cache}')
+            else:
+                return True
+        else:
+            if len(args) < 3:
+                return True
+            elif args[2] == 'cut':
+                await event.reply(f'max_cut_length: {self._client.max_cut_length}')
+            elif args[2] == 'sys':
+                await event.reply(f'system prompt: {self._client.system}')
+            elif args[2] == 'gc':
+                await event.reply(
+                    f'group_shared_cache: {self._group_shared_cache}')
+            else:
+                return True
 
 
 PluginManager().register_plugin(GlmPlugin([MessageEvent]))

+ 1 - 0
requirements.txt

@@ -0,0 +1 @@
+zhipuai