Skip to main content

Overview • Installation • Paper • Examples • Docs • Citation

version

Overview

OpenICL provides an easy interface for in-context learning, with many state-of-the-art retrieval and inference methods built in to facilitate systematic comparison of LMs and fast research prototyping. Users can easily incorporate different retrieval and inference methods, as well as different prompt instructions into their workflow.

What's News

  • v0.1.8 Support LLaMA and self-consistency

Installation

Note: OpenICL requires Python 3.8+

Using Pip

pip install openicl

Installation for local development:

git clone https://github.com/Shark-NLP/OpenICL
cd OpenICL
pip install -e .

Quick Start

Following example shows you how to perform ICL on sentiment classification dataset. More examples and tutorials can be found at examples

Step 1: Load and prepare data

from datasets import load_dataset
from openicl import DatasetReader

# Loading dataset from huggingface
dataset = load_dataset('gpt3mix/sst2')

# Define a DatasetReader, with specified column names where input and output are stored.
data = DatasetReader(dataset, input_columns=['text'], output_column='label')

Step 2: Define the prompt template (Optional)

from openicl import PromptTemplate
tp_dict = {
    0: "</E>Positive Movie Review: </text>",
    1: "</E>Negative Movie Review: </text>" 
}

template = PromptTemplate(tp_dict, {'text': '</text>'}, ice_token='</E>')

The placeholder </E> and </text> will be replaced by in-context examples and testing input, respectively. For more detailed information about PromptTemplate (such as string-type template) , please see tutorial1.

Step 3: Initialize the Retriever

from openicl import TopkRetriever
# Define a retriever using the previous `DataLoader`.
# `ice_num` stands for the number of data in in-context examples.
retriever = TopkRetriever(data, ice_num=8)

Here we use the popular TopK method to build the retriever.

Step 4: Initialize the Inferencer

from openicl import PPLInferencer
inferencer = PPLInferencer(model_name='distilgpt2')

Step 5: Inference and scoring

from openicl import AccEvaluator
# the inferencer requires retriever to collect in-context examples, as well as a template to wrap up these examples.
predictions = inferencer.inference(retriever, ice_template=template)
# compute accuracy for the prediction
score = AccEvaluator().score(predictions=predictions, references=data.references)
print(score)

Docs

(updating...)

OpenICL Documentation

Citation

If you find this repository helpful, feel free to cite our paper:

@article{wu2023openicl,
  title={OpenICL: An Open-Source Framework for In-context Learning},
  author={Zhenyu Wu, Yaoxiang Wang, Jiacheng Ye, Jiangtao Feng, Jingjing Xu, Yu Qiao, Zhiyong Wu},
  journal={arXiv preprint arXiv:2303.02913},
  year={2023}
}

Metadata

Release files for openicl 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openicl 0.1.8
File Size Uploaded Provenance Actions
openicl-0.1.8.tar.gz 30.0 kB Without provenance Details for openicl-0.1.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for openicl 0.1.8
File Interpreter ABI Platform Actions
openicl-0.1.8-py3-none-any.whl Python 3 none any Details for openicl-0.1.8-py3-none-any.whl

Total release size: 75.8 kB

Release files / openicl-0.1.8.tar.gz

Download URL openicl-0.1.8.tar.gz
Size 30.0 kB
Tags Source
SHA-256 checksum
How to use checksums
65b03cc90351201c1493726d9b90a1356acf02b1b509ab8d7e89656b4c1bb301
BLAKE2b-256 checksum
How to use checksums
45b90b41d78adad75c03a788bfab1fea3cab7aed9fcc7e250fda25d2b6796d7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Release files / openicl-0.1.8-py3-none-any.whl

Download URL openicl-0.1.8-py3-none-any.whl
Size 45.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46d86b74b922f5ed1f869344f73ed0fa7d37978da8fbfd963ffee0838c1f21b9
BLAKE2b-256 checksum
How to use checksums
dcd427e7823d9a022e6caed9a8b50fa1b53f931a5a07d6edde4122cecdb57b25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Security for openicl 0.1.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page