quivr/backend/llm/utils/call_brain_api.py
Mamadou DICKO db5a6e4b9b
feat: allow users to chat with apis (#1612)
You can now create a brain which can fetch data from external APIs with
or without authentification

- POST query example with authentification 


https://github.com/StanGirard/quivr/assets/63923024/15013ba9-dedb-4f24-9e06-49daad9de7f3


- Get query example with authentification and search params



https://github.com/StanGirard/quivr/assets/63923024/1763875d-a8e9-4478-b07c-e99ca7337942


- Get query without authentification and search params



https://github.com/StanGirard/quivr/assets/63923024/f2742963-790d-4cb2-864a-8173979b650a
2023-11-09 16:58:51 +01:00

46 lines
1.5 KiB
Python

from uuid import UUID
from fastapi import HTTPException
from llm.utils.extract_api_brain_definition_values_from_llm_output import (
extract_api_brain_definition_values_from_llm_output,
)
from llm.utils.make_api_request import get_api_call_response_as_text
from repository.api_brain_definition.get_api_brain_definition import (
get_api_brain_definition,
)
from repository.external_api_secret.read_secret import read_secret
def call_brain_api(brain_id: UUID, user_id: UUID, arguments: dict) -> str:
brain_definition = get_api_brain_definition(brain_id)
if brain_definition is None:
raise HTTPException(
status_code=404, detail=f"Brain definition {brain_id} not found"
)
brain_params_values = extract_api_brain_definition_values_from_llm_output(
brain_definition.params, arguments
)
brain_search_params_values = extract_api_brain_definition_values_from_llm_output(
brain_definition.search_params, arguments
)
secrets = brain_definition.secrets
secrets_values = {}
for secret in secrets:
secret_value = read_secret(
user_id=user_id, brain_id=brain_id, secret_name=secret.name
)
secrets_values[secret.name] = secret_value
return get_api_call_response_as_text(
api_url=brain_definition.url,
params=brain_params_values,
search_params=brain_search_params_values,
secrets=secrets_values,
method=brain_definition.method,
)