-
Notifications
You must be signed in to change notification settings - Fork 365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/tools #308
Merged
Merged
Feat/tools #308
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
700cbd1
init tool
tomasliu-agora c8eda45
tools wip
tomasliu-agora a729e98
fix python exception
tomasliu-agora 23dc06d
feat: add tool in v2v
e8d6240
init tool
tomasliu-agora 1266b38
fix python exception
tomasliu-agora f486037
fix
tomasliu-agora 1c20910
remove out of date file
tomasliu-agora 59ee505
remove standalone graph
tomasliu-agora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import copy | ||
from typing import Dict, Any | ||
from functools import partial | ||
|
||
from .log import logger | ||
|
||
class ToolRegistry: | ||
tools: Dict[str, dict[str, Any]] = {} | ||
def register(self, name:str, description: str, callback, parameters: Any = None) -> None: | ||
info = { | ||
"type": "function", | ||
"name": name, | ||
"description": description, | ||
"callback": callback | ||
} | ||
if parameters: | ||
info["parameters"] = parameters | ||
self.tools[name] = info | ||
logger.info(f"register tool {name} {description}") | ||
|
||
def to_prompt(self) -> str: | ||
prompt = "" | ||
if self.tools: | ||
prompt = "You have several tools that you can get help from:\n" | ||
for name, t in self.tools.items(): | ||
desc = t["description"] | ||
prompt += f"- ***{name}***: {desc}" | ||
return prompt | ||
|
||
def unregister(self, name:str) -> None: | ||
if name in self.tools: | ||
del self.tools[name] | ||
logger.info(f"unregister tool {name}") | ||
|
||
def get_tools(self) -> list[dict[str, Any]]: | ||
result = [] | ||
for _, t in self.tools.items(): | ||
info = copy.copy(t) | ||
del info["callback"] | ||
result.append(info) | ||
return result | ||
|
||
async def on_func_call(self, call_id: str, name: str, args: str, callback): | ||
try: | ||
if name in self.tools: | ||
t = self.tools[name] | ||
# FIXME add args check | ||
if t.get("callback"): | ||
p = partial(callback, call_id) | ||
await t["callback"](name, args, p) | ||
else: | ||
logger.warning(f"Failed to find func {name}") | ||
except: | ||
logger.exception(f"Failed to call func {name}") | ||
# TODO What to do if func call is dead | ||
callback(None) | ||
|
||
if __name__ == "__main__": | ||
r = ToolRegistry() | ||
|
||
def weather_check(location:str = "", datetime:str = ""): | ||
logger.info(f"on weather check {location}, {datetime}") | ||
|
||
def on_tool_completion(result: Any): | ||
logger.info(f"on tool completion {result}") | ||
|
||
r.register( | ||
name="weather", description="This is a weather check func, if the user is asking about the weather. you need to summarize location and time information from the context as parameters. if the information is lack, please ask for more detail before calling.", | ||
callback=weather_check, | ||
parameters={ | ||
"type": "object", | ||
"properties": { | ||
"location": { | ||
"type": "string", | ||
"description": "The location or region for the weather check.", | ||
}, | ||
"datetime": { | ||
"type": "string", | ||
"description": "The date and time for the weather check. The datetime should use format like 2024-10-01T16:42:00.", | ||
} | ||
}, | ||
"required": ["location"], | ||
}) | ||
print(r.to_prompt()) | ||
print(r.get_tools()) | ||
print(r.on_func_call("weather", {"location":"LA", "datetime":"2024-10-01T16:43:01"}, on_tool_completion)) | ||
r.unregister("weather") | ||
print(r.to_prompt()) | ||
print(r.get_tools()) | ||
print(r.on_func_call("weather", {"location":"LA", "datetime":"2024-10-01T16:43:01"}, on_tool_completion)) | ||
|
21 changes: 21 additions & 0 deletions
21
agents/ten_packages/extension/weatherapi_tool_python/BUILD.gn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# | ||
# | ||
# Agora Real Time Engagement | ||
# Created by Wei Hu in 2022-11. | ||
# Copyright (c) 2024 Agora IO. All rights reserved. | ||
# | ||
# | ||
import("//build/feature/ten_package.gni") | ||
|
||
ten_package("weatherapi_tool_python") { | ||
package_kind = "extension" | ||
|
||
resources = [ | ||
"__init__.py", | ||
"addon.py", | ||
"extension.py", | ||
"log.py", | ||
"manifest.json", | ||
"property.json", | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use logger instead of print.
and set levels to debug for debugging info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is test script.