LLM Chains allow us to divide a complex task into a Chain of smaller tasks, where the output of one prompt becomes the input of the next.
This is not significantly slower, despite requiring a greater number of calls/requests to the LLM, because short outputs take a proportionally shorter time to generate. Although the input prompt tokens also need to be processed each time, they are less time-expensive than output generated tokens in the case of LLaMA 2.
![image](https://private-user-images.githubusercontent.com/4990842/273700146-bc520330-62d3-4bb6-8d37-6638df721587.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjYxNDkwMjYsIm5iZiI6MTcyNjE0ODcyNiwicGF0aCI6Ii80OTkwODQyLzI3MzcwMDE0Ni1iYzUyMDMzMC02MmQzLTRiYjYtOGQzNy02NjM4ZGY3MjE1ODcucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkxMiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MTJUMTM0NTI2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZGJmNTY2Yzg0ZTA0MDY4MWRmZWNjZGJmOGE4NWFmMzZhNWY4ZmM5NTBiZDVlMDRiNDJiN2NlMjA5OTc1ZjQ2NSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.7ERalNapIS0XGwlqJ_ndMOtnCrW5JVsWFA_yrIeRpa4)
The entity_extractor.py script extracts clinical entities from free text using LLMs. It uses the FHIR API to retrieve clinical data and the OpenAI, BARD, or Llama API to extract entities from the text.
To run the entity_extractor.py script, you will need:
- Python 3.x
- OpenAI: An API key for OpenAI Api (see https://openai.com/blog/openai-api)
- Llama-2: Llama.cpp installed, and a local copy of Llama-2, quantized for use in llama.cpp (see https://github.com/ggerganov/llama.cpp)
To use the entity_extractor.py script, follow these steps:
- Clone the repository to your local machine.
- Open the project in your preferred code editor.
- Install any necessary dependencies by running pip install -r requirements.txt in your terminal.
- If you need to use OpenAI models, save your OpenAI API key in a file named "openai.key".
- Run the script by typing python entity_extractor.py -a --model in your terminal, where is the name of the LLM API you want to use (openai, bard, or llama) and is the name of the model you want to run for OpenAI or path to the model in the case of Llama-2.
- View the output in the terminal or in the output pane of your code editor.
Examples:
python3.10 entity_extractor.py --api llama --model /my-drive/models/llama2/llama-2-13b-chat/ggml-model-q4_0.bin --sentences=example_cases/clinical_text.txt
python3 entity_extractor.py --api openai --model gpt-4o --sentences=example_cases/clinical_text.txt
- Wu, T., Terry, M., & Cai, C. J. (2022, April). AI chains: Transparent and controllable human-ai interaction by chaining large language model prompts. In Proceedings of the 2022 CHI conference on human factors in computing systems (pp. 1-22). https://doi.org/10.1145/3491102.3517582
- Cursor https://www.cursor.so/blog/llama-inference#user-content-fn-llama-paper [Retrieved 08/2023]
- LLaMA 2 – Meta AI https://ai.meta.com/llama/ [Retrieved 08/2023]