Skip to content

Commit

Permalink
Improved configurability, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Aug 28, 2024
1 parent 78c263a commit f9d7d9e
Show file tree
Hide file tree
Showing 104 changed files with 865 additions and 2,287 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,10 @@ $yourApiKey = Env::get('OPENAI_API_KEY'); // use your own API key
// Create instance of OpenAI client initialized with custom parameters
$client = new OpenAIClient(
$yourApiKey,
baseUri: 'https://api.openai.com', // you can change base URI
organization: '',
baseUri: 'https://api.openai.com/v1', // you can change base URI
connectTimeout: 3,
requestTimeout: 30,
metadata: ['organization' => ''],
);

/// Get Instructor with the default client component overridden with your own
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"symfony/css-selector": "^7.1",
"symfony/dom-crawler": "^7.1",
"duzun/hquery": "^3.1",
"ext-dom": "*"
"ext-dom": "*",
"psy/psysh": "@stable"
},
"config": {
"allow-plugins": {
Expand Down
123 changes: 123 additions & 0 deletions config/instructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
use Cognesy\Instructor\ApiClient\Enums\ClientType;
use Cognesy\Instructor\Utils\Env;

return [
'defaultConnection' => 'openai',
'cache' => [
'enabled' => Env::get('INSTRUCTOR_CACHE_ENABLED', false),
'expiry' => 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),

'connections' => [
'anthropic' => [
'clientType' => ClientType::Anthropic->value,
'apiUrl' => Env::get('ANTHROPIC_API_URL', 'https://api.anthropic.com/v1'),
'apiKey' => Env::get('ANTHROPIC_API_KEY', ''),
'metadata' => [
'apiVersion' => Env::get('ANTHROPIC_API_VERSION', '2023-06-01'),
'beta' => Env::get('ANTHROPIC_BETA', 'prompt-caching-2024-07-31'),
],
'defaultModel' => Env::get('ANTHROPIC_DEFAULT_MODEL', 'claude-3-haiku-20240307'),
'defaultMaxTokens' => Env::get('ANTHROPIC_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('ANTHROPIC_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('ANTHROPIC_REQUEST_TIMEOUT', 30),
],
'azure' => [
'clientType' => ClientType::Azure->value,
'apiUrl' => Env::get('AZURE_OPENAI_BASE_URI', 'https://api.openai.com/v1'),
'apiKey' => Env::get('AZURE_OPENAI_API_KEY', ''),
'metadata' => [
'apiVersion' => Env::get('AZURE_OPENAI_API_VERSION', '2023-03-15-preview'),
'resourceName' => Env::get('AZURE_OPENAI_RESOURCE_NAME', 'instructor-dev'),
'deploymentName' => Env::get('AZURE_OPENAI_DEPLOYMENT_NAME', 'gpt-4o-mini'),
],
'defaultModel' => Env::get('AZURE_OPENAI_DEFAULT_MODEL', 'gpt-4o-mini'),
'defaultMaxTokens' => Env::get('AZURE_OPENAI_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('AZURE_OPENAI_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('AZURE_OPENAI_REQUEST_TIMEOUT', 30),
],
'cohere' => [
'clientType' => ClientType::Cohere->value,
'apiUrl' => Env::get('COHERE_API_URL', 'https://api.cohere.ai/v1'),
'apiKey' => Env::get('COHERE_API_KEY', ''),
'defaultModel' => Env::get('COHERE_DEFAULT_MODEL', 'cohere-r-plus'),
'defaultMaxTokens' => Env::get('COHERE_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('COHERE_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('COHERE_REQUEST_TIMEOUT', 30),
],
'fireworks' => [
'clientType' => ClientType::Fireworks->value,
'apiUrl' => Env::get('FIREWORKS_API_URL', 'https://api.fireworks.ai/inference/v1'),
'apiKey' => Env::get('FIREWORKS_API_KEY', ''),
'defaultModel' => Env::get('FIREWORKS_DEFAULT_MODEL', 'accounts/fireworks/models/mixtral-8x7b-instruct'),
'defaultMaxTokens' => Env::get('FIREWORKS_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('FIREWORKS_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('FIREWORKS_REQUEST_TIMEOUT', 30),
],
'gemini' => [
'clientType' => ClientType::Gemini->value,
'apiUrl' => Env::get('GEMINI_API_URL', 'https://generativelanguage.googleapis.com/v1beta'),
'apiKey' => Env::get('GEMINI_API_KEY', ''),
'defaultModel' => Env::get('GEMINI_DEFAULT_MODEL', 'gemini-1.5-flash'),
'defaultMaxTokens' => Env::get('GEMINI_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('GEMINI_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('GEMINI_REQUEST_TIMEOUT', 30),
],
'groq' => [
'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'),
'defaultMaxTokens' => Env::get('GROQ_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('GROQ_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('GROQ_REQUEST_TIMEOUT', 30),
],
'mistral' => [
'clientType' => ClientType::Mistral->value,
'apiUrl' => Env::get('MISTRAL_API_URL', 'https://api.mistral.ai/v1'),
'apiKey' => Env::get('MISTRAL_API_KEY', ''),
'defaultModel' => Env::get('MISTRAL_DEFAULT_MODEL', 'mistral-small-latest'),
'defaultMaxTokens' => Env::get('MISTRAL_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('MISTRAL_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('MISTRAL_REQUEST_TIMEOUT', 30),
],
'openai' => [
'clientType' => ClientType::OpenAI->value,
'apiUrl' => Env::get('OPENAI_API_URL', 'https://api.openai.com/v1'),
'apiKey' => Env::get('OPENAI_API_KEY', ''),
'metadata' => [
'organization' => ''
],
'defaultModel' => Env::get('OPENAI_DEFAULT_MODEL', 'gpt-4o-mini'),
'defaultMaxTokens' => Env::get('OPENAI_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('OPENAI_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('OPENAI_REQUEST_TIMEOUT', 30),
],
'openrouter' => [
'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'),
'defaultMaxTokens' => Env::get('OPENROUTER_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('OPENROUTER_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('OPENROUTER_REQUEST_TIMEOUT', 30),
],
'together' => [
'clientType' => ClientType::Together->value,
'apiUrl' => Env::get('TOGETHER_API_URL', 'https://api.together.xyz/v1'),
'apiKey' => Env::get('TOGETHER_API_KEY', ''),
'defaultModel' => Env::get('TOGETHER_DEFAULT_MODEL', 'mistralai/Mixtral-8x7B-Instruct-v0.1'),
'defaultMaxTokens' => Env::get('TOGETHER_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('TOGETHER_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('TOGETHER_REQUEST_TIMEOUT', 30),
],
],
];
2 changes: 1 addition & 1 deletion docs/advanced/model_options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ use Cognesy\Instructor\Clients\OpenAI\OpenAIClient;
$client = new OpenAIClient(
apiKey: $yourApiKey,
baseUri: 'https://api.openai.com/v1',
organization: '',
connectTimeout: 3,
requestTimeout: 30,
metadata: ['organization' => ''],
);

/// Get Instructor with the default client component overridden with your own
Expand Down
5 changes: 1 addition & 4 deletions docs/cookbook/examples/advanced/partials.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ function partialUpdate($partial) {
// Clear the screen and move the cursor to the top
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';

// Print explanation
echo "Waiting 250ms on every update received to make changes easier to observe...\n";

// Display the partial object
dump($partial);

// Wait a bit before clearing the screen to make partial changes slower.
// Don't use this in your application :)
usleep(250000);
//usleep(250000);
}
?>
```
Expand Down
4 changes: 0 additions & 4 deletions docs/cookbook/examples/advanced/streaming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ function partialUpdate($partial) {
// Clear the screen and move the cursor to the top
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';

// Print explanation
echo "Waiting 250ms on every update received to make changes easier to observe...\n";

// Display the partial object

dump($partial);

// Wait a bit before clearing the screen to make partial changes slower.
Expand Down
8 changes: 5 additions & 3 deletions docs/cookbook/examples/api_support/azure_openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class User {
/// Custom client parameters: base URI
$client = (new AzureClient(
apiKey: Env::get('AZURE_OPENAI_API_KEY'), // set your own value/source
resourceName: Env::get('AZURE_OPENAI_RESOURCE_NAME'), // set your own value/source
deploymentId: Env::get('AZURE_OPENAI_DEPLOYMENT_NAME'), // set your own value/source
apiVersion: Env::get('AZURE_OPENAI_API_VERSION'), // set your own value/source
metadata: [
'resourceName' => Env::get('AZURE_OPENAI_RESOURCE_NAME'), // set your own value/source
'deploymentId' => Env::get('AZURE_OPENAI_DEPLOYMENT_NAME'), // set your own value/source
'apiVersion' => Env::get('AZURE_OPENAI_API_VERSION'), // set your own value/source
],
));

/// Get Instructor with the default client component overridden with your own
Expand Down
2 changes: 1 addition & 1 deletion docs/cookbook/examples/api_support/openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ $yourApiKey = Env::get('OPENAI_API_KEY'); // use your own API key
$client = new OpenAIClient(
apiKey: $yourApiKey,
baseUri: 'https://api.openai.com/v1',
organization: '',
connectTimeout: 3,
requestTimeout: 30,
metadata: ['organization' => ''],
);

/// Get Instructor with the default client component overridden with your own
Expand Down
2 changes: 2 additions & 0 deletions examples/02_Advanced/Caching/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class User {
responseModel: User::class,
)->withRequestCache()->get();

eval(\Psy\sh());

$delta = Profiler::mark('cache 1st call')->mili();
echo "Time elapsed (cache on, 1st call): $delta msec\n\n";

Expand Down
5 changes: 1 addition & 4 deletions examples/02_Advanced/PartialUpdates/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ function partialUpdate($partial) {
// Clear the screen and move the cursor to the top
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';

// Print explanation
echo "Waiting 250ms on every update received to make changes easier to observe...\n";

// Display the partial object
dump($partial);

// Wait a bit before clearing the screen to make partial changes slower.
// Don't use this in your application :)
usleep(250000);
//usleep(250000);
}
?>
```
Expand Down
4 changes: 0 additions & 4 deletions examples/02_Advanced/Streaming/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ function partialUpdate($partial) {
// Clear the screen and move the cursor to the top
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';

// Print explanation
echo "Waiting 250ms on every update received to make changes easier to observe...\n";

// Display the partial object

dump($partial);

// Wait a bit before clearing the screen to make partial changes slower.
Expand Down
8 changes: 5 additions & 3 deletions examples/05_APISupport/LLMSupportAzureOAI/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class User {
/// Custom client parameters: base URI
$client = (new AzureClient(
apiKey: Env::get('AZURE_OPENAI_API_KEY'), // set your own value/source
resourceName: Env::get('AZURE_OPENAI_RESOURCE_NAME'), // set your own value/source
deploymentId: Env::get('AZURE_OPENAI_DEPLOYMENT_NAME'), // set your own value/source
apiVersion: Env::get('AZURE_OPENAI_API_VERSION'), // set your own value/source
metadata: [
'resourceName' => Env::get('AZURE_OPENAI_RESOURCE_NAME'), // set your own value/source
'deploymentId' => Env::get('AZURE_OPENAI_DEPLOYMENT_NAME'), // set your own value/source
'apiVersion' => Env::get('AZURE_OPENAI_API_VERSION'), // set your own value/source
]
));

/// Get Instructor with the default client component overridden with your own
Expand Down
2 changes: 1 addition & 1 deletion examples/05_APISupport/LLMSupportFireworksAI/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class User {
}

// Mistral instance params
$yourApiKey = Env::get('FIREWORKSAI_API_KEY'); // set your own API key
$yourApiKey = Env::get('FIREWORKS_API_KEY'); // set your own API key

// Create instance of client initialized with custom parameters
$client = new FireworksAIClient(
Expand Down
2 changes: 1 addition & 1 deletion examples/05_APISupport/LLMSupportOpenAI/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class User {
$client = new OpenAIClient(
apiKey: $yourApiKey,
baseUri: 'https://api.openai.com/v1',
organization: '',
connectTimeout: 3,
requestTimeout: 30,
metadata: ['organization' => ''],
);

/// Get Instructor with the default client component overridden with your own
Expand Down
2 changes: 1 addition & 1 deletion examples/05_APISupport/LLMSupportTogetherAI/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class User {

// Create instance of client initialized with custom parameters
$client = new TogetherAIClient(
apiKey: Env::get('TOGETHERAI_API_KEY'),
apiKey: Env::get('TOGETHER_API_KEY'),
);

/// Get Instructor with the default client component overridden with your own
Expand Down
7 changes: 5 additions & 2 deletions notes/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ Catch up with the latest additions.

## TODOs

- Parallel tool calls
- Multiple tools with tool selection
- Modules: Add module observability via events - currently no access to this info
- Parallel tool calls
- Generate unstructured, then format to structured - to improve reasoning

### API Client

- Clean up predefined models, prices, etc.

### Addon: Modules

- Modules: Add module observability via events - currently no access to this info

### Configuration

- Export configuration to user folder / use external configuration
Expand Down
4 changes: 4 additions & 0 deletions src/ApiClient/ApiClient.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Cognesy\Instructor\ApiClient;

use Cognesy\Instructor\ApiClient\Enums\ClientType;
use Cognesy\Instructor\ApiClient\RequestConfig\ApiRequestConfig;
use Cognesy\Instructor\ApiClient\Contracts\CanCallApi;
use Cognesy\Instructor\Enums\Mode;
Expand All @@ -26,10 +27,13 @@ abstract class ApiClient implements CanCallApi
use Traits\HandlesStreamApiResponse;
use Traits\ReadsStreamResponse;

protected ClientType $clientType;

public function __construct(
EventDispatcher $events = null,
) {
$this->withEventDispatcher($events ?? new EventDispatcher('api-client'));
$this->clientType = ClientType::fromClientClass(static::class);
}

/// PUBLIC API //////////////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 6 additions & 0 deletions src/ApiClient/ApiConnector.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Cognesy\Instructor\ApiClient;

use Saloon\Contracts\Authenticator;
use Saloon\Http\Auth\TokenAuthenticator;
use Saloon\Http\Connector;
use Saloon\Traits\Plugins\AlwaysThrowOnErrors;
use Saloon\Traits\Plugins\HasTimeout;
Expand Down Expand Up @@ -46,4 +48,8 @@ protected function defaultHeaders(): array {
public function defaultConfig(): array {
return ['stream' => true];
}

protected function defaultAuth() : Authenticator {
return new TokenAuthenticator($this->apiKey);
}
}
3 changes: 3 additions & 0 deletions src/ApiClient/Contracts/CanCallApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cognesy\Instructor\ApiClient\Contracts;

use Cognesy\Instructor\ApiClient\Requests\ApiRequest;
use Cognesy\Instructor\ApiClient\Responses\ApiResponse;
use Cognesy\Instructor\ApiClient\Responses\PartialApiResponse;
use Cognesy\Instructor\Enums\Mode;
Expand All @@ -24,4 +25,6 @@ public function defaultMaxTokens() : int;
public function defaultModel() : string;

public function getModeRequestClass(Mode $mode) : string;

public function withApiRequest(ApiRequest $request) : static;
}
4 changes: 3 additions & 1 deletion src/ApiClient/Enums/ClientType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

enum ClientType : string
{
use Traits\HandlesCreation;
use Traits\HandlesAccess;
use Traits\HandlesCreation;
use Traits\HandlesMapping;
use Traits\HandlesResponse;
use Traits\HandlesStreamData;

case Anthropic = 'anthropic';
//case Anyscale = 'anyscale';
Expand Down
Loading

0 comments on commit f9d7d9e

Please sign in to comment.