ChemCrow Documentation

repository·main·Indexed 21 days ago

https://github.com/ur-whitelab/chemcrow-public

An open-source package for solving reasoning-intensive chemical tasks using large language models augmented with specialized tools such as RDKit, paper-qa, Pubchem, and chem-space. It includes a ChemCrow agent built with Langchain and supports self-hosting RXN4Chem tools for retrosynthetic planning and reaction product prediction via Docker containers.

Tokens
1.2K
Snippets
10
Records
10
Agent score
76%

What's inside ChemCrow

  1. Self-host RXN4Chem tools using Docker

    main

    By default, ChemCrow uses the RXN4Chem API for retrosynthetic planning and reaction product prediction. To avoid API latency or key requirements, you can self-host these tools using Docker containers.

    Run the following commands to start the prediction and retrosynthesis services:

    docker run --gpus all -d -p 8051:5000 doncamilom/rxnpred:latest
    docker run --gpus all -d -p 8052:5000 doncamilom/retrosynthesis:latest
  2. Configure environment variables for ChemCrow

    main

    Before running ChemCrow, you must set your OpenAI API key. You can optionally set a Serp API key for enhanced search capabilities.

    export OPENAI_API_KEY=your-openai-api-key
    
    # Optional
    export SERP_API_KEY=your-serpapi-api-key
  3. Run organic chemistry tools via Docker

    main

    Each tool in ChemCrow is provided as a Docker container that exposes an API for requests. To run a tool, use docker run with the -d flag for detached mode and map a host port to the container's fixed internal port 5000.

    For example, to expose the tool on host port 8052, use:

    docker run -d -p 8052:5000 doncamilom/rxnpred:latest
  4. Make API requests to ChemCrow tools using curl

    main

    Once a tool container is running, you can send a POST request to the /api/v1/run endpoint. The request body must be a JSON object containing a smiles key with the SMILES string of the reactants.

    curl -X POST -H "Content-Type: application/json" -d '{"smiles": "O=C(OC(C)(C)C)c1ccc(C(=O)Nc2ccc(Cl)cc2)cc1"}' http://localhost:8052/api/v1/run
  5. Predict reactions using the Python requests library

    main

    You can interact with the ChemCrow tool APIs using the requests library in Python. The following function sends a SMILES string to the tool's /api/v1/run endpoint and returns the first predicted product from the JSON response.

    import json
    import requests
    
    def reaction_predict(reactants):
        response = requests.post(
            "http://localhost:8052/api/v1/run",
            headers={"Content-Type": "application/json"},
            data=json.dumps({"smiles": reactants})
        )
        return response.json()['product'][0]
    
    product = reaction_predict('CCOCCCCO.CC(=O)Cl')
  6. Use the ChemCrow agent for chemical tasks

    main

    To use ChemCrow, import the ChemCrow class from chemcrow.agents. Initialize the agent with a specific model (e.g., gpt-4-0613), temperature, and streaming preference, then use the .run() method to execute chemical queries.

    from chemcrow.agents import ChemCrow
    
    chem_model = ChemCrow(model="gpt-4-0613", temp=0.1, streaming=False)
    chem_model.run("What is the molecular weight of tylenol?")
  7. Use self-hosted RXN4Chem tools in ChemCrow

    main

    When running local Docker containers for RXN4Chem tools, initialize the ChemCrow agent with local_rxn=True to instruct it to use your local services instead of the remote API.

    from chemcrow.agents import ChemCrow
    
    chem_model = ChemCrow(model="gpt-4-0613", temp=0.1, streaming=False, local_rxn=True)
    chem_model.run("What is the product of the reaction between styrene and dibromine?")