poe-platform / fastapi_poe

A helper library for writing Poe API bots using FastAPI
Apache License 2.0
112 stars 21 forks source link

Get Syncronize Response #88

Closed Daniel-Xiong closed 2 months ago

Daniel-Xiong commented 2 months ago

Is there a way I can get Syncronize response? Feel hard to integrate with llama-index

class PoeLLM(CustomLLM):
    context_window: int = 4096
    num_output: int = 1024
    model_name: str = "custom"
    dummy_response: str = "My response"
    chunk_size_limit: int = 1000

    def poeSimple(self, systemMessage: str = "", userMessage: str = ""):
        print(['length', len(userMessage)])
        print(userMessage)
        mes = [
            fp.ProtocolMessage(role='system', content=systemMessage),
            fp.ProtocolMessage(role='user', content=userMessage),
        ]
        a = fp.get_bot_response(mes, bot_name='Assistant', api_key='')

        message = []

        # 直接在poeSimple方法中异步迭代生成器并收集响应
        async def collect_response():
            async for value in a:
                message.append(value.text)
            print(message)

        # 运行异步事件循环
        executor = ThreadPoolExecutor()
        loop = asyncio.new_event_loop()
        result = loop.run_in_executor(executor=executor, func=collect_response)
        loop.run_until_complete(result)
        return message

    @property
    def metadata(self) -> LLMMetadata:
        """Get LLM metadata."""
        return LLMMetadata(
            context_window=self.context_window,
            num_output=self.num_output,
            model_name=self.model_name,
        )

    @llm_completion_callback()
    def complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:
        rs = self.poeSimple(userMessage=prompt)
        self.dummy_response = rs
        return CompletionResponse(text=rs)

    @llm_completion_callback()
    def stream_complete(
        self, prompt: str, **kwargs: Any
    ) -> CompletionResponseGen:
        response = ""
        for token in self.dummy_response:
            response += token
            yield CompletionResponse(text=response, delta=token)

keep having await problems of not being await

Daniel-Xiong commented 2 months ago

here is the solution! snapshot: use asyncio.run()

https://github.com/run-llama/llama_index/issues/12734