Hello, I have some tool functions which will generate files, do I need to wrap them to make them async ? For example:
from langchain_core.tools import tool
from ase.build import molecule
from ase.collections import g2
from ase.io import write
from ase import Atoms
from tools.core import ToolOutput
import os
import asyncio
@tool
async def build_molecule_by_formula(formula: str, save_path:str):
'''
Create a molecule by its formula. Only support simple molecules present in ASE `g2` collection. The molecule will be saved in `{formula}.xyz`.
Args:
formula: The chemical formula of the molecule.
save_path: The absolute path to save the molecule.
'''
if not formula in g2.names:
raise KeyError(f"Error: Molecule {formula} is not included in ASE g2 collection. Build it by SMILES string (`build_molecule_by_smiles`).")
mol = molecule(formula)
filename = os.path.join(save_path, f"{formula}.xyz")
# recommended by AI, I don't know if this is OK.
await asyncio.to_thread(write, filename=filename, images=mol)
return ToolOutput(content=f"Molecule {formula} is written to {filename}",
outfile_list=[filename]).model_dump(mode='python')
Here, ase.io.write and ase.io.read are synchronous functions, they will take some time if read or write large files.