This is an unofficial PHP client for HuggingChat (OpenAssistant's LLaMA model).
HuggingChat API is evolving fast with recurring breaking changes. I try to keep up with it, but it may not always work as expected. Feel free to open an issue if you encounter any problem.
composer require maximerenou/hugging-chat
Run examples/chat.php
to test it.
use MaximeRenou\HuggingChat\Client as HuggingChat;
use MaximeRenou\HuggingChat\Prompt;
$ai = new HuggingChat();
$conversation = $ai->createConversation();
// $answer - full answer
$answer = $conversation->ask(new Prompt("Hello World"));
Real-time / progressive answer
You may pass a function as second argument to get real-time progression:
// $current_answer - incomplete answer
// $tokens - last tokens received
$final_answer = $conversation->ask($prompt, function ($current_answer, $tokens) {
echo $tokens;
});
Resume a conversation
If you want to resume a previous conversation, you can retrieve its identifiers:
// Get current identifiers
$identifiers = $conversation->getIdentifiers();
// ...
// Resume conversation with $identifiers parameter
$conversation = $ai->resumeConversation($identifiers);
Use another model
You can use a specific model:
$conversation = $ai->createConversation("bigcode/starcoder");
Default is OpenAssistant.
Generate a conversation's summary
Useful to give a title to a conversation.
// Question asked: "Who's Einstein?"
// ...
$summary = $conversation->getSummary();
// Result: Famous genius mathematician.
Turn on/off data sharing
HuggingChat share your conversations to improve the model. You can turn on/off data sharing:
$conversation->enableSharing(); // on
$conversation->disableSharing(); // off (module default)
Delete a conversation
$conversation->delete();
Handle HuggingChat errors
The code throws exceptions when it receives an error from HuggingChat. You can therefore use a try/catch block to handle errors.
Answers are sometimes malformed (or dumb)
Answers quality depends on the model you're using.
Using HuggingChat outside huggingface.co/chat may violate HuggingFace terms. Use it at your own risk.