CoolFace
Apppublic

aphilippov/python-server-api

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
fake_comments_generator.py102 linesDownload Raw Back to api
1import os2import time3 4from dotenv import load_dotenv5 6import autogen7from .logger import logger8 9load_dotenv()10 11 12class FakeCommentsGenerator(object):13    def __init__(self) -> None:14        pass15 16    def run_flow(self, prompt: str):17        configurations = [18            {19                "model": os.getenv("OPENAI_MODEL_NAME"),20                "api_key": os.getenv("OPENAI_API_KEY")21            }22        ]23 24        user_proxy = autogen.UserProxyAgent(25            name="Human",26            human_input_mode="NEVER",27            max_consecutive_auto_reply=2,28            is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE") or x.get(29                "content",30                ""31            ).rstrip().endswith("TERMINATE."),32            system_message="""33            Reply TERMINATE if the task has been solved at full satisfaction. 34            Otherwise, reply CONTINUE, or the reason why the task is not solved yet.35            """,36        )37 38        writer = autogen.AssistantAgent(39            name="Writer",40            llm_config={41                "config_list": configurations42            },43            max_consecutive_auto_reply=3,44            system_message="""Writer. You a write a unique comments for a specific article. 45        The number of comments is given by Human. Make comments from different person types and different 46        length, style language intonation and writing style and starting in different way. They must be different. 47        50% of the comments should contain opinion on the post matter or represent a positive attitude to the post content.48        20% should contain opinion and meaningful question that should stimulate further discussion on the topic of the post.49        10% should contain some critical approach, concerns, or doubts.50        20% should be very short comments (1-2 words).51        You must not use "Spot on", "Thought-provoking read", or similar cliche phrases.52        Revise the comments based on feedback from a Critic, until the Critic approval.""",53        )54 55        critic = autogen.AssistantAgent(56            name="Critic",57            max_consecutive_auto_reply=3,58            system_message="""Critic. Check the comments for the article and provide feedback to Writer.59        Double check the comments text, emotions, proof read text, provide feedback to Writer. 60        First, check:61            - 50% of the comments should contain opinion on the post matter or represent a positive attitude to the post content.62            - 20% should contain opinion and meaningful question that should stimulate further discussion on the topic of the post.63            - 10% should contain some critical approach, concerns, or doubts.64            - 20% should be very short comments (1-2 words).65        Second, check the number of comments written by Writer - they must be the same number as was asked by the Human.66        Third, check the Writer does not use "Spot on", "Thought-provoking read", or similar cliche phrases.67        If the Writer makes any changes to the comments - you should check everything again and provide feedback to Writer. 68        You may ask the Writer to rewrite some comments you do not like or add some words to some comments.69        Reply "TERMINATE" in the end when you approve all comments from the Writer.""",70            llm_config={71                "config_list": configurations72            },73        )74 75        group_chat = autogen.GroupChat(76            agents=[user_proxy, writer, critic],77            messages=[],78            max_round=579        )80 81        manager = autogen.GroupChatManager(82            groupchat=group_chat,83            llm_config={84                "config_list": configurations85            },86        )87 88        start_time = time.time()89        user_proxy.initiate_chat(90            manager,91            message=prompt,92        )93 94        chat_messages = user_proxy.chat_messages[manager]95 96        response = {97            "messages": chat_messages[1:],98            "duration": time.time() - start_time,99        }100 101        return response102