Giter Site home page Giter Site logo

promptclasses's Introduction

PromptClasses

ChatGpt reusable objects

Installation

composer require angelxmoreno/prompt-classes

Getting started

First thing you will need to get started is an OpenAI api key.

Getting an OpenAI Api Key

To get started, visit the official OpenAI platform website. If you're new, create an account using simple steps. Next, sign in using your OpenAI account email and password, or use your Google/Microsoft account. Once logged in, you'll find your name and profile icon on the top-right corner of the OpenAI platform homepage. To obtain an API Key, click on your name in the top-right corner for a dropdown menu, and select "View API keys." On the resulting page, find the "Create new secret key" option in the center. If you don't have an API key, click this button to generate one. Remember to save the API key immediately, as you won't be able to retrieve it once the window closes.

Simple Hello Prompt

  1. Create a PromptClass

    <?php
    
    declare(strict_types=1);
    
    namespace YourNamespace\Prompts;
    
    use PromptClasses\Core\AbstractPrompt;
    
    class HelloPrompt extends AbstractPrompt
    {
        public string $promptTpl = 'Say "Hello {{name}}. Good {{time_of_day}}"';
        public array $promptParams = ['name', 'time_of_day'];
    }
  2. Instantiate and send

    $helloPrompt = new HelloPrompt($openaiApiKey);
    $response = $helloPrompt->send('John', 'Morning');
    // returns `Hello John. Good Morning`

    The order of params is the order of the $promptParams array. This means that if you change the $promptParams for the HelloPrompt class to ['time_of_day','name'];, you would have to call ->send() like so:

    $response = $helloPrompt->send('Morning', 'John');
    // still returns `Hello John. Good Morning`

SEO Meta DescriptionPrompt

  1. Create a PromptClass

    <?php
    
    declare(strict_types=1);
    
    namespace YourNamespace\Prompts;
    
    use PromptClasses\Core\AbstractPrompt;
    
    class MetaDescriptionPrompt extends AbstractPrompt
    {
        public string $promptTpl = 'Write a meta description no longer than 160 characters including spaces for a product page with the title "{{title}}"';
        public array $promptParams = ['title'];
    }
  2. Instantiate and send

    $metaDescriptionPrompt = new MetaDescriptionPrompt($openaiApiKey);
    $response = $metaDescriptionPrompt->send('Supernatural Dean Winchester That was Scary Vintage Sunset T-Shirt');
    // returns `Get the Supernatural Dean Winchester 'That was Scary' Vintage Sunset T-Shirt. Show your love for the hit TV show with this iconic design.`

Configuration

Under the hood the library uses orhanerday/open-ai. The default configuration for the calls to OpenAI Chat Completions are:

Name Current Value Info
model gpt-3.5-turbo ID of the model to use. See Model overview for descriptions of them.
temperature 1 What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

To discover more options available take a look at the official documentation for the chat completions endpoint. Values can be updated for your PromptClass by extending the $openaiParams property of the AbstractPrompt class like so:

    <?php
    
    declare(strict_types=1);
    
    namespace YourNamespace\Prompts;
    
    use PromptClasses\Core\AbstractPrompt;
    
    class MetaDescriptionPrompt extends AbstractPrompt
    {
        public string $promptTpl = 'Write a meta description no longer than 160 characters including spaces for a product page with the title "{{title}}"';
        public array $promptParams = ['title'];
        
        protected array $openaiParams = [
            'model' => 'gpt-3.5-turbo',
            'temperature' => 0.2,
            'frequency_penalty' => 1.8
        ];
    }

Requirements

PHP 7.4+

Support

For bugs and feature requests, please use the issues section of this repository.

License

Licensed under the MIT License. Redistributions of the source code included in this repository must retain the copyright notice found in each file.

promptclasses's People

Contributors

angelxmoreno avatar

Watchers

 avatar  avatar

promptclasses's Issues

Create `regenerate` option AKA Message History

Currently, message history is not stored in any way. Every call to AbstractPrompt::send() essentially starts a new chat. On the chatGpt web ui you can continue a conversation and refer to previous messages. Furthermore, you can say things like "Give me another example" or "Generate more".

It would be useful if the library offered the option to retain message history and thus, allow the option to "regenerate" a response.

I'm thinking something that implements the Adapter pattern similar to CakePHP's Cache Engines.

Remove double quotes from respones

When getting a response using the library, i am seeing things like

"\"Optimize Your CakePHP Testing with Our Expert Tips - Get Started Today!\""

When I am expected to see

"Optimize Your CakePHP Testing with Our Expert Tips - Get Started Today!"

Adding (Better) Exception handling

Sometimes the OpenAI response has errors. These get lost and null is returned without knowing the cause.

Exceptions should be thrown when

  • The OpenAI response returns an error
  • The choices array returns no content

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.