Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support HuggingFace Text Generation including meta-llama2 model #266

Merged
merged 4 commits into from
Aug 3, 2023

Conversation

kokokuo
Copy link
Contributor

@kokokuo kokokuo commented Aug 2, 2023

Description

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

VulcanSQL supports the Using Text Generation by using the huggingface_text_generation filter. The result will be a string from huggingface_text_generation.

📢 Notice: The Text Generation default model is gpt2, If you would like to use the Meta LLama2 models, you have two method 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."
  }
]

Screenshot

SQL and API Schema
llama2-sample

Question 1 - Which university is the top-ranked university?
Question1

Question 2 - Which university located in the UK is ranked at the top of the list?
Question2

Additional Context

  • Refactor the original TableQuestionAnsweringFilter function logistic to keep simple and readable.
  • Support endpoint field for making users could use their HuggingFace Inference Endpoint when using huggingface_xxx filter.
  • Move the original request method to the request.ts and support try-catch to bypass the Axios error message.
  • Refactor for moving the sample data to test-data folder for reusing data and use describe.
  • Create model.ts to define the common type or const value.
  • Skip the test case of testing llama2 model, because using llama2 model needs to subscribe Pro Account and pay $9/month.

- Add the "TextGenerationFilter".
- support huggingface filters could pass "endpoint" keyword arguments when using different filter task.
- add test cases of "TextGenerationFilter".
@vercel
Copy link

vercel bot commented Aug 2, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
vulcan-sql-document ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 2, 2023 2:25pm

@kokokuo kokokuo changed the title Feature: Support Huggingface Text Generation ( including llama2 model ) Feature: Support HuggingFace Text Generation including meta-llama2 model Aug 2, 2023
@kokokuo
Copy link
Contributor Author

kokokuo commented Aug 2, 2023

@cyyeh, Please assist me to check Document content - HuggingFace Text Generation, thanks!

Btw, I added the endpoint field in the Table Question Answering to support changing the API endpoint when using huggingface_xxxx filter.

Copy link
Contributor

@onlyjackfrost onlyjackfrost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides some comments, others LGTM

if (!(typeof args === 'object') || !has(args, 'query'))
throw new InternalError('Must provide "query" keyword argument');
if (!args['query'])
throw new InternalError('The "query" argument must have value');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about why we removed this query check.

Copy link
Contributor Author

@kokokuo kokokuo Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for asking the question. I added back the logistic for checking "query" has value or not with test cases in 5e22e51


Using the `huggingface_text_generation` filter. The result will be a string from `huggingface_text_generation`.

**Notice**: The **Text Generation** default model is **gpt2**, If you would like to use the [Meta LLama2](https://huggingface.co/meta-llama) models, you have two method to do:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"If you would like to use the [Meta LLama2] models, you have two method to do"
Check the grammar.

Copy link
Contributor Author

@kokokuo kokokuo Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding the grammar issue, I have fixed the method to methods at 5e22e51

2. Select one of the [Meta LLama2](https://huggingface.co/meta-llama) Models and deploy it to the [Inference Endpoint](https://huggingface.co/inference-endpoints). Set the endpoint URL using the `endpoint` keyword argument in `huggingface_text_generation`.

```sql
SELECT {{ data | huggingface_text_generation(query="Which university is the top-ranked university?", endpoint='xxx.yyy.zzz.huggingface.cloud') }} as result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can merge these to code snippet and use "comment" to describe the detail.
I think it will be more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the code snippet, the marked code is older code. After discussion with @onlyjackfrost and checking, no need to change.

export default [
HuggingFaceTableQuestionAnsweringFilterBuilder,
HuggingFaceTableQuestionAnsweringFilterRunner,
TextGenerationFilterBuilder,
TextGenerationFilterRunner,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure that the naming is aligned with HuggingFace, either by using it as a prefix or without it.

Copy link
Contributor Author

@kokokuo kokokuo Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding the naming issue, it has been fixed at 5e22e51

100 * 1000
);

// Skip the test case because the "meta-llama/Llama-2-13b-chat-hf" model need to upgrade your huggingface account to Pro Account by paying $9 per month
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any model that is free and can be used for testing?
If the structure of API response payload is the same, I think it could be used for testing.

Copy link
Contributor Author

@kokokuo kokokuo Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion, the free model has been added to the test cases and I renamed to Should not throw when passing the "query" argument by dynamic parameter through HuggingFace default recommended "gpt2" model 5e22e51

@codecov-commenter
Copy link

codecov-commenter commented Aug 2, 2023

Codecov Report

Patch coverage: 80.76% and project coverage change: -0.03% ⚠️

Comparison is base (180f8a6) 90.25% compared to head (8c6a02c) 90.23%.

❗ Current head 8c6a02c differs from pull request most recent head 5e22e51. Consider uploading reports for the commit 5e22e51 to get more accurate results

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #266      +/-   ##
===========================================
- Coverage    90.25%   90.23%   -0.03%     
===========================================
  Files          346      344       -2     
  Lines         5931     5722     -209     
  Branches       794      769      -25     
===========================================
- Hits          5353     5163     -190     
+ Misses         421      405      -16     
+ Partials       157      154       -3     
Flag Coverage Δ
extension-driver-ksqldb ?
extension-huggingface 86.25% <80.76%> (+0.53%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
...ges/extension-huggingface/src/lib/utils/request.ts 55.55% <55.55%> (ø)
...gingface/src/lib/filters/tableQuestionAnswering.ts 88.46% <81.81%> (+7.90%) ⬆️
...sion-huggingface/src/lib/filters/textGeneration.ts 85.71% <85.71%> (ø)
packages/extension-huggingface/src/index.ts 100.00% <100.00%> (ø)
packages/extension-huggingface/src/lib/model.ts 100.00% <100.00%> (ø)
...kages/extension-huggingface/src/lib/utils/index.ts 100.00% <100.00%> (ø)
...tension-huggingface/test/test-data/repositories.ts 100.00% <100.00%> (ø)

... and 8 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

…has value with test cases for huggingface filter

- fix grammar in README.
- fix the section of document .
- add logistic for checking query has value with test cases
Copy link
Contributor

@onlyjackfrost onlyjackfrost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kokokuo kokokuo merged commit 5df01ae into develop Aug 3, 2023
5 checks passed
@hanshino hanshino deleted the feature/huggingface-text-generation branch January 31, 2024 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants