Skip to content

Commit 71f3b27

Browse files
authored
Merge pull request #16 from fetchai/feat/new-endpoints-integration-and-tools
new endpoints integrations + tools
2 parents e2b6c54 + 62f4266 commit 71f3b27

File tree

8 files changed

+250
-25
lines changed

8 files changed

+250
-25
lines changed

.github/workflows/ci-sdk-tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
on:
22
push:
33
branches:
4-
- main
4+
- master
55
pull_request:
66
branches:
7-
- main
7+
- master
88

99
jobs:
1010
test:

ai_engine_sdk/client.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ class FunctionGroup(BaseModel):
6262
isPrivate: bool
6363

6464

65+
class Function(BaseModel):
66+
uuid: str
67+
name: str
68+
69+
70+
class FunctionGroupFunctions(BaseModel):
71+
name: str
72+
73+
6574
class CreateFunctionGroupSchema(BaseModel):
6675
# Not working, this is pydantic v2 style and we still on the v1
6776
name: str
@@ -271,7 +280,6 @@ def __init__(self, api_key: str, options: Optional[dict] = None):
271280
####
272281
# Function groups
273282
####
274-
275283
async def get_function_groups(self) -> List[FunctionGroup]:
276284
logger.debug("get_function_groups")
277285
publicGroups, privateGroups = await asyncio.gather(
@@ -335,6 +343,67 @@ async def delete_function_group(self, function_group_id: str):
335343
endpoint=f"/v1beta1/function-groups/{function_group_id}/",
336344
)
337345
logger.debug(f"Function group deleted: {function_group_id}")
346+
raw_response: dict = await make_api_request(
347+
api_base_url=self._api_base_url,
348+
api_key=self._api_key,
349+
method='GET',
350+
endpoint="/v1beta1/function-groups/public/"
351+
)
352+
return list(
353+
map(
354+
lambda item: FunctionGroup.model_validate(item),
355+
raw_response
356+
)
357+
)
358+
359+
async def get_function_group_by_function(self, function_id: str):
360+
raw_response: dict = await make_api_request(
361+
api_base_url=self._api_base_url,
362+
api_key=self._api_key,
363+
method='GET',
364+
endpoint=f"/v1beta1/function/{function_id}/groups"
365+
)
366+
return list(
367+
map(
368+
lambda item: FunctionGroup.model_validate(item),
369+
raw_response
370+
)
371+
)
372+
###
373+
# Functions
374+
###
375+
async def get_functions_by_function_group(self, function_group_id: str) -> list[FunctionGroupFunctions]:
376+
raw_response: dict = await make_api_request(
377+
api_base_url=self._api_base_url,
378+
api_key=self._api_key,
379+
method='GET',
380+
endpoint=f"/v1beta1/function-groups/{function_group_id}/functions/"
381+
)
382+
result = []
383+
if "functions" in raw_response:
384+
list(
385+
map(
386+
lambda function_name: FunctionGroupFunctions.parse_obj({"name": function_name}),
387+
raw_response["functions"]
388+
)
389+
)
390+
391+
return result
392+
393+
394+
async def get_functions(self) -> list[Function]:
395+
raw_response: dict = await make_api_request(
396+
api_base_url=self._api_base_url,
397+
api_key=self._api_key,
398+
method='GET',
399+
endpoint=f"/v1beta1/functions/"
400+
)
401+
return list(
402+
map(
403+
lambda item: Function.parse_obj(item),
404+
raw_response
405+
)
406+
)
338407
####
339408
# Model
340409
####
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import argparse
2+
import asyncio
3+
import os
4+
from pprint import pprint
5+
6+
from ai_engine_sdk import AiEngine
7+
8+
9+
async def main(target_environment: str, agentverse_api_key: str, function_groups: list[str]):
10+
# Request from cli args.
11+
options = {}
12+
if target_environment:
13+
options = {"api_base_url": target_environment}
14+
15+
c = AiEngine(api_key=agentverse_api_key, options=options)
16+
17+
for function_group in function_groups:
18+
print(f"======= function_group: {function_group} =======")
19+
user_functions = await c.get_functions_by_function_group(function_group_id=function_group)
20+
pprint(user_functions)
21+
print("-----")
22+
23+
24+
if __name__ == '__main__':
25+
from dotenv import load_dotenv
26+
load_dotenv()
27+
api_key = os.getenv("AV_API_KEY", "")
28+
29+
parser = argparse.ArgumentParser()
30+
parser.add_argument(
31+
"-e",
32+
"--target_environment",
33+
type=str,
34+
required=False,
35+
help="The target environment: staging, localhost, production... You need to explicitly add the domain. By default it will be production."
36+
)
37+
parser.add_argument(
38+
"-fgs",
39+
"--function_groups",
40+
nargs="+",
41+
required=True,
42+
help="List of function groups ids to get their functions."
43+
)
44+
args = parser.parse_args()
45+
46+
target_environment = args.target_environment
47+
function_groups: list[str] = args.function_groups
48+
49+
asyncio.run(main(agentverse_api_key=api_key, target_environment=target_environment, function_groups=function_groups))
50+

examples/list_function_groups_by.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import argparse
2+
import asyncio
3+
import os
4+
from pprint import pprint
5+
6+
from ai_engine_sdk import AiEngine, FunctionGroup
7+
from dotenv import load_dotenv
8+
9+
load_dotenv()
10+
AV_API_KEY=os.getenv("AV_API_KEY", "")
11+
12+
13+
async def get_functions_groups_by_user(
14+
client: AiEngine,
15+
) -> list[FunctionGroup]:
16+
return await client.get_function_groups()
17+
18+
async def get_fgs_by_function(client: AiEngine, function_id: str) -> list[FunctionGroup]:
19+
return await client.get_function_group_by_function(function_id)
20+
21+
def render(data: list[FunctionGroup], field: str) -> list:
22+
result = []
23+
if field:
24+
for i in data:
25+
# item = {f: i.__getattribute__(f) for f in fields}
26+
# result.append(item)
27+
result.append(i.__getattribute__(field))
28+
else:
29+
result = data
30+
pprint(result)
31+
32+
33+
if __name__ == "__main__":
34+
parser = argparse.ArgumentParser()
35+
parser.add_argument(
36+
"-e",
37+
"--target_environment",
38+
type=str,
39+
required=False,
40+
help="The target environment: staging, localhost, production... You need to explicitly add the domain. By default it will be production."
41+
)
42+
parser.add_argument(
43+
"-by",
44+
"--get-by",
45+
type=str,
46+
help="You can get function groups by: function, user",
47+
default="user",
48+
required=False
49+
)
50+
parser.add_argument(
51+
"-by-v",
52+
"--get-by-value",
53+
type=str,
54+
help="Value of the field you want filter by (if user do not provide anything)",
55+
required=False
56+
)
57+
# parser.add_argument(
58+
# "-f",
59+
# "--filter",
60+
# required=False,
61+
# nargs="+",
62+
# help="You can ask for returning just a list of a concrete field."
63+
# )
64+
parser.add_argument(
65+
"-f",
66+
"--filter",
67+
required=False,
68+
type=str,
69+
help="You can ask for returning just a list of a concrete field."
70+
)
71+
args = parser.parse_args()
72+
target_environment = args.target_environment
73+
filter_fields = args.filter
74+
get_by = args.get_by
75+
options = {}
76+
if target_environment:
77+
options = {"api_base_url": target_environment}
78+
79+
80+
c = AiEngine(api_key=AV_API_KEY, options=options)
81+
82+
if get_by == "function":
83+
function_id = args.get_by_value
84+
function_to_execute = get_fgs_by_function(client=c, function_id=function_id)
85+
elif get_by == "user":
86+
function_to_execute = get_functions_groups_by_user(client=c)
87+
result = asyncio.run(function_to_execute)
88+
print(filter_fields)
89+
90+
91+
render(data=result, field=filter_fields)

examples/list_function_groups_by_user_sdk.py renamed to examples/list_user_functions.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
from pprint import pprint
55

66
from ai_engine_sdk import AiEngine
7-
from dotenv import load_dotenv
87

9-
load_dotenv()
10-
AV_API_KEY=os.getenv("AV_API_KEY", "")
118

9+
async def main(target_environment: str, agentverse_api_key: str):
10+
# Request from cli args.
11+
options = {}
12+
if target_environment:
13+
options = {"api_base_url": target_environment}
1214

13-
async def get_functions_by_user(
14-
client: AiEngine,
15-
):
16-
return await client.get_function_groups()
15+
c = AiEngine(api_key=agentverse_api_key, options=options)
1716

17+
user_functions = await c.get_functions()
18+
pprint(user_functions)
19+
20+
21+
if __name__ == '__main__':
22+
from dotenv import load_dotenv
23+
load_dotenv()
24+
api_key = os.getenv("AV_API_KEY", "")
1825

19-
if __name__ == "__main__":
2026
parser = argparse.ArgumentParser()
2127
parser.add_argument(
2228
"-e",
@@ -27,19 +33,7 @@ async def get_functions_by_user(
2733
)
2834
args = parser.parse_args()
2935

30-
# user_email = args.user_email
3136
target_environment = args.target_environment
3237

33-
options = {}
34-
if target_environment:
35-
options = {"api_base_url": target_environment}
36-
c = AiEngine(api_key=AV_API_KEY, options=options)
37-
38-
39-
result = asyncio.run(
40-
get_functions_by_user(
41-
client=c
42-
)
43-
)
38+
asyncio.run(main(agentverse_api_key=api_key, target_environment=target_environment))
4439

45-
pprint(result)

examples/share_function_group.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ async def main(
2626
return result
2727

2828

29-
return result
3029
if __name__ == "__main__":
3130
from dotenv import load_dotenv
3231
load_dotenv()

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from ai_engine_sdk import AiEngine, FunctionGroup
6+
from ai_engine_sdk.client import Session
67

78

89
def get_ai_engine_client(api_key: str, environment_domain: str) -> AiEngine:
@@ -25,3 +26,12 @@ def ai_engine_client(request) -> AiEngine:
2526
@pytest.fixture(scope="module")
2627
async def function_groups(ai_engine_client) -> list[FunctionGroup]:
2728
function_groups: list[FunctionGroup] = await ai_engine_client.get_function_groups()
29+
return function_groups
30+
31+
# @pytest.fixture(scope="module")
32+
# async def session_with_next_generation_model(ai_engine_client) -> Session:
33+
# # TODO: We need a concrete function group id for the integration tests in the CI.
34+
# session: Session = await ai_engine_client.create_session(
35+
# function_group=function_groups, opts={"model": "next-gen"}
36+
# )
37+
# return session

tests/integration/test_session.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
from ai_engine_sdk.client import Session
4+
5+
6+
# class TestSessionClient:
7+
# @pytest.mark.asyncio
8+
# async def test_start_session_with_execute_functions(self, session_with_next_generation_model: Session):
9+
# # TODO: we need to define the function ids related to the function-group related to the user test.
10+
# session_with_next_generation_model.execute_function(function_ids=..., objective=..., context=...)
11+
#
12+
#

0 commit comments

Comments
 (0)