Coder Social home page Coder Social logo

kargnas / instructrice Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adrienbrault/instructrice

1.0 0.0 0.0 375 KB

๐Ÿ‘ฉโ€๐Ÿซ Typed LLM Outputs in PHP. Supports GPT, Claude, Gemini or any OpenAI compatible provider!

License: MIT License

Shell 0.35% PHP 99.65%

instructrice's Introduction

๐Ÿ‘ฉโ€๐Ÿซ adrienbrault/instructrice

GitHub Actions Packagist License

Typing LLM completions

Best in class LLMs are able to output JSON following a schema you provide, usually JSON-Schema. This significantly expands the ways you can leverage LLMs in your application!

Think of the input as:

  • A context, anything that is or can be converted to text, like emails/pdfs/html/xlsx
  • A schema, "Here is the form you need to fill to complete your task"
  • An optional prompt, giving a specific task, rules, etc

And the output/outcome is whichever structure best matches your use case and domain.

The python instructor cookbook has interesting examples.

Introduction

Instructrice is a PHP library that simplifies working with structured output from LLMs in a type-safe manner.

Features:

  • Flexible schema options:
  • symfony/serializer integration to deserialize LLMs outputs
  • Streaming first:
    • As a developer you can be more productive with faster feedback loops than waiting for outputs to complete. This also makes slower local models more usable.
    • You can provide a much better and snappier UX to your users.
    • The headaches of parsing incomplete JSON are handled for you.
  • A set of pre-configured LLMs with the best available settings. Set your API keys and switch between different providers and models without having to think about the model name, json mode, function calling, etc.

A Symfony Bundle is also available.

Installation and Usage

composer require kargnas/instructrice
use AdrienBrault\Instructrice\InstructriceFactory;
use AdrienBrault\Instructrice\LLM\Provider\Ollama;
use AdrienBrault\Instructrice\LLM\Provider\OpenAi;
use AdrienBrault\Instructrice\LLM\Provider\Anthropic;

$instructrice = InstructriceFactory::create(
    defaultLlm: Ollama::HERMES2THETA_LLAMA3_8B,
    apiKeys: [ // Unless you inject keys here, api keys will be fetched from environment variables
        OpenAi::class => $openAiApiKey,
        Anthropic::class => $anthropicApiKey,
    ],
);

List of object

use AdrienBrault\Instructrice\Attribute\Prompt;

class Character
{
    // The prompt annotation lets you add instructions specific to a property
    #[Prompt('Just the first name.')]
    public string $name;
    public ?string $rank = null;
}

$characters = $instructrice->getList(
    Character::class,
    'Colonel Jack O\'Neil walks into a bar and meets Major Samanta Carter. They call Teal\'c to join them.',
);

/*
dump($characters);
array:3 [
  0 => Character^ {
    +name: "Jack"
    +rank: "Colonel"
  }
  1 => Character^ {
    +name: "Samanta"
    +rank: "Major"
  }
  2 => Character^ {
    +name: "Teal'c"
    +rank: null
  }
]
*/

Object

$character = $instructrice->get(
    type: Character::class,
    context: 'Colonel Jack O\'Neil.',
);

/*
dump($character);
Character^ {
  +name: "Jack"
  +rank: "Colonel"
}
*/

Dynamic Schema

$label = $instructrice->get(
    type: [
        'type' => 'string',
        'enum' => ['positive', 'neutral', 'negative'],
    ],
    context: 'Amazing great cool nice',
    prompt: 'Sentiment analysis',
);

/*
dump($label);
"positive"
*/

You can also use third party json schema libraries like goldspecdigital/oooas to generate the schema:

CleanShot.2024-04-18.at.14.11.39.mp4

Supported providers

Provider Environment Variables Enum API Key Creation URL
Ollama OLLAMA_HOST Ollama
OpenAI OPENAI_API_KEY OpenAi API Key Management
Anthropic ANTHROPIC_API_KEY Anthropic API Key Management
Mistral MISTRAL_API_KEY Mistral API Key Management
Fireworks AI FIREWORKS_API_KEY Fireworks API Key Management
Groq GROQ_API_KEY Groq API Key Management
Together AI TOGETHER_API_KEY Together API Key Management
Deepinfra DEEPINFRA_API_KEY Deepinfra API Key Management
Perplexity PERPLEXITY_API_KEY Perplexity API Key Management
Anyscale ANYSCALE_API_KEY Anyscale API Key Management
OctoAI OCTOAI_API_KEY OctoAI API Key Management

The supported providers are Enums, which you can pass to the llm argument of InstructriceFactory::create:

use AdrienBrault\Instructrice\InstructriceFactory;
use AdrienBrault\Instructrice\LLM\Provider\OpenAi;

$instructrice->get(
    ...,
    llm: OpenAi::GPT_4T, // API Key will be fetched from the OPENAI_API_KEY environment variable
);

Supported models

Strategy ๐Ÿ“„ Text ๐Ÿงฉ JSON ๐Ÿš€ Function
Commercial usage ๐Ÿ’ผ โœ… Yes โš ๏ธ Yes, but โŒ Nope

Open Weights

Foundation

๐Ÿ’ผ ctx Ollama Mistral Fireworks Groq Together DeepInfra Perplexity Anyscale OctoAI
Mistral 7B โœ… 32k ๐Ÿงฉ ๐Ÿงฉ 68/s ๐Ÿ“„ 98/s ๐Ÿ“„ 88/s !ctx=16k! ๐Ÿงฉ ๐Ÿงฉ
Mixtral 8x7B โœ… 32k ๐Ÿงฉ ๐Ÿงฉ 44/s ๐Ÿงฉ 237/s ๐Ÿš€ 560/s ๐Ÿš€ 99/s ๐Ÿ“„ 119/s !ctx=16k! ๐Ÿงฉ ๐Ÿงฉ
Mixtral 8x22B โœ… 65k ๐Ÿงฉ ๐Ÿงฉ 77/s ๐Ÿงฉ 77/s ๐Ÿ“„ 52/s ๐Ÿงฉ 40/s ๐Ÿ“„ 62/s !ctx=16k! ๐Ÿงฉ ๐Ÿงฉ
Phi-3-Mini-4K โœ… 4k ๐Ÿงฉ
Phi-3-Mini-128K โœ… 128k ๐Ÿงฉ
Phi-3-Medium-4K โœ… 4k ๐Ÿงฉ
Phi-3-Medium-128K โœ… 128k ๐Ÿงฉ
Qwen2 0.5B โœ… 32k ๐Ÿงฉ
Qwen2 1.5B โœ… 32k ๐Ÿงฉ
Qwen2 7B โœ… 128k ๐Ÿงฉ
Llama3 8B โš ๏ธ 8k ๐Ÿ“„ ๐Ÿงฉ 280/s ๐Ÿš€ 800/s ๐Ÿ“„ 194/s ๐Ÿงฉ 133/s ๐Ÿ“„ 121/s ๐Ÿงฉ ๐Ÿงฉ
Llama3 70B โš ๏ธ 8k ๐Ÿงฉ ๐Ÿงฉ 116/s ๐Ÿš€ 270/s ๐Ÿ“„ 105/s ๐Ÿงฉ 26/s ๐Ÿ“„ 42/s ๐Ÿงฉ ๐Ÿงฉ
Gemma 7B โš ๏ธ 8k ๐Ÿš€ 800/s ๐Ÿ“„ 118/s ๐Ÿงฉ 64/s ๐Ÿงฉ
DBRX โš ๏ธ 32k ๐Ÿงฉ 50/s ๐Ÿ“„ 72/s ๐Ÿงฉ
Qwen2 72B โš ๏ธ 128k ๐Ÿงฉ
Qwen1.5 32B โš ๏ธ 32k ๐Ÿ“„ ๐Ÿงฉ
Command R โŒ 128k ๐Ÿ“„
Command R+ โŒ 128k ๐Ÿ“„

Throughputs from https://artificialanalysis.ai/leaderboards/providers .

Fine Tune

๐Ÿ’ผ ctx Base Ollama Fireworks Together DeepInfra OctoAI
Hermes 2 Pro Mistral 7B โœ… Mistral 7B ๐Ÿงฉ ๐Ÿงฉ ๐Ÿงฉ
FireFunction V1 โœ… Mixtral 8x7B ๐Ÿš€
WizardLM 2 7B โœ… Mistral 7B ๐Ÿงฉ
WizardLM 2 8x22B โœ… Mixtral 8x7B ๐Ÿ“„ ๐Ÿงฉ ๐Ÿงฉ
Capybara 34B โœ… 200k Yi 34B ๐Ÿงฉ
Hermes 2 Pro Llama3 8B โš ๏ธ Llama3 8B ๐Ÿ“„
Hermes 2 Theta Llama3 8B โš ๏ธ Llama3 8B ๐Ÿ“„
Dolphin 2.9 โš ๏ธ 8k Llama3 8B ๐Ÿงฉ ๐Ÿ“„ ๐Ÿงฉ

Proprietary

Provider Model ctx
Mistral Large 32k โœ… 26/s
OpenAI GPT-4o 128k ๐Ÿš€ 83/s
OpenAI GPT-4o mini 128k ๐Ÿš€ 140/s
OpenAI GPT-4 Turbo 128k ๐Ÿš€ 28/s
OpenAI GPT-3.5 Turbo 16k ๐Ÿš€ 72/s
Anthropic Claude 3 Haiku 200k ๐Ÿ“„ 88/s
Anthropic Claude 3 Sonnet 200k ๐Ÿ“„ 59/s
Anthropic Claude 3 Opus 200k ๐Ÿ“„ 26/s
Google Gemini 1.5 Flash 1000k ๐Ÿงฉ 136/s
Google Gemini 1.5 Pro 1000k ๐Ÿงฉ 57/s
Perplexity Sonar Small Chat 16k ๐Ÿ“„
Perplexity Sonar Small Online 12k ๐Ÿ“„
Perplexity Sonar Medium Chat 16k ๐Ÿ“„
Perplexity Sonar Medium Online 12k ๐Ÿ“„

Throughputs from https://artificialanalysis.ai/leaderboards/providers .

Automate updating these tables by scraping https://artificialanalysis.ai , along with chatboard arena elo.? Would be a good use case / showcase of this library/cli?

Custom Models

Ollama

If you want to use an Ollama model that is not available in the enum, you can use the Ollama::create static method:

use AdrienBrault\Instructrice\LLM\LLMConfig;
use AdrienBrault\Instructrice\LLM\Cost;
use AdrienBrault\Instructrice\LLM\OpenAiJsonStrategy;
use AdrienBrault\Instructrice\LLM\Provider\Ollama;

$instructrice->get(
    ...,
    llm: Ollama::create(
        'codestral:22b-v0.1-q5_K_M', // check its license first!
        32000,
    ),
);

OpenAI

You can also use any OpenAI compatible api by passing an LLMConfig:

use AdrienBrault\Instructrice\LLM\LLMConfig;
use AdrienBrault\Instructrice\LLM\Cost;
use AdrienBrault\Instructrice\LLM\OpenAiJsonStrategy;

$instructrice->get(
    ...,
    llm: new LLMConfig(
        uri: 'https://api.together.xyz/v1/chat/completions',
        model: 'meta-llama/Llama-3-70b-chat-hf',
        contextWindow: 8000,
        label: 'Llama 3 70B',
        provider: 'Together',
        cost: Cost::create(0.9),
        strategy: OpenAiJsonStrategy::JSON,
        headers: [
            'Authorization' => 'Bearer ' . $apiKey,
        ]
    ),
);

DSN

You may configure the LLM using a DSN:

  • the scheme is the provider: openai, openai-http, anthropic, google
  • the password is the api key
  • the host, port and path are the api endpoints without the scheme
  • the query string:
    • model is the model name
    • context is the context window
    • strategy is the strategy to use:
      • json for json mode with the schema in the prompt only
      • json_with_schema for json mode with probably the completion perfectly constrained to the schema
      • tool_any
      • tool_auto
      • tool_function

Examples:

use AdrienBrault\Instructrice\InstructriceFactory;

$instructrice = InstructriceFactory::create(
    defaultLlm: 'openai://:[email protected]/v1/chat/completions?model=gpt-3.5-turbo&strategy=tool_auto&context=16000'
);

$instructrice->get(
    ...,
    llm: 'openai-http://localhost:11434?model=adrienbrault/nous-hermes2theta-llama3-8b&strategy=json&context=8000'
);

$instructrice->get(
    ...,
    llm: 'openai://:[email protected]/inference/v1/chat/completions?model=accounts/fireworks/models/llama-v3-70b-instruct&context=8000&strategy=json_with_schema'
);

$instructrice->get(
    ...,
    llm: 'google://:[email protected]/v1beta/models?model=gemini-1.5-flash&context=1000000'
);

$instructrice->get(
    ...,
    llm: 'anthropic://:[email protected]?model=claude-3-haiku-20240307&context=200000'
);

LLMInterface

You may also implement LLMInterface.

Acknowledgements

Obviously inspired by instructor-php and instructor.

How is it different from instructor php?

Both libraries essentially do the same thing:

  • Automatic schema generation from classes
  • Multiple LLM/Providers abstraction/support
  • Many strategies to extract data: function calling, json mode, etc
  • Automatic deserialization/hydration
  • Maybe validation/retries later for this lib.

However, instructice differs with:

  • Streaming first.
  • Preconfigured provider+llms, to not have to worry about:
    • Json mode, function calling, etc
    • The best prompt format to use
    • Your options for local models
    • Whether streaming works. For example, groq can only do streaming without json-mode/function calling.
  • PSR-3 logging
  • Guzzle+symfony/http-client support
  • No messages. You just pass context, prompt.
    • I am hoping that this choice enables cool things later like supporting few-shots examples, evals, etc
  • More flexible schema options
  • Higher level abstraction. You aren't able to provide a list of messages, while it is possible with instructor-php.

Notes/Ideas

Things to look into:

DSPy is very interesting. There are great ideas to be inspired by.

Ideally this library is good to prototype with, but can support more advanced extraction workflows with few shot examples, some sort of eval system, generating samples/output like DSPy, etc

Would be cool to have a CLI, that accepts a FQCN and a context.

instructrice get "App\Entity\Customer" "$(cat some_email_body.md)" 

Autosave all input/schema/output in sqlite db. Like llm? Leverage that to test examples, add few shots, evals?

instructrice's People

Contributors

adrienbrault avatar kargnas avatar mykiwi avatar

Stargazers

Marios Vasiliou avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.