Skip to content

Commit

Permalink
Extras: Codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Sep 16, 2024
1 parent c64dbc7 commit 55731cb
Show file tree
Hide file tree
Showing 163 changed files with 1,252 additions and 429 deletions.
97 changes: 97 additions & 0 deletions config/embed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
use Cognesy\Instructor\ApiClient\Enums\ClientType;
use Cognesy\Instructor\Utils\Env;

return [
'defaultConnection' => 'openai',
'connections' => [
'azure' => [
'clientType' => ClientType::Azure->value,
'apiUrl' => Env::get('AZURE_OPENAI_BASE_URI', 'openai.azure.com'),
'apiKey' => Env::get('AZURE_OPENAI_API_KEY', ''),
'endpoint' => Env::get('AZURE_OPENAI_EMBED_ENDPOINT', '/embeddings'),
'metadata' => [
'apiVersion' => Env::get('AZURE_OPENAI_API_VERSION', '2023-03-15-preview'),
'resourceName' => Env::get('AZURE_OPENAI_RESOURCE_NAME', 'instructor-dev'),
'deploymentId' => Env::get('AZURE_OPENAI_DEPLOYMENT_NAME', 'gpt-4o-mini'),
],
'defaultModel' => Env::get('AZURE_OPENAI_EMBED_MODEL', 'text-embedding-3-small'),
'defaultDimensions' => Env::get('AZURE_OPENAI_EMBED_DIMENSIONS', 1536),
'maxInputs' => Env::get('AZURE_OPENAI_MAX_INPUTS', 16),
'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', ''),
'endpoint' => Env::get('COHERE_EMBED_ENDPOINT', '/embed'),
'defaultModel' => Env::get('COHERE_EMBED_MODEL', 'embed-multilingual-v3.0'),
'defaultDimensions' => Env::get('COHERE_EMBED_DIMENSIONS', 1024),
'maxInputs' => Env::get('COHERE_MAX_INPUTS', 96),
'connectTimeout' => Env::get('COHERE_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('COHERE_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', ''),
'endpoint' => Env::get('GEMINI_EMBED_ENDPOINT', '/{model}:batchEmbedContents'),
'defaultModel' => Env::get('GEMINI_EMBED_MODEL', 'models/text-embedding-004'),
'defaultDimensions' => Env::get('GEMINI_EMBED_DIMENSIONS', 768),
'maxInputs' => Env::get('GEMINI_MAX_INPUTS', 100), // max 2048 tokens
'connectTimeout' => Env::get('GEMINI_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('GEMINI_REQUEST_TIMEOUT', 30),
],
'jina' => [
'clientType' => ClientType::Jina->value,
'apiUrl' => Env::get('JINA_API_URL', 'https://api.jina.ai/v1'),
'apiKey' => Env::get('JINA_API_KEY', ''),
'endpoint' => Env::get('JINA_EMBED_ENDPOINT', '/embeddings'),
'metadata' => [
'organization' => ''
],
'defaultModel' => Env::get('JINA_EMBED_MODEL', 'jina-embeddings-v2-base-en'),
'defaultDimensions' => Env::get('JINA_EMBED_DIMENSIONS', 768),
'maxInputs' => Env::get('JINA_MAX_INPUTS', 500), // max 8192 tokens
'connectTimeout' => Env::get('JINA_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('JINA_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', ''),
'endpoint' => Env::get('MISTRAL_EMBED_ENDPOINT', '/embeddings'),
'defaultModel' => Env::get('MISTRAL_EMBED_MODEL', 'mistral-embed'),
'defaultDimensions' => Env::get('MISTRAL_EMBED_DIMENSIONS', 1024),
'maxInputs' => Env::get('MISTRAL_MAX_INPUTS', 512), // max 512 tokens
'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', ''),
'endpoint' => Env::get('OLLAMA_EMBED_ENDPOINT', '/embeddings'),
'defaultModel' => Env::get('OLLAMA_EMBED_MODEL', 'nomic-embed-text'),
'defaultDimensions' => Env::get('OLLAMA_EMBED_DIMENSIONS', 1024),
'maxInputs' => Env::get('OLLAMA_MAX_INPUTS', 512),
'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'),
'apiKey' => Env::get('OPENAI_API_KEY', ''),
'endpoint' => Env::get('OPENAI_EMBED_ENDPOINT', '/embeddings'),
'metadata' => [
'organization' => ''
],
'defaultModel' => Env::get('OPENAI_EMBED_MODEL', 'text-embedding-3-small'),
'defaultDimensions' => Env::get('OPENAI_EMBED_DIMENSIONS', 1536),
'maxInputs' => Env::get('OPENAI_MAX_INPUTS', 2048), // max 8192 tokens
'connectTimeout' => Env::get('OPENAI_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('OPENAI_REQUEST_TIMEOUT', 30),
],
],
];
5 changes: 2 additions & 3 deletions docs/cookbook/examples/api_support/anthropic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ Mode compatibility:
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\Anthropic\AnthropicClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -40,7 +38,8 @@ class User {
public array $hobbies;
}

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('anthropic');

$user = $instructor->respond(
Expand Down
5 changes: 2 additions & 3 deletions docs/cookbook/examples/api_support/azure_openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ using Azure OpenAI service console.
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\Azure\AzureClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -37,7 +35,8 @@ class User {
public array $hobbies;
}

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('azure');

// Call with your model name and preferred execution mode
Expand Down
5 changes: 2 additions & 3 deletions docs/cookbook/examples/api_support/cohere.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ Reasons Mode::Tools is not recommended:
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\Cohere\CohereClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -46,7 +44,8 @@ class User {
public array $hobbies;
}

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('cohere');

$user = $instructor->respond(
Expand Down
8 changes: 2 additions & 6 deletions docs/cookbook/examples/api_support/fireworks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ Mode compatibility:
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\FireworksAI\FireworksAIClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -41,10 +39,8 @@ class User {
public array $hobbies;
}

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

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('fireworks');

$user = $instructor
Expand Down
5 changes: 2 additions & 3 deletions docs/cookbook/examples/api_support/google_gemini.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ Here's how you can use Instructor with Gemini API.
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\Gemini\GeminiClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -39,7 +37,8 @@ class User {
public array $hobbies;
}

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('gemini');

$user = $instructor
Expand Down
8 changes: 2 additions & 6 deletions docs/cookbook/examples/api_support/groq.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ Here's how you can use Instructor with Groq API.
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\Groq\GroqClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -42,10 +40,8 @@ class User {
public ?int $age;
}

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

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('groq');

$user = $instructor
Expand Down
8 changes: 2 additions & 6 deletions docs/cookbook/examples/api_support/mistralai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ Mode compatibility:
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\Mistral\MistralClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -44,10 +42,8 @@ class User {
public array $hobbies;
}

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

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('mistral');

$user = $instructor
Expand Down
4 changes: 2 additions & 2 deletions docs/cookbook/examples/api_support/ollama.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Supported modes:
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\Ollama\OllamaClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;

Expand All @@ -40,7 +39,8 @@ class User {
public array $hobbies;
}

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('ollama');

$user = $instructor->respond(
Expand Down
8 changes: 2 additions & 6 deletions docs/cookbook/examples/api_support/openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ Mode compatibility:
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\OpenAI\OpenAIClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -40,10 +38,8 @@ class User {
public array $hobbies;
}

// OpenAI auth params
$yourApiKey = Env::get('OPENAI_API_KEY'); // use your own API key

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('openai');

$user = $instructor->respond(
Expand Down
5 changes: 2 additions & 3 deletions docs/cookbook/examples/api_support/openrouter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ in case of extraction issues.
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\OpenRouter\OpenRouterClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -42,7 +40,8 @@ class User {
public array $hobbies;
}

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('openrouter');

$user = $instructor->withDebug()->respond(
Expand Down
5 changes: 2 additions & 3 deletions docs/cookbook/examples/api_support/togetherai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ Mode compatibility:
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Clients\TogetherAI\TogetherAIClient;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

enum UserType : string {
case Guest = 'guest';
Expand All @@ -45,7 +43,8 @@ class User {
public array $hobbies;
}

/// Get Instructor with the default client component overridden with your own
// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withClient('together');

$user = $instructor
Expand Down
1 change: 1 addition & 0 deletions docs/cookbook/examples/basics/basic_use.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: 'Basic use'
docname: 'basic_use'
path: ''
---

## Overview
Expand Down
7 changes: 1 addition & 6 deletions docs/cookbook/examples/extras/complex_extraction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,7 @@ enum StakeholderRole: string {
case Other = 'other';
}

$client = new OpenAIClient(
apiKey: Env::get('OPENAI_API_KEY'),
requestTimeout: 90,
);

$instructor = (new Instructor)->withClient($client);
$instructor = new Instructor;

echo "PROJECT EVENTS:\n\n";

Expand Down
9 changes: 1 addition & 8 deletions docs/cookbook/examples/extras/complex_extraction_claude.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ enum StakeholderRole: string {
case Other = 'other';
}

// Create instance of client initialized with custom parameters
$client = new AnthropicClient(
apiKey: Env::get('ANTHROPIC_API_KEY'),
requestTimeout: 90,
);

/// Get Instructor with the default client component overridden with your own
$instructor = (new Instructor)->withClient('anthropic');

echo "PROJECT EVENTS:\n\n";
Expand All @@ -125,7 +118,7 @@ $events = $instructor
model: 'claude-3-haiku-20240307', //'claude-3-sonnet-20240229',
prompt: 'Extract a list of project events with all the details from the provided input in JSON format using schema: <|json_schema|>',
mode: Mode::Json,
examples: [['input' => 'Acme Insurance project to implement SalesTech CRM solution is currently in RED status due to delayed delivery of document production system, led by 3rd party vendor - Alfatech. Customer (Acme) is discussing the resolution with the vendor. Production deployment plan has been finalized on Aug 15th and awaiting customer approval.', 'output' => [["type" => "object", "title" => "sequenceOfProjectEvent", "description" => "A sequence of ProjectEvent", "properties" => ["list" => [["title" => "Absorbing delay by deploying extra resources", "description" => "System integrator (SysCorp) are working to absorb some of the delay by deploying extra resources to speed up development when the doc production is done.", "type" => "action", "status" => "open", "stakeholders" => [["name" => "SysCorp", "role" => "system integrator", "details" => "System integrator",],], "date" => "2021-09-01",], ["title" => "Finalization of production deployment plan", "description" => "Production deployment plan has been finalized on Aug 15th and awaiting customer approval.", "type" => "progress", "status" => "open", "stakeholders" => [["name" => "Acme", "role" => "customer", "details" => "Customer",],], "date" => "2021-08-15",],],]]]]],
// examples: [['input' => 'Acme Insurance project to implement SalesTech CRM solution is currently in RED status due to delayed delivery of document production system, led by 3rd party vendor - Alfatech. Customer (Acme) is discussing the resolution with the vendor. Production deployment plan has been finalized on Aug 15th and awaiting customer approval.', 'output' => [["type" => "object", "title" => "sequenceOfProjectEvent", "description" => "A sequence of ProjectEvent", "properties" => ["list" => [["title" => "Absorbing delay by deploying extra resources", "description" => "System integrator (SysCorp) are working to absorb some of the delay by deploying extra resources to speed up development when the doc production is done.", "type" => "action", "status" => "open", "stakeholders" => [["name" => "SysCorp", "role" => "system integrator", "details" => "System integrator",],], "date" => "2021-09-01",], ["title" => "Finalization of production deployment plan", "description" => "Production deployment plan has been finalized on Aug 15th and awaiting customer approval.", "type" => "progress", "status" => "open", "stakeholders" => [["name" => "Acme", "role" => "customer", "details" => "Customer",],], "date" => "2021-08-15",],],]]]]],
options: [
'max_tokens' => 4096,
'stream' => true,
Expand Down
4 changes: 0 additions & 4 deletions docs/cookbook/examples/extras/complex_extraction_cohere.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ enum StakeholderRole: string {
case Other = 'other';
}

$client = new CohereClient(
apiKey: Env::get('COHERE_API_KEY'),
);

$instructor = (new Instructor)->withClient('cohere');

echo "PROJECT EVENTS:\n\n";
Expand Down
Loading

0 comments on commit 55731cb

Please sign in to comment.