Skip to content

Commit

Permalink
Configurability cont.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Aug 29, 2024
1 parent f9d7d9e commit 1a5b931
Show file tree
Hide file tree
Showing 25 changed files with 49 additions and 142 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"symfony/type-info": "^7.1",
"symfony/validator": "^6.4 || ^7.0",
"vlucas/phpdotenv": "^5.6",
"spatie/array-to-xml": "^3.3"
"spatie/array-to-xml": "^3.3",
"adbario/php-dot-notation": "^3.3"
},
"scripts": {
"tests": "@php vendor/bin/pest",
Expand Down
19 changes: 14 additions & 5 deletions config/instructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
use Cognesy\Instructor\Utils\Env;

return [
'defaultConnection' => 'openai',
'useObjectReferences' => Env::get('INSTRUCTOR_USE_OBJECT_REFERENCES', false),
'cache' => [
'enabled' => Env::get('INSTRUCTOR_CACHE_ENABLED', false),
'expiry' => Env::get('INSTRUCTOR_CACHE_EXPIRY', 3600),
'expiryInSeconds' => Env::get('INSTRUCTOR_CACHE_EXPIRY', 3600),
'path' => Env::get('INSTRUCTOR_CACHE_PATH', '/tmp/instructor/cache'),
],
'debug' => [
'enabled' => Env::get('INSTRUCTOR_DEBUG', false),
'stopOnDebug' => Env::get('INSTRUCTOR_STOP_ON_DEBUG', false),
'forceDebug' => Env::get('INSTRUCTOR_FORCE_DEBUG', false),
],
'useObjectReferences' => Env::get('INSTRUCTOR_USE_OBJECT_REFERENCES', false),

'defaultConnection' => 'openai',
'connections' => [
'anthropic' => [
'clientType' => ClientType::Anthropic->value,
Expand Down Expand Up @@ -75,7 +75,7 @@
'clientType' => ClientType::Groq->value,
'apiUrl' => Env::get('GROQ_API_URL', 'https://api.groq.com/openai/v1'),
'apiKey' => Env::get('GROQ_API_KEY', ''),
'defaultModel' => Env::get('GROQ_DEFAULT_MODEL', 'llama3-8b-8192'),
'defaultModel' => Env::get('GROQ_DEFAULT_MODEL', 'gemma2-9b-it'),
'defaultMaxTokens' => Env::get('GROQ_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('GROQ_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('GROQ_REQUEST_TIMEOUT', 30),
Expand All @@ -89,6 +89,15 @@
'connectTimeout' => Env::get('MISTRAL_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('MISTRAL_REQUEST_TIMEOUT', 30),
],
'ollama' => [
'clientType' => ClientType::Ollama->value,
'apiUrl' => Env::get('OLLAMA_API_URL', 'http://localhost:11434/v1'),
'apiKey' => Env::get('OLLAMA_API_KEY', ''),
'defaultModel' => Env::get('OLLAMA_DEFAULT_MODEL', 'gemma2:2b'),
'defaultMaxTokens' => Env::get('OLLAMA_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('OLLAMA_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('OLLAMA_REQUEST_TIMEOUT', 30),
],
'openai' => [
'clientType' => ClientType::OpenAI->value,
'apiUrl' => Env::get('OPENAI_API_URL', 'https://api.openai.com/v1'),
Expand All @@ -105,7 +114,7 @@
'clientType' => ClientType::OpenRouter->value,
'apiUrl' => Env::get('OPENROUTER_API_URL', 'https://api.openrouter.io/v1'),
'apiKey' => Env::get('OPENROUTER_API_KEY', ''),
'defaultModel' => Env::get('OPENROUTER_DEFAULT_MODEL', 'gpt-4o-mini'),
'defaultModel' => Env::get('OPENROUTER_DEFAULT_MODEL', 'microsoft/phi-3.5-mini-128k-instruct'),
'defaultMaxTokens' => Env::get('OPENROUTER_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('OPENROUTER_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('OPENROUTER_REQUEST_TIMEOUT', 30),
Expand Down
4 changes: 3 additions & 1 deletion src/ApiClient/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cognesy\Instructor\Events\EventDispatcher;
use Cognesy\Instructor\Events\Traits\HandlesEventListeners;
use Cognesy\Instructor\Events\Traits\HandlesEvents;
use Cognesy\Instructor\Utils\Settings;
use Saloon\Enums\Method;

abstract class ApiClient implements CanCallApi
Expand All @@ -19,7 +20,6 @@ abstract class ApiClient implements CanCallApi
use Traits\HandlesApiRequest;
use Traits\HandlesApiRequestFactory;
use Traits\HandlesApiResponse;
use Traits\HandlesAsyncApiResponse;
use Traits\HandlesDebug;
use Traits\HandlesDefaultMaxTokens;
use Traits\HandlesDefaultModel;
Expand All @@ -34,6 +34,8 @@ public function __construct(
) {
$this->withEventDispatcher($events ?? new EventDispatcher('api-client'));
$this->clientType = ClientType::fromClientClass(static::class);
$this->withMaxTokens(Settings::get("connections.{$this->clientType->value}.defaultMaxTokens", 1024));
$this->withModel(Settings::get("connections.{$this->clientType->value}.defaultModel", ''));
}

/// PUBLIC API //////////////////////////////////////////////////////////////////////////////////////////
Expand Down
11 changes: 3 additions & 8 deletions src/ApiClient/Contracts/CanCallApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@
use Cognesy\Instructor\ApiClient\Responses\PartialApiResponse;
use Cognesy\Instructor\Enums\Mode;
use Generator;
use GuzzleHttp\Promise\PromiseInterface;
use Saloon\Enums\Method;

interface CanCallApi
{
public function request(array $body, string $endpoint, Method $method): static;

public function respond(ApiRequest $request) : ApiResponse;
public function get() : ApiResponse;

/** @return Generator<PartialApiResponse> */
public function stream() : Generator;

public function async() : PromiseInterface;
public function getModeRequestClass(Mode $mode) : string;
public function withApiRequest(ApiRequest $request) : static;

public function defaultMaxTokens() : int;
public function defaultModel() : string;

public function getModeRequestClass(Mode $mode) : string;

public function withApiRequest(ApiRequest $request) : static;
}
13 changes: 3 additions & 10 deletions src/ApiClient/Factories/ApiClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,26 @@
use Cognesy\Instructor\ApiClient\Contracts\CanCallApi;
use Cognesy\Instructor\ApiClient\Enums\ClientType;
use Cognesy\Instructor\Events\EventDispatcher;
use Cognesy\Instructor\Utils\Settings;
use InvalidArgumentException;

class ApiClientFactory
{
protected CanCallApi $defaultClient;
protected array $config = [];

public function __construct(
public string $configPath,
public EventDispatcher $events,
public ApiRequestFactory $apiRequestFactory,
) {
$this->config = $this->readConfig($this->configPath);

$defaultConnection = $this->config['defaultConnection'] ?? null;
$defaultConnection = Settings::get('defaultConnection');
if (!$defaultConnection) {
throw new InvalidArgumentException("No default client connection found");
}
$this->defaultClient = $this->client($defaultConnection);
}

public function client(string $connection) : CanCallApi {
$clientConfig = $this->config['connections'][$connection] ?? null;
$clientConfig = Settings::get("connections.$connection");
if (!$clientConfig) {
throw new InvalidArgumentException("No client connection config found for '{$connection}'");
}
Expand Down Expand Up @@ -60,8 +57,4 @@ protected function getFromConfig(array $config): CanCallApi {
->withModel($config['defaultModel'] ?? '')
->withMaxTokens($config['defaultMaxTokens'] ?? 1024);
}

protected function readConfig(string $configPath): array {
return require __DIR__ . $configPath;
}
}
55 changes: 0 additions & 55 deletions src/ApiClient/Traits/HandlesAsyncApiResponse.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/ApiClient/Traits/HandlesDefaultMaxTokens.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

namespace Cognesy\Instructor\ApiClient\Traits;

trait HandlesDefaultMaxTokens
{
public int $defaultMaxTokens = 512;
public int $defaultMaxTokens = 1024;

public function defaultMaxTokens() : int {
return $this->defaultMaxTokens;
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/Anthropic/AnthropicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class AnthropicClient extends ApiClient
{
public string $defaultModel = 'claude-3-5-sonnet-20240620';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/Azure/AzureClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class AzureClient extends ApiClient
{
public string $defaultModel = 'gpt-3.5-turbo';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/Cohere/CohereClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class CohereClient extends ApiClient
{
public string $defaultModel = 'command-r';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/FireworksAI/FireworksAIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class FireworksAIClient extends ApiClient
{
public string $defaultModel = 'accounts/fireworks/models/mixtral-8x7b-instruct';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/Gemini/GeminiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class GeminiClient extends ApiClient
{
public string $defaultModel = 'gemini-1.5-flash';
public int $defaultMaxTokens = 4096;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/Groq/GroqClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class GroqClient extends ApiClient
{
public string $defaultModel = 'gemma2-9b-it';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/Mistral/MistralClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class MistralClient extends ApiClient
{
public string $defaultModel = 'mistral-small';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/Ollama/OllamaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class OllamaClient extends ApiClient
{
public string $defaultModel = 'gemma2:2b';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/OpenAI/OpenAIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

class OpenAIClient extends ApiClient
{
public string $defaultModel = 'gpt-4o-mini';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/OpenRouter/OpenRouterClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class OpenRouterClient extends ApiClient
{
public string $defaultModel = 'microsoft/phi-3.5-mini-128k-instruct';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
3 changes: 0 additions & 3 deletions src/Clients/TogetherAI/TogetherAIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

class TogetherAIClient extends ApiClient
{
public string $defaultModel = 'mistralai/Mixtral-8x7B-Instruct-v0.1';
public int $defaultMaxTokens = 1024;

public function __construct(
protected string $apiKey = '',
protected string $baseUri = '',
Expand Down
17 changes: 8 additions & 9 deletions src/Configs/RequestHandlingConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Cognesy\Instructor\Schema\Factories\SchemaFactory;
use Cognesy\Instructor\Schema\Factories\ToolCallBuilder;
use Cognesy\Instructor\Schema\Utils\ReferenceQueue;
use Cognesy\Instructor\Utils\Env;
use Cognesy\Instructor\Utils\Settings;

class RequestHandlingConfig implements CanAddConfiguration
{
Expand All @@ -29,7 +29,6 @@ class: ApiRequestFactory::class,
$config->object(
class: ApiClientFactory::class,
context: [
'configPath' => Env::get('INSTRUCTOR_CONFIG_PATH', '/../../../config/instructor.php'),
'events' => $config->reference(EventDispatcher::class),
'apiRequestFactory' => $config->reference(ApiRequestFactory::class),
],
Expand Down Expand Up @@ -66,7 +65,7 @@ class: ToolCallBuilder::class,
$config->object(
class: SchemaFactory::class,
context: [
'useObjectReferences' => $_ENV['INSTRUCTOR_USE_OBJECT_REFERENCES'] ?? false,
'useObjectReferences' => Settings::get('useObjectReferences') ?? false,
]
);

Expand All @@ -84,18 +83,18 @@ class: ApiRequestConfig::class,
$config->object(
class: CacheConfig::class,
context: [
'enabled' => $_ENV['INSTRUCTOR_CACHE_ENABLED'] ?? false,
'expiryInSeconds' => $_ENV['INSTRUCTOR_CACHE_EXPIRY'] ?? 3600,
'cachePath' => $_ENV['INSTRUCTOR_CACHE_PATH'] ?? '/tmp/instructor/cache',
'enabled' => Settings::get('cache.enabled', false),
'expiryInSeconds' => Settings::get('cache.expiryInSeconds', 3600),
'cachePath' => Settings::get('cache.path', '/tmp/instructor/cache'),
]
);

$config->object(
class: DebugConfig::class,
context: [
'debug' => $_ENV['INSTRUCTOR_DEBUG'] ?? false,
'stopOnDebug' => $_ENV['INSTRUCTOR_STOP_ON_DEBUG'] ?? false,
'forceDebug' => $_ENV['INSTRUCTOR_FORCE_DEBUG'] ?? false,
'debug' => Settings::get('debug.enabled', false),
'stopOnDebug' => Settings::get('debug.stopOnDebug', false),
'forceDebug' => Settings::get('debug.forceDebug', false),
]
);

Expand Down
Loading

0 comments on commit 1a5b931

Please sign in to comment.