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

Agent fails to run if serialized, restored, serialized again, and restored again #92

Closed
shmuelk opened this issue Oct 15, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@shmuelk
Copy link

shmuelk commented Oct 15, 2024

Describe the bug
A have a simplistic Agent, basically the example examples/agents/first.ts, changed to work with BAM and wrapped with a Web Server, written using Express. At the end of every interaction the agent is serialized and set into the user's session. When requests come in, if the session has a serialized agent in it, it is restored, otherwise the agent is created from scratch.

This works for two iterations. On the third one it fails, with an exception:

undefined:3
return (...args)=>target.invoke(...args)
                         ^

ReferenceError: target is not defined

To Reproduce

  1. Add extra dependencies: yarn add express express-session @types/express @types/express-session
  2. yarn start <code below>
  3. In another terminal session:
    a. curl -X POST --data-binary '{"text": "Who is the president of the USA"}' -H "Content-Type: application/json" --cookie cookiefile --cookie-jar cookiefile http://localhost:3000
    b. curl -X POST --data-binary '{"text": "How old is he?"}' -H "Content-Type: application/json" --cookie cookiefile --cookie-jar cookiefile http://localhost:3000
    c. curl -X POST --data-binary '{"text": "Where was he born?"}' -H "Content-Type: application/json" --cookie cookiefile --cookie-jar cookiefile http://localhost:3000

Expected behavior
It should not fail.

Screenshots
Here is the terminal output of the server when it fails:

(node:52233) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
[server]: Server is running at http://localhost:3000
Creating agent
Agent created
Running agent
Agent ran
Serializing agent
Agent serialized
Restoring agent from session
Agent restored from session
Running agent
Agent ran
Serializing agent
Agent serialized
Restoring agent from session
Agent restored from session
Running agent
undefined:3
return (...args)=>target.invoke(...args)
                         ^

ReferenceError: target is not defined
    at Object.eval [as callback] (eval at <anonymous> (file:///Users/kallner/Projects/ai-agents/bee-agent-framework/src/serializer/serializer.ts:1:11657), <anonymous>:3:26)
    at run (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:203:40)
    at _Emitter.invoke (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:204:60)
    at Object.on.toBoundedFunction.value (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:106:29)
    at run (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:203:40)
    at _Emitter.invoke (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:204:60)
    at Object.on.toBoundedFunction.value (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:106:29)
    at run (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:203:40)
    at _Emitter.invoke (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:204:60)
    at _Emitter.emit (/Users/kallner/Projects/ai-agents/bee-agent-framework/src/emitter/emitter.ts:189:23)

Code snippets

Here is the code being run above:

import "dotenv/config.js";
import express, {Express, Request, Response } from 'express';
import session from 'express-session';

import { BeeAgent } from "bee-agent-framework/agents/bee/agent";
import { BAMChatLLM } from "bee-agent-framework/adapters/bam/chat";
import { TokenMemory } from "bee-agent-framework/memory/tokenMemory";
import { DuckDuckGoSearchTool } from "bee-agent-framework/tools/search/duckDuckGoSearch";
import { OpenMeteoTool } from "bee-agent-framework/tools/weather/openMeteo";

const app: Express = express();
const port = process.env.PORT || 3000;

app.use(express.json());
app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: false
}));

declare module 'express-session' {
    interface SessionData {
        agent: string;
    }
}

app.post("/", async (req: Request, res: Response) => {
    var agent: BeeAgent;

    if (req.session && req.session.agent) {
        console.log("Restoring agent from session");
        agent = BeeAgent.fromSerialized(req.session.agent);
        console.log("Agent restored from session");
    } else {
        console.log("Creating agent");
        const llm = BAMChatLLM.fromPreset("meta-llama/llama-3-8b-instruct");
        agent = new BeeAgent({
            llm, // for more explore 'bee-agent-framework/adapters'
            memory: new TokenMemory({ llm }), // for more explore 'bee-agent-framework/memory'
            tools: [new DuckDuckGoSearchTool(), new OpenMeteoTool()], // for more explore 'bee-agent-framework/tools'
        });
        console.log("Agent created");
    }
    console.log("Running agent");
    const response = await agent.run(
        { prompt: req.body.text },
        {
          execution: {
            maxRetriesPerStep: 3,
            totalMaxRetries: 10,
            maxIterations: 20,
          },
        }
    );
    console.log("Agent ran");

    console.log("Serializing agent");
    req.session.agent = agent.serialize();
    console.log("Agent serialized");

    res.send(response.result.text);
});

app.listen(port, () => {
    console.log(`[server]: Server is running at http://localhost:${port}`);
});

Set-up:

  • Bee version: [e.g. v0.0.29]
  • Model provider [e.g. BAM]
@shmuelk shmuelk added the bug Something isn't working label Oct 15, 2024
Tomas2D added a commit that referenced this issue Oct 15, 2024
Ref: #92
Signed-off-by: Tomas Dvorak <toomas2d@gmail.com>
@Tomas2D
Copy link
Member

Tomas2D commented Oct 15, 2024

Good catch. Thanks for the report.

Fixed by 58d072f.

@shmuelk
Copy link
Author

shmuelk commented Oct 15, 2024

The fix works. Thanks.

@Tomas2D
Copy link
Member

Tomas2D commented Oct 15, 2024

Great. Released in v0.0.30.

@Tomas2D Tomas2D closed this as completed Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants