Python SDK¶
Python SDK ของ OpenJarvis มี interface ระดับสูงสำหรับโต้ตอบกับ inference
engine ในเครื่อง, จัดการ memory และรันเวิร์กโฟลว์ของเอเจนต์ จุดเริ่มต้นหลัก
คือคลาส Jarvis
การติดตั้ง¶
Quick Start¶
from openjarvis import Jarvis
j = Jarvis()
response = j.ask("What is the capital of France?")
print(response)
j.close()
คลาส Jarvis¶
Constructor¶
Jarvis(
*,
config: JarvisConfig | None = None,
config_path: str | None = None,
engine_key: str | None = None,
model: str | None = None,
)
| พารามิเตอร์ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
config |
JarvisConfig |
None |
ระบุ configuration object ที่สร้างไว้ล่วงหน้า |
config_path |
str |
None |
Path ไปยังไฟล์ configuration แบบ TOML |
engine_key |
str |
None |
Override engine backend ("ollama", "vllm" ฯลฯ) |
model |
str |
None |
Override โมเดลเริ่มต้น (เช่น "qwen3:8b") |
หากไม่ระบุ config หรือ config_path SDK จะโหลด configuration จากตำแหน่ง
เริ่มต้น (~/.openjarvis/config.toml) และ fallback ไปใช้ค่าเริ่มต้นในตัว
ตัวอย่าง:
# Default configuration — auto-detects engine
j = Jarvis()
# Override the model
j = Jarvis(model="qwen3:8b")
# Override the engine
j = Jarvis(engine_key="ollama")
# Load from a specific config file
j = Jarvis(config_path="/path/to/config.toml")
Properties¶
| Property | ชนิด | คำอธิบาย |
|---|---|---|
config |
JarvisConfig |
Configuration object ที่กำลังใช้งานอยู่ |
version |
str |
สตริงเวอร์ชันของ OpenJarvis |
memory |
MemoryHandle |
Proxy สำหรับการทำงานกับ memory |
Method ask()¶
ส่งคำถามและรับคำตอบแบบข้อความล้วน
ask(
query: str,
*,
model: str | None = None,
agent: str | None = None,
tools: list[str] | None = None,
temperature: float = 0.7,
max_tokens: int = 1024,
context: bool = True,
) -> str
| พารามิเตอร์ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
query |
str |
-- | คำถามหรือ prompt ที่จะส่ง |
model |
str |
None |
Override โมเดลสำหรับการเรียกนี้ |
agent |
str |
None |
จัดเส้นทางผ่านเอเจนต์ ("simple", "orchestrator") |
tools |
list[str] |
None |
ชื่อ tool ที่จะเปิดใช้งาน (ต้องใช้โหมด agent) |
temperature |
float |
0.7 |
Temperature ของ sampling |
max_tokens |
int |
1024 |
จำนวน token สูงสุดที่จะสร้าง |
context |
bool |
True |
จะแทรกบริบทจาก memory หรือไม่ |
คืนค่า: str ที่มีข้อความคำตอบของโมเดล
ตัวอย่าง:
# Simple query
response = j.ask("What is machine learning?")
# Override model for this call
response = j.ask("Hello", model="llama3.2:3b")
# Disable memory context injection
response = j.ask("Tell me about Python", context=False)
# Adjust generation parameters
response = j.ask("Write a haiku", temperature=0.3, max_tokens=50)
Method ask_full()¶
ส่งคำถามและรับ dictionary ผลลัพธ์แบบละเอียดพร้อม metadata
ask_full(
query: str,
*,
model: str | None = None,
agent: str | None = None,
tools: list[str] | None = None,
temperature: float = 0.7,
max_tokens: int = 1024,
context: bool = True,
) -> dict[str, Any]
พารามิเตอร์เหมือนกับ ask() ทุกประการ
คืนค่า: dictionary ที่มีคีย์ดังนี้:
| คีย์ | ชนิด | คำอธิบาย |
|---|---|---|
content |
str |
ข้อความคำตอบ |
usage |
dict |
การใช้ token (prompt_tokens, completion_tokens, total_tokens) |
model |
str |
โมเดลที่ใช้ |
engine |
str |
Engine backend ที่ใช้ |
| คีย์ | ชนิด | คำอธิบาย |
|---|---|---|
content |
str |
ข้อความคำตอบ |
usage |
dict |
การใช้ token (อาจว่างเปล่าในโหมด agent) |
tool_results |
list[dict] |
ผลลัพธ์การรัน tool |
turns |
int |
จำนวนรอบของเอเจนต์ที่ใช้ |
model |
str |
โมเดลที่ใช้ |
engine |
str |
Engine backend ที่ใช้ |
ตัวอย่าง:
result = j.ask_full("What is 2+2?")
print(result["content"]) # "4"
print(result["model"]) # "qwen3:8b"
print(result["engine"]) # "ollama"
print(result["usage"]) # {"prompt_tokens": 10, ...}
โหมด Agent¶
ส่งพารามิเตอร์ agent เพื่อจัดเส้นทางคำถามผ่านเอเจนต์ เอเจนต์จัดการ
บทสนทนาแบบหลายรอบและใช้ tool ได้
# Simple agent — single turn, no tools
response = j.ask("Hello", agent="simple")
# Orchestrator agent — multi-turn with tool calling
response = j.ask(
"What is sqrt(144) + 3^2?",
agent="orchestrator",
tools=["calculator", "think"],
)
เมื่อใช้โหมด agent กับ ask_full() ผลลัพธ์จะมี tool_results แสดงการเรียก
ใช้ tool แต่ละครั้ง:
result = j.ask_full(
"Calculate 15% of 340",
agent="orchestrator",
tools=["calculator"],
)
print(result["content"]) # "15% of 340 is 51.0"
print(result["turns"]) # 2
print(result["tool_results"])
# [{"tool_name": "calculator", "content": "51.0", "success": True}]
เอเจนต์ที่มีให้ใช้: simple, orchestrator, operative, monitor_operative
Tool ที่มีให้ใช้: calculator, think, retrieval, llm, file_read
MemoryHandle¶
attribute Jarvis.memory มี MemoryHandle สำหรับการ index เอกสาร, ค้นหา และ
สถิติ memory backend จะถูก initialize แบบ lazy ในการใช้งานครั้งแรก
index()¶
Index ไฟล์หรือไดเรกทอรีเข้า memory store
| พารามิเตอร์ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
path |
str |
-- | Path ของไฟล์หรือไดเรกทอรีที่จะ index |
chunk_size |
int |
512 |
ขนาด chunk เป็น token |
chunk_overlap |
int |
64 |
ส่วนที่ทับซ้อนระหว่าง chunk เป็น token |
คืนค่า: dictionary ที่มี chunks (จำนวน), doc_ids (รายการ) และ path
result = j.memory.index("./docs/")
print(f"Indexed {result['chunks']} chunks")
# Indexed 42 chunks
# Custom chunking parameters
result = j.memory.index("./notes/", chunk_size=256, chunk_overlap=32)
search()¶
ค้นหา memory store หา chunk ที่เกี่ยวข้อง
| พารามิเตอร์ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
query |
str |
-- | คำค้นหา |
top_k |
int |
5 |
จำนวนผลลัพธ์ที่จะคืน |
คืนค่า: รายการ dictionary แต่ละอันมี content, score, source และ metadata
results = j.memory.search("neural networks")
for r in results:
print(f"[{r['score']:.4f}] {r['source']}: {r['content'][:80]}...")
stats()¶
คืนสถิติของ memory backend
คืนค่า: dictionary ที่มี backend (ชื่อ) และ count (จำนวนเอกสาร ถ้ามี)
close()¶
คืนทรัพยากรของ memory backend
การค้นหาโมเดลและ Engine¶
list_models()¶
คืนรายการ identifier ของโมเดลที่มีอยู่บน engine ที่ใช้งานอยู่
list_engines()¶
คืนรายการคีย์ของ engine ที่ลงทะเบียนไว้
การจัดการทรัพยากร¶
close()¶
คืนทรัพยากรทั้งหมดที่ instance ของ Jarvis ถืออยู่ รวมถึง memory backend,
telemetry store และการเชื่อมต่อ engine
รูปแบบ Context Manager
แม้ Jarvis จะไม่ได้ implement __enter__/__exit__ โดยตรง คุณควรเรียก close() เสมอเมื่อใช้งานเสร็จ เพื่อคืน connection ฐานข้อมูลและทรัพยากรอื่นๆ:
ตัวอย่างแบบสมบูรณ์¶
from openjarvis import Jarvis
# Initialize with auto-detected engine
j = Jarvis(model="qwen3:8b")
# Index documents for context-augmented responses
result = j.memory.index("./docs/")
print(f"Indexed {result['chunks']} chunks from {result['path']}")
# Simple query with memory context
response = j.ask("What are the main features?")
print(response)
# Detailed query with agent and tools
full_result = j.ask_full(
"Calculate the square root of 256 and add 10",
agent="orchestrator",
tools=["calculator"],
)
print(f"Answer: {full_result['content']}")
print(f"Turns: {full_result['turns']}")
print(f"Tools used: {[t['tool_name'] for t in full_result['tool_results']]}")
# Search memory directly
results = j.memory.search("configuration")
for r in results:
print(f" [{r['score']:.3f}] {r['source']}")
# List available models
print("Models:", j.list_models())
# Clean up
j.close()