Runner
adk-runner से निष्पादन रनटाइम जो एजेंट निष्पादन को व्यवस्थित करता है।
अवलोकन
Runner एजेंट निष्पादन के संपूर्ण जीवनचक्र का प्रबंधन करता है:
- सेशन प्रबंधन (सेशन बनाना/पुनः प्राप्त करना)
- मेमोरी इंजेक्शन (प्रासंगिक मेमोरीज को खोजना और इंजेक्ट करना)
- आर्टिफैक्ट हैंडलिंग (स्कोप किए गए आर्टिफैक्ट तक पहुंच)
- इवेंट स्ट्रीमिंग (इवेंट्स को प्रोसेस करना और अग्रेषित करना)
- एजेंट ट्रांसफर (मल्टी-एजेंट हैंडऑफ़ को संभालना)
स्थापना
[dependencies]
adk-runner = "0.2.0"
RunnerConfig
आवश्यक सेवाओं के साथ Runner को कॉन्फ़िगर करें:
use adk_runner::{Runner, RunnerConfig};
use adk_session::InMemorySessionService;
use adk_artifact::InMemoryArtifactService;
use std::sync::Arc;
let config = RunnerConfig {
app_name: "my_app".to_string(),
agent: Arc::new(my_agent),
session_service: Arc::new(InMemorySessionService::new()),
artifact_service: Some(Arc::new(InMemoryArtifactService::new())),
memory_service: None,
run_config: None,
};
let runner = Runner::new(config)?;
कॉन्फ़िगरेशन फ़ील्ड्स
| Field | Type | आवश्यक | विवरण |
|---|---|---|---|
app_name | String | हाँ | एप्लिकेशन पहचानकर्ता |
agent | Arc<dyn Agent> | हाँ | निष्पादित करने के लिए रूट एजेंट |
session_service | Arc<dyn SessionService> | हाँ | सेशन स्टोरेज बैकएंड |
artifact_service | Option<Arc<dyn ArtifactService>> | नहीं | आर्टिफैक्ट स्टोरेज |
memory_service | Option<Arc<dyn Memory>> | नहीं | दीर्घकालिक मेमोरी |
run_config | Option<RunConfig> | नहीं | निष्पादन विकल्प |
एजेंट्स को चलाना
उपयोगकर्ता इनपुट के साथ एक एजेंट को निष्पादित करें:
use adk_core::Content;
use futures::StreamExt;
let user_content = Content::new("user").with_text("Hello!");
let mut stream = runner.run(
"user-123".to_string(),
"session-456".to_string(),
user_content,
).await?;
while let Some(event) = stream.next().await {
match event {
Ok(e) => {
if let Some(content) = e.content() {
for part in &content.parts {
if let Some(text) = part.text() {
print!("{}", text);
}
}
}
}
Err(e) => eprintln!("Error: {}", e),
}
}
निष्पादन प्रवाह
┌─────────────────────────────────────────────────────────────┐
│ Runner.run() │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ 1. Session Retrieval │
│ │
│ SessionService.get(app_name, user_id, session_id) │
│ → Creates new session if not exists │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ 2. Agent Selection │
│ │
│ Check session state for active agent │
│ → Use root agent or transferred agent │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ 3. Context Creation │
│ │
│ InvocationContext with: │
│ - Session (mutable) │
│ - Artifacts (scoped to session) │
│ - Memory (if configured) │
│ - Run config │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ 4. Agent Execution │
│ │
│ agent.run(ctx) → EventStream │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ 5. Event Processing │
│ │
│ For each event: │
│ - Update session state │
│ - Handle transfers │
│ - Forward to caller │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ 6. Session Save │
│ │
│ SessionService.append_event(session, events) │
└─────────────────────────────────────────────────────────────┘
InvocationContext
एजेंटों को निष्पादन के दौरान प्रदान किया गया संदर्भ:
pub trait InvocationContext: CallbackContext {
/// The agent being executed
fn agent(&self) -> Arc<dyn Agent>;
/// Memory service (if configured)
fn memory(&self) -> Option<Arc<dyn Memory>>;
/// Current session
fn session(&self) -> &dyn Session;
/// Execution configuration
fn run_config(&self) -> &RunConfig;
/// Signal end of invocation
fn end_invocation(&self);
/// Check if invocation has ended
fn ended(&self) -> bool;
}
RunConfig
निष्पादन विकल्प:
pub struct RunConfig {
/// Streaming mode for responses
pub streaming_mode: StreamingMode,
}
pub enum StreamingMode {
/// No streaming, return complete response
None,
/// Server-Sent Events (default)
SSE,
/// Bidirectional streaming (realtime)
Bidi,
}
ध्यान दें:
max_turnsऔरinclude_historyजैसे अतिरिक्त फ़ील्ड भविष्य के रिलीज़ के लिए नियोजित हैं।
एजेंट स्थानांतरण
Runner स्वचालित रूप से मल्टी-एजेंट स्थानांतरणों को संभालता है:
// In an agent's tool or callback
if should_transfer {
// Set transfer in event actions
ctx.set_actions(EventActions {
transfer_to_agent: Some("specialist_agent".to_string()),
..Default::default()
});
}
Runner करेगा:
- इवेंट में स्थानांतरण अनुरोध का पता लगाएं
- sub_agents में लक्षित एजेंट ढूंढें
- नई सक्रिय एजेंट के साथ सेशन स्थिति अपडेट करें
- नए एजेंट के साथ निष्पादन जारी रखें
Launcher के साथ एकीकरण
Launcher आंतरिक रूप से Runner का उपयोग करता है:
// Launcher creates Runner with default services
Launcher::new(agent)
.app_name("my_app")
.run()
.await?;
// Equivalent to:
let runner = Runner::new(RunnerConfig {
app_name: "my_app".to_string(),
agent,
session_service: Arc::new(InMemorySessionService::new()),
artifact_service: Some(Arc::new(FileArtifactService::new("./artifacts")?)),
memory_service: None,
run_config: None,
})?;
कस्टम Runner उपयोग
उन्नत परिदृश्यों के लिए, सीधे Runner का उपयोग करें:
use adk_runner::{Runner, RunnerConfig};
use adk_session::DatabaseSessionService;
use adk_artifact::S3ArtifactService;
use adk_memory::QdrantMemoryService;
// Production configuration
let config = RunnerConfig {
app_name: "production_app".to_string(),
agent: my_agent,
session_service: Arc::new(DatabaseSessionService::new(db_pool)),
artifact_service: Some(Arc::new(S3ArtifactService::new(s3_client))),
memory_service: Some(Arc::new(QdrantMemoryService::new(qdrant_client))),
run_config: None, // Uses default SSE streaming
};
let runner = Runner::new(config)?;
// Use in HTTP handler
async fn chat_handler(runner: &Runner, request: ChatRequest) -> Response {
let stream = runner.run(
request.user_id,
request.session_id,
request.content,
).await?;
// Stream events to client
Response::sse(stream)
}
पिछला: ← कोर प्रकार | अगला: Launcher →