ข้ามไปที่เนื้อหา

Code Snippets

รูปแบบที่พร้อมใช้งานสำหรับงานที่พบบ่อยที่สุดของ OpenJarvis แต่ละ snippet เป็นชุดสมบูรณ์ในตัวเอง คัดลอกไปวางได้เลย

ถามคำถาม (3 บรรทัด)

from openjarvis import Jarvis

with Jarvis() as j:
    print(j.ask("What is the capital of France?"))

สตรีม Token (4 บรรทัด)

import asyncio
from openjarvis import Jarvis

async def main():
    with Jarvis() as j:
        async for token in j.ask_stream("Tell me a story"):
            print(token, end="", flush=True)

asyncio.run(main())

เอเจนต์พร้อม Tools (5 บรรทัด)

from openjarvis import Jarvis

with Jarvis() as j:
    result = j.ask_full(
        "Search the web for the latest Python release",
        agent="orchestrator",
        tools=["web_search", "think"],
    )
    print(result["content"])

Memory: Index + ค้นหา (6 บรรทัด)

from openjarvis import Jarvis

with Jarvis() as j:
    j.memory.index("./docs/", chunk_size=512)
    results = j.memory.search("deployment options")
    for r in results:
        print(f"[{r['score']:.3f}] {r['content'][:100]}")

Recipe TOML (4 บรรทัด)

กำหนด agent pipeline ด้วย TOML — ไม่ต้องเขียนโค้ด:

[recipe]
name = "research_assistant"
agent = "orchestrator"
tools = ["web_search", "think", "file_read"]
prompt = "Research the given topic and write a summary."

รันด้วย: jarvis compose run research_assistant "quantum computing advances"

เซิร์ฟเวอร์ API (1 คำสั่ง)

jarvis serve --port 8000 --engine ollama --model qwen3:8b

client ที่เข้ากันได้กับ OpenAI ใดๆ ก็ใช้งานกับ endpoint นี้ได้

Deploy ด้วย Docker (2 คำสั่ง)

docker build -t openjarvis .
docker run -p 8000:8000 openjarvis serve --host 0.0.0.0

Tool กำหนดเอง (10 บรรทัด)

from openjarvis.core.registry import ToolRegistry
from openjarvis.core.types import ToolResult
from openjarvis.tools._stubs import BaseTool, ToolSpec

@ToolRegistry.register("my_tool")
class MyTool(BaseTool):
    tool_id = "my_tool"

    @property
    def spec(self):
        return ToolSpec(name="my_tool", description="My custom tool",
                        parameters={"type": "object", "properties": {"input": {"type": "string"}}})

    def execute(self, **params):
        return ToolResult(tool_name="my_tool", content=f"Processed: {params.get('input', '')}", success=True)

การจัดเส้นทางแบบหลายโมเดล (5 บรรทัด)

from openjarvis import Jarvis

j = Jarvis()
# Router automatically selects the best model per query
simple = j.ask("What is 2+2?")            # routes to fast/cheap model
complex = j.ask("Analyze this research paper...")  # routes to capable model
j.close()

การยืนยันแบบมีคนร่วมตัดสินใจ (Human-in-the-Loop) (6 บรรทัด)

from openjarvis import Jarvis

with Jarvis() as j:
    result = j.ask_full(
        "Delete old log files in /tmp",
        agent="orchestrator",
        tools=["shell_exec", "file_read"],
    )
    print(f"Agent took {result['turns']} turns")
    print(result["content"])

Tool อย่าง shell_exec ตั้งค่า requires_confirmation: true ใน TOML ได้ เพื่อให้ต้องขออนุมัติแบบโต้ตอบก่อนทำงาน