Skip to content

Commit

Permalink
featg: autogen extension (#8)
Browse files Browse the repository at this point in the history
* Working on autogen

* Added Autogen Files

* Autogen code modified

* Changes to Gitignore and Cli clink print

* Fixed array handling in toolspec

* issues fixed

* issues fixed lf

---------

Co-authored-by: Utkarsh Dixit <[email protected]>
  • Loading branch information
sawradip and utkarsh-dixit authored Mar 26, 2024
1 parent 1885f9a commit 923a479
Show file tree
Hide file tree
Showing 3 changed files with 510 additions and 0 deletions.
326 changes: 326 additions & 0 deletions auto_gen/composio_autogen/autogen_toolspec.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,326 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from inspect import Parameter, Signature\n",
"from typing import Annotated, get_type_hints"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"{'properties': {\n",
" 'body': {\n",
" 'default': '',\n",
" 'description': 'Body of the issue',\n",
" 'examples': ['The code is not working',\n",
" 'I would like to request '\n",
" 'a new feature'],\n",
" 'title': 'Body',\n",
" 'type': 'string'},\n",
" 'owner': {'description': 'Owner of the '\n",
" 'repository',\n",
" 'examples': ['openai', 'facebook'],\n",
" 'title': 'Owner',\n",
" 'type': 'string'},\n",
" 'repo': {'description': 'Name of the '\n",
" 'repository',\n",
" 'examples': ['gpt-3', 'react'],\n",
" 'title': 'Repo',\n",
" 'type': 'string'},\n",
" 'title': {'description': 'Title of the issue',\n",
" 'examples': ['Bug in the code',\n",
" 'Feature request'],\n",
" 'title': 'Title',\n",
" 'type': 'string'}},\n",
"'required': ['owner', 'repo', 'title'],\n",
"'title': 'CreateIssueRequest',\n",
"'type': 'object'}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"schema_type_python_type_dict = {\n",
" 'string': str,\n",
" 'number': float,\n",
" 'boolean': bool,\n",
" # 'array': list,\n",
" # 'object': dict\n",
"}\n",
"\n",
"fallback_values = {\n",
" 'string': \"\",\n",
" 'number': 0.0,\n",
" 'boolean': False,\n",
" 'object': {},\n",
" 'array': []\n",
"}\n",
"def pydantic_model_from_param_schema(param_title, param_schema):\n",
" fields = {}\n",
" for prop_name, prop_schema in param_schema.items():\n",
"\n",
" prop_name: (str, Field(..., title=prop_info['title'], default=prop_info.get('default')))\n",
" if prop_name in required else\n",
" (str, Field(None, title=prop_info['title'], default=prop_info.get('default')))\n",
"\n",
"\n",
" \n",
"\n",
"def get_signature_format_from_schema_params(\n",
" schema_params\n",
"):\n",
" parameters = []\n",
"\n",
" for param_name, param_schema in schema_params['properties'].items():\n",
" param_type = param_schema['type']\n",
" param_title = param_schema['title'].replace(\" \", \"\")\n",
" required_params = param_schema.get('required', [])\n",
"\n",
" if param_type in schema_type_python_type_dict:\n",
" signature_param_type = schema_type_python_type_dict[param_type]\n",
" else:\n",
" signature_param_type = get_signature_format_from_schema_params(param_title,\n",
" param_schema,\n",
" required_params)\n",
"\n",
" # param_type = schema_type_python_type_dict[param_schema['type']]\n",
" param_name = param_schema['name']\n",
" param_default = param_schema.get('default', fallback_values[param_type])\n",
" param_annotation = Annotated[signature_param_type, param_schema['description']]\n",
" param = Parameter(\n",
" name=param_name,\n",
" kind=Parameter.POSITIONAL_OR_KEYWORD,\n",
" annotation=param_annotation,\n",
" default=param_default\n",
" )\n",
" parameters.append(param)\n",
"\n",
"\n",
" \n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.<lambda>(base: typing.Annotated[str, 'Base currency: amount and currency symbol'], quote_currency: typing.Annotated[str, 'Quote currency symbol']) -> str>"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import types\n",
"\n",
"# Not supporting posiotonal arguments intentionally\n",
"# demo_func = lambda *args, **kwargs: print(args, kwargs)\n",
"\n",
"demo_func = lambda *args, **kwargs: print(args, kwargs)\n",
"\n",
"parameters = [\n",
" Parameter(\n",
" name=\"base\", \n",
" kind=Parameter.POSITIONAL_OR_KEYWORD, \n",
" annotation=Annotated[str, \"Base currency: amount and currency symbol\"], \n",
" # default=Parameter.empty if param in required_params else Parameter.default\n",
" ),\n",
" Parameter(\n",
" name=\"quote_currency\", \n",
" kind=Parameter.POSITIONAL_OR_KEYWORD, \n",
" annotation=Annotated[str, \"Quote currency symbol\"], \n",
" # default=Parameter.empty if param in required_params else Parameter.default\n",
" ),\n",
"]\n",
"new_sig = Signature(parameters, return_annotation=str)\n",
"\n",
"func = types.FunctionType(demo_func.__code__, \n",
" globals=globals(), \n",
" name=\"currency_calculator2\", \n",
" # closure=template_function.__closure__\n",
" )\n",
"func.__signature__ = new_sig\n",
"func\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 5) {}\n"
]
}
],
"source": [
"func(3, 5)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Signature (base: typing.Annotated[str, 'Base currency: amount and currency symbol'], quote_currency: typing.Annotated[str, 'Quote currency symbol']) -> str>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inspect.signature(func)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import inspect\n",
"from pydantic import BaseModel, Field\n",
"from typing_extensions import Annotated"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.currency_calculator(base: typing.Annotated[str, 'Base currency: amount and currency symbol'], quote_currency: typing.Annotated[str, 'Quote currency symbol'] = 'USD') -> str>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def currency_calculator(\n",
" base: Annotated[str, \"Base currency: amount and currency symbol\"],\n",
" quote_currency: Annotated[str, \"Quote currency symbol\"] = \"USD\",\n",
") -> str:\n",
" quote_amount = base + \" \" + quote_currency\n",
" return quote_amount\n",
"\n",
"currency_calculator"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"currency_calculator.__closure__"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Signature (base: typing.Annotated[str, 'Base currency: amount and currency symbol'], quote_currency: typing.Annotated[str, 'Quote currency symbol'] = 'USD') -> str>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inspect.signature(currency_calculator)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"composio.autogen.register.slack()\n",
"composio.autogen.register.hubspot()\n",
"\n",
"composio.autogen.register(\n",
" [composio.slack.send_message.args(\n",
" caller=chatbot,\n",
" executor=user_proxy,\n",
" ),\n",
" composio.slack.block_one],\n",
" caller=chatbot,\n",
" executor=user_proxy,\n",
")\n",
"\n",
"\n",
"autogen.agentchat.register_function(\n",
" composio.slack.send_message,\n",
" caller=chatbot,\n",
" executor=user_proxy,\n",
" name=\"sh\",\n",
" description=\"run a shell script and return the execution result.\",\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 923a479

Please sign in to comment.