完成函数调用部分的定义
parent
e5a643afd2
commit
83bb6ad213
@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
from typing import Any
|
||||
from agentkit.context import ConversationContext
|
||||
from agentkit.types import PluginConfigParam
|
||||
|
||||
|
||||
class AgentKitCondition:
|
||||
INPUT: list[PluginConfigParam] = []
|
||||
"""
|
||||
Input parameters for the condition.
|
||||
"""
|
||||
|
||||
def __init__(self, props: dict):
|
||||
pass
|
||||
|
||||
|
||||
async def __aenter__(self) -> AgentKitCondition:
|
||||
return self
|
||||
|
||||
|
||||
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
|
||||
return None
|
||||
|
||||
|
||||
async def evaluate(self, context: ConversationContext, **kwargs) -> bool:
|
||||
"""
|
||||
Evaluate the condition.
|
||||
"""
|
||||
return False
|
@ -0,0 +1,34 @@
|
||||
from typing import Any, Union
|
||||
from agentkit.base.llm_function import AgentKitLLMFunction
|
||||
from agentkit.context import ConversationContext
|
||||
from agentkit.errors import AgentKitNotFoundError
|
||||
from agentkit.hooks import AgentKitHooks
|
||||
from agentkit.types import LLMFunctionInfo, LLMFunctionResponse
|
||||
|
||||
|
||||
class AgentKitLLMFunctionContainer:
|
||||
def __init__(self, props: dict, llm_function_configs: dict[str, list]):
|
||||
self.props = props
|
||||
self.llm_function_list: list[AgentKitLLMFunction] = AgentKitHooks.instantiate(
|
||||
"llm_function", llm_function_configs, props
|
||||
)
|
||||
|
||||
async def get_llm_function_info(self) -> dict[str, dict]:
|
||||
llm_function_info: list[LLMFunctionInfo] = []
|
||||
for llm_function in self.llm_function_list:
|
||||
info = await llm_function.get_llm_function_info()
|
||||
if info:
|
||||
llm_function_info.append(info)
|
||||
return llm_function_info
|
||||
|
||||
|
||||
async def call_llm_function(self, context: ConversationContext, llm_function_name: str, params: Any) -> Union[str, LLMFunctionResponse]:
|
||||
for llm_function in self.llm_function_list:
|
||||
info = await llm_function.get_llm_function_info()
|
||||
if info and info["name_for_llm"] == llm_function_name:
|
||||
result = await llm_function.run(context, params)
|
||||
# Mark the task as complete, if the function does not do it
|
||||
await context.progress.task_complete()
|
||||
return result
|
||||
|
||||
return AgentKitNotFoundError("LLM function not found", "llm_function", llm_function_name)
|
@ -0,0 +1,36 @@
|
||||
import inspect
|
||||
from typing import Optional
|
||||
from agentkit.types import OnTaskStartCallback, OnTaskProgressCallback, OnTaskCompleteCallback
|
||||
|
||||
class AgentKitTaskProgress:
|
||||
def __init__(self):
|
||||
self.on_task_start_listeners: list[OnTaskStartCallback] = []
|
||||
self.on_task_progress_listeners: list[OnTaskProgressCallback] = []
|
||||
self.on_task_complete_listeners: list[OnTaskCompleteCallback] = []
|
||||
|
||||
def add_on_task_start_listener(self, listener: OnTaskStartCallback) -> None:
|
||||
self.on_task_start_listeners.append(listener)
|
||||
|
||||
def add_on_task_progress_listener(self, listener: OnTaskProgressCallback) -> None:
|
||||
self.on_task_progress_listeners.append(listener)
|
||||
|
||||
def add_on_task_complete_listener(self, listener: OnTaskCompleteCallback) -> None:
|
||||
self.on_task_complete_listeners.append(listener)
|
||||
|
||||
async def _emit(self, listeners: list, *args) -> None:
|
||||
for listener in listeners:
|
||||
ret = listener(*args)
|
||||
if inspect.isawaitable(ret):
|
||||
await ret
|
||||
|
||||
async def task_start(self, task_title: str, task_description: str = "") -> None:
|
||||
await self._emit(self.on_task_start_listeners, task_title, task_description)
|
||||
|
||||
async def task_progress(self, current: int, total: Optional[int] = None, task_description: Optional[str] = None) -> None:
|
||||
await self._emit(self.on_task_progress_listeners, current, total, task_description)
|
||||
|
||||
async def task_progress_infinite(self, task_description: Optional[str] = None) -> None:
|
||||
await self._emit(self.on_task_progress_listeners, 0, 0, task_description)
|
||||
|
||||
async def task_complete(self) -> None:
|
||||
await self._emit(self.on_task_complete_listeners)
|
@ -0,0 +1,11 @@
|
||||
from agentkit.types import PluginConfigParam
|
||||
|
||||
|
||||
def create_plugin_params(input_params: dict, param_def: list[PluginConfigParam]) -> dict:
|
||||
params = {}
|
||||
for param in param_def:
|
||||
if param["id"] in input_params:
|
||||
params[param["id"]] = input_params[param["id"]]
|
||||
else:
|
||||
params[param["id"]] = param["default"]
|
||||
return params
|
Loading…
Reference in New Issue