2023-11-08 18:07:21 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
2023-11-09 18:58:51 +03:00
|
|
|
from fastapi import HTTPException
|
|
|
|
from llm.utils.extract_api_brain_definition_values_from_llm_output import (
|
|
|
|
extract_api_brain_definition_values_from_llm_output,
|
2023-11-08 18:07:21 +03:00
|
|
|
)
|
2023-11-09 18:58:51 +03:00
|
|
|
from llm.utils.make_api_request import get_api_call_response_as_text
|
2023-12-04 20:38:54 +03:00
|
|
|
from modules.brain.service.brain_service import BrainService
|
2023-11-08 18:07:21 +03:00
|
|
|
from repository.api_brain_definition.get_api_brain_definition import (
|
|
|
|
get_api_brain_definition,
|
|
|
|
)
|
2023-12-04 20:38:54 +03:00
|
|
|
|
|
|
|
brain_service = BrainService()
|
2023-11-08 18:07:21 +03:00
|
|
|
|
|
|
|
|
2023-11-09 18:58:51 +03:00
|
|
|
def call_brain_api(brain_id: UUID, user_id: UUID, arguments: dict) -> str:
|
2023-11-08 18:07:21 +03:00
|
|
|
brain_definition = get_api_brain_definition(brain_id)
|
2023-11-09 18:58:51 +03:00
|
|
|
|
2023-11-08 18:07:21 +03:00
|
|
|
if brain_definition is None:
|
2023-11-09 18:58:51 +03:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=404, detail=f"Brain definition {brain_id} not found"
|
|
|
|
)
|
2023-11-08 18:07:21 +03:00
|
|
|
|
2023-11-09 18:58:51 +03:00
|
|
|
brain_params_values = extract_api_brain_definition_values_from_llm_output(
|
|
|
|
brain_definition.params, arguments
|
2023-11-08 18:07:21 +03:00
|
|
|
)
|
|
|
|
|
2023-11-09 18:58:51 +03:00
|
|
|
brain_search_params_values = extract_api_brain_definition_values_from_llm_output(
|
|
|
|
brain_definition.search_params, arguments
|
2023-11-08 18:07:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
secrets = brain_definition.secrets
|
|
|
|
secrets_values = {}
|
|
|
|
|
|
|
|
for secret in secrets:
|
2023-12-04 20:38:54 +03:00
|
|
|
secret_value = brain_service.external_api_secrets_repository.read_secret(
|
2023-11-08 18:07:21 +03:00
|
|
|
user_id=user_id, brain_id=brain_id, secret_name=secret.name
|
|
|
|
)
|
|
|
|
secrets_values[secret.name] = secret_value
|
|
|
|
|
2023-11-09 18:58:51 +03:00
|
|
|
return get_api_call_response_as_text(
|
2023-11-08 18:07:21 +03:00
|
|
|
api_url=brain_definition.url,
|
|
|
|
params=brain_params_values,
|
|
|
|
search_params=brain_search_params_values,
|
|
|
|
secrets=secrets_values,
|
2023-11-09 18:58:51 +03:00
|
|
|
method=brain_definition.method,
|
2023-11-08 18:07:21 +03:00
|
|
|
)
|