JSPyBridge icon indicating copy to clipboard operation
JSPyBridge copied to clipboard

The FFID attribute in a Python object that's passed to OpenAI's client lib and API causes an error

Open antont opened this issue 5 months ago • 0 comments

Hi, and thanks a lot for the cool and well working bridge!

I'm testing using it to run https://github.com/opensouls/SocialAGI in our Python written backend. I ported the beginning of one of their examples and it actually works, using OpenAI's API for GPT, which is great.

However, I encountered a problem when trying to pass Python defined message objects for the JS lib. They are very simple objects with just two strings as params, role and content. The problem is that the bridge adds the ffid param to the object as well, and that apparently then gets included in the json which the js lib sends to the OpenAI API, which then rejects the request:

[JSE] error: { [JSE] message: "Additional properties are not allowed ('ffid' was unexpected) - 'messages.0'",

Would it be possible to somehow hide the ffid param from the foreign objects, to avoid such problems? I don't know if there is some technique in JS that would allow it. In Python, objects have ids, so it would be possible to have a map using those, but JS objs don't have ids so that's hard?

This is the code that fails with the above error:

import javascript
from pydantic import BaseModel

socialagi = javascript.require('socialagi')

ChatMessageRoleEnum = socialagi.ChatMessageRoleEnum
CortexStep = socialagi.CortexStep
externalDialog = socialagi.externalDialog

class ChatMessage(BaseModel):
    role: str #ChatMessageRoleEnum
    content: str

initial_memory = [
    ChatMessage(
        role=ChatMessageRoleEnum.System,
        content="Hi"
    )
]

dialog = CortexStep("BotName");
dialog = dialog.withMemory(initial_memory);

says = dialog.next(externalDialog());
print(says)

Luckily, this problem is possible to work-around by creating the message object in js, so i made a little helper library which has:

function createMessage(role, content) {
    return {
        role: role,
        content: content
    };
}

export { createMessage };

And then this works:

js_socialagi = javascript.require('./js_socialagi/main.mjs')
createMessage = js_socialagi.createMessage

initial_memory = [
    createMessage(
        ChatMessageRoleEnum.System,
        "Hi"
    )
]

To fix the problem in the earlier version.

I can deal with this by making such JS helper funcs, but figured to note about the issue anyway, and it would be nice to get it fixed. I was thinking that could use https://pypi.org/project/ts2python/ to generate py(dantic) types from the typescript lib later on, to get nice support for creating such messages and other objs that the lib has interfaces for.

Oh and ofc another way to fix this would be to hack the SocialAGI lib to strip out the ffid param, or make the json serialization with those types somehow smarter there, I did not look in that direction (yet).

antont avatar Feb 01 '24 08:02 antont