Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize and deserialize chains #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andresgutgon
Copy link
Contributor

@andresgutgon andresgutgon commented Jan 2, 2025

What?

The short version is that we want a way to serialize the chain to be stored in a cache system. Things that have to be serialized are the ast of the prompt and the scope. Now Scopes are JS classes that we need to transform into serializable JS objects

More details in this PR

TODO

  • Add ESlint 9 + Fix Prettier + Add CI 💅
  • Serialize ast
  • Serialize scope
  • Serialize adapter
  • Serialize compilerOptions
  • Add tests for serialize
  • Implement deserialize to read from a string OR JS object or JSON-like data and build a chain
  • Add tests for deserialize

Adapter

Adapter is a class so it needs to be serialized. Maybe just a reference to the identifier default, openai, anthrophic,...

NOTE

Added .prettierrc so some unrelated changes

@andresgutgon andresgutgon added the 🚧 wip Work in Progress label Jan 2, 2025
@andresgutgon andresgutgon changed the title Serialize chains Serialize and deserialize chains Jan 3, 2025
@andresgutgon andresgutgon removed the 🚧 wip Work in Progress label Jan 3, 2025
@andresgutgon andresgutgon force-pushed the feature/serialize-chain branch from c9473ff to 160ec67 Compare January 3, 2025 08:39
@andresgutgon andresgutgon requested a review from neoxelox January 3, 2025 08:41
package.json Outdated Show resolved Hide resolved
@andresgutgon andresgutgon force-pushed the feature/serialize-chain branch 2 times, most recently from 44b42aa to c2cdd49 Compare January 3, 2025 09:01
const compilerOptions = serialized.compilerOptions || {}

if (
typeof serialized !== 'object' ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a rigorous check because Arrays will also be true for this. I saw this check Variable.constructor === Object is more rigorous. https://www.geeksforgeeks.org/how-to-check-if-a-value-is-object-like-in-javascript/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But an array is not a valid serialized value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then why are you checking that these are objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, honestly this was AI generated 😂

</step>
`)

const chain = new Chain({ prompt, adapter: Adapters.openai })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do more tests that include parameters and compile options

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a test that tests that consecutive serialize(deserialize(serialize())) operations do not degrade the state of the chain would be nice too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is it possible to degrade the state?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk we could break something in the future, the test is to ensure we catch that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that for example, serialize(deserialize(serialize())) == serialize()

We want to allow to store prompt state half compiled. This is helpful
when using tool calls where user needs to receive a response from the AI that contains a tool call, do some work and respond back with the result of that work. In this scenario we need to serialize current compilation state to restart the compilation from that point.
src/compiler/scope.ts Show resolved Hide resolved
src/compiler/chain.ts Show resolved Hide resolved
const compilerOptions = serialized.compilerOptions || {}

if (
typeof serialized !== 'object' ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then why are you checking that these are objects?

</step>
`)

const chain = new Chain({ prompt, adapter: Adapters.openai })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk we could break something in the future, the test is to ensure we catch that

</step>
`)

const chain = new Chain({ prompt, adapter: Adapters.openai })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that for example, serialize(deserialize(serialize())) == serialize()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants