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

👑 [是否有计划支持websocket] #77

Open
Antony-Jia opened this issue Feb 2, 2024 · 10 comments
Open

👑 [是否有计划支持websocket] #77

Antony-Jia opened this issue Feb 2, 2024 · 10 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@Antony-Jia
Copy link

Antony-Jia commented Feb 2, 2024

🥰 需求描述

能否支持websocket的streaming流模式,目前一些对话需要在后台处理以后才能发送到前端,非常感谢!

🧐 解决方案

🚑 其他信息

@arvinxx
Copy link
Collaborator

arvinxx commented Feb 2, 2024

能不能给个 demo 关于使用 ws 的请求的?我们好了解下具体咋用的

@Antony-Jia
Copy link
Author

Antony-Jia commented Feb 2, 2024

@arvinxx 您看您是否方便看看https://github.com/pors/langchain-chat-websockets 这个示例,

核心就是我们在chain中完成了检索、对话、存储,然后在对话返回过程中通过websocket向前端返回,待都返回完成后后端进行存储。

@Tss-16
Copy link

Tss-16 commented Feb 22, 2024

使用websocket请求更新messages有完整使用示例代码吗?

@ONLY-yours
Copy link
Collaborator

使用websocket请求更新messages有完整使用示例代码吗?

有一个差不多的例子,可以先参考这个

https://pro-chat.antdigital.dev/guide/servers-push

@ONLY-yours ONLY-yours added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 22, 2024
@Tss-16
Copy link

Tss-16 commented Feb 23, 2024

使用websocket请求更新messages有完整使用示例代码吗?

有一个差不多的例子,可以先参考这个

https://pro-chat.antdigital.dev/guide/servers-push

使用websocket怎么在服务端推送消息时再响应更新assistant的消息而不是user一发送就response消息啊?

@ONLY-yours
Copy link
Collaborator

使用websocket请求更新messages有完整使用示例代码吗?

有一个差不多的例子,可以先参考这个
https://pro-chat.antdigital.dev/guide/servers-push

使用websocket怎么在服务端推送消息时再响应更新assistant的消息而不是user一发送就response消息啊?

现在可以这样子做,首先你要在 request 里面阻止当前的内容生成,有一个 hooks stopGenerateMessage 停止生成消息,这样子相当于打破了默认的逻辑。然后返回一个空的 new response。

然后你使用 Chats 进行受控,你想什么时候响应更新就都可以了。

这块我稍后补一个文档。当然最好的方式还是让服务端把 websocket 改成 sse 哈哈哈

@Antony-Jia
Copy link
Author

readable = new ReadableStream({ async start(controller) { }

我就是这样构造一个websocket得ReadableStream就可以了

@Tss-16
Copy link

Tss-16 commented Feb 26, 2024

使用websocket请求更新messages有完整使用示例代码吗?

有一个差不多的例子,可以先参考这个
https://pro-chat.antdigital.dev/guide/servers-push

使用websocket怎么在服务端推送消息时再响应更新assistant的消息而不是user一发送就response消息啊?

现在可以这样子做,首先你要在 request 里面阻止当前的内容生成,有一个 hooks stopGenerateMessage 停止生成消息,这样子相当于打破了默认的逻辑。然后返回一个空的 new response。

然后你使用 Chats 进行受控,你想什么时候响应更新就都可以了。

这块我稍后补一个文档。当然最好的方式还是让服务端把 websocket 改成 sse 哈哈哈

这个stopGenerateMessage 没有文档吗?具体怎么使用啊-_-

@KrishnaPG
Copy link

KrishnaPG commented May 30, 2024

readable = new ReadableStream({ async start(controller) { }

That's how I construct a websocket and get a ReadableStream

@Antony-Jia Could you please give some more details about the implementation of start() method with websockets? I am not able to get it work (the UI continuously spins without showing the reply). My main concern is:

  1. how to handle errors?
  2. how to close the stream properly

This is what I am doing:

export default async function getResponse(ws, reqMessages) {
  const readableStream = new ReadableStream({
    async start(controller) {
      ws.send(JSON.stringify(reqMessages)); //send the request messages on websocket

      const encoder = new TextEncoder();
      ws.on("message", ev => controller.enqueue(encoder.encode(ev.data)));
      ws.on("close", ev => controller.close());
      ws.on("error", ev => controller.error(ev));
    },
  });
  return new Response(readableStream);
}

The above is used as below:

      <ProChat
        request={messages => getResponse(ws, messages)}
      />

The websocket works fine. I am getting the response from the websocket and it is being enqueued to the controller. But when it is enqueued the UI does not update with the partial data. It just keeps spinning.

What am I doing wrong? (The WS code above is simplified here for example purposes, but its lifetime is taken care of to avoid duplicate connections etc.)

@Antony-Jia
Copy link
Author

Antony-Jia commented Jun 5, 2024

Sorry, I'm done replying. I've encountered what you said online, but not locally. The way I use it is roughly the same as yours.
This is part of my code, the react-websocket I use.

`
request= async (messages: Array) => {

  sendMessage(last.content)

  let readable = new ReadableStream({
    async start(controller) {
      setController(controller)
    }
  });

  return new Response(readable);
}}

`

I use useEffect to controll the controller close or enqueue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants