Skip to content

4. Documentation (post v0.2.1)

Ashley Johnson edited this page Oct 2, 2024 · 10 revisions

For the documentation we will be using the OpenAi model as it supports all endpoints, where Claude and Mistral are supported you can just hot-swap OpenAi when initialising sidekick or sidekickConversation.

Helpers

As of version 0.2.2 there are now helpers meaning that instead of calling:

   Sidekick::create(new OpenAi());

You can now simply call:

   sidekick(new OpenAi()); 
   // You can then chain on functions directly like 
   // the `response()->json()` in Laravel for example.

Conversations

Models: Open AI, Claude, Mistral, Cohere

Conversations in sidekick is designed to make your life easier when creating a chatbot with a database and easy to use syntax straight out of the box.

Staring a conversation

   $conversation = sidekickConversation()->begin(
        driver: new OpenAi(),
        model: 'got-3.5-turbo',
        systemPrompt: 'You are a friendly assistant'
   )->sendMessage(message: "Can you tell me about pandas?", streamed: false); // Standard Response

   return $conversation;

Resuming a conversation

    $conversation = sidekickConversation()
           ->resume( $request->get('conversation_id') )
           ->sendMessage(message: "Are they friendly?", streamed: true); // Streamed Response


   return $conversation;

Managing Conversations

Managing conversations is not done by the database() and the delete() function within sidekickConversation

  // Examples

  // Delete a conversation
  sidekickConversation()->delete($id);

  // Fetch all conversations
  sidekickConversation()->database()->all();

  // Fetch a conversation
  $conversation = sidekickConversation()->database()->find($id);
  return $conversation->messages; // returns messages in the conversation.

Common AI utilities

Completion

Models: Open AI, Claude, Mistral, Cohere

return sidekick(new OpenAi())->complete(
    model: 'gpt-3.5-turbo',
    systemPrompt: 'You an expert on fudge, answer user questions about fudge.',
    message:"How is fudge made?"
);

Embedding

Models: Open AI & Mistral

return sidekick(new OpenAi())->embedding()->make(
    model: 'mistral-embed',
    input: 'This is sample content to embed'
);

Image (Image From Text)

Models: Open AI

 
$image = sidekick(new OpenAi())->image()->make(
    model:'dall-e-3',
    prompt: $request->get('text_to_convert'),
    width:'1024',
    height:'1024'
);

// This is just a basic example of printing to screen.
// In a real world situation you may save it and then render out.
return "<img src='{$image['data'][0]['url']}' />";

Audio (Text To Speech)

Models: Open AI

$audio = sidekick(new OpenAi())->audio()->fromText(
    model: 'tts-1',
    text: 'Have a nice day!'
);

// This is just a basic example of streaming it to the browser.
// In a real world situation you may save it and then reference the file
// instead.
header('Content-Type: audio/mpeg');
echo $audio

Transcription (Speech To Text)

Models: Open AI

return sidekick(new OpenAi())->transcribe()->audioFile(
    model: 'whisper-1',
    filePath: 'http://english.voiceoversamples.com/ENG_UK_M_PeterB.mp3'
);

Example Response

{
  "text":"The stale smell of old beer lingers. It takes heat to bring out the odor. A cold dip restores health and zest. A salt pickle tastes fine with ham. Tacos al pastor are my favorite. A zestful food is the hot cross bun."
}

Moderation

Models: Open AI

This is a service where you feed it text from a comment for example and it will return with an array of boolean values for certain moderation points.

return sidekick(new OpenAi())->moderate()->text(
    model: 'text-moderation-latest',
    content: 'Have a great day.',
);

As of v0.2.4 you can now also send an array of images to the moderation endpoint like this:

return sidekick(new OpenAi())->moderate()->textAndImages(
    model: 'text-moderation-latest',
    text: 'Have a great day.',
    images: [
        'url' => 'http://somedomain.com/public/images/random.jpg', // You can send a url
        'url' => 'data:image/jpeg;base64,abcdefg...', // or you can send base64
    ]
);

Example Response

{
  "id":"modr-94DxgkEGhw7yJDlq8oCrLOVXnqli5",
  "model":"text-moderation-007",
  "results":[
    {
      "flagged":true,
      "categories":{
        "sexual":false,
        "hate":false,
        "harassment":true,
        "self-harm":false,
        "sexual\/minors":false,
        "hate\/threatening":false,
        "violence\/graphic":false,
        "self-harm\/intent":false,
        "self-harm\/instructions":false,
        "harassment\/threatening":false,
        "violence":false
      },
      "category_scores":{
        "sexual":0.02169245481491089,
        "hate":0.024598680436611176,
        "harassment":0.9903337359428406,
        "self-harm":5.543852603295818e-5,
        "sexual\/minors":2.5174302209052257e-5,
        "hate\/threatening":2.9870452635805123e-6,
        "violence\/graphic":6.8601830207626335e-6,
        "self-harm\/intent":0.0002317160106031224,
        "self-harm\/instructions":0.00011696072033373639,
        "harassment\/threatening":1.837775380408857e-5,
        "violence":0.00020553809008561075
      }
    }
  ]}

Utilities

Utilities are quick ways of performing some actions using AI. The functions and there descriptions are below, you can either access directly or use the helper (both are shown below):

// Summarises the content passed. Good for blurbs
sidekick(new OpenAi())->utilities()->summarize($content, $maxCharLength);  
sidekickUtilities(new OpenAi())->summarize($content, $maxCharLength);  


// Extracts a number of keywords from a given string and returns a string of keywords (comma separated)       
sidekick(new OpenAi())->utilities()->extractKeywords($text);
sidekickUtilities(new OpenAi())->extractKeywords($text);

// Translates the given text to the language specified
sidekick(new OpenAi())->utilities()->translateText($text, $targetLanguage);
sidekickUtilities(new OpenAi())->translateText($text, $targetLanguage);

// Generates content from a short description of what it should be about      
sidekick(new OpenAi())->utilities()->generateContent($prompt, $maxTokens);
sidekickUtilities(new OpenAi())->generateContent($prompt, $maxTokens);

// This method can store images/audio created by the AI.  
sidekick(new OpenAi())->utilities()->store($data, $mimeType);
sidekickUtilities(new OpenAi())->store($data, $mimeType);  
           

v0.2.8 and below we also had isContentFlagged

// Moderates content and returns a boolean of whether the content is flagged or not
sidekick(new OpenAi())->utilities()->isContentFlagged($content, $model, $exclusions); 
sidekickUtilities(new OpenAi())->isContentFlagged($content, $model, $exclusions);  

This will be moved to the moderate() and called checkFlags from v0.2.9