MiniChain 是一个用于使用大型语言模型进行编码的开源小型库。
这个轻量级的Python对话AI框架,专注于让任何人都可以快速上手创建对话机器人。核心代码以及后端实现总共 500 多行代码。
几十这么 Mini ,也有自己的内部功能模块化设计思想,可以灵活组合和扩展不同模块。
废话不多说直接介绍功能。
OpenAI 调用
定义下面方法,输入一个单词,然后让 OpenAI 判断一下这是不是一个颜色:
@prompt(OpenAI())
def color_prompt(model, input):
return model(f"Answer 'Yes' if this is a color, {input}. Answer:")
使用 run 方法测试执行下:
if color_prompt("blue").run() == "Yes":
print("It's a color")
就可以快乐的运行了。
使用模板
你在编写的时候可以使用 Jinja 语言的模板来格式化你的提示,这样就不用把提示和代码混在一起了。
@prompt(OpenAI(), template_file="math.pmpt.tpl")
def math_prompt(model, question):
return model(dict(question=question))
可视化 UI
MiniChain 有一个内置的提示可视化系统,使用 Gradio. 如果您构造一个调用提示链的函数,您可以通过调用show和来可视化它。
show(math_demo,
examples=["What is the sum of the powers of 3 (3^i) that are smaller than 100?",
"What is the sum of the 10 first positive integers?"],
subprompts=[math_prompt, python],
out_type="markdown").queue().launch()
实现 Memory
MiniChain 并不构建显式的有状态内存类。可以使用队列快速的实现 Memory 的能力。
比如下面用简单的 List 来实现对话 Memory 功能。
@dataclass
class State:
memory: List[Tuple[str, str]]
human_input: str = ""
def push(self, response: str) -> "State":
memory = self.memory if len(self.memory) < MEMORY_LIMIT else self.memory[1:]
return State(memory + [(self.human_input, response)])
工具和代理
MiniChain 同样不提供这样的概念,也不区分 LLM 和 Tools,所有你要知道的只是一个概念:后端。
• OpenAI 是一个后端,
• Python 是一个后端,
• Bash 是一个后端
• ...
当然,你把他理解成 工具也行。
你可以简单的把想要接入的后端(工具),当做参数传入 prompt 注解即可。
@prompt([Python(), Bash()])
def math_prompt(model, input, lang):
return model(input, tool_num= 0 if lang == "python" else 1)
当然,实现自己的后端非常容易。
例子
最后跟大家一起看一下官方刚开始的例子,仔细看下执行和模板的过程:
from minichain import show, prompt, OpenAI, Python, GradioConf
import gradio as gr
@prompt(OpenAI(), template_file="math.pmpt.tpl",
gradio_conf=GradioConf(block_input=gr.Markdown))
def math_prompt(model, question):
"Prompt to call GPT with a Jinja template"
return model(dict(question=question))
@prompt(Python(), template="import math\n{{code}}")
def python(model, code):
"Prompt to call Python interpreter"
code = "\n".join(code.strip().split("\n")[1:-1])
return model(dict(code=code))
def math_demo(question):
"Chain them together"
return python(math_prompt(question))
比如输入:
小于 100 的 3 (3^i) 次幂之和是多少?
返回:
55
其内部的执行过程如下:
看明白了是对的 Prompt 工程实现很好的借鉴。
具体执行分析
先执行 math_prompt,
加载 math.pmpt.tpl,模板内容如下:
#### Question:
* What is 37593 * 67?
#### Code:
```python
print(37593 * 67)
```
#### Question:
* Janet's ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers' market?
#### Code:
```python
print((16-3-4)*2)
```
#### Question:
* How many of the integers between 0 and 99 inclusive are divisible by 8?
#### Code:
```python
count = 0
for i in range(0, 99+1):
if i % 8 == 0: count += 1
print(count)
```
#### Question:
* A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?
#### Code:
```python
print(2 + 2/2)
```
#### Question:
* {{question}}
#### Code:
```
Math
```python
{
"string": "```python\nprint(sum(range(1,11)))\n```"
}
```
```shell
#### Question:
* What is 37593 * 67?
#### Code:
给 OpenAI 之后输出:
{
"string": "```python\nprint(sum(range(1,11)))\n```"
}
然后把输出扔给 Python 后端执行:
import math
print(sum(range(1,11)))
最终的返回
55
总结
MiniChain 是一个很简单的项目,其设计思想和符合 AI 时代的 Prompt 工程和简化思想,比起 LangChain 的那么多概念,用着不要太爽。
如果早点儿看到 MiniChain,我可能都不会拿 LangChain 去写东西。就像之前以为国外网友说的那样,用 OpenAI 原生接口把 基于LangChain 的项目重写了, 发现最终工作量少了很多。
--- END ---

