From 6c57f5699252f5ce5eaf4c83fedb24d0dfcd697a Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Fri, 15 Nov 2024 15:30:29 +0000 Subject: [PATCH] first commit --- README.md | 29 ++++++++++++ README.md.tent | 29 ++++++++++++ __init__.py | 6 +++ addon.py | 19 ++++++++ addon.py.tent | 19 ++++++++ extension.py | 49 +++++++++++++++++++++ extension.py.tent | 49 +++++++++++++++++++++ manifest.json | 107 +++++++++++++++++++++++++++++++++++++++++++++ manifest.json.tent | 107 +++++++++++++++++++++++++++++++++++++++++++++ property.json | 1 + requirements.txt | 0 11 files changed, 415 insertions(+) create mode 100644 README.md create mode 100644 README.md.tent create mode 100644 __init__.py create mode 100644 addon.py create mode 100644 addon.py.tent create mode 100644 extension.py create mode 100644 extension.py.tent create mode 100644 manifest.json create mode 100644 manifest.json.tent create mode 100644 property.json create mode 100644 requirements.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..31ee575 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# default_async_llm_extension_python + + + +## Features + + + +- xxx feature + +## API + +Refer to `api` definition in [manifest.json] and default values in [property.json](property.json). + + + +## Development + +### Build + + + +### Unit test + + + +## Misc + + diff --git a/README.md.tent b/README.md.tent new file mode 100644 index 0000000..a327c56 --- /dev/null +++ b/README.md.tent @@ -0,0 +1,29 @@ +# {{package_name}} + + + +## Features + + + +- xxx feature + +## API + +Refer to `api` definition in [manifest.json] and default values in [property.json](property.json). + + + +## Development + +### Build + + + +### Unit test + + + +## Misc + + diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..72593ab --- /dev/null +++ b/__init__.py @@ -0,0 +1,6 @@ +# +# This file is part of TEN Framework, an open source project. +# Licensed under the Apache License, Version 2.0. +# See the LICENSE file for more information. +# +from . import addon diff --git a/addon.py b/addon.py new file mode 100644 index 0000000..5d09583 --- /dev/null +++ b/addon.py @@ -0,0 +1,19 @@ +# +# This file is part of TEN Framework, an open source project. +# Licensed under the Apache License, Version 2.0. +# See the LICENSE file for more information. +# +from ten import ( + Addon, + register_addon_as_extension, + TenEnv, +) +from .extension import DefaultAsyncLLMExtension + + +@register_addon_as_extension("default_async_llm_extension_python") +class DefaultAsyncLLMExtensionAddon(Addon): + def on_create_instance(self, ten_env: TenEnv, name: str, context) -> None: + ten_env.log_info("on_create_instance") + ten_env.on_create_instance_done( + DefaultAsyncLLMExtension(name), context) diff --git a/addon.py.tent b/addon.py.tent new file mode 100644 index 0000000..c4dab71 --- /dev/null +++ b/addon.py.tent @@ -0,0 +1,19 @@ +# +# This file is part of TEN Framework, an open source project. +# Licensed under the Apache License, Version 2.0. +# See the LICENSE file for more information. +# +from ten import ( + Addon, + register_addon_as_extension, + TenEnv, +) +from .extension import {{class_name_prefix}}Extension + + +@register_addon_as_extension("{{package_name}}") +class {{class_name_prefix}}ExtensionAddon(Addon): + def on_create_instance(self, ten_env: TenEnv, name: str, context) -> None: + ten_env.log_info("on_create_instance") + ten_env.on_create_instance_done( + {{class_name_prefix}}Extension(name), context) diff --git a/extension.py b/extension.py new file mode 100644 index 0000000..53107fa --- /dev/null +++ b/extension.py @@ -0,0 +1,49 @@ +# +# This file is part of TEN Framework, an open source project. +# Licensed under the Apache License, Version 2.0. +# See the LICENSE file for more information. +# +from ten import AsyncTenEnv +from ten_ai_base import ( + AsyncLLMBaseExtension, LLMCallCompletionArgs, LLMDataCompletionArgs, LLMToolMetadata, BaseConfig +) +from dataclasses import dataclass + + +@dataclass +class DefaultAsyncLLMConfig(BaseConfig): + model: str = "" + # TODO: add extra config fields here + + +class DefaultAsyncLLMExtension(AsyncLLMBaseExtension): + async def on_start(self, ten_env: AsyncTenEnv) -> None: + await super().on_start(ten_env) + + # initialize configuration + self.config = DefaultAsyncLLMConfig.create(ten_env=ten_env) + ten_env.log_info(f"config: {self.config}") + + """Implement this method to construct and start your resources.""" + ten_env.log_debug("TODO: on_start") + + async def on_stop(self, ten_env: AsyncTenEnv) -> None: + await super().on_stop(ten_env) + + """Implement this method to stop and destruct your resources.""" + ten_env.log_debug("TODO: on_stop") + + async def on_call_chat_completion(self, ten_env: AsyncTenEnv, **kargs: LLMCallCompletionArgs) -> any: + """Called when a chat completion is requested by cmd call. Implement this method to process the chat completion.""" + ten_env.log_debug("TODO: on_call_chat_completion") + + async def on_data_chat_completion(self, ten_env: AsyncTenEnv, **kargs: LLMDataCompletionArgs) -> None: + """ + Called when a chat completion is requested by data input. Implement this method to process the chat completion. + Note that this method is stream-based, and it should consider supporting local context caching. + """ + ten_env.log_debug("TODO: on_data_chat_completion") + + async def on_tools_update(self, ten_env: AsyncTenEnv, tool: LLMToolMetadata) -> None: + """Called when a new tool is registered. Implement this method to process the new tool.""" + ten_env.log_debug("TODO: on_tools_update") diff --git a/extension.py.tent b/extension.py.tent new file mode 100644 index 0000000..138123d --- /dev/null +++ b/extension.py.tent @@ -0,0 +1,49 @@ +# +# This file is part of TEN Framework, an open source project. +# Licensed under the Apache License, Version 2.0. +# See the LICENSE file for more information. +# +from ten import AsyncTenEnv +from ten_ai_base import ( + AsyncLLMBaseExtension, LLMCallCompletionArgs, LLMDataCompletionArgs, LLMToolMetadata, BaseConfig +) +from dataclasses import dataclass + + +@dataclass +class {{class_name_prefix}}Config(BaseConfig): + model: str = "" + # TODO: add extra config fields here + + +class {{class_name_prefix}}Extension(AsyncLLMBaseExtension): + async def on_start(self, ten_env: AsyncTenEnv) -> None: + await super().on_start(ten_env) + + # initialize configuration + self.config = {{class_name_prefix}}Config.create(ten_env=ten_env) + ten_env.log_info(f"config: {self.config}") + + """Implement this method to construct and start your resources.""" + ten_env.log_debug("TODO: on_start") + + async def on_stop(self, ten_env: AsyncTenEnv) -> None: + await super().on_stop(ten_env) + + """Implement this method to stop and destruct your resources.""" + ten_env.log_debug("TODO: on_stop") + + async def on_call_chat_completion(self, ten_env: AsyncTenEnv, **kargs: LLMCallCompletionArgs) -> any: + """Called when a chat completion is requested by cmd call. Implement this method to process the chat completion.""" + ten_env.log_debug("TODO: on_call_chat_completion") + + async def on_data_chat_completion(self, ten_env: AsyncTenEnv, **kargs: LLMDataCompletionArgs) -> None: + """ + Called when a chat completion is requested by data input. Implement this method to process the chat completion. + Note that this method is stream-based, and it should consider supporting local context caching. + """ + ten_env.log_debug("TODO: on_data_chat_completion") + + async def on_tools_update(self, ten_env: AsyncTenEnv, tool: LLMToolMetadata) -> None: + """Called when a new tool is registered. Implement this method to process the new tool.""" + ten_env.log_debug("TODO: on_tools_update") diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..7c91ef7 --- /dev/null +++ b/manifest.json @@ -0,0 +1,107 @@ +{ + "type": "extension", + "name": "default_async_llm_extension_python", + "version": "0.1.0", + "dependencies": [ + { + "type": "system", + "name": "ten_ai_base", + "version": "0.1.0" + } + ], + "package": { + "include": [ + "manifest.json", + "property.json", + "requirements.txt", + "**.tent", + "**.py", + "README.md" + ] + }, + "api": { + "property": { + "model": { + "type": "string" + } + }, + "cmd_in": [ + { + "name": "tool_register", + "property": { + "tool": { + "type": "string" + } + }, + "required": [ + "tool" + ] + }, + { + "name": "call_chat_completion", + "property": { + "messages": { + "type": "string" + }, + "stream": { + "type": "bool" + } + }, + "required": [ + "messages" + ], + "result": { + "property": { + "text": { + "type": "string" + } + }, + "required": [ + "text" + ] + } + }, + { + "name": "flush" + } + ], + "cmd_out": [ + { + "name": "flush" + } + ], + "data_in": [ + { + "name": "text_data", + "property": { + "text": { + "type": "string" + }, + "is_final": { + "type": "bool" + } + }, + "required": [ + "text" + ] + } + ], + "data_out": [ + { + "name": "text_data", + "property": { + "text": { + "type": "string" + }, + "end_of_segment": { + "type": "bool" + } + }, + "required": [ + "text", + "end_of_segment" + ] + } + ] + } +} \ No newline at end of file diff --git a/manifest.json.tent b/manifest.json.tent new file mode 100644 index 0000000..dcf4f60 --- /dev/null +++ b/manifest.json.tent @@ -0,0 +1,107 @@ +{ + "type": "extension", + "name": "{{package_name}}", + "version": "0.1.0", + "dependencies": [ + { + "type": "system", + "name": "ten_ai_base", + "version": "0.1.0" + } + ], + "package": { + "include": [ + "manifest.json", + "property.json", + "requirements.txt", + "**.tent", + "**.py", + "README.md" + ] + }, + "api": { + "property": { + "model": { + "type": "string" + } + }, + "cmd_in": [ + { + "name": "tool_register", + "property": { + "tool": { + "type": "string" + } + }, + "required": [ + "tool" + ] + }, + { + "name": "call_chat_completion", + "property": { + "messages": { + "type": "string" + }, + "stream": { + "type": "bool" + } + }, + "required": [ + "messages" + ], + "result": { + "property": { + "text": { + "type": "string" + } + }, + "required": [ + "text" + ] + } + }, + { + "name": "flush" + } + ], + "cmd_out": [ + { + "name": "flush" + } + ], + "data_in": [ + { + "name": "text_data", + "property": { + "text": { + "type": "string" + }, + "is_final": { + "type": "bool" + } + }, + "required": [ + "text" + ] + } + ], + "data_out": [ + { + "name": "text_data", + "property": { + "text": { + "type": "string" + }, + "end_of_segment": { + "type": "bool" + } + }, + "required": [ + "text", + "end_of_segment" + ] + } + ] + } +} \ No newline at end of file diff --git a/property.json b/property.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/property.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29