使用langchain和websockets完成流式聊天

fbcarpbf  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(530)

我不知道我做错了什么,我正在使用长链补全,并希望将其发布到我的WebSocket房间。使用BaseCallbackHandler,我能够将令牌打印到控制台,然而,使用AsyncCallbackHandler是一个挑战,基本上,似乎什么都没有发生,我尝试打印东西,但在init上的打印消息之后,似乎什么都没有发生。

async def send_message_to_room(room_group_name, message):
    print("sending message to room", room_group_name, message)
    channel_layer = get_channel_layer()
    await channel_layer.group_send(
        room_group_name,
        {
            "type": "chat_message",
            "message": message,
        }
    )

class MyCustomHandler(AsyncCallbackHandler):

    def __init__(self, room_group_name):
        self.channel_layer = get_channel_layer()
        self.room_group_name = room_group_name

        print("MyCustomHandler init")

    async def on_llm_new_token(self, token: str, **kwargs):
        print(token)
        await send_message_to_room(self.room_group_name, token)

def generate_cited_answer_stream(roomname, question=question, texts=texts, responsetype="Simple and Pedagogical"
                                       , system_message_with_response_type=system_message_with_response_type
                                       , human_message_with_response_type=human_message_with_response_type):

    handler = MyCustomHandler(room_group_name=roomname)

    chat = ChatOpenAI(temperature=0, streaming=True, callbacks=[handler])

    system_message_with_response_type = SystemMessagePromptTemplate.from_template(system_message_with_response_type)
    human_message_prompt = HumanMessagePromptTemplate.from_template(human_message_with_response_type)

    chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

    prompt_value = chat_prompt.format_prompt(question=question, texts=texts, responsetype=responsetype)

    chat(prompt_value.to_messages())
5n0oy7gb

5n0oy7gb1#

从一段代码中截取

from langchain.callbacks.streaming_aiter import AsyncIteratorCallbackHandler

class TestLangchainAsync(unittest.IsolatedAsyncioTestCase):
    async def test_aiter(self):
        handler = AsyncIteratorCallbackHandler()
        llm = OpenAI(
            temperature=0,
            streaming=True,
            callbacks=[handler],
            openai_api_key="sk-xxxxx",
            openai_proxy="http://127.0.0.1:7890",
        )
        prompt = PromptTemplate(
            input_variables=["product"],
            template="What is a good name for a company that makes {product}?",
        )
        prompt = prompt.format(product="colorful socks")
        asyncio.create_task(llm.agenerate([prompt]))
        async for i in handler.aiter():
            print(i)

参考文献
https://github.com/hwchase17/langchain/issues/2428#:~:text=AsyncIteratorCallbackHandler%20works.%20Here%20is%20the%20example%20code%3A

相关问题