Skip to content

Commit abc61f0

Browse files
committed
Docs update
1 parent 880424b commit abc61f0

File tree

7 files changed

+176
-2
lines changed

7 files changed

+176
-2
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: 'Chat with summary'
3+
docname: 'chat_with_summary'
4+
---
5+
6+
## Overview
7+
8+
9+
## Example
10+
11+
12+
```php
13+
<?php
14+
15+
use Cognesy\Instructor\Extras\Chat\Pipelines\ChatWithSummary;
16+
use Cognesy\Instructor\Extras\Chat\Utils\SummarizeMessages;
17+
use Cognesy\Instructor\Features\LLM\Inference;
18+
use Cognesy\Instructor\Features\LLM\LLM;
19+
use Cognesy\Instructor\Utils\Debug\Debug;
20+
use Cognesy\Instructor\Utils\Messages\Message;
21+
use Cognesy\Instructor\Utils\Messages\Messages;
22+
use Cognesy\Instructor\Utils\Messages\Script;
23+
24+
$loader = require 'vendor/autoload.php';
25+
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');
26+
27+
$maxSteps = 5;
28+
$sys = [
29+
'You are helpful assistant explaining Challenger Sale method, you answer questions. Provide very brief answers, not more than one sentence. Simplify things, don\'t go into details, but be very pragmatic and focused on practical bizdev problems.',
30+
'You are curious novice growth expert working to promote Instructor library, you keep asking questions. Use your knowledge of Instructor library and marketing of tech products for developers. Ask short, simple questions. Always ask a single question.',
31+
];
32+
$startMessage = new Message('assistant', 'Help me get better sales results. Be brief and concise.');
33+
34+
$context = "# CONTEXT\n\n" . file_get_contents(__DIR__ . '/summary.md');
35+
36+
$summarizer = new SummarizeMessages(
37+
//prompt: 'Summarize the messages.',
38+
llm: LLM::connection('deepseek'),
39+
//model: 'gpt-4o-mini',
40+
tokenLimit: 1024,
41+
);
42+
43+
//$chat = new ChatWithSummary(
44+
// null,
45+
// 256,
46+
// 256,
47+
// 1024,
48+
// true,
49+
// true,
50+
// $summarizer,
51+
//);
52+
53+
$chat = ChatWithSummary::create(
54+
256,
55+
256,
56+
1024,
57+
$summarizer,
58+
);
59+
$chat->script()->section('main')->appendMessage($startMessage);
60+
61+
//Debug::enable();
62+
63+
for($i = 0; $i < $maxSteps; $i++) {
64+
$chat->script()
65+
->section('system')
66+
->withMessages(Messages::fromString($sys[$i % 2], 'system'));
67+
$chat->script()
68+
->section('context')
69+
->withMessages(Messages::fromString($context, 'system'));
70+
71+
$messages = $chat->script()
72+
->select(['system', 'context', 'summary', 'buffer', 'main'])
73+
->toMessages()
74+
->remapRoles(['assistant' => 'user', 'user' => 'assistant', 'system' => 'system']);
75+
76+
dump($messages->toRoleString());
77+
78+
$response = Inference::text(
79+
messages: $messages->toArray(),
80+
connection: 'deepseek',
81+
options: ['max_tokens' => 256],
82+
);
83+
84+
echo "\n";
85+
dump('>>> '.$response);
86+
echo "\n";
87+
$chat->appendMessage(new Message(role: 'assistant', content: $response), 'main');
88+
}
89+
//dump($chat->script());

docs/cookbook/examples/extras/complex_extraction.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ enum StakeholderRole: string {
9797
case Other = 'other';
9898
}
9999

100-
//Debug::enable();
100+
Debug::enable();
101101

102102
$instructor = new Instructor;
103103

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: 'Parallel Calls'
3+
docname: 'parallel_calls'
4+
path: ''
5+
---
6+
7+
## Overview
8+
9+
Work in progress.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: 'Reasoning Content Access'
3+
docname: 'reasoning_content'
4+
path: ''
5+
---
6+
7+
## Overview
8+
9+
Deepseek API allows to access reasoning content, which is a detailed explanation of how the response was generated.
10+
This feature is useful for debugging and understanding the reasoning behind the response.
11+
12+
## Example
13+
14+
```php
15+
<?php
16+
$loader = require 'vendor/autoload.php';
17+
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');
18+
19+
use Cognesy\Instructor\Features\LLM\Inference;
20+
use Cognesy\Instructor\Utils\Debug\Debug;
21+
use Cognesy\Instructor\Utils\Str;
22+
23+
//Debug::enable();
24+
25+
// EXAMPLE 1: regular API, allows to customize inference options
26+
$response = (new Inference)
27+
->withConnection('deepseek-r') // optional, default is set in /config/llm.php
28+
->create(
29+
messages: [['role' => 'user', 'content' => 'What is the capital of France. Answer with just a name.']],
30+
options: ['max_tokens' => 64]
31+
)
32+
->response();
33+
echo "\nCASE #1: Sync response\n";
34+
echo "USER: What is capital of France\n";
35+
echo "ASSISTANT: {$response->content()}\n";
36+
echo "REASONING: {$response->reasoningContent()}\n";
37+
assert($response->content() !== '');
38+
assert(Str::contains($response->content(), 'Paris'));
39+
assert($response->reasoningContent() !== '');
40+
41+
// EXAMPLE 2: streaming response
42+
$stream = (new Inference)
43+
->withConnection('deepseek-r') // optional, default is set in /config/llm.php
44+
->create(
45+
messages: [['role' => 'user', 'content' => 'What is capital of Brasil. Answer with just a name.']],
46+
options: ['max_tokens' => 128, 'stream' => true]
47+
)
48+
->stream();
49+
50+
echo "\nCASE #2: Streamed response\n";
51+
echo "USER: What is capital of Brasil\n";
52+
echo "ASSISTANT: ";
53+
foreach ($stream->responses() as $partial) {
54+
echo $partial->contentDelta;
55+
}
56+
echo "\n";
57+
echo "REASONING: {$stream->final()->reasoningContent()}\n";
58+
assert($stream->final()->reasoningContent() !== '');
59+
assert($stream->final()->content() !== '');
60+
assert(Str::contains($stream->final()->content(), 'Brasília'));
61+
?>
62+
```

docs/mint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
{
193193
"group": "Extras",
194194
"pages": [
195+
"cookbook/examples/extras/chat_with_summary",
195196
"cookbook/examples/extras/complex_extraction",
196197
"cookbook/examples/extras/complex_extraction_claude",
197198
"cookbook/examples/extras/complex_extraction_cohere",
@@ -206,7 +207,9 @@
206207
"cookbook/examples/extras/llm_json_schema",
207208
"cookbook/examples/extras/llm_md_json",
208209
"cookbook/examples/extras/llm_tools",
210+
"cookbook/examples/extras/parallel_calls",
209211
"cookbook/examples/extras/prompt_text",
212+
"cookbook/examples/extras/reasoning_content",
210213
"cookbook/examples/extras/schema",
211214
"cookbook/examples/extras/schema_dynamic",
212215
"cookbook/examples/extras/summary_with_llm",
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
<?php
1+
---
2+
title: 'Parallel Calls'
3+
docname: 'parallel_calls'
4+
path: ''
5+
---
6+
7+
## Overview
8+
9+
Work in progress.

examples/A05_Extras/ReasoningContent/run.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
## Overview
88

9+
Deepseek API allows to access reasoning content, which is a detailed explanation of how the response was generated.
10+
This feature is useful for debugging and understanding the reasoning behind the response.
11+
912
## Example
1013

1114
```php

0 commit comments

Comments
 (0)