Skip to content

Commit b521e39

Browse files
committed
fix ollama stream
1 parent f4471ee commit b521e39

File tree

11 files changed

+26
-34
lines changed

11 files changed

+26
-34
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
use GuzzleHttp\Client;
66

7-
trait HandleSetClient
7+
trait HandleClient
88
{
9-
public function setClient(Client $client): static
9+
public function setClient(Client $client): AIProviderInterface
1010
{
1111
$this->client = $client;
12-
1312
return $this;
1413
}
1514
}

src/Providers/Ollama/HandleChat.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ public function chat(array $messages): Message
2121
$json = [
2222
'stream' => false,
2323
'model' => $this->model,
24+
'temperature' => $this->temperature,
2425
'messages' => $mapper->map(),
2526
];
2627

27-
if ($this->temperature) {
28-
$json['temperature'] = $this->temperature;
29-
}
30-
3128
if (! empty($this->tools)) {
3229
$json['tools'] = $this->generateToolsPayload();
3330
}

src/Providers/Ollama/HandleStream.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public function stream(array|string $messages, callable $executeToolsCallback):
1616

1717
$mapper = new MessageMapper($messages);
1818

19-
$json = \array_filter([
19+
$json = [
2020
'stream' => true,
2121
'model' => $this->model,
2222
'temperature' => $this->temperature,
2323
'messages' => $mapper->map(),
24-
]);
24+
];
2525

2626
if (!empty($this->tools)) {
2727
$json['tools'] = $this->generateToolsPayload();
@@ -31,9 +31,6 @@ public function stream(array|string $messages, callable $executeToolsCallback):
3131
'chat', compact('json')
3232
)->getBody();
3333

34-
$text = '';
35-
$toolCalls = [];
36-
3734
while (! $stream->eof()) {
3835
if (!$line = $this->parseNextJson($stream)) {
3936
continue;
@@ -45,7 +42,6 @@ public function stream(array|string $messages, callable $executeToolsCallback):
4542

4643
// Process regular content
4744
$content = $line['message']['content']??'';
48-
$text .= $content;
4945

5046
yield $content;
5147
}

src/Providers/Ollama/Ollama.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
use NeuronAI\Chat\Messages\Message;
77
use NeuronAI\Chat\Messages\ToolCallMessage;
88
use NeuronAI\Providers\AIProviderInterface;
9-
use NeuronAI\Providers\HandleSetClient;
9+
use NeuronAI\Providers\HandleClient;
1010
use NeuronAI\Providers\HandleWithTools;
1111
use NeuronAI\Tools\ToolInterface;
1212
use NeuronAI\Tools\ToolProperty;
1313

1414
class Ollama implements AIProviderInterface
1515
{
1616
use HandleChat;
17-
use HandleSetClient;
17+
use HandleClient;
1818
use HandleStream;
1919
use HandleWithTools;
2020

tests/ChatHistoryTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use NeuronAI\Chat\History\AbstractChatHistory;
66
use NeuronAI\Chat\History\FileChatHistory;
77
use NeuronAI\Chat\History\InMemoryChatHistory;
8-
use NeuronAI\Chat\Messages\Message;
98
use NeuronAI\Chat\Messages\Usage;
109
use NeuronAI\Chat\Messages\UserMessage;
1110
use NeuronAI\Exceptions\ChatHistoryException;
@@ -23,20 +22,20 @@ public function setUp(): void
2322
{
2423
}
2524

26-
public function testChatHistoryInstance()
25+
public function test_chat_history_instance()
2726
{
2827
$history = new InMemoryChatHistory();
2928
$this->assertInstanceOf(AbstractChatHistory::class, $history);
3029
}
3130

32-
public function testChatHistoryAddMessage()
31+
public function test_chat_history_add_message()
3332
{
3433
$history = new InMemoryChatHistory();
3534
$history->addMessage(new UserMessage('Hello!'));
3635
$this->assertCount(1, $history->getMessages());
3736
}
3837

39-
public function testChatHistoryTruncate()
38+
public function test_chat_history_truncate()
4039
{
4140
$message = new UserMessage('Hello!');
4241
$message->setUsage(new Usage(100, 100));
@@ -46,7 +45,7 @@ public function testChatHistoryTruncate()
4645
$this->assertCount(1, $history->getMessages());
4746
}
4847

49-
public function testChatHistoryClear()
48+
public function test_chat_history_clear()
5049
{
5150
$history = new InMemoryChatHistory();
5251
$history->addMessage(new UserMessage('Hello!'));
@@ -58,7 +57,7 @@ public function testChatHistoryClear()
5857
/**
5958
* @throws ChatHistoryException
6059
*/
61-
public function testFileChatHistory()
60+
public function test_file_chat_history()
6261
{
6362
$history = new FileChatHistory(__DIR__, 'test');
6463
$this->assertFileDoesNotExist(__DIR__.DIRECTORY_SEPARATOR.'neuron_test.chat');
@@ -72,7 +71,7 @@ public function testFileChatHistory()
7271
$this->assertCount(0, $history->getMessages());
7372
}
7473

75-
public function testFileChatHistoryInit()
74+
public function test_file_chat_history_init()
7675
{
7776
$history = new FileChatHistory(__DIR__, 'test');
7877
$history->addMessage(new UserMessage('Hello!'));

tests/DataLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
class DataLoaderTest extends TestCase
1111
{
12-
public function testStringDataLoader()
12+
public function test_string_data_loader()
1313
{
1414
$documents = StringDataLoader::for('test')->getDocuments();
1515
$this->assertCount(1, $documents);
1616
$this->assertEquals('test', $documents[0]->content);
1717
$this->assertEquals(\hash('sha256', 'test'), $documents[0]->hash);
1818
}
1919

20-
public function testSplitLongText()
20+
public function test_split_long_text()
2121
{
2222
$doc = new Document(file_get_contents(__DIR__.'/stubs/long-text.txt'));
2323

tests/NeuronAITest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function setUp(): void
2626
{
2727
}
2828

29-
public function testAgentInstance()
29+
public function test_agent_instance()
3030
{
3131
$neuron = new Agent();
3232
$this->assertInstanceOf(AgentInterface::class, $neuron);
@@ -35,7 +35,7 @@ public function testAgentInstance()
3535
$this->assertInstanceOf(Agent::class, $neuron);
3636
}
3737

38-
public function testMessageInstance()
38+
public function test_message_instance()
3939
{
4040
$tools = [
4141
new Tool('example', 'example')
@@ -46,7 +46,7 @@ public function testMessageInstance()
4646
$this->assertInstanceOf(Message::class, new ToolCallMessage('', $tools));
4747
}
4848

49-
public function testToolInstance()
49+
public function test_tool_instance()
5050
{
5151
$tool = new Tool('example', 'example');
5252
$this->assertInstanceOf(ToolInterface::class, $tool);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace NeuronAI\Tests;
3+
namespace NeuronAI\Tests\Providers;
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Handler\MockHandler;
@@ -45,6 +45,7 @@ public function test_chat_request(): void
4545
$expectedResponse = [
4646
'stream' => false,
4747
'model' => 'llama3.2',
48+
'temperature' => 0,
4849
'messages' => [
4950
[
5051
'role' => 'user',

tests/VectorStore/ElasticsearchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ private function isPortOpen(string $host, int $port, int $timeout = 1): bool
3737
return false;
3838
}
3939

40-
public function testElasticsearchInstance()
40+
public function test_elasticsearch_instance()
4141
{
4242
$store = new ElasticsearchVectorStore($this->client, 'test');
4343
$this->assertInstanceOf(VectorStoreInterface::class, $store);
4444
}
4545

46-
public function testAddDocumentAndSearch()
46+
public function test_add_document_and_search()
4747
{
4848
$store = new ElasticsearchVectorStore($this->client, 'test');
4949

tests/VectorStore/MemoryVectorStoreTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ protected function setUp(): void
1717
$this->embedding = json_decode(file_get_contents(__DIR__ . '/../stubs/hello-world.embeddings'), true);
1818
}
1919

20-
public function testVectorStoreInstance()
20+
public function test_memory_store_instance()
2121
{
2222
$store = new MemoryVectorStore();
2323
$this->assertInstanceOf(VectorStoreInterface::class, $store);
2424
}
2525

26-
public function testAddDocumentAndSearch()
26+
public function test_add_document_and_search()
2727
{
2828
$document = new Document('Hello World!');
2929
$document->embedding = $this->embedding;

0 commit comments

Comments
 (0)