Files
semantic-kernel/python/tests
Karol ŻakandAbby Harrison 601c9de124 Python: Add HF model and pipeline kwargs to allow model loading customization (#2547)
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

I was trying to use this library to load and use [HF Falcon
model](https://huggingface.co/tiiuae/falcon-7b-instruct). Unfortunately
in order to make it work I needed to pass in some additional arguments
such as `torch_dtype`, `cache_dir` or `tokenizer`. It was not possible
with current implementation so I decided to add `model_kwargs` and
`pipeline_kwargs` to the `HuggingFaceTextCompletion` class init function
so now, with the changes I introduced I can successfully load any
`falcon` models by using this code:
```python
import semantic_kernel as sk
import semantic_kernel.connectors.ai.hugging_face as sk_hf
import torch
from transformers import AutoTokenizer


cache_dir = "/mnt/tmp"
model_name = "tiiuae/falcon-7b-instruct"

tokenizer = AutoTokenizer.from_pretrained(
    pretrained_model_name_or_path=model_name,
    cache_dir=cache_dir,
    trust_remote_code=True)

hf_model = sk_hf.HuggingFaceTextCompletion(
    model_name,
    task="text-generation",
    model_kwargs={
        "cache_dir": cache_dir,
        "torch_dtype": torch.bfloat16
    },
    pipeline_kwargs={
        "tokenizer": tokenizer,
        "trust_remote_code": True,
        "device": 0,
    }
)
```

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

The changes I introduced add `model_kwargs` and `pipeline_kwargs` to
`HuggingFaceTextCompletion` class so that these arguments are then
passed to the underlying `transformers.pipeline()` function and thus
allow more customization options.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
2023-08-24 18:24:12 +00:00
..
2023-04-13 16:12:36 -07:00