লিনাক্স এবং ম্যাকওএস- এর জন্য LiteRT-LM-এর পাইথন এপিআই (উইন্ডোজ সাপোর্ট শীঘ্রই আসছে)। এতে মাল্টি-মোডালিটি এবং টুলস ব্যবহারের মতো ফিচারগুলো সমর্থিত, এবং জিপিইউ অ্যাক্সিলারেশন শীঘ্রই আসছে।
ভূমিকা
পাইথন এপিআই দিয়ে তৈরি একটি নমুনা টার্মিনাল চ্যাট অ্যাপ নিচে দেওয়া হলো:
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 একটি পাইথন লাইব্রেরি হিসেবে উপলব্ধ। আপনি PyPI থেকে এর নাইটলি সংস্করণটি ইনস্টল করতে পারেন:
# Using pip
pip install litert-lm-api-nightly
# Using uv
uv pip install litert-lm-api-nightly
১. ইঞ্জিন চালু করুন
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
২. একটি কথোপকথন তৈরি করুন
একটি 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
৩. বার্তা পাঠানো
আপনি সিনক্রোনাসলি অথবা অ্যাসিনক্রোনাসলি (স্ট্রিমিং) মেসেজ পাঠাতে পারেন।
সিঙ্ক্রোনাস উদাহরণ:
# 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()
৪. বহু-পদ্ধতি
# 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"])
৫. সরঞ্জামের সংজ্ঞা ও ব্যবহার
আপনি পাইথন ফাংশনগুলোকে এমন টুল হিসেবে সংজ্ঞায়িত করতে পারেন, যেগুলোকে মডেল স্বয়ংক্রিয়ভাবে কল করতে পারে।
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 মডেলটির জন্য টুল স্কিমা তৈরি করতে ফাংশনের ডকস্ট্রিং এবং টাইপ হিন্ট ব্যবহার করে।