Natural Language Processing
On this page, we’ll dive into the different natural language processing tasks you can use.
These tasks are all powered by the huggingface's transformers.js library. You can use any of the pre-trained ONNX models from the huggingface models or use your own.
Headers
- Name
x-api-group(optional)- Type
- string, default 'main'
- Description
The id of the peer-ai compute group you want to run this compute on.
- Name
x-api-key- Type
- string
- Description
The API key for your PeerAI account.
Sentiment Analysis
Sentiment analysis is the process of determining whether a piece of text is positive, negative, or neutral. It’s a useful tool for understanding how people feel about your product, service, or brand.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'sentiment-analysis', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used. For example, use 'Xenova/distilbert-base-uncased-finetuned-sst-2-english' to use the model from this repo https://huggingface.co/Xenova/distilbert-base-uncased-finetuned-sst-2-english
- Name
inputs.0- Type
- string
- Description
The text to analyze.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"sentiment-analysis\", \"inputs\": [\"I love transformers!\"]}"
Response
[
{
"label": "POSITIVE",
"score": 0.999
}
]
Token Classification
Token classification is the process of assigning labels to individual tokens in a piece of text. Each token is assigned a label based on its meaning, entity type, or role in the text.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'sentiment-analysis', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used.
- Name
inputs.0- Type
- string
- Description
The text to analyze.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"token-classification\", \"inputs\": [\"The Golden State Warriors are an American professional basketball team based in San Francisco.\"]}"
Response
[
{
"entity": "B-ORG",
"score": 0.9998,
"index": 2,
"word": "Golden",
"start": null,
"end": null
},
{
"entity": "I-ORG",
"score": 0.9999,
"index": 3,
"word": "State",
"start": null,
"end": null
},
{
"entity": "I-ORG",
"score": 0.9999,
"index": 4,
"word": "Warriors",
"start": null,
"end": null
},
{
"entity": "B-LOC",
"score": 0.9997,
"index": 13,
"word": "San",
"start": null,
"end": null
},
{
"entity": "I-LOC",
"score": 0.9994,
"index": 14,
"word": "Francisco",
"start": null,
"end": null
}
]
Question Answering
Question answering is the task of automatically providing an accurate and concise answer to a given question, based on a given context or set of documents.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'sentiment-analysis', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used.
- Name
inputs.0- Type
- string
- Description
The question
- Name
inputs.1- Type
- string
- Description
The context to find the answer from.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"question-answering\", \"inputs\": [\"Who was Jim Henson?\", \"Jim Henson was a nice puppet.\"]}"
Response
{
"answer": "a nice puppet",
"score": 0.577
}
Feature Extraction
Feature extraction is the process of obtaining a compact representation of raw input data, which is suitable for a wide range of downstream tasks such as classification, clustering, and retrieval. It allows you to extract meaningful features from text, images, or other types of data.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'feature-extraction', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used.
- Name
inputs.0- Type
- string
- Description
The text to extract features from.
- Name
options(optional)- Type
- object, default null
- Description
Additional options for feature extraction. For example, you can specify "pooling" (e.g., 'mean', 'max', 'first', 'last') and "normalize" (true/false) options.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"feature-extraction\", \"model\": \"Xenova/all-MiniLM-L6-v2\", \"inputs\": [\"This is a simple test.\"], \"options\": {\"pooling\": \"mean\", \"normalize\": true}}"
Response
{
"dims": [
1,
384
],
"type": "float32",
"data": {
"0": 0.08325745910406113,
"1": -0.02984856441617012,
"2": 0.011567394249141216,
"3": 0.04427928477525711,
"4": -0.003810758236795664,
...
},
"size": 384
}
Text-to-Text Generation
Text-to-Text generation is the task of generating a text from a given input text. It can be used for a wide range of tasks such as summarization, translation, and text generation.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'text2text-generation', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used. For example, use 'Xenova/distilbart-cnn-6-6' to use the model from this repo https://huggingface.co/Xenova/distilbart-cnn-6-6
- Name
inputs.0- Type
- string
- Description
The text to generate from.
- Name
options.max_new_tokens(optional)- Type
- number, default null
- Description
The maximum number of new tokens to generate. If not specified, the default value will be used.
- Name
options.temperature(optional)- Type
- number, default null
- Description
The temperature value for controlling the randomness of the generated text. If not specified, the default value will be used.
- Name
options.repetition_penalty(optional)- Type
- number, default null
- Description
The repetition penalty value for controlling the likelihood of repeated tokens in the generated text. If not specified, the default value will be used.
- Name
options.no_repeat_ngram_size(optional)- Type
- number, default null
- Description
The size of the n-gram to avoid repeating in the generated text. If not specified, the default value will be used.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"text2text-generation\", \"model\": \"Xenova/distilbart-cnn-6-6\", \"inputs\": [\"The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.\"], \"options\": {\"max_new_tokens\": 200, \"temperature\": 0.9, \"repetition_penalty\": 2.0, \"no_repeat_ngram_size\": 3}}"
Response
[
" The Eiffel Tower is 324 metres (1,063 ft) tall, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During construction, the Eiffel Tower surpassed the Washington Monument to become"
]
Text Generation
Text generation is the task of generating new text based on a given prompt or input. It can be used for various applications such as generating creative writing, completing sentences, or generating responses in chatbots.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'sentiment-analysis', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used.
- Name
inputs.0- Type
- string
- Description
The text prompt or input.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"text-generation\", \"inputs\": [\"I enjoy walking with my cute dog,\"]}"
Response
[
{
"generated_text": "I enjoy walking with my cute dog, and I love to ride along with my friends."
}
]
Fill Mask
The fill-mask pipeline is used to predict the most likely word to fill in a masked position in a given sentence.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'sentiment-analysis', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used.
- Name
inputs.0- Type
- string
- Description
The sentence with a masked position.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"fill-mask\", \"inputs\": [\"This is a [MASK] library\"]}"
Response
[
{
"score": 0.1069,
"token": 2270,
"token_str": "public",
"sequence": "this is a public library"
},
{
"score": 0.0296,
"token": 2235,
"token_str": "small",
"sequence": "this is a small library"
},
{
"score": 0.0285,
"token": 2047,
"token_str": "new",
"sequence": "this is a new library"
},
{
"score": 0.0260,
"token": 2797,
"token_str": "private",
"sequence": "this is a private library"
},
{
"score": 0.0187,
"token": 2312,
"token_str": "large",
"sequence": "this is a large library"
}
]
Summarization
Summarization is the process of condensing a piece of text into a shorter version while retaining its main ideas and key points.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'summarization', 'text-generation'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used.
- Name
inputs.0- Type
- string
- Description
The text to summarize.
- Name
options- Type
- object
- Description
Additional options for the summarization task. Available options include:
max_length(integer): The maximum length of the generated summary.min_length(integer): The minimum length of the generated summary.do_sample(boolean): Whether to use sampling for generating the summary.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"summarization\", \"inputs\": [\"Sam Shleifer writes the best docstring examples in the whole world.\"], \"options\": {\"max_length\": 20, \"min_length\": 5, \"do_sample\": false}}"
Response
[
{
"summary_text": " Sam Shleifer writes the best docstring examples in the world."
}
]
Translation
Translation is the process of converting text from one language to another. It allows you to communicate and understand content in different languages.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'sentiment-analysis', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used.
- Name
inputs.0- Type
- string
- Description
The text to translate.
- Name
options.max_length(optional)- Type
- number, default null
- Description
The maximum length of the translated text. If not specified, the default maximum length will be used.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"translation\", \"inputs\": [\"translate English to German: The answer to the universe is 42\"], \"options\": {\"max_length\": 40}}"
Response
[
{
"translation_text": "Die Antwort auf das Universum ist 42"
}
]
Zero-Shot Classification
Zero-shot classification is a technique that allows you to classify text into multiple predefined categories, even if the text has not been explicitly trained on those categories. It uses a pre-trained model to assign scores to each category based on the input text.
Body
- Name
task- Type
- string
- Description
The task of the pipeline. e.g., 'sentiment-analysis', 'text-classification'
- Name
model(optional)- Type
- string, default null
- Description
The name of the pre-trained model to use. If not specified, the default model for the task will be used.
- Name
inputs.0- Type
- string
- Description
The text to classify.
- Name
inputs.1- Type
- array
- Description
An array of labels representing the categories to classify the text into.
Request
curl -X POST https://api.peer-ai.com/v1/pipeline \
-H "X-API-Group: {YOUR_COMPUTE_GROUP}" \
-H "X-API-Key: {YOUR_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"task\": \"zero-shot-classification\", \"inputs\": [\"This is a course about the Transformers library\", [\"education\", \"politics\", \"business\"]]}"
Response
{
"sequence": "This is a course about the Transformers library",
"labels": [
"education",
"politics",
"business"
],
"scores": [
0.47096972348010013,
0.3030572879037793,
0.22597298861612056
]
}
Sentence Similarity
Sentence similarity is the process of determining how similar two sentences are. It is often used in natural language processing to determine if two sentences are paraphrases of each other.
Coming Soon