Skip to content

Add initial Hugging Face support - #359

Merged
hadley merged 16 commits into
tidyverse:mainfrom
s-spavound:add_hf_support
Apr 17, 2025
Merged

Add initial Hugging Face support#359
hadley merged 16 commits into
tidyverse:mainfrom
s-spavound:add_hf_support

Conversation

@s-spavound

Copy link
Copy Markdown

This PR adds chat_hf() which is a light wrapper around the OpenAI provider to allow for Hugging Face serverless inference APIs.

It essentially just modifies the base URL to be compatible with Hugging Face requirements as well as the authentication step. I based it on how chat_github() has been constructed.

As Hugging Face supports multiple ways of hosting models, I expect that this will need to be further worked on to support all of these methods. For example it is a little tricky that certain models will not support system messages (even though they support the chat completions interface) - I don't know of any way (yet) to detect which methods are supported so its currently on the user.

An alternative I thought about was documenting that hugging face serverless can be done via chat_openai() as follows:

chat <- chat_openai(base_url = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.1-8B-Instruct/v1",
api_key = HF_API_KEY,
model = "meta-llama/Llama-3.1-8B-Instruct")

Which does work.

I also fixed a tiny typo in one piece of documentation.

Please let me know if you were thinking about doing this a different way or want me to change anything else. Thanks!

@hadley hadley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for working on this! A few style comments below.

Comment thread R/provider-huggingface.r Outdated
Comment thread R/provider-huggingface.r Outdated
Comment thread R/provider-huggingface.r Outdated
resp <- chat$chat("What is 1 + 1?", echo = FALSE)
expect_match(resp, "2")
expect_equal(chat$last_turn()@tokens > 0, c(TRUE, TRUE))
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you want to add any other tests to verify that (eg.) tool calling works?

@s-spavound

Copy link
Copy Markdown
Author

Thanks @hadley for the suggestions - I am going to put it into draft mode as I have found some issues with tool calling that I have not been able to resolve yet - do you have any suggestions for how best to debug this package? Many of the errors I am getting are many layers deep in a trace and not very helpful

@hadley

hadley commented Mar 12, 2025

Copy link
Copy Markdown
Member

@s-spavound debugging tool calling is hard 😞 If you have reprexes, I'm happy to take a look and help out.

@hadley

hadley commented Apr 2, 2025

Copy link
Copy Markdown
Member

I've generally better aligned chat_huggingface() with the other chat_ functions, but I can't test any further without getting a hugging face pro account.

@hadley

hadley commented Apr 9, 2025

Copy link
Copy Markdown
Member

Looks like tool calling is currently broken: huggingface/text-generation-inference#2986

@hadley hadley added this to the 0.2.0 milestone Apr 17, 2025
@hadley
hadley merged commit 2a52832 into tidyverse:main Apr 17, 2025
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.

2 participants