Skip to content

Commit

Permalink
feat: implement hybrid message processing with Rust and Python integr…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
HsiangNianian committed Feb 4, 2025
1 parent 17f1f0b commit 2710e4b
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 111 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"version": "0.2.0",
"configurations": [

{
"name": "Python: Current File",
"type": "python",
Expand Down
101 changes: 84 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ crate-type = ["cdylib"]
[dependencies]
image = "0.25.5"
num-complex = "0.4.6"
pyo3 = { version = "0.19.2", features = ["extension-module", "abi3-py39"] }
pyo3 = { version = "0.19.2", features = [
"extension-module",
"abi3-py39",
"serde",
] }
rand = "0.8.5"
regex = "1.11.1"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
time = "0.3.36"
35 changes: 35 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
rule_file: "rules.yaml"
platforms:
- type: WeChat
name: "企业微信"
config:
api_key: "your_key"

- type: Slack
name: "Slack生产环境"
config:
token: "xoxb-123"

# rules.yaml
rules:
- name: 技术支持请求
when:
contains: "帮助"
or:
contains: "怎么操作"
then:
- type: reply
content: "请访问帮助中心:help.example.com"
- type: forward
platform: "Slack"
channel: "support-channel"

- name: 紧急情况处理
when:
contains: "紧急"
platform: ["wechat", "slack"]
then:
- type: webhook
url: "https://alert.example.com/notify"
- type: reply
content: "已收到您的紧急请求,正在处理中..."
File renamed without changes.
88 changes: 88 additions & 0 deletions examples/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from iamai._core import RuleEngine, process_message, PyMessage
from typing import List, Dict
import json


class HybridMessage(PyMessage):
"""Python-Rust共享消息结构"""

def to_json(self) -> str:
return json.dumps(
{
"content": self.content,
"platform": self.platform,
"user_id": self.user_id,
}
)


class HybridRuleEngine:
def __init__(self):
self.rust_engine = RuleEngine()
self.py_plugins: List[object] = []

def add_regex_rule(self, pattern: str):
"""添加高性能正则规则到Rust引擎"""
self.rust_engine.add_regex_rule(pattern)

def add_py_plugin(self, plugin: object):
"""添加Python插件"""
self.py_plugins.append(plugin)

def process(self, msg: HybridMessage) -> List[str]:
"""混合处理流程"""
responses = []

# Rust侧处理
rust_resp = process_message(msg)
responses.append(rust_resp)

# Rust正则匹配
matched_rules = self.rust_engine.match_message(msg)
for rule in matched_rules:
responses.append(f"[Rust规则匹配] {rule}")

# Python插件处理
for plugin in self.py_plugins:
if hasattr(plugin, "match_rule") and plugin.match_rule(msg):
responses.append(plugin.execute_action(msg))

return responses


# math_plugin.py
class MathPlugin:
def match_rule(self, msg: HybridMessage) -> bool:
return msg.content.startswith("计算")

def execute_action(self, msg: HybridMessage) -> str:
try:
expr = msg.content[2:].strip()
return f"计算结果: {expr} = {eval(expr)}"
except:
return "计算失败"


import time
from iamai._core import HybridMessage

# 创建测试消息
msg = HybridMessage(content="计算2+2", platform="test", user_id="perf")

# 纯Python版本
start = time.time()
for _ in range(10_000):
MathPlugin().execute_action(msg)
py_time = time.time() - start

# Rust混合版本
from iamai._core import process_message

start = time.time()
for _ in range(10_000):
process_message(msg)
rs_time = time.time() - start

print(f"Python耗时: {py_time:.4f}s")
print(f"Rust耗时: {rs_time:.4f}s")
print(f"性能提升: {(py_time/rs_time):.1f}x")
Empty file added examples/mathplugin.py
Empty file.
Empty file added examples/test.py
Empty file.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies = [
"loguru>=0.7.2",
"polib>=1.2.0",
"pydantic>=2.10.1",
"pyyaml>=6.0.2",
"requests>=2.32.3",
"rich>=13.9.4",
"scikit-learn>=1.6.1",
Expand Down
1 change: 1 addition & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod rule;
8 changes: 8 additions & 0 deletions src/engine/rule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use pyo3::prelude::*;

#[pyclass]
struct Rule {
condition: PyObject,
action: PyObject,
priority: i32,
}
Loading

0 comments on commit 2710e4b

Please sign in to comment.