Convert Figma logo to code with AI

OpenBMB logoBMTools

Tool Learning for Big Models, Open-Source Solutions of ChatGPT-Plugins

2,793
256
2,793
12

Top Related Projects

11,862

An open-source NLP research library, built on PyTorch.

31,682

Facebook AI Research Sequence-to-Sequence Toolkit written in Python.

39,112

DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.

Code for the paper "Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer"

An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries

Quick Overview

BMTools is a Python library that provides a set of tools for working with large language models (LLMs) and other natural language processing (NLP) tasks. It includes utilities for model loading, tokenization, text generation, and more, making it easier to work with LLMs in a variety of applications.

Pros

  • Comprehensive Functionality: BMTools offers a wide range of tools and utilities for working with LLMs, covering tasks such as model loading, tokenization, text generation, and more.
  • Ease of Use: The library provides a user-friendly API, making it easy for developers to integrate LLM functionality into their applications.
  • Flexibility: BMTools supports multiple LLM backends, allowing users to work with different models and frameworks.
  • Active Development: The project is actively maintained and regularly updated, ensuring that it stays up-to-date with the latest developments in the LLM ecosystem.

Cons

  • Limited Documentation: While the project has some documentation, it could be more comprehensive, making it harder for new users to get started.
  • Dependency on External Libraries: BMTools relies on several external libraries, which can increase the complexity of the setup process and introduce potential compatibility issues.
  • Performance Concerns: Depending on the specific use case and the LLM being used, the performance of BMTools may not be optimal, especially for large-scale or real-time applications.
  • Narrow Focus: The project is primarily focused on LLM-related tasks, which may limit its usefulness for users who need a more general-purpose NLP toolkit.

Code Examples

Here are a few examples of how to use BMTools:

  1. Loading a Pre-trained Model:
from bmtools.models import load_model
model = load_model('gpt2')

This code loads a pre-trained GPT-2 model using the load_model function from the bmtools.models module.

  1. Tokenizing Text:
from bmtools.tokenizers import get_tokenizer
tokenizer = get_tokenizer('gpt2')
tokens = tokenizer.encode('This is a sample text.')

This code creates a tokenizer for the GPT-2 model and uses it to tokenize a sample text.

  1. Generating Text:
from bmtools.generation import generate_text
text = generate_text(model, 'This is a prompt.', max_length=50, num_return_sequences=3)
print(text)

This code uses the generate_text function from the bmtools.generation module to generate three text sequences based on the provided prompt.

  1. Performing Sentiment Analysis:
from bmtools.tasks import sentiment_analysis
sentiment = sentiment_analysis(model, 'I love this product!')
print(sentiment)

This code demonstrates how to use the sentiment_analysis function from the bmtools.tasks module to perform sentiment analysis on a given text.

Getting Started

To get started with BMTools, follow these steps:

  1. Install the library using pip:
pip install bmtools
  1. Import the necessary modules and functions from the library:
from bmtools.models import load_model
from bmtools.tokenizers import get_tokenizer
from bmtools.generation import generate_text
from bmtools.tasks import sentiment_analysis
  1. Load a pre-trained model:
model = load_model('gpt2')
  1. Use the various functions provided by the library to perform tasks such as tokenization, text generation, and sentiment analysis:
tokens = get_tokenizer('gpt2').encode('This is a sample text.')
text = generate_text(model, 'This is a prompt.', max_length=50, num_return_sequences=3)
sentiment = sentiment_analysis(model, 'I love this product!')
  1. Explore the documentation and the available modules to learn more about the capabilities of BMTools and how to integrate it into your own projects.

Competitor Comparisons

11,862

An open-source NLP research library, built on PyTorch.

Pros of allenai/allennlp

  • Extensive documentation and tutorials, making it easier for new users to get started.
  • Supports a wide range of NLP tasks, including text classification, sequence labeling, and question answering.
  • Active community with regular updates and bug fixes.

Cons of allenai/allennlp

  • Larger codebase and more complex to set up compared to OpenBMB/BMTools.
  • May have a steeper learning curve for users not familiar with the AllenNLP framework.
  • Potentially slower performance for certain tasks due to the overhead of the framework.

Code Comparison

OpenBMB/BMTools

from bmtools.models import BertModel
model = BertModel.from_pretrained('bert-base-uncased')
output = model(input_ids, attention_mask)

allenai/allennlp

from allennlp.models.archival import load_archive
archive = load_archive('https://storage.googleapis.com/allennlp-public-models/bert-base-uncased-sst-2-v1.0.0.tar.gz')
model = archive.model
output = model(input_ids, attention_mask)
31,682

Facebook AI Research Sequence-to-Sequence Toolkit written in Python.

Pros of facebookresearch/fairseq

  • Extensive documentation and active community support
  • Supports a wide range of NLP tasks, including machine translation, language modeling, and text generation
  • Provides pre-trained models for various languages and tasks

Cons of facebookresearch/fairseq

  • Relatively complex to set up and configure compared to BMTools
  • May have a steeper learning curve for beginners
  • Requires more computational resources for training and inference

Code Comparison

BMTools:

from bmtools.models import BertModel
model = BertModel.from_pretrained('bert-base-uncased')
output = model(input_ids, attention_mask)

fairseq:

from fairseq.models.transformer import TransformerModel
model = TransformerModel.from_pretrained('wmt14.en-fr', checkpoint_file='model.pt')
output = model.forward(src_tokens, src_lengths)

Both libraries provide a similar interface for loading pre-trained models and performing inference. However, the specific model classes and method names may differ between the two libraries.

39,112

DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.

Pros of DeepSpeed

  • Focuses on optimizing deep learning training and inference, offering significant speed improvements and memory efficiency
  • Provides a comprehensive suite of optimization techniques, including ZeRO, 3D parallelism, and pipeline parallelism
  • Has extensive documentation and is widely adopted in the AI research community

Cons of DeepSpeed

  • Steeper learning curve due to its complexity and advanced features
  • Primarily designed for large-scale models and may be overkill for smaller projects
  • Requires more setup and configuration compared to simpler tools

Code Comparison

DeepSpeed:

import deepspeed
model_engine, optimizer, _, _ = deepspeed.initialize(
    args=args,
    model=model,
    model_parameters=params
)

BMTools:

from bmtools import load_tool
tool = load_tool("web_search")
result = tool.run("What is the capital of France?")

Key Differences

  • DeepSpeed is focused on optimizing deep learning models, while BMTools is a collection of AI tools for various tasks
  • DeepSpeed offers more advanced features for large-scale model training, whereas BMTools provides simpler, task-specific tools
  • BMTools is more accessible for beginners and smaller projects, while DeepSpeed is better suited for advanced users and large-scale deployments

Code for the paper "Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer"

Pros of Text-to-Text Transfer Transformer

  • Extensive documentation and resources available, making it easier to get started and understand the model.
  • Supports a wide range of text-to-text tasks, including translation, summarization, and question answering.
  • Provides pre-trained models that can be fine-tuned for specific tasks, saving time and effort.

Cons of Text-to-Text Transfer Transformer

  • Larger model size and higher computational requirements compared to BMTools, which may limit its use on resource-constrained devices.
  • Primarily focused on text-to-text tasks, while BMTools offers a broader range of capabilities, including image-to-text and other multimodal tasks.

Code Comparison

Text-to-Text Transfer Transformer:

from transformers import T5ForConditionalGeneration, T5Tokenizer

model = T5ForConditionalGeneration.from_pretrained('t5-base')
tokenizer = T5Tokenizer.from_pretrained('t5-base')

input_text = "Translate this sentence to French."
input_ids = tokenizer.encode(input_text, return_tensors='pt')

output_ids = model.generate(input_ids, max_length=50, num_beams=4, early_stopping=True)
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
print(output_text)

BMTools:

from bmtools.models import BMModel

model = BMModel.from_pretrained('bmtools/base')
input_text = "Translate this sentence to French."
output_text = model.generate(input_text, task='translation', target_language='fr')
print(output_text)

An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries

Pros of GPT-NeoX

  • Supports a wider range of language models, including GPT-3, GPT-J, and GPT-NeoX
  • Provides a more comprehensive set of tools for training and evaluating language models
  • Offers better support for distributed training on multiple GPUs

Cons of GPT-NeoX

  • Requires more computational resources for training and deployment
  • May have a steeper learning curve for users unfamiliar with large language models
  • Lacks some of the specialized features and tools available in BMTools

Code Comparison

BMTools

from bmtools.model import BMModel
model = BMModel.from_pretrained("OpenBMB/chinese-roberta-wwm")

GPT-NeoX

from gpt_neox import GPTNeoXModel, GPTNeoXConfig
config = GPTNeoXConfig.from_pretrained("EleutherAI/gpt-neox-20b")
model = GPTNeoXModel(config)

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

BMTools

News • Setup • How To Use • Paper • Docs • Paper List • Demo • Citation •

Read this in Chinese.



BMTools is an open-source repository that extends language models using tools and serves as a platform for the community to build and share tools. In this repository, you can (1) easily build a plugin by writing python functions (2) use external ChatGPT-Plugins.

This project is inspired by the open-source project LangChain and optimized for the usage of open-sourced tools like ChatGPT-Plugins, striving to achieve the open-source academic version of ChatGPT-Plugins.

  • For new features and further developments, please go to XAgent.

  • A demo of using BMTools to manipulate tools for meta analysis.

What's New

  • [2023/5/28] We release ToolBench, a large-scale tool learning benchmark together with a capable model.

  • [2023/5/25] The evaluation data used in the paper is partially released at data-test, we have also created large-scale SFT (100k+) high-quality tool-use training data at data-sft.

  • [2023/5/19] Three new Tools are supported: Baidu Map, Google Scholar Search, and Zillow

  • [2023/5/18] WebCPM is accepted by ACL 2023, a Chinese version of WebGPT.

  • [older] Auto-GPT and BabyAGI are supported in BMTools.

1. Setup

git clone git@github.com:OpenBMB/BMTools.git
cd BMTools
pip install --upgrade pip
pip install -r requirements.txt
python setup.py develop

To support CPM-Bee:

git clone -b main --single-branch https://github.com/OpenBMB/CPM-Bee.git
cp -rf CPM-Bee/src/cpm_live bmtools/models/

2. Use existing tools

2.1 Set up tools

2.1.1 Local tools

Add your api keys to secret_keys.sh, then start the local tools

source secret_keys.sh
python host_local_tools.py

Then set the url of the plugin to http://127.0.0.1:8079/tools/{tool_name}/ (Remember the tailing /).

2.1.2 Use online ChatGPT-Plugins

Just load it with the URL pointed to the .well-known/ai-plugin.json For example, set the url to https://www.klarna.com/, where https://www.klarna.com/.well-known/ai-plugin.json is a valid configuration.

2.2 Use a single tool

from bmtools.agent.singletool import load_single_tools, STQuestionAnswerer

tool_name, tool_url = 'klarna',  'https://www.klarna.com/'
tool_name, tool_config = load_single_tools(tool_name, tool_url)
print(tool_name, tool_config)
stqa =  STQuestionAnswerer()

agent = stqa.load_tools(tool_name, tool_config)
agent("{Your Question}")

2.3 Use multiple tools

We can use multiple tools at the same time. Basically, the language model will do it recursively. It will treat the whole tool as an API, send questions to it, and the tool calls its sub-APIs to solve the question and send it back to parent tools.

Try this functionality using scripts like:

from bmtools.agent.tools_controller import load_valid_tools, MTQuestionAnswerer
tools_mappings = {
    "klarna": "https://www.klarna.com/",
    "chemical-prop": "http://127.0.0.1:8079/tools/chemical-prop/",
    "wolframalpha": "http://127.0.0.1:8079/tools/wolframalpha/",
}

tools = load_valid_tools(tools_mappings)

qa =  MTQuestionAnswerer(openai_api_key='', all_tools=tools)

agent = qa.build_runner()

agent("How many benzene rings are there in 9H-Carbazole-3-carboxaldehyde? and what is sin(x)*exp(x)'s plot, what is it integrated from 0 to 1? ")

2.4 Use the web demo

  1. Add your plugin to the mappings at beginning of web_demo.py

  2. Start the webdemo

python web_demo.py

3. Use customized tools

3.1 Develop a tool locally

To develop a tool locally, you need to write a python function to build the tool and register it to the registry.

For example, you can write a tool that can execute python code and return the result. The following is a sample code:

from bmtools.tools import Tool
from pydantic import BaseModel

class ExecutionQuery(BaseModel):
    code: str

class ExecutionResult(BaseModel):
    result: str

def build_python_tool(config) -> Tool:
    tool = Tool(
        "PythonTool",
        "A plugin that can execute python code",
        name_for_model="python", 
        description_for_model="A plugin that can execute python code",
        contact_email="your@email",
    )

    @tool.post("/execute")
    def execute_python_code(query : ExecutionQuery) -> ExecutionResult:
        return ExecutionResult(
            result=eval(query.code)
        )
    
    return tool

Then you need to register the tool to the registry using the following code:

from bmtools.tools import register

@register("python")
def register_python_tool():
    return build_python_tool

Here we register the tool with the name python.

3.2 Contributing to BMTools

After you have developed a tool, you can contribute it to BMTools by following the steps below:

  1. Fork this repository
  2. Create a folder in bmtools/tools/{tool_name}
  3. Add an api.py to the folder: bmtools/tools/{tool_name}/api.py and a __init__.py to the folder: bmtools/tools/{tool_name}/__init__.py
  4. Register the tool in the __init__.py file you created in step 3 using the code in section 3.1
  5. Import your tool in the __init__.py file under bmtools/tools
  6. Add a test.py to test your tool automatically
  7. Add a readme.md in your folder containing a brief introduction, contributor information, or anything you want to let others know.

4. Optimize your tool's prompt

The functions you wrote will be converted into an interface compatible with the OpenAI plugin. The AI models will read the name, description of the tools, as well as the name and descriptions of the tools' APIs. You can adjust the following aspect to make your API better understood by AI models.

  • (1) name_for_model (tell the model what the tool is)
  • (2) description_for_model (this will be displayed to the model before the tool is called, and you can include information on how to use the APIs)
  • (3) The function name for each API function, as well as the name in @tool.get(). It's best if these two names match, as the name plays an important role in the model's API selection.
  • (4) The function's doc string (can suggest to the model whether to use this API or not)
  • (5) The function's return value, which can provide the model with error messages to guide its next steps, such as retrying or indicating a preferred next step
  • (6) Reduce the errors in your API function.

A simple example to refer to is the Wolfram Alpha API.

Citation

If you use BMTools in your research, please cite:

@misc{qin2023tool,
      title={Tool Learning with Foundation Models}, 
      author={Yujia Qin and Shengding Hu and Yankai Lin and Weize Chen and Ning Ding and Ganqu Cui and Zheni Zeng and Yufei Huang and Chaojun Xiao and Chi Han and Yi Ren Fung and Yusheng Su and Huadong Wang and Cheng Qian and Runchu Tian and Kunlun Zhu and Shihao Liang and Xingyu Shen and Bokai Xu and Zhen Zhang and Yining Ye and Bowen Li and Ziwei Tang and Jing Yi and Yuzhang Zhu and Zhenning Dai and Lan Yan and Xin Cong and Yaxi Lu and Weilin Zhao and Yuxiang Huang and Junxi Yan and Xu Han and Xian Sun and Dahai Li and Jason Phang and Cheng Yang and Tongshuang Wu and Heng Ji and Zhiyuan Liu and Maosong Sun},
      year={2023},
      eprint={2304.08354},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}

Star History