LiteRT-LM Swift API

LiteRT-LM का Swift API, लार्ज लैंग्वेज मॉडल को iOS और macOS ऐप्लिकेशन में नेटिव तौर पर इंटिग्रेट करने की सुविधा देता है. एक से ज़्यादा मोड में काम करने, टूल इस्तेमाल करने, और जीपीयू की मदद से तेज़ी से काम करने (Metal के ज़रिए) जैसी सुविधाओं का पूरी तरह से इस्तेमाल किया जा सकता है.

परिचय

यहां मॉडल को शुरू करने और मैसेज भेजने के लिए, Swift API का इस्तेमाल करने का एक उदाहरण दिया गया है:

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 को अपने ऐप्लिकेशन में इंटिग्रेट करने का तरीका बताया गया है.

Swift Package Manager (SPM)

Swift Package Manager का इस्तेमाल करके, LiteRT-LM को अपने Xcode प्रोजेक्ट में इंटिग्रेट किया जा सकता है.

  1. Xcode में अपना प्रोजेक्ट खोलें और File > Add Package Dependencies... पर जाएं
  2. पैकेज रिपॉज़िटरी का यूआरएल डालें: https://github.com/google-ai-edge/LiteRT-LM
  3. अपने ऐप्लिकेशन टारगेट में जोड़ने के लिए, LiteRTLM लाइब्रेरी चुनें.

अगर Package.swift का इस्तेमाल करके कोई पैकेज बनाया जा रहा है, तो उसे अपनी डिपेंडेंसी में जोड़ें:

dependencies: [
  .package(url: "https://github.com/google-ai-edge/LiteRT-LM", from: "0.12.0")
]

Core API Guide

इस सेक्शन में, 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()

इमेज इनपुट (Vision)

पाथ या रॉ बाइट के तौर पर इमेज दें:

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)

🔴 नई सुविधा: मल्टी-टोकन प्रेडिक्शन (एमटीपी)

मल्टी-टोकन प्रेडिक्शन (एमटीपी), परफ़ॉर्मेंस को ऑप्टिमाइज़ करने की एक तकनीक है. इससे डिकोड करने की स्पीड काफ़ी बढ़ जाती है. यह GPU/Metal बैकएंड का इस्तेमाल करने वाले सभी टास्क के लिए, हर जगह सुझाया जाता है.

एमटीपी का इस्तेमाल करने के लिए, इंजन को शुरू करने से पहले एक्सपेरिमेंटल फ़्लैग में स्पेकुलेटिव डिकोडिंग चालू करें.

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()

टूल को परिभाषित और इस्तेमाल करना

Swift स्ट्रक्चर को ऐसे टूल के तौर पर तय किया जा सकता है जिन्हें मॉडल, लॉजिक को लागू करने के लिए अपने-आप कॉल कर सकता है.

  1. Tool प्रोटोकॉल के मुताबिक हो.
  2. @ToolParam प्रॉपर्टी रैपर का इस्तेमाल करके, पैरामीटर घोषित करें.
  3. 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)