API Calling: Base Classes
We provide base classes for the API Agent implementation to align individual modules.
Abstract base classes
Abstract base classes for API interaction components.
Provides base classes for query builders, fetchers, and interpreters used in API interactions and result processing.
BaseAPIModel
Bases: BaseModel
A base class for all API models.
Includes default fields uuid
and method_name
.
Source code in biochatter/api_agent/base/agent_abc.py
BaseFetcher
Bases: ABC
Abstract base class for fetchers.
A fetcher is responsible for submitting queries (in systems where
submission and fetching are separate) and fetching and saving results of
queries. It has to implement a fetch_results()
method, which can wrap a
multi-step procedure to submit and retrieve. Should implement retry method to
account for connectivity issues or processing times.
Source code in biochatter/api_agent/base/agent_abc.py
fetch_results(query_models, retries=3)
abstractmethod
Fetch results by submitting a query.
Can implement a multi-step procedure if submitting and fetching are distinct processes (e.g., in the case of long processing times as in the case of BLAST).
query_models: list of Pydantic models describing the parameterised
queries
Source code in biochatter/api_agent/base/agent_abc.py
BaseInterpreter
Bases: ABC
Abstract base class for result interpreters.
The interpreter is aware of the nature and structure of the results and can extract and summarise information from them.
Source code in biochatter/api_agent/base/agent_abc.py
summarise_results(question, conversation_factory, response_text)
abstractmethod
Summarise an answer based on the given parameters.
question (str): The question that was asked.
conversation_factory (Callable): A function that creates a
BioChatter conversation.
response_text (str): The response.text returned from the request.
A summary of the answer.
Todo:
Genericise (remove file path and n_lines parameters, and use a
generic way to get the results). The child classes should manage the
specifics of the results.
Source code in biochatter/api_agent/base/agent_abc.py
BaseQueryBuilder
Bases: ABC
An abstract base class for query builders.
Source code in biochatter/api_agent/base/agent_abc.py
structured_output_prompt
property
Define a structured output prompt template.
This provides a default implementation for an API agent that can be overridden by subclasses to return a ChatPromptTemplate-compatible object.
create_runnable(query_parameters, conversation)
abstractmethod
Create a runnable object for executing queries.
Must be implemented by subclasses. Should use the LangChain
create_structured_output_runnable
method to generate the Callable.
query_parameters: A Pydantic data model that specifies the fields of
the API that should be queried.
conversation: A BioChatter conversation object.
A Callable object that can execute the query.
Source code in biochatter/api_agent/base/agent_abc.py
parameterise_query(question, conversation)
abstractmethod
Parameterise a query object.
Parameterises a Pydantic model with the fields of the API based on the given question using a BioChatter conversation instance. Must be implemented by subclasses.
question (str): The question to be answered.
conversation: The BioChatter conversation object containing the LLM
that should parameterise the query.
A list containing one or more parameterised instance(s) of the query
object (Pydantic BaseModel).
Source code in biochatter/api_agent/base/agent_abc.py
BaseTools
Abstract base class for tools.
Source code in biochatter/api_agent/base/agent_abc.py
make_pydantic_tools()
Uses pydantics create_model to create a list of pydantic tools from a dictionary of parameters
Source code in biochatter/api_agent/base/agent_abc.py
The API Agent
Base API agent module.
APIAgent
Source code in biochatter/api_agent/base/api_agent.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
__init__(conversation_factory, query_builder, fetcher, interpreter)
API agent class to interact with a tool's API for querying and fetching
results. The query fields have to be defined in a Pydantic model
(BaseModel
) and used (i.e., parameterised by the LLM) in the query
builder. Specific API agents are defined in submodules of this directory
(api_agent
). The agent's logic is implemented in the execute
method.
Attributes
conversation_factory (Callable): A function used to create a
BioChatter conversation, providing LLM access.
query_builder (BaseQueryBuilder): An instance of a child of the
BaseQueryBuilder class.
result_fetcher (BaseFetcher): An instance of a child of the
BaseFetcher class.
result_interpreter (BaseInterpreter): An instance of a child of the
BaseInterpreter class.
Source code in biochatter/api_agent/base/api_agent.py
execute(question)
Wrapper that uses class methods to execute the API agent logic. Consists of 1) query generation, 2) query submission, 3) results fetching, and 4) answer extraction. The final answer is stored in the final_answer attribute.
question (str): The question to be answered.
Source code in biochatter/api_agent/base/api_agent.py
fetch_results(query_models)
Fetch the results of the query using the individual API's implementation (either single-step or submit-retrieve).
query_models: list of parameterised query Pydantic models
Source code in biochatter/api_agent/base/api_agent.py
parameterise_query(question)
Use LLM to parameterise a query (a list of Pydantic models) based on the given question using a BioChatter conversation instance.
Source code in biochatter/api_agent/base/api_agent.py
summarise_results(question, response_text)
Summarise the retrieved results to extract the answer to the question.