Python SDK#
客户端#
LlamaDeploy Python 客户端。
客户端提供了对 asyncio 和非 asyncio API 的访问。要访问同步 API,只需使用 client.sync
的方法。
使用示例
from llama_deploy.client import Client
# Use the same client instance
c = Client()
async def an_async_function():
status = await client.apiserver.status()
def normal_function():
status = client.sync.apiserver.status()
源代码位于 llama_deploy/client/client.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
API Server 功能#
SessionCollection #
基类: Collection
表示给定 Deployment 的 Session 集合的模型。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
deployment_id
|
str
|
|
必需 |
源代码位于 llama_deploy/client/models/apiserver.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
delete async
#
delete(session_id: str) -> None
删除具有提供的 session_id
的会话。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
session_id
|
str
|
将要删除的会话 ID |
必需 |
引发
类型 | 描述 |
---|---|
HTTPException
|
如果找不到具有提供的 ID 的会话。 |
源代码位于 llama_deploy/client/models/apiserver.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
create async
#
create() -> SessionDefinition
源代码位于 llama_deploy/client/models/apiserver.py
44 45 46 47 48 49 50 51 52 53 54 55 |
|
list async
#
list() -> list[SessionDefinition]
返回给定部署中所有会话的集合。
源代码位于 llama_deploy/client/models/apiserver.py
57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
Task #
基类: Model
表示给定部署中给定会话所属任务的模型。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
deployment_id
|
str
|
|
必需 |
session_id
|
str
|
|
必需 |
源代码位于 llama_deploy/client/models/apiserver.py
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 |
|
results async
#
results() -> TaskResult
返回给定任务的结果。
源代码位于 llama_deploy/client/models/apiserver.py
78 79 80 81 82 83 84 85 86 87 88 89 |
|
send_event async
#
send_event(ev: Event, service_name: str) -> EventDefinition
发送人工响应事件。
源代码位于 llama_deploy/client/models/apiserver.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
events async
#
events() -> AsyncGenerator[dict[str, Any], None]
返回一个生成器对象,用于消费从服务流式传输的事件。
源代码位于 llama_deploy/client/models/apiserver.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
TaskCollection #
基类: Collection
表示给定部署的任务集合的模型。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
deployment_id
|
str
|
|
必需 |
源代码位于 llama_deploy/client/models/apiserver.py
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 |
|
run async
#
run(task: TaskDefinition) -> Any
运行一个任务并在完成后返回结果。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
task
|
TaskDefinition
|
我们想要运行的任务定义。 |
必需 |
源代码位于 llama_deploy/client/models/apiserver.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
create async
#
create(task: TaskDefinition) -> Task
运行一个任务并立即返回,不等待结果。
源代码位于 llama_deploy/client/models/apiserver.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
list async
#
返回此集合中的任务列表。
源代码位于 llama_deploy/client/models/apiserver.py
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 |
|
Deployment #
基类: Model
表示部署的模型。
源代码位于 llama_deploy/client/models/apiserver.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
DeploymentCollection #
基类: Collection
表示当前活动部署集合的模型。
源代码位于 llama_deploy/client/models/apiserver.py
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 |
|
create async
#
create(config: TextIO, reload: bool = False) -> Deployment
从部署文件创建一个新的部署。
如果 reload
为 true,则将重新加载现有部署,否则将引发错误。
示例
with open("deployment.yml") as f:
await client.apiserver.deployments.create(f)
源代码位于 llama_deploy/client/models/apiserver.py
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 |
|
get async
#
get(id: str) -> Deployment
按 ID 获取部署。
源代码位于 llama_deploy/client/models/apiserver.py
256 257 258 259 260 261 262 263 264 265 266 267 |
|
ApiServer #
基类: Model
表示 API 服务器实例的模型。
源代码位于 llama_deploy/client/models/apiserver.py
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 |
|
status async
#
status() -> Status
返回 API 服务器的状态。
源代码位于 llama_deploy/client/models/apiserver.py
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 |
|
控制平面功能#
Session #
基类: Model
源代码位于 llama_deploy/client/models/core.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 |
|
run async
#
run(service_name: str, **run_kwargs: Any) -> str
实现会话基于工作流的 run API。
源代码位于 llama_deploy/client/models/core.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
run_nowait async
#
run_nowait(service_name: str, **run_kwargs: Any) -> str
实现会话基于工作流的 run API,但不等待任务完成。
源代码位于 llama_deploy/client/models/core.py
38 39 40 41 42 43 44 45 |
|
create_task async
#
create_task(task_def: TaskDefinition) -> str
在此会话中创建一个新任务。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
task_def
|
Union[str, TaskDefinition]
|
任务定义或输入字符串。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
str |
str
|
创建的任务 ID。 |
源代码位于 llama_deploy/client/models/core.py
47 48 49 50 51 52 53 54 55 56 |
|
get_task_result async
#
get_task_result(task_id: str) -> TaskResult | None
如果任务存在结果,则在此会话中获取该任务的结果。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
task_id
|
str
|
要获取结果的任务 ID。 |
必需 |
返回
类型 | 描述 |
---|---|
TaskResult | None
|
Optional[TaskResult]: 任务的结果(如果存在),否则为 None。 |
源代码位于 llama_deploy/client/models/core.py
65 66 67 68 69 70 71 72 73 74 |
|
get_tasks async
#
get_tasks() -> list[TaskDefinition]
获取此会话中的所有任务。
返回
类型 | 描述 |
---|---|
list[TaskDefinition]
|
list[TaskDefinition]: 会话中的任务定义列表。 |
源代码位于 llama_deploy/client/models/core.py
85 86 87 88 89 90 91 92 93 |
|
send_event async
#
send_event(service_name: str, task_id: str, ev: Event) -> None
向工作流服务发送事件。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
event
|
Event
|
要提交给工作流的事件。 |
必需 |
返回
类型 | 描述 |
---|---|
None
|
None |
源代码位于 llama_deploy/client/models/core.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
send_event_def async
#
send_event_def(task_id: str, ev_def: EventDefinition) -> None
向工作流服务发送事件。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
event
|
Event
|
要提交给工作流的事件。 |
必需 |
返回
类型 | 描述 |
---|---|
None
|
None |
源代码位于 llama_deploy/client/models/core.py
112 113 114 115 116 117 118 119 120 121 122 |
|
get_task_result_stream async
#
get_task_result_stream(task_id: str) -> AsyncGenerator[dict[str, Any], None]
如果任务存在结果,则在此会话中获取该任务的结果。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
task_id
|
str
|
要获取结果的任务 ID。 |
必需 |
返回
类型 | 描述 |
---|---|
AsyncGenerator[dict[str, Any], None]
|
AsyncGenerator[str, None, None]: 生成任务结果的生成器。 |
源代码位于 llama_deploy/client/models/core.py
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 |
|
SessionCollection #
基类: Collection
源代码位于 llama_deploy/client/models/core.py
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 |
|
list async
#
返回集合中所有会话的列表。
源代码位于 llama_deploy/client/models/core.py
161 162 163 164 165 166 167 168 169 |
|
create async
#
create() -> Session
创建一个新会话并返回一个 Session 对象。
返回
名称 | 类型 | 描述 |
---|---|---|
Session |
Session
|
表示新创建会话的 Session 对象。 |
源代码位于 llama_deploy/client/models/core.py
171 172 173 174 175 176 177 |
|
get async
#
get(id: str) -> Session
按 ID 获取会话。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
session_id
|
要获取的会话 ID。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
Session |
Session
|
表示指定会话的 Session 对象。 |
引发
类型 | 描述 |
---|---|
ValueError
|
如果会话不存在。 |
源代码位于 llama_deploy/client/models/core.py
187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
get_or_create async
#
get_or_create(id: str) -> Session
按 ID 获取会话,如果不存在则创建一个新会话。
返回
名称 | 类型 | 描述 |
---|---|---|
Session |
Session
|
表示指定会话的 Session 对象。 |
源代码位于 llama_deploy/client/models/core.py
209 210 211 212 213 214 215 216 217 218 219 220 |
|
delete async
#
delete(session_id: str) -> None
按 ID 删除会话。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
session_id
|
str
|
要删除的会话 ID。 |
必需 |
源代码位于 llama_deploy/client/models/core.py
222 223 224 225 226 227 228 229 |
|
Service #
基类: Model
源代码位于 llama_deploy/client/models/core.py
232 233 |
|
ServiceCollection #
基类: Collection
源代码位于 llama_deploy/client/models/core.py
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 |
|
list async
#
返回包含所有注册到控制平面的服务的列表。
返回
类型 | 描述 |
---|---|
list[Service]
|
list[Service]: 注册到控制平面的服务列表。 |
源代码位于 llama_deploy/client/models/core.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
register async
#
register(service: ServiceDefinition) -> Service
向控制平面注册服务。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
service
|
ServiceDefinition
|
要注册的服务定义。 |
必需 |
源代码位于 llama_deploy/client/models/core.py
253 254 255 256 257 258 259 260 261 262 263 264 |
|
deregister async
#
deregister(service_name: str) -> None
从控制平面注销服务。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
service_name
|
str
|
要注销的服务名称。 |
必需 |
源代码位于 llama_deploy/client/models/core.py
266 267 268 269 270 271 272 273 274 275 276 277 |
|
Core #
基类: Model
源代码位于 llama_deploy/client/models/core.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
services property
#
services: ServiceCollection
sessions property
#
sessions: SessionCollection