/php-hugging-chat

HuggingChat PHP client (OpenAssistant's LLaMA)

Primary LanguagePHP

HuggingChat + PHP

HuggingChat client

Latest Stable Version PHP version cURL extension required

This is an unofficial PHP client for HuggingChat (OpenAssistant's LLaMA model).

Installation

composer require maximerenou/hugging-chat

Demo

Run examples/chat.php to test it.

Prompt Demo

Usage

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->resumeChatConversation($identifiers);
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.
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)

That's what OpenAssistant's LLaMA model used by HuggingChat generates...


Disclaimer

Using HuggingChat outside huggingface.co/chat may violate HuggingFace terms. Use it at your own risk.