LiteRT-LM-এর সুইফট এপিআই আপনাকে iOS এবং macOS অ্যাপ্লিকেশনগুলিতে নেটিভভাবে বড় ল্যাঙ্গুয়েজ মডেলগুলিকে একীভূত করার সুযোগ দেয়। মাল্টি-মোডালিটি , টুলের ব্যবহার এবং জিপিইউ অ্যাক্সিলারেশনের (মেটালের মাধ্যমে) মতো বৈশিষ্ট্যগুলি সম্পূর্ণরূপে সমর্থিত।
ভূমিকা
সুইফট এপিআই ব্যবহার করে একটি মডেল ইনিশিয়ালাইজ করা এবং মেসেজ পাঠানোর একটি উদাহরণ নিচে দেওয়া হলো:
import LiteRTLM
// 1. Initialize the Engine with your model
let config = try EngineConfig(
modelPath: "path/to/model.litertlm",
backend: .gpu, // Use .cpu() for CPU execution
cacheDir: NSTemporaryDirectory()
)
let engine = Engine(engineConfig: config)
try await engine.initialize()
// 2. Start a new Conversation
let conversation = try await engine.createConversation()
// 3. Send a message and print the response
let response = try await conversation.sendMessage(Message("What is the capital of France?"))
print(response.toString)
শুরু করা
এই বিভাগে আপনার অ্যাপ্লিকেশনে LiteRT-LM Swift API সংহত করার নির্দেশাবলী দেওয়া হয়েছে।
সুইফট প্যাকেজ ম্যানেজার (এসপিএম)
আপনি সুইফট প্যাকেজ ম্যানেজার ব্যবহার করে আপনার এক্সকোড প্রোজেক্টে LiteRT-LM সংহত করতে পারেন।
- Xcode-এ আপনার প্রজেক্টটি খুলুন এবং File > Add Package Dependencies...- এ যান।
- প্যাকেজ রিপোজিটরি URL লিখুন:
https://github.com/google-ai-edge/LiteRT-LM - আপনার অ্যাপ্লিকেশন টার্গেটে যোগ করতে LiteRTLM লাইব্রেরিটি নির্বাচন করুন।
আপনি যদি Package.swift ব্যবহার করে কোনো প্যাকেজ তৈরি করেন, তাহলে এটিকে আপনার dependencies-এ যোগ করুন:
dependencies: [
.package(url: "https://github.com/google-ai-edge/LiteRT-LM", from: "0.12.0")
]
কোর এপিআই গাইড
এই বিভাগে LiteRT-LM Swift API ব্যবহারের মৌলিক উপাদান এবং কার্যপ্রবাহ বিস্তারিতভাবে বর্ণনা করা হয়েছে, যার মধ্যে ইঞ্জিন প্রারম্ভিকীকরণ, কথোপকথন ব্যবস্থাপনা এবং বার্তা প্রেরণ অন্তর্ভুক্ত রয়েছে।
ইঞ্জিন চালু করুন
Engine মডেল লোডিং, রিসোর্স বরাদ্দ এবং লাইফসাইকেল ম্যানেজমেন্টের কাজ করে।
import LiteRTLM
let engineConfig = try EngineConfig(
modelPath: "path/to/your/model.litertlm",
backend: .gpu, // Use .gpu for Metal hardware acceleration
maxNumTokens: 512, // Size of the KV-cache
cacheDir: NSTemporaryDirectory() // Writable directory for compilation cache
)
let engine = Engine(engineConfig: engineConfig)
try await engine.initialize()
একটি কথোপকথন তৈরি করুন
একটি Conversation চ্যাটের ইতিহাস, সিস্টেম নির্দেশাবলী এবং স্যাম্পলার কনফিগারেশন পরিচালনা করে।
// Configure custom sampling parameters
let samplerConfig = try SamplerConfig(
topK: 40,
topP: 0.95,
temperature: 0.7
)
// Create the conversation config with system instructions
let config = ConversationConfig(
systemMessage: Message("You are a helpful assistant."),
samplerConfig: samplerConfig
)
let conversation = try await engine.createConversation(with: config)
বার্তা পাঠান
আপনি মডেলটির সাথে সিনক্রোনাসভাবে অথবা অ্যাসিনক্রোনাসভাবে (স্ট্রিমিং) ইন্টারঅ্যাক্ট করতে পারেন।
সিঙ্ক্রোনাস উদাহরণ
let response = try await conversation.sendMessage(Message("Hello!"))
print(response.toString)
অ্যাসিঙ্ক্রোনাস (স্ট্রিমিং) উদাহরণ
let message = Message("Tell me a long story.")
for try await chunk in conversation.sendMessageStream(message) {
// Output response chunks in real-time
print(chunk.toString, terminator: "")
}
print()
বহু-পদ্ধতি
ভিশন বা অডিও ফিচার ব্যবহার করতে হলে, ইঞ্জিন ইনিশিয়ালাইজেশনের সময় বিশেষায়িত ব্যাকএন্ডগুলো কনফিগার করে নিতে হবে।
let engineConfig = try EngineConfig(
modelPath: "path/to/multimodal_model.litertlm",
backend: .gpu,
visionBackend: .cpu(), // Enable CPU vision executor
audioBackend: .cpu(), // Enable CPU audio executor
cacheDir: NSTemporaryDirectory()
)
let engine = Engine(engineConfig: engineConfig)
try await engine.initialize()
চিত্র ইনপুট (দৃষ্টি)
ছবিটি পাথ অথবা র বাইট হিসেবে প্রদান করুন:
let imagePath = Bundle.main.path(forResource: "scenery", ofType: "jpg")!
let message = Message(contents: [
Content.imageFile(imagePath),
Content.text("Describe this image.")
])
let response = try await conversation.sendMessage(message)
print(response.toString)
অডিও ইনপুট
একটি অডিও পথ প্রদান করুন:
let audioPath = Bundle.main.path(forResource: "recording", ofType: "wav")!
let message = Message(contents: [
Content.audioFile(audioPath),
Content.text("Transcribe this recording.")
])
let response = try await conversation.sendMessage(message)
print(response.toString)
🔴 নতুন: মাল্টি-টোকেন প্রেডিকশন (MTP)
মাল্টি-টোকেন প্রেডিকশন (MTP) হলো একটি পারফরম্যান্স অপটিমাইজেশন যা ডিকোড করার গতি উল্লেখযোগ্যভাবে বাড়িয়ে দেয়। GPU/মেটাল ব্যাকএন্ড ব্যবহারকারী সকল কাজের জন্য এটি সর্বজনীনভাবে সুপারিশ করা হয়।
MTP ব্যবহার করতে, ইঞ্জিন চালু করার আগে এক্সপেরিমেন্টাল ফ্ল্যাগ-এ স্পেকুলেটিভ ডিকোডিং সক্রিয় করুন।
import LiteRTLM
// Opt into experimental APIs to configure MTP
ExperimentalFlags.optIntoExperimentalAPIs()
ExperimentalFlags.enableSpeculativeDecoding = true
let engineConfig = try EngineConfig(
modelPath: "path/to/model.litertlm",
backend: .gpu,
cacheDir: NSTemporaryDirectory()
)
let engine = Engine(engineConfig: engineConfig)
try await engine.initialize()
সরঞ্জামগুলি সংজ্ঞায়িত করুন এবং ব্যবহার করুন
আপনি সুইফট স্ট্রাকচারগুলোকে এমন টুল হিসেবে সংজ্ঞায়িত করতে পারেন, যেগুলোকে মডেল স্বয়ংক্রিয়ভাবে লজিক কার্যকর করার জন্য কল করতে পারে।
-
Toolপ্রোটোকল মেনে চলুন। -
@ToolParamপ্রপার্টি র্যাপার ব্যবহার করে প্যারামিটার ঘোষণা করুন। -
run()মেথডটি বাস্তবায়ন করুন।
import LiteRTLM
// 1. Define your custom tool
struct GetCurrentWeatherTool: Tool {
static let name = "get_current_weather"
static let description = "Get the current weather for a location."
@ToolParam(description: "The city and state, e.g. San Francisco, CA")
var location: String
@ToolParam(description: "The temperature unit to use (celsius or fahrenheit)")
var unit: String = "celsius"
func run() async throws -> Any {
// Call your weather API here
return [
"location": location,
"temperature": "22",
"unit": unit,
"condition": "sunny"
]
}
}
// 2. Register the tool in your conversation configuration
let config = ConversationConfig(
tools: [GetCurrentWeatherTool()]
)
let conversation = try await engine.createConversation(with: config)
// 3. The model will invoke the tool automatically if needed
let response = try await conversation.sendMessage(Message("What is the weather in Paris right now?"))
print(response.toString)