LiteRT-LM Python API

Linux और macOS के लिए LiteRT-LM का Python API (Windows के लिए यह सुविधा जल्द ही उपलब्ध होगी). मल्टी-मॉडल और टूल इस्तेमाल करने जैसी सुविधाएं काम करती हैं. हालांकि, जीपीयू ऐक्सेलरेटेड सुविधा जल्द ही उपलब्ध होगी.

परिचय

यहां Python API की मदद से बनाए गए टर्मिनल चैट ऐप्लिकेशन का एक उदाहरण दिया गया है:

import litert_lm

litert_lm.set_min_log_severity(litert_lm.LogSeverity.ERROR) # Hide log for TUI app

with litert_lm.Engine("path/to/model.litertlm") as engine:
  with engine.create_conversation() as conversation:
    while True:
      user_input = input("\n>>> ")
      for chunk in conversation.send_message_async(user_input):
        print(chunk["content"][0]["text"], end="", flush=True)

शुरू करें

LiteRT-LM, Python लाइब्रेरी के तौर पर उपलब्ध है. PyPI से, Nightly वर्शन इंस्टॉल किया जा सकता है:

# Using pip
pip install litert-lm-api-nightly

# Using uv
uv pip install litert-lm-api-nightly

1. इंजन को शुरू करना

Engine, एपीआई का एंट्री पॉइंट है. यह मॉडल को लोड करने और संसाधनों को मैनेज करने का काम करता है. इसे कॉन्टेक्स्ट मैनेजर (with स्टेटमेंट के साथ) के तौर पर इस्तेमाल करने से, यह पक्का होता है कि नेटिव रिसॉर्स तुरंत रिलीज़ हो जाएं.

ध्यान दें: इंजन को शुरू करने में कुछ सेकंड लग सकते हैं, ताकि मॉडल लोड हो सके.

import litert_lm

# Initialize with the model path and optionally specify the backend.
# backend can be Backend.CPU (default). GPU support is upcoming.
with litert_lm.Engine(
    "path/to/your/model.litertlm",
    backend=litert_lm.Backend.CPU,
    # Optional: Pick a writable dir for caching compiled artifacts.
    # cache_dir="/tmp/litert-lm-cache"
) as engine:
    # ... Use the engine to create a conversation ...
    pass

2. बातचीत शुरू करना

Conversation, मॉडल के साथ आपके इंटरैक्शन की स्थिति और इतिहास को मैनेज करता है.

# Optional: Configure system instruction and initial messages
messages = [
    {"role": "system", "content": [{"type": "text", "text": "You are a helpful assistant."}]},
]

# Create the conversation
with engine.create_conversation(messages=messages) as conversation:
    # ... Interact with the conversation ...
    pass

3. मैसेज भेजना

मैसेज को सिंक्रोनस या असिंक्रोनस (स्ट्रीमिंग) तरीके से भेजा जा सकता है.

सिंक्रोनस का उदाहरण:

# Simple string input
response = conversation.send_message("What is the capital of France?")
print(response["content"][0]["text"])

# Or with full message structure
# response = conversation.send_message({"role": "user", "content": "..."})

एसिंक्रोनस (स्ट्रीमिंग) का उदाहरण:

# sendMessageAsync returns an iterator of response chunks
stream = conversation.send_message_async("Tell me a long story.")
for chunk in stream:
    # Chunks are dictionaries containing pieces of the response
    for item in chunk.get("content", []):
      if item.get("type") == "text":
        print(item["text"], end="", flush=True)
print()

4. मल्टी-मोडैलिटी

# Initialize with vision and/or audio backends if needed
with litert_lm.Engine(
    "path/to/multimodal_model.litertlm",
    audio_backend=litert_lm.Backend.CPU,
    # vision_backend=litert_lm.Backend.CPU, (GPU support is upcoming)
) as engine:
    with engine.create_conversation() as conversation:
        user_message = {
            "role": "user",
            "content": [
                {"type": "audio", "path": "/path/to/audio.wav"},
                {"type": "text", "text": "Describe this audio."},
            ],
        }
        response = conversation.send_message(user_message)
        print(response["content"][0]["text"])

5. टूल तय करना और उनका इस्तेमाल करना

Python फ़ंक्शन को ऐसे टूल के तौर पर तय किया जा सकता है जिन्हें मॉडल अपने-आप कॉल कर सकता है.

def add_numbers(a: float, b: float) -> float:
    """Adds two numbers.

    Args:
        a: The first number.
        b: The second number.
    """
    return a + b

# Register the tool in the conversation
tools = [add_numbers]
with engine.create_conversation(tools=tools) as conversation:
    # The model will call add_numbers automatically if it needs to sum values
    response = conversation.send_message("What is 123 + 456?")
    print(response["content"][0]["text"])

LiteRT-LM, मॉडल के लिए टूल स्कीमा जनरेट करने के लिए, फ़ंक्शन के डॉकस्ट्रिंग और टाइप हिंट का इस्तेमाल करता है.