Skip to content

Commit

Permalink
Merge pull request #16 from fetchai/feat/new-endpoints-integration-an…
Browse files Browse the repository at this point in the history
…d-tools

new endpoints integrations + tools
  • Loading branch information
qati authored Sep 6, 2024
2 parents e2b6c54 + 62f4266 commit 71f3b27
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-sdk-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master

jobs:
test:
Expand Down
71 changes: 70 additions & 1 deletion ai_engine_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class FunctionGroup(BaseModel):
isPrivate: bool


class Function(BaseModel):
uuid: str
name: str


class FunctionGroupFunctions(BaseModel):
name: str


class CreateFunctionGroupSchema(BaseModel):
# Not working, this is pydantic v2 style and we still on the v1
name: str
Expand Down Expand Up @@ -271,7 +280,6 @@ def __init__(self, api_key: str, options: Optional[dict] = None):
####
# Function groups
####

async def get_function_groups(self) -> List[FunctionGroup]:
logger.debug("get_function_groups")
publicGroups, privateGroups = await asyncio.gather(
Expand Down Expand Up @@ -335,6 +343,67 @@ async def delete_function_group(self, function_group_id: str):
endpoint=f"/v1beta1/function-groups/{function_group_id}/",
)
logger.debug(f"Function group deleted: {function_group_id}")
raw_response: dict = await make_api_request(
api_base_url=self._api_base_url,
api_key=self._api_key,
method='GET',
endpoint="/v1beta1/function-groups/public/"
)
return list(
map(
lambda item: FunctionGroup.model_validate(item),
raw_response
)
)

async def get_function_group_by_function(self, function_id: str):
raw_response: dict = await make_api_request(
api_base_url=self._api_base_url,
api_key=self._api_key,
method='GET',
endpoint=f"/v1beta1/function/{function_id}/groups"
)
return list(
map(
lambda item: FunctionGroup.model_validate(item),
raw_response
)
)
###
# 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,
api_key=self._api_key,
method='GET',
endpoint=f"/v1beta1/function-groups/{function_group_id}/functions/"
)
result = []
if "functions" in raw_response:
list(
map(
lambda function_name: FunctionGroupFunctions.parse_obj({"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,
api_key=self._api_key,
method='GET',
endpoint=f"/v1beta1/functions/"
)
return list(
map(
lambda item: Function.parse_obj(item),
raw_response
)
)
####
# Model
####
Expand Down
50 changes: 50 additions & 0 deletions examples/functions_by_function_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import argparse
import asyncio
import os
from pprint import pprint

from ai_engine_sdk import AiEngine


async def main(target_environment: str, agentverse_api_key: str, function_groups: list[str]):
# Request from cli args.
options = {}
if target_environment:
options = {"api_base_url": target_environment}

c = AiEngine(api_key=agentverse_api_key, options=options)

for function_group in function_groups:
print(f"======= function_group: {function_group} =======")
user_functions = await c.get_functions_by_function_group(function_group_id=function_group)
pprint(user_functions)
print("-----")


if __name__ == '__main__':
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("AV_API_KEY", "")

parser = argparse.ArgumentParser()
parser.add_argument(
"-e",
"--target_environment",
type=str,
required=False,
help="The target environment: staging, localhost, production... You need to explicitly add the domain. By default it will be production."
)
parser.add_argument(
"-fgs",
"--function_groups",
nargs="+",
required=True,
help="List of function groups ids to get their functions."
)
args = parser.parse_args()

target_environment = args.target_environment
function_groups: list[str] = args.function_groups

asyncio.run(main(agentverse_api_key=api_key, target_environment=target_environment, function_groups=function_groups))

91 changes: 91 additions & 0 deletions examples/list_function_groups_by.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import argparse
import asyncio
import os
from pprint import pprint

from ai_engine_sdk import AiEngine, FunctionGroup
from dotenv import load_dotenv

load_dotenv()
AV_API_KEY=os.getenv("AV_API_KEY", "")


async def get_functions_groups_by_user(
client: AiEngine,
) -> list[FunctionGroup]:
return await client.get_function_groups()

async def get_fgs_by_function(client: AiEngine, function_id: str) -> list[FunctionGroup]:
return await client.get_function_group_by_function(function_id)

def render(data: list[FunctionGroup], field: str) -> list:
result = []
if field:
for i in data:
# item = {f: i.__getattribute__(f) for f in fields}
# result.append(item)
result.append(i.__getattribute__(field))
else:
result = data
pprint(result)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-e",
"--target_environment",
type=str,
required=False,
help="The target environment: staging, localhost, production... You need to explicitly add the domain. By default it will be production."
)
parser.add_argument(
"-by",
"--get-by",
type=str,
help="You can get function groups by: function, user",
default="user",
required=False
)
parser.add_argument(
"-by-v",
"--get-by-value",
type=str,
help="Value of the field you want filter by (if user do not provide anything)",
required=False
)
# parser.add_argument(
# "-f",
# "--filter",
# required=False,
# nargs="+",
# help="You can ask for returning just a list of a concrete field."
# )
parser.add_argument(
"-f",
"--filter",
required=False,
type=str,
help="You can ask for returning just a list of a concrete field."
)
args = parser.parse_args()
target_environment = args.target_environment
filter_fields = args.filter
get_by = args.get_by
options = {}
if target_environment:
options = {"api_base_url": target_environment}


c = AiEngine(api_key=AV_API_KEY, options=options)

if get_by == "function":
function_id = args.get_by_value
function_to_execute = get_fgs_by_function(client=c, function_id=function_id)
elif get_by == "user":
function_to_execute = get_functions_groups_by_user(client=c)
result = asyncio.run(function_to_execute)
print(filter_fields)


render(data=result, field=filter_fields)
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
from pprint import pprint

from ai_engine_sdk import AiEngine
from dotenv import load_dotenv

load_dotenv()
AV_API_KEY=os.getenv("AV_API_KEY", "")

async def main(target_environment: str, agentverse_api_key: str):
# Request from cli args.
options = {}
if target_environment:
options = {"api_base_url": target_environment}

async def get_functions_by_user(
client: AiEngine,
):
return await client.get_function_groups()
c = AiEngine(api_key=agentverse_api_key, options=options)

user_functions = await c.get_functions()
pprint(user_functions)


if __name__ == '__main__':
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("AV_API_KEY", "")

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-e",
Expand All @@ -27,19 +33,7 @@ async def get_functions_by_user(
)
args = parser.parse_args()

# user_email = args.user_email
target_environment = args.target_environment

options = {}
if target_environment:
options = {"api_base_url": target_environment}
c = AiEngine(api_key=AV_API_KEY, options=options)


result = asyncio.run(
get_functions_by_user(
client=c
)
)
asyncio.run(main(agentverse_api_key=api_key, target_environment=target_environment))

pprint(result)
1 change: 0 additions & 1 deletion examples/share_function_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async def main(
return result


return result
if __name__ == "__main__":
from dotenv import load_dotenv
load_dotenv()
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from ai_engine_sdk import AiEngine, FunctionGroup
from ai_engine_sdk.client import Session


def get_ai_engine_client(api_key: str, environment_domain: str) -> AiEngine:
Expand All @@ -25,3 +26,12 @@ def ai_engine_client(request) -> AiEngine:
@pytest.fixture(scope="module")
async def function_groups(ai_engine_client) -> list[FunctionGroup]:
function_groups: list[FunctionGroup] = await ai_engine_client.get_function_groups()
return function_groups

# @pytest.fixture(scope="module")
# async def session_with_next_generation_model(ai_engine_client) -> Session:
# # TODO: We need a concrete function group id for the integration tests in the CI.
# session: Session = await ai_engine_client.create_session(
# function_group=function_groups, opts={"model": "next-gen"}
# )
# return session
12 changes: 12 additions & 0 deletions tests/integration/test_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest

from ai_engine_sdk.client import Session


# class TestSessionClient:
# @pytest.mark.asyncio
# async def test_start_session_with_execute_functions(self, session_with_next_generation_model: Session):
# # TODO: we need to define the function ids related to the function-group related to the user test.
# session_with_next_generation_model.execute_function(function_ids=..., objective=..., context=...)
#
#

0 comments on commit 71f3b27

Please sign in to comment.