Skip to content

Commit

Permalink
Merge pull request #13 from SamparkAI/kaavee/autogen_openapi_changes
Browse files Browse the repository at this point in the history
Kaavee/autogen openapi changes
  • Loading branch information
kaavee315 committed Apr 11, 2024
2 parents f3e5891 + a578cdf commit 812314f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 44 deletions.
91 changes: 53 additions & 38 deletions autogen/composio_autogen/autogen_toolspec.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import types
import logging
import hashlib
from inspect import Parameter, Signature
from typing import Union, List, Annotated

Expand Down Expand Up @@ -54,46 +55,48 @@ def pydantic_model_from_param_schema(param_schema):
Field(title=prop_title,
default=prop_default
))

fieldModel = create_model(param_title, **required_fields, **optional_fields)
return fieldModel




def get_signature_format_from_schema_params(
schema_params
):
def get_signature_format_from_schema_params(schema_params):
required_parameters = []
optional_parameters = []
required_params = schema_params.get('required', [])

required_params = schema_params.get('required', [])
for param_name, param_schema in schema_params.get('properties', {}).items():
param_type = param_schema['type']
param_title = param_schema['title'].replace(" ", "")

if param_type in schema_type_python_type_dict:
signature_param_type = schema_type_python_type_dict[param_type]
else:
signature_param_type = pydantic_model_from_param_schema(param_schema)

param_default = param_schema.get('default', fallback_values[param_type])
param_annotation = Annotated[signature_param_type, param_schema.get('description',
param_schema.get('desc',
param_title))]
param = Parameter(
name=param_name,
kind=Parameter.POSITIONAL_OR_KEYWORD,
annotation=param_annotation,
default=Parameter.empty if param_name in required_params else param_default
)

is_required = param_name in required_params
if is_required:
required_parameters.append(param)
else:
optional_parameters.append(param)
try:
param_type = param_schema['type']

param_title = param_schema['title'].replace(" ", "")

if param_type in schema_type_python_type_dict:
signature_param_type = schema_type_python_type_dict[param_type]
else:
signature_param_type = pydantic_model_from_param_schema(param_schema)

param_default = param_schema.get('default', fallback_values[param_type])
param_annotation = Annotated[signature_param_type, param_schema.get('description',
param_schema.get('desc',
param_title))]
is_required = param_name in required_params
param = Parameter(
name=param_name,
kind=Parameter.POSITIONAL_OR_KEYWORD,
annotation=param_annotation,
default=Parameter.empty if param_name in required_params else param_default
)
if is_required:
required_parameters.append(param)
else :
optional_parameters.append(param)
except Exception as e:
logger.error(f"Error while processing param {param_name} with schema {param_schema}")
pass
return required_parameters + optional_parameters


class ComposioToolset:
def __init__(self, caller = None, executor = None):
self.caller = caller
Expand Down Expand Up @@ -144,36 +147,48 @@ def register_actions(
caller = caller if caller else self.caller,
executor = executor if executor else self.executor)


def process_function_name_for_registration(self, input_string, max_allowed_length = 64, num_hash_char = 10):
hash_obj = hashlib.sha256(input_string.encode())
hash_hex = hash_obj.hexdigest()

num_input_str_char = max_allowed_length - (num_hash_char + 1)
hash_chars_to_attach = hash_hex[:10]
input_str_to_attach = input_string[-num_input_str_char:]
processed_name = input_str_to_attach + "_" + hash_chars_to_attach

return processed_name

def _register_schema_to_autogen(self,
action_schema,
caller: ConversableAgent,
executor: ConversableAgent):

name = action_schema["name"]
processed_name = self.process_function_name_for_registration(name)
appName = action_schema["appName"]
description = action_schema["description"]

parameters = get_signature_format_from_schema_params(
action_schema["parameters"])
action_signature = Signature(parameters=parameters)

placeholder_function = lambda **kwargs: self.client.execute_action(
self.client.get_action_enum(name, appName),
kwargs)
def placeholder_function(**kwargs):
return self.client.execute_action(
self.client.get_action_enum(name, appName),
kwargs)
action_func = types.FunctionType(
placeholder_function.__code__,
globals=globals(),
name=name,
name=processed_name,
closure=placeholder_function.__closure__
)
action_func.__signature__ = action_signature
action_func.__doc__ = description
action_func.__doc__ = description if description else f"Action {name} from {appName}"

autogen.agentchat.register_function(
action_func,
caller=caller,
executor=executor,
name=name,
description=description
name=processed_name,
description=description if description else f"Action {name} from {appName}"
)
2 changes: 1 addition & 1 deletion autogen/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ name = "composio_autogen"

[build-system]
requires = [ "setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
build-backend = "setuptools.build_meta"
9 changes: 6 additions & 3 deletions core/composio/sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from typing import Union

def _get_enum_key(name):
return name.upper().replace(' ', '_').replace('-', '_')
characters_to_replace = [' ', '-', '/', '(', ')', '\\', ':', '"', '\'', '.']
for char in characters_to_replace:
name = name.replace(char, '_')
return name.upper()

def generate_enums():
sdk_client = Composio(get_base_account_api_key())
Expand Down Expand Up @@ -36,7 +39,7 @@ def generate_enums():
app_key = app['key']
app_actions = [action for action in actions if action["appKey"] == app_key]
for action in app_actions:
enum_name = f'{app_key.upper()}_{_get_enum_key(action["display_name"])}'
enum_name = f'{_get_enum_key(action["name"])}'
enum_value = f'("{app_key}", "{action["name"]}")'
enum_content += f' {enum_name} = {enum_value}\n'

Expand Down Expand Up @@ -66,4 +69,4 @@ def get_git_user_info() -> GitUserInfo:
email = subprocess.check_output(['git', 'config', 'user.email']).strip().decode('utf-8')
return GitUserInfo(name=name, email=email)
except subprocess.CalledProcessError:
return GitUserInfo(name=None, email=None)
return GitUserInfo(name=None, email=None)
2 changes: 1 addition & 1 deletion crew_ai/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
composio_langchain===0.1.86
composio_langchain===0.1.86
2 changes: 1 addition & 1 deletion langchain/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ langchain>=0.1.0
langchain-openai>=0.0.2.post1
pydantic>=2.6.4
langchainhub>=0.1.15
composio_core===0.1.86
composio_core===0.1.86

0 comments on commit 812314f

Please sign in to comment.