Skip to content

Commit

Permalink
Tool use addon
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Nov 14, 2024
1 parent 3d76886 commit a1c2d76
Show file tree
Hide file tree
Showing 37 changed files with 2,978 additions and 2,279 deletions.
282 changes: 145 additions & 137 deletions config/llm.php
Original file line number Diff line number Diff line change
@@ -1,137 +1,145 @@
<?php

use Cognesy\Instructor\Features\LLM\Enums\LLMProviderType;
use Cognesy\Instructor\Utils\Env;

return [
'useObjectReferences' => false,
'defaultConnection' => 'openai',

'defaultToolName' => 'extracted_data',
'defaultToolDescription' => 'Function call based on user instructions.',
'defaultRetryPrompt' => "JSON generated incorrectly, fix following errors:\n",
'defaultMdJsonPrompt' => "Response must validate against this JSON Schema:\n<|json_schema|>\n. Respond correctly with strict JSON object within a ```json {} ``` codeblock.\n",
'defaultJsonPrompt' => "Response must follow JSON Schema:\n<|json_schema|>\n. Respond correctly with strict JSON object.\n",
'defaultToolsPrompt' => "Extract correct and accurate data from the input using provided tools.\n",

'connections' => [
'anthropic' => [
'providerType' => LLMProviderType::Anthropic->value,
'apiUrl' => 'https://api.anthropic.com/v1',
'apiKey' => Env::get('ANTHROPIC_API_KEY', ''),
'endpoint' => '/messages',
'metadata' => [
'apiVersion' => '2023-06-01',
'beta' => 'prompt-caching-2024-07-31',
],
'defaultModel' => 'claude-3-haiku-20240307',
'defaultMaxTokens' => 1024,
],
'azure' => [
'providerType' => LLMProviderType::Azure->value,
'apiUrl' => 'https://{resourceName}.openai.azure.com/openai/deployments/{deploymentId}',
'apiKey' => Env::get('AZURE_OPENAI_API_KEY', ''),
'endpoint' => '/chat/completions',
'metadata' => [
'apiVersion' => '2024-08-01-preview',
'resourceName' => 'instructor-dev',
'deploymentId' => 'gpt-4o-mini',
],
'defaultModel' => 'gpt-4o-mini',
'defaultMaxTokens' => 1024,
],
'cohere1' => [
'providerType' => LLMProviderType::CohereV1->value,
'apiUrl' => 'https://api.cohere.ai/v1',
'apiKey' => Env::get('COHERE_API_KEY', ''),
'endpoint' => '/chat',
'defaultModel' => 'command-r-plus-08-2024',
'defaultMaxTokens' => 1024,
],
'cohere2' => [
'providerType' => LLMProviderType::CohereV2->value,
'apiUrl' => 'https://api.cohere.ai/v2',
'apiKey' => Env::get('COHERE_API_KEY', ''),
'endpoint' => '/chat',
'defaultModel' => 'command-r-plus-08-2024',
'defaultMaxTokens' => 1024,
],
'fireworks' => [
'providerType' => LLMProviderType::Fireworks->value,
'apiUrl' => 'https://api.fireworks.ai/inference/v1',
'apiKey' => Env::get('FIREWORKS_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'accounts/fireworks/models/mixtral-8x7b-instruct',
'defaultMaxTokens' => 1024,
],
'gemini' => [
'providerType' => LLMProviderType::Gemini->value,
'apiUrl' => 'https://generativelanguage.googleapis.com/v1beta',
'apiKey' => Env::get('GEMINI_API_KEY', ''),
'endpoint' => '/models/{model}:generateContent',
'defaultModel' => 'gemini-1.5-flash-latest',
'defaultMaxTokens' => 1024,
],
'grok' => [
'providerType' => LLMProviderType::Grok->value,
'apiUrl' => 'https://api.x.ai/v1',
'apiKey' => Env::get('GROK_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'grok-beta',
'defaultMaxTokens' => 1024,
],
'groq' => [
'providerType' => LLMProviderType::Groq->value,
'apiUrl' => 'https://api.groq.com/openai/v1',
'apiKey' => Env::get('GROQ_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'llama3-groq-8b-8192-tool-use-preview', // 'gemma2-9b-it',
'defaultMaxTokens' => 1024,
],
'mistral' => [
'providerType' => LLMProviderType::Mistral->value,
'apiUrl' => 'https://api.mistral.ai/v1',
'apiKey' => Env::get('MISTRAL_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'mistral-small-latest',
'defaultMaxTokens' => 1024,
],
'ollama' => [
'providerType' => LLMProviderType::Ollama->value,
'apiUrl' => 'http://localhost:11434/v1',
'apiKey' => Env::get('OLLAMA_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'qwen2.5:0.5b', //'gemma2:2b',
'defaultMaxTokens' => 1024,
'httpClient' => 'http-ollama',
],
'openai' => [
'providerType' => LLMProviderType::OpenAI->value,
'apiUrl' => 'https://api.openai.com/v1',
'apiKey' => Env::get('OPENAI_API_KEY', ''),
'endpoint' => '/chat/completions',
'metadata' => [
'organization' => '',
'project' => '',
],
'defaultModel' => 'gpt-4o-mini',
'defaultMaxTokens' => 1024,
],
'openrouter' => [
'providerType' => LLMProviderType::OpenRouter->value,
'apiUrl' => 'https://openrouter.ai/api/v1',
'apiKey' => Env::get('OPENROUTER_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'qwen/qwen-2.5-72b-instruct', //'microsoft/phi-3.5-mini-128k-instruct',
'defaultMaxTokens' => 1024,
],
'together' => [
'providerType' => LLMProviderType::Together->value,
'apiUrl' => 'https://api.together.xyz/v1',
'apiKey' => Env::get('TOGETHER_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'mistralai/Mixtral-8x7B-Instruct-v0.1',
'defaultMaxTokens' => 1024,
],
],
];
<?php

use Cognesy\Instructor\Features\LLM\Enums\LLMProviderType;
use Cognesy\Instructor\Utils\Env;

return [
'useObjectReferences' => false,
'defaultConnection' => 'openai',

'defaultToolName' => 'extracted_data',
'defaultToolDescription' => 'Function call based on user instructions.',
'defaultRetryPrompt' => "JSON generated incorrectly, fix following errors:\n",
'defaultMdJsonPrompt' => "Response must validate against this JSON Schema:\n<|json_schema|>\n. Respond correctly with strict JSON object within a ```json {} ``` codeblock.\n",
'defaultJsonPrompt' => "Response must follow JSON Schema:\n<|json_schema|>\n. Respond correctly with strict JSON object.\n",
'defaultToolsPrompt' => "Extract correct and accurate data from the input using provided tools.\n",

'connections' => [
'anthropic' => [
'providerType' => LLMProviderType::Anthropic->value,
'apiUrl' => 'https://api.anthropic.com/v1',
'apiKey' => Env::get('ANTHROPIC_API_KEY', ''),
'endpoint' => '/messages',
'metadata' => [
'apiVersion' => '2023-06-01',
'beta' => 'prompt-caching-2024-07-31',
],
'defaultModel' => 'claude-3-haiku-20240307',
'defaultMaxTokens' => 1024,
],
'azure' => [
'providerType' => LLMProviderType::Azure->value,
'apiUrl' => 'https://{resourceName}.openai.azure.com/openai/deployments/{deploymentId}',
'apiKey' => Env::get('AZURE_OPENAI_API_KEY', ''),
'endpoint' => '/chat/completions',
'metadata' => [
'apiVersion' => '2024-08-01-preview',
'resourceName' => 'instructor-dev',
'deploymentId' => 'gpt-4o-mini',
],
'defaultModel' => 'gpt-4o-mini',
'defaultMaxTokens' => 1024,
],
'cohere1' => [
'providerType' => LLMProviderType::CohereV1->value,
'apiUrl' => 'https://api.cohere.ai/v1',
'apiKey' => Env::get('COHERE_API_KEY', ''),
'endpoint' => '/chat',
'defaultModel' => 'command-r-plus-08-2024',
'defaultMaxTokens' => 1024,
],
'cohere2' => [
'providerType' => LLMProviderType::CohereV2->value,
'apiUrl' => 'https://api.cohere.ai/v2',
'apiKey' => Env::get('COHERE_API_KEY', ''),
'endpoint' => '/chat',
'defaultModel' => 'command-r-plus-08-2024',
'defaultMaxTokens' => 1024,
],
'fireworks' => [
'providerType' => LLMProviderType::Fireworks->value,
'apiUrl' => 'https://api.fireworks.ai/inference/v1',
'apiKey' => Env::get('FIREWORKS_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'accounts/fireworks/models/mixtral-8x7b-instruct',
'defaultMaxTokens' => 1024,
],
'gemini' => [
'providerType' => LLMProviderType::Gemini->value,
'apiUrl' => 'https://generativelanguage.googleapis.com/v1beta',
'apiKey' => Env::get('GEMINI_API_KEY', ''),
'endpoint' => '/models/{model}:generateContent',
'defaultModel' => 'gemini-1.5-flash-latest',
'defaultMaxTokens' => 1024,
],
'gemini-oai' => [
'providerType' => LLMProviderType::GeminiOAI->value,
'apiUrl' => 'https://generativelanguage.googleapis.com/v1beta/openai',
'apiKey' => Env::get('GEMINI_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'gemini-1.5-flash',
'defaultMaxTokens' => 1024,
],
'grok' => [
'providerType' => LLMProviderType::Grok->value,
'apiUrl' => 'https://api.x.ai/v1',
'apiKey' => Env::get('GROK_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'grok-beta',
'defaultMaxTokens' => 1024,
],
'groq' => [
'providerType' => LLMProviderType::Groq->value,
'apiUrl' => 'https://api.groq.com/openai/v1',
'apiKey' => Env::get('GROQ_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'llama3-groq-8b-8192-tool-use-preview', // 'gemma2-9b-it',
'defaultMaxTokens' => 1024,
],
'mistral' => [
'providerType' => LLMProviderType::Mistral->value,
'apiUrl' => 'https://api.mistral.ai/v1',
'apiKey' => Env::get('MISTRAL_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'mistral-small-latest',
'defaultMaxTokens' => 1024,
],
'ollama' => [
'providerType' => LLMProviderType::Ollama->value,
'apiUrl' => 'http://localhost:11434/v1',
'apiKey' => Env::get('OLLAMA_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'qwen2.5-coder:3b', //'gemma2:2b',
'defaultMaxTokens' => 1024,
'httpClient' => 'http-ollama',
],
'openai' => [
'providerType' => LLMProviderType::OpenAI->value,
'apiUrl' => 'https://api.openai.com/v1',
'apiKey' => Env::get('OPENAI_API_KEY', ''),
'endpoint' => '/chat/completions',
'metadata' => [
'organization' => '',
'project' => '',
],
'defaultModel' => 'gpt-4o-mini',
'defaultMaxTokens' => 1024,
],
'openrouter' => [
'providerType' => LLMProviderType::OpenRouter->value,
'apiUrl' => 'https://openrouter.ai/api/v1',
'apiKey' => Env::get('OPENROUTER_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'qwen/qwen-2.5-72b-instruct', //'microsoft/phi-3.5-mini-128k-instruct',
'defaultMaxTokens' => 1024,
],
'together' => [
'providerType' => LLMProviderType::Together->value,
'apiUrl' => 'https://api.together.xyz/v1',
'apiKey' => Env::get('TOGETHER_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'mistralai/Mixtral-8x7B-Instruct-v0.1',
'defaultMaxTokens' => 1024,
],
],
];
13 changes: 9 additions & 4 deletions evals/LLMModes/CompanyEval.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cognesy\Instructor\Extras\Evals\Contracts\CanGenerateObservations;
use Cognesy\Instructor\Extras\Evals\Execution;
use Cognesy\Instructor\Extras\Evals\Observation;
use Cognesy\Instructor\Features\LLM\Data\ToolCall;
use Cognesy\Instructor\Utils\Str;

class CompanyEval implements CanGenerateObservations
Expand Down Expand Up @@ -50,10 +51,14 @@ public function correctness(Execution $execution): Observation {
}

private function validateToolsData(Execution $execution) : bool {
$data = $execution->get('response')->toolsData[0] ?? [];
return 'store_company' === ($data['name'] ?? '')
&& 'ACME' === ($data['arguments']['name'] ?? '')
&& 2020 === (int) ($data['arguments']['year'] ?? 0);
/** @var ToolCall $toolCall */
$toolCall = $execution->get('response')->toolCalls?->first();
if (null === $toolCall) {
return false;
}
return 'store_company' === $toolCall->name()
&& 'ACME' === $toolCall->stringValue('name')
&& 2020 === $toolCall->intValue('year');
}

private function validateDefault(Execution $execution) : bool {
Expand Down
Loading

0 comments on commit a1c2d76

Please sign in to comment.