Skip to content

Commit 5e84aea

Browse files
committed
simplify ai provider interface
1 parent 164a24f commit 5e84aea

File tree

5 files changed

+16
-66
lines changed

5 files changed

+16
-66
lines changed

src/Providers/AIProviderInterface.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace NeuronAI\Providers;
44

55
use NeuronAI\Messages\Message;
6-
use NeuronAI\Providers\Embeddings\EmbeddingsProviderInterface;
76

87
interface AIProviderInterface
98
{
@@ -38,14 +37,14 @@ public function chat(array|string $prompt): Message;
3837
/**
3938
* The context window limitation of the LLM.
4039
*
41-
* @return int
40+
* @return ?int
4241
*/
43-
public function contextWindow(): int;
42+
//public function contextWindow(): ?int;
4443

4544
/**
4645
* The maximum number of tokens to generate before stopping.
4746
*
48-
* @return int
47+
* @return ?int
4948
*/
50-
public function maxCompletionTokens(): int;
49+
//public function maxCompletionTokens(): ?int;
5150
}

src/Providers/Anthropic.php

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace NeuronAI\Providers;
44

5+
use GuzzleHttp\Exception\GuzzleException;
6+
use NeuronAI\Messages\AssistantMessage;
57
use NeuronAI\Messages\Message;
8+
use NeuronAI\Messages\UserMessage;
69
use NeuronAI\Tools\Tool;
710
use NeuronAI\Tools\ToolInterface;
811
use NeuronAI\Tools\ToolCallMessage;
@@ -40,9 +43,8 @@ class Anthropic implements AIProviderInterface
4043
public function __construct(
4144
protected string $key,
4245
protected string $model,
43-
protected string $version,
44-
protected int $max_tokens,
45-
protected int $context_window,
46+
protected string $version = '2023-06-01',
47+
protected int $max_tokens = 8192,
4648
protected ?float $temperature = null,
4749
protected ?array $stop_sequences = null,
4850
) {
@@ -68,14 +70,14 @@ public function systemPrompt(string $prompt): AIProviderInterface
6870
/**
6971
* Send a prompt to the AI agent.
7072
*
71-
* @param array|string $prompt
72-
* @return Message
73-
* @throws \Exception
73+
* @throws GuzzleException
7474
*/
7575
public function chat(array|string $prompt): Message
7676
{
7777
if (\is_string($prompt)) {
78-
$prompt = [['role' => 'user', 'content' => $prompt]];
78+
$prompt = [
79+
new UserMessage($prompt)
80+
];
7981
}
8082

8183
$json = \array_filter([
@@ -106,7 +108,7 @@ public function chat(array|string $prompt): Message
106108
$content['input']
107109
);
108110
} else {
109-
$response = new Message('assistant', \last($result['content'])['text']);
111+
$response = new AssistantMessage(\last($result['content'])['text']);
110112
}
111113

112114
// Attach the usage for the current interaction
@@ -122,26 +124,6 @@ public function chat(array|string $prompt): Message
122124
return $response;
123125
}
124126

125-
/**
126-
* The context window limitation of the LLM.
127-
*
128-
* @return int
129-
*/
130-
public function contextWindow(): int
131-
{
132-
return $this->context_window;
133-
}
134-
135-
/**
136-
* The maximum number of tokens to generate before stopping.
137-
*
138-
* @return int
139-
*/
140-
public function maxCompletionTokens(): int
141-
{
142-
return $this->max_tokens;
143-
}
144-
145127
public function setTools(array $tools): self
146128
{
147129
$this->tools = $tools;

src/Providers/Log.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ public function chat(array|string $prompt): Message
4444
return new AssistantMessage("I'm the log Neuron AI driver");
4545
}
4646

47-
public function contextWindow(): int
48-
{
49-
return 1000000000;
50-
}
51-
52-
public function maxCompletionTokens(): int
53-
{
54-
return 1000000000;
55-
}
56-
5747
public function setTools(array $tools): AIProviderInterface
5848
{
5949
return $this;

src/Providers/Mistral.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Mistral implements AIProviderInterface
2727
public function __construct(
2828
protected string $key,
2929
protected string $model,
30-
protected int $max_tokens
30+
protected int $max_tokens = 1024
3131
) {
3232
$this->client = new Client([
3333
'base_uri' => 'https://api.mistral.ai/v1',
@@ -71,16 +71,6 @@ public function chat(array|string $prompt): Message
7171
return new AssistantMessage($result['choices'][0]['message']['content']);
7272
}
7373

74-
public function contextWindow(): int
75-
{
76-
return $this->max_tokens;
77-
}
78-
79-
public function maxCompletionTokens(): int
80-
{
81-
return $this->max_tokens;
82-
}
83-
8474
public function setTools(array $tools): AIProviderInterface
8575
{
8676
throw new \LogicException('Not implemented');

src/Providers/OpenAI.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ class OpenAI implements AIProviderInterface
2727
public function __construct(
2828
protected string $key,
2929
protected string $model,
30-
protected int $context_window,
31-
protected int $max_tokens,
30+
protected int $max_tokens = 1024,
3231
) {
3332
$this->client = new Client([
3433
'base_uri' => 'https://api.openai.com/v1',
@@ -72,16 +71,6 @@ public function chat(array|string $prompt): Message
7271
return new AssistantMessage($result['choices'][0]['message']['content']);
7372
}
7473

75-
public function contextWindow(): int
76-
{
77-
return $this->context_window;
78-
}
79-
80-
public function maxCompletionTokens(): int
81-
{
82-
return $this->max_tokens;
83-
}
84-
8574
public function setTools(array $tools): AIProviderInterface
8675
{
8776
throw new \LogicException('Not implemented.');

0 commit comments

Comments
 (0)