Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions flo_ai/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def async_invoke(self, query, config=None) -> Iterator[Union[dict[str, Any], Any
def build(
session: FloSession,
yaml: Optional[str] = None,
yaml_path: Optional[str] = None,
routed_team: Optional[FloRouter] = None,
log_level: Optional[str] = None,
):
Expand All @@ -65,6 +66,19 @@ def build(
stacklevel=2,
)
Flo.set_log_level(log_level)
if yaml_path:
if yaml is not None:
raise FloException(
'Cannot specify both `yaml` and `yaml_path`. Use only one.'
)
try:
with open(yaml_path) as file:
yaml = file.read()
except FileNotFoundError:
raise FloException(f'YAML file at path {yaml_path} not found.')
except Exception:
raise FloException(f'Error reading YAML file at path {yaml_path}.')

if yaml is not None:
get_logger().info('Building Flo instance from YAML ...', session)
executable: ExecutableFlo = build_supervised_team(
Expand Down
11 changes: 11 additions & 0 deletions tests/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: flo/alpha-v1
kind: FloAgent
name: weather-assistant
agent:
name: WeatherAssistant
kind: agentic
job: >
Given the city name, you are capable of answering the latest weather this time of the year
by searching the internet.
tools:
- name: adder
55 changes: 55 additions & 0 deletions tests/test_yaml_file_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from flo_ai import Flo, FloSession
from langchain_openai import ChatOpenAI
from flo_ai.tools.flo_tool import flotool
from flo_ai.error.flo_exception import FloException
from typing import List
import asyncio
import pytest


@pytest.fixture
def initialize_session():
llm = ChatOpenAI(temperature=0, model_name='gpt-4o-mini', api_key='TEST_KEY')
session = FloSession(llm)
session.register_tool('adder', addition_tool)
session.register_tool('muller', mul_tool)
return session


@flotool(name='AdditionTool', description='Tool to add numbers')
async def addition_tool(numbers: List[int]) -> str:
result = sum(numbers)
await asyncio.sleep(1)
return f'The sum is {result}'


@flotool(
name='MultiplicationTool',
description='Tool to multiply numbers to get product of numbers',
)
def mul_tool(numbers: List[int]) -> str:
result = sum(numbers)
return f'The product is {result}'


def test_valid_path(initialize_session):
Flo.build(initialize_session, yaml_path='tests/test.yaml')


def test_invalid_path(initialize_session):
try:
yaml_path = 'test/test.yaml'
Flo.build(initialize_session, yaml_path=yaml_path)
except FloException as e:
assert str(e) == f'[Error -1] YAML file at path {yaml_path} not found.'


def test_both_yaml(initialize_session):
try:
yaml_path = 'test/test.yaml'
Flo.build(initialize_session, yaml='', yaml_path=yaml_path)
except FloException as e:
assert (
str(e)
== '[Error -1] Cannot specify both `yaml` and `yaml_path`. Use only one.'
)