Skip to main content

Text Generation

The Text Generation is one of the Natural Language Processing tasks supported by Hugging Face.

Using the huggingface_text_generation filter.

The result will be a string from huggingface_text_generation.

info

The Text Generation default model is gpt2, If you would like to use the Meta LLama2 models, you have two methods to do:

  1. Subscribe to the Pro Account.
  • Set the Meta LLama2 model using the model keyword argument in huggingface_text_generation, e.g: meta-llama/Llama-2-13b-chat-hf.
  1. Using Inference Endpoint.
  • Select one of the Meta LLama2 Models and deploy it to the Inference Endpoint.
  • Set the endpoint URL using the endpoint keyword argument in huggingface_text_generation.

Sample 1 - Subscribe to the Pro Account:

{% set data = [
{
"rank": 1,
"institution": "Massachusetts Institute of Technology (MIT)",
"location code":"US",
"location":"United States"
},
{
"rank": 2,
"institution": "University of Cambridge",
"location code":"UK",
"location":"United Kingdom"
},
{
"rank": 3,
"institution": "Stanford University"
"location code":"US",
"location":"United States"
}
-- other universities.....
] %}

SELECT {{ data | huggingface_text_generation(query="Which university is the top-ranked university?", model="meta-llama/Llama-2-13b-chat-hf") }} as result

Sample 1 - Response:

[
{
"result": "Answer: Based on the provided list, the top-ranked university is Massachusetts Institute of Technology (MIT) with a rank of 1."
}
]

Sample 2 - Using Inference Endpoint:

{% req universities %}
SELECT rank,institution,"location code", "location" FROM read_csv_auto('2023-QS-World-University-Rankings.csv')
{% endreq %}

SELECT {{ universities.value() | huggingface_text_generation(query="Which university located in the UK is ranked at the top of the list?", endpoint='xxx.yyy.zzz.huggingface.cloud') }} as result

Sample 2 - Response:

[
{
"result": "Answer: Based on the list provided, the top-ranked university in the UK is the University of Cambridge, which is ranked at number 2."
}
]

Arguments

Some default value was changed, so it may different from Text Generation default value.

NameRequiredDefaultDescription
queryYThe query in plain text that you want to ask the table.
endpointNThe inference endpoint URL, when using endpoint, it replaces the original default value of model.
modelNgpt2The model id of a pre-trained model hosted inside a model repo on huggingface.co. See: https://huggingface.co/models?pipeline_tag=text-generation
top_kNInteger value to define the top tokens considered within the sample operation to create new text.
top_pNFloat value to define the tokens that are within the sample operation of text generation. Add tokens in the sample for more probable to least probable until the sum of the probabilities is greater than top_p.
temperatureN0.1Range: (0.0 - 100.0). The temperature of the sampling operation. 1 means regular sampling, 0 means always take the highest score, 100.0 is getting closer to uniform probability.
repetition_penaltyNRange: (0.0 - 100.0). The more a token is used within generation the more it is penalized to not be picked in successive generation passes.
max_new_tokensN250The amount of new tokens to be generated, this does not include the input length it is a estimate of the size of generated text you want. Each new tokens slows down the request, so look for balance between response times and length of text generated.
max_timeNRange (0-120.0). The amount of time in seconds that the query should take maximum. Network can cause some overhead so it will be a soft limit. Use that in combination with max_new_tokens for best results.
return_full_textNfalseIf set to False, the return results will not contain the original query making it easier for prompting.
num_return_sequencesN1The number of proposition you want to be returned.
do_sampleNWhether or not to use sampling, use greedy decoding otherwise.
use_cacheNtrueThere is a cache layer on the inference API to speedup requests we have already seen
wait_for_modelNfalseIf the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done