From a76f81b58eceabc217df812844fefc2d0523067b Mon Sep 17 00:00:00 2001 From: Tanay Godse Date: Wed, 18 Sep 2024 10:18:36 -0700 Subject: [PATCH 1/4] Removed digest from model, hardcoded a value 'nextgen' for now, removed 'args' from the payload value --- ai_engine_sdk/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ai_engine_sdk/client.py b/ai_engine_sdk/client.py index 309b28e..2998bcf 100644 --- a/ai_engine_sdk/client.py +++ b/ai_engine_sdk/client.py @@ -289,8 +289,8 @@ async def get_messages(self) -> List[ApiBaseMessage]: 'id': message['message_id'], 'timestamp': message['timestamp'], 'text': agent_json['text'], - 'model': agent_json['context_json']['digest'], - 'payload': agent_json['context_json']['args'], + 'model': "nextgen",#agent_json['context_json']['digest'], + 'payload': agent_json['context_json'], }) ) elif is_data_request_message(message_type=agent_json_type): @@ -579,4 +579,4 @@ async def share_function_group( payload=payload ) logger.debug(f"FG successfully shared: {function_group_id} with {target_user_email}") - return raw_response + return raw_response \ No newline at end of file From ebe6ed75aaad071a112d885a312c2bcfa3eddb93 Mon Sep 17 00:00:00 2001 From: Tanay Godse Date: Thu, 19 Sep 2024 09:56:10 -0700 Subject: [PATCH 2/4] Remove 'model' key from pydantic model --- .../api_models/agents_json_messages.py | 8 +-- ai_engine_sdk/client.py | 50 ++++++++++++------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/ai_engine_sdk/api_models/agents_json_messages.py b/ai_engine_sdk/api_models/agents_json_messages.py index 552b8b3..733062f 100644 --- a/ai_engine_sdk/api_models/agents_json_messages.py +++ b/ai_engine_sdk/api_models/agents_json_messages.py @@ -37,7 +37,6 @@ class TaskSelectionMessage(AgentJsonMessage): text: str options: Dict[str, TaskOption] - def get_options_keys(self) -> list[TaskOption]: return [option for option in self.options] @@ -50,7 +49,6 @@ class DataRequestMessage(AgentJsonMessage): class ConfirmationMessage(AgentJsonMessage): type: Literal[AgentJsonMessageTypes.CONFIRMATION] = AgentJsonMessageTypes.CONFIRMATION text: str - model: str payload: Dict[str, Any] @@ -70,14 +68,16 @@ def is_agent_json_confirmation_message(message_type: str) -> bool: def is_task_selection_message(message_type: str) -> bool: union_of_type = TaskSelectionTypes - allowed_values = [literal for lit in get_args(union_of_type) for literal in get_args(lit)] + allowed_values = [literal for lit in get_args( + union_of_type) for literal in get_args(lit)] return message_type.upper() in allowed_values def is_data_request_message(message_type: str) -> bool: union_of_type = DataRequestTypes if get_origin(union_of_type) is Union: - allowed_values = [literal for lit in get_args(union_of_type) for literal in get_args(lit)] + allowed_values = [literal for lit in get_args( + union_of_type) for literal in get_args(lit)] elif get_origin(union_of_type) is Literal: allowed_values = get_args(union_of_type) diff --git a/ai_engine_sdk/client.py b/ai_engine_sdk/client.py index 2998bcf..96f9ff1 100644 --- a/ai_engine_sdk/client.py +++ b/ai_engine_sdk/client.py @@ -92,12 +92,14 @@ async def make_api_request( } async with aiohttp.ClientSession() as session: - logger.debug(f"\n\n 📤 Request triggered : {method} {api_base_url}{endpoint}") + logger.debug(f"\n\n 📤 Request triggered : { + method} {api_base_url}{endpoint}") logger.debug(f"{body=}") logger.debug("---------------------------\n\n") async with session.request(method, f"{api_base_url}{endpoint}", headers=headers, data=body) as response: if not bool(re.search(pattern="^2..$", string=str(response.status))): - raise Exception(f"Request failed with status {response.status} to {method}: {endpoint}") + raise Exception(f"Request failed with status { + response.status} to {method}: {endpoint}") return await response.json() @@ -113,6 +115,7 @@ class Session: _messages (List[ApiBaseMessage]): A list to store messages associated with the session. _message_ids (set[str]): A set to store unique message IDs to prevent duplication. """ + def __init__(self, api_base_url: str, api_key: str, session_id: str, function_group: str): """ Initializes a new session with the given parameters. @@ -254,12 +257,14 @@ async def get_messages(self) -> List[ApiBaseMessage]: Each message type has a different purpose as the name indicates. """ - queryParams = f"?last_message_id={self._messages[-1]['message_id']}" if self._messages else "" + queryParams = f"?last_message_id={ + self._messages[-1]['message_id']}" if self._messages else "" response = await make_api_request( api_base_url=self._api_base_url, api_key=self._api_key, method='GET', - endpoint=f"/v1beta1/engine/chat/sessions/{self.session_id}/new-messages{queryParams}" + endpoint=f"/v1beta1/engine/chat/sessions/{ + self.session_id}/new-messages{queryParams}" ) newMessages: List[ApiBaseMessage] = [] @@ -273,14 +278,15 @@ async def get_messages(self) -> List[ApiBaseMessage]: agent_json: dict = message['agent_json'] agent_json_type: str = agent_json['type'].upper() if is_task_selection_message(message_type=agent_json_type): - indexed_task_options: dict = get_indexed_task_options_from_raw_api_response(raw_api_response=message) + indexed_task_options: dict = get_indexed_task_options_from_raw_api_response( + raw_api_response=message) newMessages.append( TaskSelectionMessage.model_validate({ 'type': agent_json_type, 'id': message['message_id'], 'timestamp': message['timestamp'], 'text': agent_json['text'], - 'options':indexed_task_options + 'options': indexed_task_options }) ) elif is_api_context_json(message_type=agent_json_type, agent_json_text=agent_json['text']): @@ -289,7 +295,6 @@ async def get_messages(self) -> List[ApiBaseMessage]: 'id': message['message_id'], 'timestamp': message['timestamp'], 'text': agent_json['text'], - 'model': "nextgen",#agent_json['context_json']['digest'], 'payload': agent_json['context_json'], }) ) @@ -352,7 +357,7 @@ async def delete(self): endpoint=f"/v1beta1/engine/chat/sessions/{self.session_id}" ) - async def execute_function(self, function_ids: list[str], objective: str, context: str|None = None): + async def execute_function(self, function_ids: list[str], objective: str, context: str | None = None): await self._submit_message( payload=ApiUserMessageExecuteFunctions.model_validate({ "functions": function_ids, @@ -362,15 +367,17 @@ async def execute_function(self, function_ids: list[str], objective: str, contex }) ) + class AiEngine: def __init__(self, api_key: str, options: Optional[dict] = None): - self._api_base_url = options.get('api_base_url') if options and 'api_base_url' in options else default_api_base_url + self._api_base_url = options.get( + 'api_base_url') if options and 'api_base_url' in options else default_api_base_url self._api_key = api_key - #### # Function groups #### + async def get_function_groups(self) -> List[FunctionGroup]: logger.debug("get_function_groups") publicGroups, privateGroups = await asyncio.gather( @@ -463,6 +470,7 @@ async def get_function_group_by_function(self, function_id: str): ### # Functions ### + async def get_functions_by_function_group(self, function_group_id: str) -> list[FunctionGroupFunctions]: raw_response: dict = await make_api_request( api_base_url=self._api_base_url, @@ -474,14 +482,14 @@ async def get_functions_by_function_group(self, function_group_id: str) -> list[ if "functions" in raw_response: list( map( - lambda function_name: FunctionGroupFunctions.model_validate({"name": function_name}), + lambda function_name: FunctionGroupFunctions.model_validate( + {"name": function_name}), raw_response["functions"] ) ) return result - async def get_functions(self) -> list[Function]: raw_response: dict = await make_api_request( api_base_url=self._api_base_url, @@ -498,8 +506,10 @@ async def get_functions(self) -> list[Function]: #### # Model #### + async def get_models(self) -> List[Model]: - pending_credits = [self.get_model_credits(model_id) for model_id in DefaultModelIds] + pending_credits = [self.get_model_credits( + model_id) for model_id in DefaultModelIds] models = [Model( id=model_id, @@ -534,7 +544,8 @@ async def get_model_credits(self, model: Union[KnownModelId, CustomModel]) -> in api_base_url=self._api_base_url, api_key=self._api_key, method='GET', - endpoint=f"/v1beta1/engine/credit/remaining_tokens?models={model_id}" + endpoint=f"/v1beta1/engine/credit/remaining_tokens?models={ + model_id}" ) return response['model_tokens'].get(model_id, 0) @@ -546,7 +557,8 @@ async def create_session(self, function_group: str, opts: Optional[dict] = None) email=opts.get('email') if opts else "", functionGroup=function_group, preferencesEnabled=False, - requestModel=opts.get('model') if opts and 'model' in opts else DefaultModelId + requestModel=opts.get( + 'model') if opts and 'model' in opts else DefaultModelId ) response = await make_api_request( api_base_url=self._api_base_url, @@ -575,8 +587,10 @@ async def share_function_group( api_base_url=self._api_base_url, api_key=self._api_key, method='PUT', - endpoint=f"/v1beta1/function-groups/{function_group_id}/permissions/", + endpoint=f"/v1beta1/function-groups/{ + function_group_id}/permissions/", payload=payload ) - logger.debug(f"FG successfully shared: {function_group_id} with {target_user_email}") - return raw_response \ No newline at end of file + logger.debug(f"FG successfully shared: { + function_group_id} with {target_user_email}") + return raw_response From b7368eb449fcfba920c7d11c4b5975235525f9ec Mon Sep 17 00:00:00 2001 From: Tanay Godse Date: Tue, 24 Sep 2024 01:15:21 -0700 Subject: [PATCH 3/4] 120 characters fix --- ai_engine_sdk/client.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/ai_engine_sdk/client.py b/ai_engine_sdk/client.py index 96f9ff1..7bb1108 100644 --- a/ai_engine_sdk/client.py +++ b/ai_engine_sdk/client.py @@ -92,14 +92,12 @@ async def make_api_request( } async with aiohttp.ClientSession() as session: - logger.debug(f"\n\n 📤 Request triggered : { - method} {api_base_url}{endpoint}") + logger.debug(f"\n\n 📤 Request triggered : {method} {api_base_url}{endpoint}") logger.debug(f"{body=}") logger.debug("---------------------------\n\n") async with session.request(method, f"{api_base_url}{endpoint}", headers=headers, data=body) as response: if not bool(re.search(pattern="^2..$", string=str(response.status))): - raise Exception(f"Request failed with status { - response.status} to {method}: {endpoint}") + raise Exception(f"Request failed with status {response.status} to {method}: {endpoint}") return await response.json() @@ -257,14 +255,12 @@ async def get_messages(self) -> List[ApiBaseMessage]: Each message type has a different purpose as the name indicates. """ - queryParams = f"?last_message_id={ - self._messages[-1]['message_id']}" if self._messages else "" + queryParams = f"?last_message_id={self._messages[-1]['message_id']}" if self._messages else "" response = await make_api_request( api_base_url=self._api_base_url, api_key=self._api_key, method='GET', - endpoint=f"/v1beta1/engine/chat/sessions/{ - self.session_id}/new-messages{queryParams}" + endpoint=f"/v1beta1/engine/chat/sessions/{self.session_id}/new-messages{queryParams}" ) newMessages: List[ApiBaseMessage] = [] @@ -544,8 +540,7 @@ async def get_model_credits(self, model: Union[KnownModelId, CustomModel]) -> in api_base_url=self._api_base_url, api_key=self._api_key, method='GET', - endpoint=f"/v1beta1/engine/credit/remaining_tokens?models={ - model_id}" + endpoint=f"/v1beta1/engine/credit/remaining_tokens?models={model_id}" ) return response['model_tokens'].get(model_id, 0) @@ -587,10 +582,8 @@ async def share_function_group( api_base_url=self._api_base_url, api_key=self._api_key, method='PUT', - endpoint=f"/v1beta1/function-groups/{ - function_group_id}/permissions/", + endpoint=f"/v1beta1/function-groups/{function_group_id}/permissions/", payload=payload ) - logger.debug(f"FG successfully shared: { - function_group_id} with {target_user_email}") + logger.debug(f"FG successfully shared: {function_group_id} with {target_user_email}") return raw_response From 6e8c4940332b68486ce7f7e689e1bb7277030fcb Mon Sep 17 00:00:00 2001 From: Tanay Godse Date: Sun, 29 Sep 2024 19:32:26 -0700 Subject: [PATCH 4/4] Fixed all the excess new lines --- ai_engine_sdk/client.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/ai_engine_sdk/client.py b/ai_engine_sdk/client.py index 7bb1108..7c0279c 100644 --- a/ai_engine_sdk/client.py +++ b/ai_engine_sdk/client.py @@ -113,7 +113,6 @@ class Session: _messages (List[ApiBaseMessage]): A list to store messages associated with the session. _message_ids (set[str]): A set to store unique message IDs to prevent duplication. """ - def __init__(self, api_base_url: str, api_key: str, session_id: str, function_group: str): """ Initializes a new session with the given parameters. @@ -274,8 +273,7 @@ async def get_messages(self) -> List[ApiBaseMessage]: agent_json: dict = message['agent_json'] agent_json_type: str = agent_json['type'].upper() if is_task_selection_message(message_type=agent_json_type): - indexed_task_options: dict = get_indexed_task_options_from_raw_api_response( - raw_api_response=message) + indexed_task_options: dict = get_indexed_task_options_from_raw_api_response(raw_api_response=message) newMessages.append( TaskSelectionMessage.model_validate({ 'type': agent_json_type, @@ -363,17 +361,14 @@ async def execute_function(self, function_ids: list[str], objective: str, contex }) ) - class AiEngine: def __init__(self, api_key: str, options: Optional[dict] = None): - self._api_base_url = options.get( - 'api_base_url') if options and 'api_base_url' in options else default_api_base_url + self._api_base_url = options.get('api_base_url') if options and 'api_base_url' in options else default_api_base_url self._api_key = api_key #### # Function groups #### - async def get_function_groups(self) -> List[FunctionGroup]: logger.debug("get_function_groups") publicGroups, privateGroups = await asyncio.gather( @@ -466,7 +461,6 @@ async def get_function_group_by_function(self, function_id: str): ### # Functions ### - async def get_functions_by_function_group(self, function_group_id: str) -> list[FunctionGroupFunctions]: raw_response: dict = await make_api_request( api_base_url=self._api_base_url, @@ -478,14 +472,14 @@ async def get_functions_by_function_group(self, function_group_id: str) -> list[ if "functions" in raw_response: list( map( - lambda function_name: FunctionGroupFunctions.model_validate( - {"name": function_name}), + lambda function_name: FunctionGroupFunctions.model_validate({"name": function_name}), raw_response["functions"] ) ) return result + async def get_functions(self) -> list[Function]: raw_response: dict = await make_api_request( api_base_url=self._api_base_url, @@ -502,10 +496,8 @@ async def get_functions(self) -> list[Function]: #### # Model #### - async def get_models(self) -> List[Model]: - pending_credits = [self.get_model_credits( - model_id) for model_id in DefaultModelIds] + pending_credits = [self.get_model_credits(model_id) for model_id in DefaultModelIds] models = [Model( id=model_id, @@ -552,8 +544,7 @@ async def create_session(self, function_group: str, opts: Optional[dict] = None) email=opts.get('email') if opts else "", functionGroup=function_group, preferencesEnabled=False, - requestModel=opts.get( - 'model') if opts and 'model' in opts else DefaultModelId + requestModel=opts.get('model') if opts and 'model' in opts else DefaultModelId ) response = await make_api_request( api_base_url=self._api_base_url, @@ -586,4 +577,4 @@ async def share_function_group( payload=payload ) logger.debug(f"FG successfully shared: {function_group_id} with {target_user_email}") - return raw_response + return raw_response \ No newline at end of file