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

Chat glm with fast api #21

Merged
merged 9 commits into from
Apr 22, 2023
Merged

Conversation

hamletzhang
Copy link
Contributor

增加了Fastapi接口 post /stream 可以得到服务端结果

@ypwhs ypwhs mentioned this pull request Apr 11, 2023
@ypwhs
Copy link
Owner

ypwhs commented Apr 11, 2023

相关 Issue:#17

@yuri2peter
Copy link

多谢,已经用上。按照我自己的习惯稍作修改了些:

    @app.post("/stream")
    def continue_question_stream(arg_dict: dict):

        def decorate(generator):
            for item in generator:
                yield ServerSentEvent(
                    json.dumps(item, ensure_ascii=False), event='delta')

        # inputs = [query, answer_prefix, max_length, top_p, temperature, history]
        try:
            query = arg_dict["query"]
            answer_prefix = arg_dict.get("answer_prefix", "")
            max_length = arg_dict.get("max_length", 256)
            top_p = arg_dict.get("top_p", 0.7)
            temperature = arg_dict.get("temperature", 1.0)
            history = arg_dict.get("history", [])
            allow_generate[0] = True
            logger.info("Query - {}".format(query))
            if answer_prefix:
                logger.info(f"answer_prefix - {answer_prefix}")
            history = history[-MAX_HISTORY:]
            if len(history) > 0:
                logger.info("History - {}".format(history))
            history = [tuple(h) for h in history]
            inputs = [
                query, answer_prefix, max_length, top_p, temperature,
                allow_generate, history
            ]
            return EventSourceResponse(decorate(bot.predict_continue(*inputs)))
            # return EventSourceResponse(bot.predict_continue(*inputs))
        except Exception as e:
            logger.error(f"error: {e}")
            return EventSourceResponse(
                decorate(bot.predict_continue(None, None)))
        
    
    @app.post("/interrupt")
    def interrupt():
        allow_generate[0] = False
        logger.error("Interrupted.")
        return {"message": "OK"}

@ypwhs ypwhs merged commit 1bffec6 into ypwhs:master Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants