[Change] Optimized ai API some.
[Fix] Few mem leaks.