API References
Agent ¶
Bases: LlmAgent
LLM-based Agent with Volcengine capabilities.
This class represents an intelligent agent powered by LLMs (Large Language Models), integrated with Volcengine's AI framework. It supports memory modules, sub-agents, tracers, knowledge bases, and other advanced features for A2A (Agent-to-Agent) or user-facing scenarios.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
name |
str
|
The name of the agent. |
description |
str
|
A description of the agent, useful in A2A scenarios. |
instruction |
Union[str, InstructionProvider]
|
The instruction or instruction provider. |
model_name |
str
|
Name of the model used by the agent. |
model_provider |
str
|
Provider of the model (e.g., openai). |
model_api_base |
str
|
The base URL of the model API. |
model_api_key |
str
|
The API key for accessing the model. |
model_extra_config |
dict
|
Extra configurations to include in model requests. |
tools |
list[ToolUnion]
|
Tools available to the agent. |
sub_agents |
list[BaseAgent]
|
Sub-agents managed by this agent. |
knowledgebase |
Optional[KnowledgeBase]
|
Knowledge base attached to the agent. |
short_term_memory |
Optional[ShortTermMemory]
|
Session-based memory for temporary context. |
long_term_memory |
Optional[LongTermMemory]
|
Cross-session memory for persistent user context. |
tracers |
list[BaseTracer]
|
List of tracers used for telemetry and monitoring. |
源代码位于: veadk/agent.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
run_processor = Field(default=None, exclude=True)
class-attribute
instance-attribute
¶
Optional run processor for intercepting and processing agent execution flows.
The run processor can be used to implement cross-cutting concerns such as: - Authentication flows (e.g., OAuth2 via VeIdentity) - Request/response logging - Error handling and retry logic - Performance monitoring
If not provided, a NoOpRunProcessor will be used by default.
Example
from veadk.integrations.ve_identity import AuthRequestProcessor
agent = Agent( name="my-agent", run_processor=AuthRequestProcessor() )
run(prompt, stream=False, app_name='veadk_app', user_id='veadk_user', session_id='veadk_session', load_history_sessions_from_db=False, db_url='', collect_runtime_data=False, eval_set_id='', save_session_to_memory=False, run_processor=None)
async
¶
Running the agent. The runner and session service will be created automatically.
For production, consider using Google-ADK runner to run agent, rather than invoking this method.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
prompt
|
str | list[str]
|
The prompt to run the agent. |
必需 |
stream
|
bool
|
Whether to stream the output. Defaults to False. |
False
|
app_name
|
str
|
The name of the application. Defaults to "veadk_app". |
'veadk_app'
|
user_id
|
str
|
The id of the user. Defaults to "veadk_user". |
'veadk_user'
|
session_id
|
str
|
The id of the session. Defaults to "veadk_session". |
'veadk_session'
|
load_history_sessions_from_db
|
bool
|
Whether to load history sessions from database. Defaults to False. |
False
|
db_url
|
str
|
The url of the database. Defaults to "". |
''
|
collect_runtime_data
|
bool
|
Whether to collect runtime data. Defaults to False. |
False
|
eval_set_id
|
str
|
The id of the eval set. Defaults to "". |
''
|
save_session_to_memory
|
bool
|
Whether to save this turn session to memory. Defaults to False. |
False
|
run_processor
|
Optional[BaseRunProcessor]
|
Optional run processor to use for this run. If not provided, uses the agent's default run_processor. Defaults to None. |
None
|
源代码位于: veadk/agent.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
Runner ¶
Bases: Runner
VeADK Runner that augments ADK with session, memory, tracing, and media upload.
This class builds on Google ADK's Runner and adds:
- Integration with short-term memory (ShortTermMemory) for auto session management.
- Optional long-term memory integration and session persistence.
- New message interception and media upload to TOS.
- Tracing dump and Trace ID logging.
- A simplified run entry that supports multi-turn text/multimodal inputs.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
user_id |
str
|
Default user ID. |
long_term_memory |
Long-term memory service instance, or |
|
short_term_memory |
ShortTermMemory | None
|
Short-term memory instance used to auto-create/manage sessions. |
upload_inline_data_to_tos |
bool
|
Whether to upload inline media to TOS while running. |
session_service |
bool
|
Session service instance (may come from short-term memory). |
memory_service |
bool
|
Memory service instance (may come from agent's long-term memory). |
app_name |
str
|
Application name used in session management and object pathing. |
Note
This class wraps the parent run_async at initialization to insert media
upload and post-run handling. If you override the underlying run_async,
ensure it remains compatible with this interception logic.
源代码位于: veadk/runner.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 | |
__init__(agent=None, short_term_memory=None, app_name=None, user_id='veadk_default_user', upload_inline_data_to_tos=False, run_processor=None, *args, **kwargs)
¶
Initialize a Runner instance.
Selects the session service based on provided short-term memory or an
external session_service. If long-term memory or an external
memory_service is provided, the passed service is preferred. After
construction, it injects a message interception layer into the parent's
run_async to support inline media upload and post-run handling.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
agent
|
BaseAgent | Agent
|
The agent instance used to run interactions. |
None
|
short_term_memory
|
ShortTermMemory | None
|
Optional short-term memory; if
not provided and no external |
None
|
app_name
|
str
|
Application name. Defaults to |
None
|
user_id
|
str
|
Default user ID. Defaults to |
'veadk_default_user'
|
upload_inline_data_to_tos
|
bool
|
Whether to enable inline media upload. Defaults to |
False
|
run_processor
|
BaseRunProcessor | None
|
Optional run processor for intercepting agent execution. If not provided, will try to get from agent. If agent doesn't have one, uses NoOpRunProcessor. |
None
|
*args
|
Positional args passed through to |
()
|
|
**kwargs
|
Keyword args passed through to |
{}
|
返回:
| 类型 | 描述 |
|---|---|
None
|
None |
源代码位于: veadk/runner.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
get_trace_id()
¶
Get the Trace ID from the current agent's tracer.
If the agent is not a :class:veadk.agent.Agent or no tracer is configured,
returns "<unknown_trace_id>".
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
str
|
The Trace ID or |
源代码位于: veadk/runner.py
run(messages, user_id='', session_id=f'tmp-session-{formatted_timestamp()}', run_config=None, save_tracing_data=False, upload_inline_data_to_tos=False, run_processor=None)
async
¶
Run a conversation with multi-turn text and multimodal inputs.
When short-term memory is configured, a session is auto-created as needed.
Inputs are converted into ADK message format. If upload_inline_data_to_tos
is True, media upload is enabled temporarily for this run (does not change
the Runner's global setting).
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
messages
|
RunnerMessage
|
Input messages ( |
必需 |
user_id
|
str
|
Override default user ID; if empty, uses the constructed |
''
|
session_id
|
str
|
Session ID. Defaults to a timestamp-based temporary ID. |
f'tmp-session-{formatted_timestamp()}'
|
run_config
|
RunConfig | None
|
Run config; if |
None
|
save_tracing_data
|
bool
|
Whether to dump tracing data to disk after the run. Defaults to |
False
|
upload_inline_data_to_tos
|
bool
|
Whether to enable media upload only for this run. Defaults to |
False
|
run_processor
|
BaseRunProcessor | None
|
Optional run processor to use for this run. If not provided, uses the runner's default run_processor. Defaults to None. |
None
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
The textual output from the last event, if present; otherwise an empty string. |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
If an input contains an unsupported or unrecognized media type. |
AssertionError
|
If a media MIME type is not among |
Exception
|
Exceptions from the underlying ADK/Agent execution may propagate. |
源代码位于: veadk/runner.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
save_eval_set(session_id, eval_set_id='default')
async
¶
Save the current session as part of an evaluation set and return its path.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
session_id
|
str
|
Session ID. |
必需 |
eval_set_id
|
str
|
Evaluation set identifier. Defaults to |
'default'
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
str
|
The exported evaluation set file path. |
示例:
You can save the specific session as a evaluation set in Google ADK format.
import asyncio
from veadk import Agent, Runner
agent = Agent()
runner = Runner(agent=agent)
session_id = "session"
asyncio.run(runner.run(messages="Hi!", session_id=session_id))
path = runner.save_eval_set(session_id=session_id)
print(path)
源代码位于: veadk/runner.py
save_session_to_long_term_memory(session_id, user_id='', app_name='', **kwargs)
async
¶
Save the specified session to long-term memory.
If long_term_memory is not configured, the function logs a warning and returns.
It fetches the session from the session service and then calls the long-term memory's
add_session_to_memory for persistence.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
session_id
|
str
|
Session ID. |
必需 |
user_id
|
str
|
Optional; override default user ID. If empty, uses |
''
|
app_name
|
str
|
Optional; override default app name. If empty, uses |
''
|
示例:
You can save a specific session to long-term memory.
import asyncio
from veadk import Agent, Runner
from veadk.memory import LongTermMemory
APP_NAME = "app"
agent = Agent(long_term_memory=LongTermMemory(backend="local", app_name=APP_NAME))
session_id = "session"
runner = Runner(agent=agent, app_name=APP_NAME)
asyncio.run(runner.run(messages="Hi!", session_id=session_id))
asyncio.run(runner.save_session_to_long_term_memory(session_id=session_id))
源代码位于: veadk/runner.py
save_tracing_file(session_id)
¶
Dump tracing data to disk and return the last written path.
Only effective when the agent is one of Agent/SequentialAgent/ParallelAgent/LoopAgent and a tracer is configured; otherwise returns an empty string.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
session_id
|
str
|
Session ID used to associate the tracing with a session. |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
str
|
The tracing file path; returns an empty string on failure or when no tracer. |
示例:
You can save the tracing data to a local file.
import asyncio
from veadk import Agent, Runner
agent = Agent()
runner = Runner(agent=agent)
session_id = "session"
asyncio.run(runner.run(messages="Hi!", session_id=session_id))
path = runner.save_tracing_file(session_id=session_id)
print(path)
源代码位于: veadk/runner.py
ShortTermMemory ¶
Bases: BaseModel
Short term memory for agent execution.
The short term memory represents the context of the agent model. All content in the short term memory will be sent to agent model directly, including the system prompt, historical user prompt, and historical model responses.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
backend |
Literal['local', 'mysql', 'sqlite', 'postgresql', 'database']
|
The backend of short term memory:
- |
backend_configs |
dict
|
Configuration dict for init short term memory backend. |
db_url |
str
|
Database connection url for init short term memory backend.
For example, |
local_database_path |
str
|
Local database path, only used when |
after_load_memory_callback |
Callable | None
|
A callback to be called after loading memory from the backend. The callback function should accept |
示例:
In-memory simple memory¶
You can initialize a short term memory with in-memory storage:
from veadk import Agent, Runner
from veadk.memory.short_term_memory import ShortTermMemory
import asyncio
session_id = "veadk_playground_session"
agent = Agent()
short_term_memory = ShortTermMemory(backend="local")
runner = Runner(
agent=agent, short_term_memory=short_term_memory)
# This invocation will be stored in short-term memory
response = asyncio.run(runner.run(
messages="My name is VeADK", session_id=session_id
))
print(response)
# The history invocation can be fetched by model
response = asyncio.run(runner.run(
messages="Do you remember my name?", session_id=session_id # keep the same `session_id`
))
print(response)
Memory with a Database URL¶
Also you can use a databasae connection URL to initialize a short-term memory:
from veadk.memory.short_term_memory import ShortTermMemory
short_term_memory = ShortTermMemory(db_url="...")
Memory with SQLite¶
Once you want to start the short term memory with a local SQLite, you can specify the backend to sqlite. It will create a local database in local_database_path:
from veadk.memory.short_term_memory import ShortTermMemory
short_term_memory = ShortTermMemory(backend="sqlite", local_database_path="")
源代码位于: veadk/memory/short_term_memory.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
create_session(app_name, user_id, session_id)
async
¶
Create or retrieve a user session.
Short term memory can attempt to create a new session for a given application and user. If a session with the same session_id already exists, it will be returned instead of creating a new one.
If the underlying session service is backed by a database (DatabaseSessionService), the method first lists all existing sessions for the given app_name and user_id and logs the number of sessions found. It then checks whether a session with the specified session_id already exists:
- If it exists → returns the existing session.
- If it does not exist → creates and returns a new session.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
app_name
|
str
|
The name of the application associated with the session. |
必需 |
user_id
|
str
|
The unique identifier of the user. |
必需 |
session_id
|
str
|
The unique identifier of the session to be created or retrieved. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Session | None
|
Session | None: The retrieved or newly created |
示例:
Create a new session manually:
import asyncio
from veadk.memory import ShortTermMemory
app_name = "app_name"
user_id = "user_id"
session_id = "session_id"
short_term_memory = ShortTermMemory()
session = asyncio.run(
short_term_memory.create_session(
app_name=app_name, user_id=user_id, session_id=session_id
)
)
print(session)
session = asyncio.run(
short_term_memory.session_service.get_session(
app_name=app_name, user_id=user_id, session_id=session_id
)
)
print(session)
源代码位于: veadk/memory/short_term_memory.py
LongTermMemory ¶
Bases: BaseMemoryService, BaseModel
Manages long-term memory storage and retrieval for applications.
This class provides an interface to store, retrieve, and manage long-term contextual information using different backend types (e.g., OpenSearch, Redis). It supports configuration of the backend service and retrieval behavior.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
backend |
Union[Literal['local', 'opensearch', 'redis', 'viking', 'viking_mem', 'mem0'], BaseLongTermMemoryBackend]
|
The type or instance of the long-term memory backend. Defaults to "opensearch". |
backend_config |
dict
|
Configuration parameters for the selected backend. Defaults to an empty dictionary. |
top_k |
int
|
The number of top similar documents to retrieve during search. Defaults to 5. |
index |
str
|
The name of the index or collection used for storing memory items. Defaults to an empty string. |
app_name |
str
|
The name of the application that owns this memory instance. Defaults to an empty string. |
user_id |
str
|
Deprecated attribute. Retained for backward compatibility. Defaults to an empty string. |
Notes
Please ensure that you have set the embedding-related configurations in environment variables.
示例:
Simple long-term memory¶
Once create a long-term memory withou any arguments, all configurations are come from environment variables.
import asyncio
from veadk import Agent, Runner
from veadk.memory.long_term_memory import LongTermMemory
from veadk.memory.short_term_memory import ShortTermMemory
app_name = "veadk_playground_app"
user_id = "veadk_playground_user"
long_term_memory = LongTermMemory(backend="local", app_name=app_name)
agent = Agent(long_term_memory=long_term_memory)
runner = Runner(
agent=agent,
app_name=app_name,
user_id=user_id,
short_term_memory=ShortTermMemory(),
)
# ===== add memory =====
session_id = "veadk_playground_session"
teaching_prompt = "I brought an ice-cream last week."
asyncio.run(runner.run(messages=teaching_prompt, session_id=session_id))
asyncio.run(
runner.save_session_to_long_term_memory(session_id=session_id)
) # save session to long-term memory
# ===== check memory =====
session_id = "veadk_playground_session_2" # use a new session
student_prompt = "What I brought last week?"
response = asyncio.run(runner.run(messages=student_prompt, session_id=session_id))
print(response)
Create with a backend instance¶
from veadk.memory.long_term_memory import LongTermMemory
from veadk.memory.long_term_memory.backends import LongTermMemory
long_term_memory = LongTermMemory(backend=...)
Create with backend configurations¶
from veadk.memory.long_term_memory import LongTermMemory
long_term_memory = LongTermMemory(backend="", backend_config={})
源代码位于: veadk/memory/long_term_memory.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
add_session_to_memory(session, **kwargs)
async
¶
Add a chat session's events to the long-term memory backend.
This method extracts and filters the events from a given Session object,
converts them into serialized strings, and stores them into the long-term
memory system. It is typically called after a chat session ends or when
important contextual data needs to be persisted for future retrieval.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
session
|
Session
|
The session object containing user ID and a list of events to persist. |
必需 |
示例:
session = Session(
user_id="user_123",
events=[
Event(role="user", content="I like Go and Rust."),
Event(role="assistant", content="Got it! I'll remember that."),
]
)
await memory_service.add_session_to_memory(session)
# Logs:
# Adding 2 events to long term memory: index=main
# Added 2 events to long term memory: index=main, user_id=user_123
源代码位于: veadk/memory/long_term_memory.py
search_memory(*, app_name, user_id, query)
async
¶
Search memory entries for a given user and query.
This method queries the memory backend to retrieve the most relevant stored
memory chunks for a given user and text query. It then converts those raw
memory chunks into structured MemoryEntry objects to be returned to the caller.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
app_name
|
str
|
Name of the application requesting the memory search. |
必需 |
user_id
|
str
|
Unique identifier for the user whose memory is being queried. |
必需 |
query
|
str
|
The text query to match against stored memory content. |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
SearchMemoryResponse |
SearchMemoryResponse
|
An object containing a list of |
示例:
response = await memory_service.search_memory(
app_name="chat_app",
user_id="user_123",
query="favorite programming language"
)
for memory in response.memories:
print(memory.content.parts[0].text)
# Output:
# User likes Python and TypeScript for backend development.
源代码位于: veadk/memory/long_term_memory.py
KnowledgeBase ¶
Bases: BaseModel
A knowledge base for storing user-related information.
This class represents a knowledge base used to store and retrieve user-specific data. It supports multiple backend options, including in-memory, OpenSearch, Redis, and Volcengine's VikingDB. The knowledge base allows for efficient document retrieval based on similarity, with the ability to configure backend-specific settings.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
name |
str
|
The name of the knowledge base. Default is "user_knowledgebase". |
description |
str
|
A description of the knowledge base. Default is "This knowledgebase stores some user-related information." |
backend |
Union[Literal['local', 'opensearch', 'viking', 'redis'], BaseKnowledgebaseBackend]
|
The type of backend to use for storing and querying the knowledge base. Supported options include: - 'local' for in-memory storage (data is lost when the program exits). - 'opensearch' for OpenSearch (requires OpenSearch cluster). - 'viking' for Volcengine VikingDB (requires VikingDB service). - 'redis' for Redis with vector search capability (requires Redis). Default is 'local'. |
backend_config |
dict
|
Configuration dictionary for the selected backend. |
top_k |
int
|
The number of top similar documents to retrieve during a search. Default is 10. |
app_name |
str
|
The name of the application associated with the knowledge base. If index is not provided, this value will be set to |
index |
str
|
The name of the knowledge base index. |
Notes
Please ensure that you have set the embedding-related configurations in environment variables.
示例:
Simple backend¶
Create a local knowledgebase:
from veadk import Agent, Runner
from veadk.knowledgebase.knowledgebase import KnowledgeBase
from veadk.memory.short_term_memory import ShortTermMemory
app_name = "veadk_playground_app"
user_id = "veadk_playground_user"
session_id = "veadk_playground_session"
knowledgebase = KnowledgeBase(backend="opensearch", app_name=app_name)
knowledgebase.add_from_files(files=[knowledgebase_file])
agent = Agent(knowledgebase=knowledgebase)
runner = Runner(
agent=agent,
short_term_memory=ShortTermMemory(),
app_name=app_name,
user_id=user_id,
)
response = await runner.run(
messages="Tell me the secret of green.", session_id=session_id
)
print(response)
Initialize knowledgebase with metadata¶
from veadk.knowledgebase import KnowledgeBase
knowledgebase = KnowledgeBase(
name="user_data",
description="A knowledgebase contains user hobbies.",
index="my_app",
)
Initialize knowledgebase with backend instance¶
import veadk.config # noqa
from veadk.knowledgebase import KnowledgeBase
from veadk.knowledgebase.backends.in_memory_backend import InMemoryKnowledgeBackend
backend = InMemoryKnowledgeBackend(
index="my_app",
embedding_config=...,
)
knowledgebase = KnowledgeBase(
name="user_data",
description="A knowledgebase contains user hobbies.",
backend=backend,
)
Initialize knowledgebase with backend config¶
from veadk.knowledgebase import KnowledgeBase
knowledgebase = KnowledgeBase(
name="user_data",
description="A knowledgebase contains user hobbies.",
backend="local",
backend_config={"index": "user_app"},
)
源代码位于: veadk/knowledgebase/knowledgebase.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
__getattr__(name)
¶
In case of knowledgebase have no backends' methods (delete, list_chunks, etc)
For example, knowledgebase.delete(...) -> self._backend.delete(...)
源代码位于: veadk/knowledgebase/knowledgebase.py
add_from_directory(directory, **kwargs)
¶
Add knowledge from file path to knowledgebase.
Add the files in the directory to knowledgebase backend.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
directory
|
str
|
The directory path that needs to store. |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
bool |
bool
|
True if successfully store the knowledgebase, False otherwise. |
示例:
Store a directory to knowledgebase:
knowledgebase = Knowledgebase(backend="local")
if knowledgebase.add_from_directory("./knowledgebase"):
# add successfully
...
else:
raise RuntimeError("Uploaded directory failed.")
源代码位于: veadk/knowledgebase/knowledgebase.py
add_from_files(files, **kwargs)
¶
Add knowledge files to knowledgebase.
Add a list of files to knowledgebase backend.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
files
|
str
|
The list of files. |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
bool |
bool
|
True if successfully store the knowledgebase, False otherwise. |
示例:
Store files to knowledgebase:
knowledgebase = Knowledgebase(backend="local")
if knowledgebase.add_from_files("./knowledgebase"):
# add successfully
...
else:
raise RuntimeError("Uploaded files failed.")
源代码位于: veadk/knowledgebase/knowledgebase.py
add_from_text(text, **kwargs)
¶
Add a piece of text or a list of text to knowledgebase.
The text can be a string or a list of string. The text will be embedded and stored by the corresponding backend.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
text
|
str | list[str]
|
The text string or a list of text strings. |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
bool |
bool
|
True if successfully store the knowledgebase, False otherwise. |
示例:
Store a string or a list of string to knowledgebase:
knowledgebase = Knowledgebase(backend="local")
if knowledgebase.add_from_text("./knowledgebase"):
# add successfully
...
else:
raise RuntimeError("Uploaded text failed.")
源代码位于: veadk/knowledgebase/knowledgebase.py
search(query, top_k=0, **kwargs)
¶
Search knowledge from knowledgebase