Git Product home page Git Product logo

Comments (10)

ivanvanderbyl avatar ivanvanderbyl commented on September 27, 2024 1

Did anyone get this to work?

For example

const result = await agent.stream({
	input: currentMessageContent,
	outputKey: "output",
	chat_history: memory.chatHistory.getMessages()
});

returns an IterableReadableStream<ChainValues> which isn't an accepted input for LangChainAdapter, even after the #2340

Trying what ivanvanderbyl did, didn't work for me either. Though it seems to be going in the right direction. Is this what we are expected to do? As the documentation hasn't been updated to state that.

For the most part I have been able to make this work with:

const agentStream = await agentExecutor.streamEvents(
  { messages, input: lastMessage.content },
  { version: "v2" },
);

const stream = new ReadableStream({
  async pull(controller) {
    for await (const { event, data } of agentStream) {
      if (event === "on_chat_model_stream") {
        const msg = data?.chunk as AIMessageChunk;

        if ((msg.tool_call_chunks?.length ?? 0) > 0) {
          // console.log("Tool calls", msg.tool_calls);
        } else if (msg.content) {
          controller.enqueue(msg.content as string);
        }
      }
    }
    controller.close();
  },
});

const aiStream = LangChainAdapter.toAIStream(stream);
return new StreamingTextResponse(aiStream);

You need to use streamEvents():

const agentStream = await agentExecutor.streamEvents(
	{ messages, input: lastMessage?.content ?? "" },
	{ version: "v2" },
);

const aiStream = LangChainAdapter.toAIStream(agentStream);
return new StreamingTextResponse(aiStream);

from ai.

matthias avatar matthias commented on September 27, 2024

👍 🙏

from ai.

hiddengearz avatar hiddengearz commented on September 27, 2024

+1 would be very helpful

from ai.

christophebe avatar christophebe commented on September 27, 2024

👍 🙏

from ai.

ivanvanderbyl avatar ivanvanderbyl commented on September 27, 2024

For the most part I have been able to make this work with:

const agentStream = await agentExecutor.streamEvents(
  { messages, input: lastMessage.content },
  { version: "v2" },
);

const stream = new ReadableStream({
  async pull(controller) {
    for await (const { event, data } of agentStream) {
      if (event === "on_chat_model_stream") {
        const msg = data?.chunk as AIMessageChunk;

        if ((msg.tool_call_chunks?.length ?? 0) > 0) {
          // console.log("Tool calls", msg.tool_calls);
        } else if (msg.content) {
          controller.enqueue(msg.content as string);
        }
      }
    }
    controller.close();
  },
});

const aiStream = LangChainAdapter.toAIStream(stream);
return new StreamingTextResponse(aiStream);

from ai.

lgrammel avatar lgrammel commented on September 27, 2024

#2340

from ai.

lgrammel avatar lgrammel commented on September 27, 2024

fixed in main

from ai.

hiddengearz avatar hiddengearz commented on September 27, 2024

Did anyone get this to work?

For example

const result = await agent.stream({
	input: currentMessageContent,
	outputKey: "output",
	chat_history: memory.chatHistory.getMessages()
});

returns an IterableReadableStream<ChainValues> which isn't an accepted input for LangChainAdapter, even after the #2340

Trying what ivanvanderbyl did, didn't work for me either. Though it seems to be going in the right direction. Is this what we are expected to do? As the documentation hasn't been updated to state that.

For the most part I have been able to make this work with:

const agentStream = await agentExecutor.streamEvents(
  { messages, input: lastMessage.content },
  { version: "v2" },
);

const stream = new ReadableStream({
  async pull(controller) {
    for await (const { event, data } of agentStream) {
      if (event === "on_chat_model_stream") {
        const msg = data?.chunk as AIMessageChunk;

        if ((msg.tool_call_chunks?.length ?? 0) > 0) {
          // console.log("Tool calls", msg.tool_calls);
        } else if (msg.content) {
          controller.enqueue(msg.content as string);
        }
      }
    }
    controller.close();
  },
});

const aiStream = LangChainAdapter.toAIStream(stream);
return new StreamingTextResponse(aiStream);

from ai.

lgrammel avatar lgrammel commented on September 27, 2024

@hiddengearz can you point me to the ChainValues source code / docs for the latest langchain? I can take a look add adding the relevant support.

from ai.

hiddengearz avatar hiddengearz commented on September 27, 2024

Yikes, I updated the @ai-sdk/svelte package but forgot to update ai. I won't get a chance to test it until the weekend but that fixed the build errors.

Thanks for the quick responses and great job!

from ai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.