Skip to content
tinAI
Go back

GitHub - pydantic/monty: Trình thông dịch Python tối giản và an toàn viết bằng Rust phục vụ AI

Bài gốc: GitHub - pydantic/monty: A minimal, secure Python interpreter written in Rust for use by AI

Tác giả: Unknown

Ngày đăng: Dịch ngày:

TL;DR

Monty là một trình thông dịch Python viết bằng Rust cho phép chạy mã do AI sinh ra một cách an toàn và hiệu quả. Nó có tốc độ khởi động rất nhanh và có thể kiểm soát việc sử dụng tài nguyên, thích hợp cho các tác vụ cần thực thi mã từ AI.

Monty: Trình thông dịch Python tối giản và an toàn bằng Rust

CI Badge Codspeed Badge

Giới thiệu

Monty là một trình thông dịch Python tối giản và an toàn, được viết bằng ngôn ngữ lập trình Rust, phục vụ cho các tác vụ thực thi mã do Trí tuệ Nhân tạo tạo ra. Monty giúp tránh sự phức tạp và thời gian chờ của sandbox dựa trên container.

Tính năng nổi bật của Monty:

Hạn chế của Monty:

Tại sao nên dùng Monty?

Monty giúp cho LLMs (Mô hình ngôn ngữ lớn) làm việc nhanh hơn, rẻ hơn và tin cậy hơn bằng cách cho phép chúng viết mã Python thay vì sử dụng gọi công cụ truyền thống. Điều này có thể thấy qua các sản phẩm từ Cloudflare, Anthropic và Hugging Face.

Sử dụng Monty

Python

from typing import Any
import pydantic_monty

code = '''
async def agent(prompt: str, messages: Messages):
 while True:
 print(f'messages so far: {messages}')
 output = await call_llm(prompt, messages)
 if isinstance(output, str):
 return output
 messages.extend(output)

await agent(prompt, [])
'''

...

if __name__ == '__main__':
 import asyncio
 asyncio.run(main())

Rust

use monty::{MontyRun, MontyObject, NoLimitTracker, StdPrint};

let code = r#"
def fib(n):
 if n <= 1:
 return n
 return fib(n - 1) + fib(n - 2)

fib(x)
"#;

let runner = MontyRun::new(code.to_owned(), "fib.py", vec!["x".to_owned()], vec![]).unwrap();
let result = runner.run(vec![MontyObject::Int(10)], NoLimitTracker, &mut StdPrint).unwrap();
assert_eq!(result, MontyObject::Int(55));

PydanticAI Integration

Monty sẽ hỗ trợ ‘code-mode’ trong Pydantic AI, cho phép LLM viết mã Python để gọi các công cụ của bạn một cách an toàn.

from pydantic_ai import Agent
from pydantic_ai.toolsets.code_mode import CodeModeToolset
from pydantic_ai.toolsets.function import FunctionToolset
from typing_extensions import TypedDict

class WeatherResult(TypedDict):
 city: str
 temp_c: float
 conditions: str

...

result = agent.run_sync(
 'Compare the weather and population of London, Paris, and Tokyo.'
)
print(result.output)

Alternatives

TechLanguage completenessSecurityStart latencyCostSetup complexityFile mountingSnapshotting
Montypartialstrict0.06msfreeeasyeasyeasy
Dockerfullgood195msfreeintermediateeasyintermediate
Pyodidefullpoor2800msfreeintermediateeasyhard

Monty giải quyết nhiều vấn đề mà các công nghệ hiện tại không thể bằng cách mang lại hiệu suất vượt trội và sự an toàn cao trong việc thực thi mã AI.


Read Original (EN) Quay lại Newsletter