Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit 7d30a61

Browse files
committed
feat: summarize conversation
1 parent 6e57c65 commit 7d30a61

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![PHP version](https://img.shields.io/packagist/dependency-v/maximerenou/hugging-chat/php)](https://packagist.org/packages/maximerenou/hugging-chat)
77
[![cURL extension required](https://img.shields.io/packagist/dependency-v/maximerenou/hugging-chat/ext-curl)](https://packagist.org/packages/maximerenou/hugging-chat)
88

9-
This is an unofficial Composer package for using **HuggingChat** (OpenAssistant's LLaMA model).
9+
This is an unofficial PHP client for **HuggingChat** (OpenAssistant's LLaMA model).
1010

1111
## Installation
1212

@@ -63,6 +63,20 @@ $conversation = $ai->resumeChatConversation($identifiers);
6363

6464
</details>
6565

66+
<details>
67+
<summary>Generate a conversation's summary</summary>
68+
69+
Useful to give a title to a conversation.
70+
71+
```php
72+
// Question asked: "Who's Einstein?"
73+
// ...
74+
$summary = $conversation->getSummary();
75+
// Result: Famous genius mathematician.
76+
```
77+
78+
</details>
79+
6680
<details>
6781
<summary>Handle HuggingChat errors</summary>
6882

src/Conversation.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function ask(Prompt $message, $callback = null)
136136
return $this->current_text;
137137
}
138138

139-
public function handlePacket($raw)
139+
protected function handlePacket($raw)
140140
{
141141
$data = json_decode($raw, true);
142142

@@ -165,4 +165,23 @@ public function handlePacket($raw)
165165
'final' => $data['token']['special']
166166
];
167167
}
168+
169+
public function getSummary()
170+
{
171+
$headers = [
172+
'method: POST',
173+
'accept: application/json',
174+
"referer: https://huggingface.co/chat",
175+
'content-type: application/json',
176+
"cookie: hf-chat={$this->cookie}"
177+
];
178+
179+
$data = Tools::request("https://huggingface.co/chat/conversation/{$this->id}/summarize", $headers, '', false);
180+
$data = json_decode($data, true);
181+
182+
if (! is_array($data) || empty($data['title']))
183+
throw new \Exception("Failed to get conversation's summary");
184+
185+
return trim($data['title'], '"');
186+
}
168187
}

0 commit comments

Comments
 (0)