Experiment for on how multiple specialised AI bots in a network work with human supervision

You’ve checked the API keys, right?

If thoose are your API keys inside your files, anyone downloading your code could do API requests with them and you will have to pay them.

Sorry again. Reviewed the code and it seems fine. Just looked suspicious.

Build you some basic code for a chat with PHP and some javascript frontend… Sorry, too lazy to include the syntax highlight correctly.

It looks like you are using irc bots to add the agents.

That’s not really neccessary. You can send multiple requests in parallel here (wouldn’t really suggest to use PHP for that - maybe try SuperAGI, create some agents there and call them via curl in PHP).

<?php

include('vendor/autoload.php');
use OpenAI\Client;
use OpenAI\Responses\Chat\CreateResponse;

class OpenAIChatBot
{
    /**
     * @var \OpenAI\Client
     */
    private Client $client;

    public function __construct()
    {
        $this->client = OpenAI::client(file_get_contents('key.txt'));
    }

    public function Chat($messages): null|CreateResponse
    {
        if (empty($messages)) {
            return null;
        }

        return $this->client->chat()->create([
            //'model' => 'gpt-4-32k-0613',
            'model' => 'gpt-3.5-turbo-0613',
            'messages' => $messages,
            'max_tokens' => 1000,
        ]);
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

        $prompt = "You are a helpful bot";
        $systemPrompt = ['role' => 'system', 'content' => $prompt];
        $messages[] = $systemPrompt;
        $chatBot = new OpenAIChatBot();
        $UserMessages = json_decode($_POST['messages'], true);
        $messages = array_merge($messages, $UserMessages);
        $response = $chatBot->Chat($messages);

// @todo add some logic that checks if the chatmessage is too long (tiktoken)
// @todo add the moderation endpoint or someone chatting with that could get your api key blocked


        echo json_encode(['content' => nl2br($response->choices[0]->message->content)]);
        exit;
} else {

    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>jQuery UI Ajax Call Widget Example</title>
        <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.0/themes/smoothness/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
        <!-- add bootstrap -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/11.0.1/markdown-it.min.js "></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.2.1/highlight.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.2.1/styles/default.min.css">

        <style>

            #chat {
                width: 100%;
                height: 100%;
                display: flex;
                flex-direction: column;
                justify-content: space-between;

            }
            .chat-container {
                width: 100%;
                height: 100%;
                display: flex;
                flex-direction: column;
                justify-content: space-between;
            }

            .chat-response-container {
                width: 100%;
                overflow-y: scroll;
                height: 750px;
            }

            .chat-response {
                padding: 10px;
                margin: 10px;
                border-radius: 10px;
                background-color: #cccccc;
            }

            .chat-response-user {
                background-color: #e0e0e0;
                padding: 10px;
                margin: 10px;
                border-radius: 10px;
            }

            .chat-form-container {
                height:61px;
                padding-top: 10px;
                bottom:50px;
                width:100%;
            }

            .chat-form {
                display: flex;
                flex-direction: row;
                justify-content: space-between;
            }

            .chat-prompt {
                flex-grow: 1;
                font-size:25px
            }

            .chat-submit {
                flex-grow: 0;
            }

            .feedback {
                color: #fff;
            }

        </style>

        <script>

            function markdownToHtml(md) {
                // Convert list to html list
                md = md.replace(/^-\s\[(.*?)\]\((.*?)\)$/gm, '<li><a href="$2">$1</a></li>');

                // Wrap the list items with unordered list tags
                md = '<ul>' + md + '</ul>';

                return md;
            }


            $.widget("ui.Chat", {
                options: {
                    messages: [],
                    url: ''
                },

                typingStatus: 1,
                typingInterval: null,

                addMessage: function(role, content) {
                    this.options.messages.push({
                        role: role,
                        content: content
                    });
                },

                _create: function() {
                    // Initial Chat Structure
                    const $container = this.element.addClass("container-fluid mt-12");
                    const $col = $("<div>").addClass("col-md-12").appendTo($container);
                    const $chat = $("<div>").addClass("chat bg-black").css({"padding": "2px 0 0 0", "background": "linear-gradient(90deg, rgba(80,230,255,1) 0%, rgba(0,177,242,1) 52%, rgba(0,120,212,1) 100%)"}).appendTo($col);
                    const $chatContainer = $("<div>").addClass("chat-container").css({"height":"100%", "margin-top":"3px", "padding":"0 10px", "width":"100%", "background-color":"#04496e"}).appendTo($chat);
                    this.$chatWindowBody = $("<div>").addClass("chat-window-body mt-3").css({"max-height":"400px", "overflow-x":"hidden", "overflow-y":"scroll"}).appendTo($chatContainer);
                    const $chatWindowFooter = $("<div>").addClass("chat-window-footer mt-3").appendTo($chatContainer);
                    $("<p>").addClass("feedback chat-feedback text-bg-secondary mt-1 p-1 rounded-1").text("Your partner is typing…").appendTo($chatWindowFooter);
                    const $form = $("<form>").appendTo($chatWindowFooter);
                    const $fieldset = $("<fieldset>").addClass("text-bg-dark").appendTo($form);
                    const $label = $("<label>").appendTo($fieldset);
                    this.$input = $("<input>").addClass("form-control text-bg-light text-dark mb-4").attr("type", "text").attr("placeholder", "Type your message…").appendTo($label);
                    $("<button>").addClass("btn btn-primary").attr("type", "submit").text("Send").appendTo($fieldset);

                    $form.on("submit", function(event) {
                        event.preventDefault();
                        this.appendRequest();
                        this.requestResponse(false, false);
                    }.bind(this));

                    this.hidePartnerTyping();
                },

                _init: function() {
                    this.element.empty();
                    this._create();
                },

                _setOption: function(key, value) {
                    this._super(key, value);
                },

                _setOptions: function(options) {
                    this._super(options);
                },

                _destroy: function() {
                    this.element.empty();
                },

                showPartnerTyping: function() {
                    window.setTimeout(function() {
                        let $feedback = $('.feedback');
                        $feedback.show();

                        this.typingInterval = setInterval(function() {
                            this.typingStatus = this.typingStatus % 3 + 1; // This will alternate between 1, 2, and 3
                            $feedback.text("Your partner is typing " + Array(this.typingStatus + 1).join('.'));
                        }.bind(this), 1000);
                    }.bind(this), 1500);
                },

                hidePartnerTyping: function() {
                    $('.feedback').hide();
                    clearInterval(this.typingInterval);
                    this.typingStatus = 1;
                },

                appendRequest: function() {
                    const $message = $("<div>").addClass("chat-message clearfix");
                    const $card = $("<div>").addClass("card text-bg-dark m-1").appendTo($message);
                    const $cardBody = $("<div>").addClass("card-body").appendTo($card);
                    const $row = $("<div>").addClass("row").appendTo($cardBody);
                    const $col = $("<div>").addClass("col-md-11").appendTo($row);
                    const $messageContent = $("<div>").addClass("chat-message-content clearfix").appendTo($col);
                    $("<p>").text(this.$input.val()).appendTo($messageContent);

                    this.$chatWindowBody.append($message);
                    this.$chatWindowBody.scrollTop(this.$chatWindowBody[0].scrollHeight);
                    this.addMessage('user', this.$input.val());
                    this.$input.val('');
                },

                appendResponse: function(response) {
                    const $message = $("<div>").addClass("chat-message clearfix");
                    const $card = $("<div>").addClass("card text-bg-dark m-1").appendTo($message);
                    const $cardBody = $("<div>").addClass("card-body").appendTo($card);
                    const $row = $("<div>").addClass("row").appendTo($cardBody);
                    const $col = $("<div>").addClass("col-md-11").appendTo($row);
                    const $messageContent = $("<div>").addClass("chat-message-content clearfix").appendTo($col);
                    $("<p>").html(response).appendTo($messageContent);
                    this.$chatWindowBody.append($message);
                    this.addMessage('assistant', response);
                    this.$chatWindowBody.scrollTop(this.$chatWindowBody[0].scrollHeight);
                },

                requestResponse: function() {
                    const $this = this;

                    $this.showPartnerTyping();

                    $.ajax({
                        url: this.options.url,
                        type: "POST",
                        dataType: "json",
                        data: {
                            messages: JSON.stringify(this.options.messages)
                        },
                        success: function(data) {
                            console.log(data);
                            $this.hidePartnerTyping();
                            $this.appendResponse(data.content);
                        },
                        error: function(xhr, status, error) {
                            console.log(xhr.responseText);
                        }
                    });
                }
            });

        </script>
    </head>
    <body>
    <div id="chat"></div>
    <script>
        $(document).ready(function() {
            $("#chat" ).Chat({
                url: "chat.php"
            });
        });
    </script>
    </body>
    </html>
    <?php
}

Put your key into key.txt and install the dependencies like I explained previously with composer.

Using the double trouble relay I found the human interaction greatly improvement

User > GPT 3.5 > User . GPT 3.5 > GPT 4 > replies

https://talkeetna.info/iirc.php

The real interesting thing was that 4.0 will just mimic 3.5 almost to the letter if a human does not react to the first response

btw, your jq may not work because it looks like the json is escaped like {"key"…

Clean up the json before you use jq - or even better in PHP use json_decode to create an array of the messages.

Well jq hates it and php json decode hates it I hate it so time for the buzz saw sed awk
And I can find and read my content

I will look at those tools you mentioned
Someone’s json will extract it just not bash jq or php json decode

I am too old to learn anymore json :sunglasses:

Try this:


        $json = str_replace("'", '"', $yourjson);
        $json = rtrim($json, ",\n }") . '}';
        $data = json_decode($json, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            echo 'JSON Error: ' . json_last_error_msg();
        }

btw. if 1955 is your birthyear that’s no excuse. The best dev I’ve ever worked together with was 66 when I had the honor to work with him. Experience eats young brains for breakfast when you really want to. I know that.

JSON Error: Syntax error

I used your code to take the very last $response you can see printed

Naw we just get cranky in our rocking chairs :sunglasses: sorry I went off

Can’t really see where it starts or ends. Could you paste the json here?

Ah damn,

["You awake ?{\n        \"content\": \"Yes, I am
an AI and I am always \\\"awake\\\" and ready to
assist you. How can I help you today?\"\n     
},\n      \"finish_reason\": \"stop\"\n    }\n 
],\n  \"usage\": {\n    \"prompt_tokens\": 10,\n  
 \"completion_tokens\": 27,\n    \"total_tokens\":
37\n 
}\n***********************************************
***************\nThis is Arctic
Fox\n*********************************************
*****************",

inside that you have the json:

{\n        \"content\": \"Yes, I am
an AI and I am always \\\"awake\\\" and ready to
assist you. How can I help you today?\"\n     
},\n      \"finish_reason\": \"stop\"\n    }\n 
],\n  \"usage\": {\n    \"prompt_tokens\": 10,\n  
 \"completion_tokens\": 27,\n    \"total_tokens\":
37\n 
}

And there are escaped backslashes and stuff like that.
So it was even double escaped.

Maybe

$data = json_encode($json, JSON_UNESCAPED_SLASHES);

Would start with a smaller portion of json and work up on that.

The code I posted was something I copy pasted from some of my older code - but it should show at least where the problem is.

You can use a json validator to find the problems.

1 Like

Well I appreciate your help thank you let me
Work on it and see if I can clean it up

I cut a lot of output so you can see the text easily

I am off to see if I can get Arctic fox to watch for a tag

I can’t get the trash out of the first response apparently my second call needs some of it to work

At some point the text will be manageable enough perhaps to json encode it I will keep after it

1 Like

Yes k i was already working with Phil he found escapees in a few files we have cleaned it up now thank goodness you guys keep a watch I will be more careful

2 Likes

I created a project like this a few months ago. The goal was to create a sort of organization that works through chat. The human is the CEO. The HR bots are supposed to be able to add more bots with specialty areas. Developer bots make code to interact with the real world, ops bots identify when code should be run and run the code.

I also intended for each of the bots to have separate chat streams with dedicated therapist and significant other (family, friend) bots to act as a sort of context rattler: dream state, real world. Hoping to see off the job conversations influence job conversations (inspiration!).

Contributions welcome.

2 Likes

These two twins are interesting
https://talkeetna.info/2.php