API Agent Reference
Here we handle the connection to external software tools via the parameterisation of API calls by the LLM.
The abstract base classes
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/abc.py
fetch_results(query_model, retries=3)
abstractmethod
Fetches 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_model: the Pydantic model describing the parameterised query
Source code in biochatter/api_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/abc.py
summarise_results(question, conversation_factory, response_text)
abstractmethod
Summarises 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/abc.py
BaseQueryBuilder
Bases: ABC
An abstract base class for query builders.
Source code in biochatter/api_agent/abc.py
structured_output_prompt: ChatPromptTemplate
property
Defines 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
Creates 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/abc.py
parameterise_query(question, conversation)
abstractmethod
Parameterises a query object (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 parameterised instance of the query object (Pydantic BaseModel)
Source code in biochatter/api_agent/abc.py
The API Agent
APIAgent
Source code in biochatter/api_agent/api_agent.py
23 24 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 |
|
__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/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/api_agent.py
fetch_results(query_model)
Fetch the results of the query using the individual API's implementation (either single-step or submit-retrieve).
query_model: the parameterised query Pydantic model
Source code in biochatter/api_agent/api_agent.py
parameterise_query(question)
Use LLM to parameterise a query (a Pydantic model) based on the given question using a BioChatter conversation instance.
Source code in biochatter/api_agent/api_agent.py
summarise_results(question, response_text)
Summarise the retrieved results to extract the answer to the question.
Source code in biochatter/api_agent/api_agent.py
The API Agent for the BLAST tool
Module for handling BLAST API interactions.
Provides functionality for building queries, fetching results, and interpreting BLAST (Basic Local Alignment Search Tool) sequence alignment data.
BlastFetcher
Bases: BaseFetcher
A class for retrieving API results from BLAST.
Retrieves results from BLAST given a parameterised BlastQuery.
TODO add a limit of characters to be returned from the response.text?
Source code in biochatter/api_agent/blast.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
fetch_results(query_model, retries=20)
Submit request and fetch results from BLAST API.
Wraps individual submission and retrieval of results.
query_model: the Pydantic model of the query
retries: the number of maximum retries
str: the result from the BLAST API
Source code in biochatter/api_agent/blast.py
BlastInterpreter
Bases: BaseInterpreter
A class for interpreting BLAST results.
Source code in biochatter/api_agent/blast.py
summarise_results(question, conversation_factory, response_text)
Extract the answer from the BLAST results.
question (str): The question to be answered.
conversation_factory: A BioChatter conversation object.
response_text (str): The response.text returned by NCBI.
str: The extracted answer from the BLAST results.
Source code in biochatter/api_agent/blast.py
BlastQueryBuilder
Bases: BaseQueryBuilder
A class for building a BlastQuery object.
Source code in biochatter/api_agent/blast.py
create_runnable(query_parameters, conversation)
Create a runnable object for executing queries.
Creates a runnable using the LangChain
create_structured_output_runnable
method.
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/blast.py
parameterise_query(question, conversation)
Generate a BlastQuery object.
Generates the object based on the given question, prompt, and BioChatter conversation. Uses a Pydantic model to define the API fields. Creates a runnable that can be invoked on LLMs that are qualified to parameterise functions.
question (str): The question to be answered.
conversation: The conversation object used for parameterising the
BlastQuery.
BlastQuery: the parameterised query object (Pydantic model)
Source code in biochatter/api_agent/blast.py
BlastQueryParameters
Bases: BaseModel
Pydantic model for the parameters of a BLAST query request.
The class is used for configuring and sending a request to the NCBI BLAST query API. The fields are dynamically configured by the LLM based on the user's question.
Source code in biochatter/api_agent/blast.py
The API Agent for the OncoKB tool
OncoKBFetcher
Bases: BaseFetcher
A class for retrieving API results from OncoKB given a parameterized OncoKBQuery.
Source code in biochatter/api_agent/oncokb.py
fetch_results(request_data, retries=3)
Function to submit the OncoKB query and fetch the results directly. No multi-step procedure, thus no wrapping of submission and retrieval in this case.
request_data: OncoKBQuery object (Pydantic model) containing the
OncoKB query parameters.
str: The results of the OncoKB query.
Source code in biochatter/api_agent/oncokb.py
OncoKBInterpreter
Bases: BaseInterpreter
Source code in biochatter/api_agent/oncokb.py
summarise_results(question, conversation_factory, response_text)
Function to extract the answer from the BLAST results.
question (str): The question to be answered.
conversation_factory: A BioChatter conversation object.
response_text (str): The response.text returned by OncoKB.
str: The extracted answer from the BLAST results.
Source code in biochatter/api_agent/oncokb.py
OncoKBQueryBuilder
Bases: BaseQueryBuilder
A class for building an OncoKBQuery object.
Source code in biochatter/api_agent/oncokb.py
create_runnable(query_parameters, conversation)
Creates a runnable object for executing queries using the LangChain
create_structured_output_runnable
method.
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/oncokb.py
parameterise_query(question, conversation)
Generates an OncoKBQuery object based on the given question, prompt, and BioChatter conversation. Uses a Pydantic model to define the API fields. Creates a runnable that can be invoked on LLMs that are qualified to parameterise functions.
question (str): The question to be answered.
conversation: The conversation object used for parameterising the
OncoKBQuery.
OncoKBQueryParameters: the parameterised query object (Pydantic model)