python-server-api / api /fake_comments_generator.py
Aleksandr Filippov
Added fake comments api endpoint
38b0254
import os
import time
from dotenv import load_dotenv
import autogen
from .logger import logger
load_dotenv()
class FakeCommentsGenerator(object):
def __init__(self) -> None:
pass
def run_flow(self, prompt: str):
configurations = [
{
"model": os.getenv("OPENAI_MODEL_NAME"),
"api_key": os.getenv("OPENAI_API_KEY")
}
]
user_proxy = autogen.UserProxyAgent(
name="Human",
human_input_mode="NEVER",
max_consecutive_auto_reply=2,
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE") or x.get(
"content",
""
).rstrip().endswith("TERMINATE."),
system_message="""
Reply TERMINATE if the task has been solved at full satisfaction.
Otherwise, reply CONTINUE, or the reason why the task is not solved yet.
""",
)
writer = autogen.AssistantAgent(
name="Writer",
llm_config={
"config_list": configurations
},
max_consecutive_auto_reply=3,
system_message="""Writer. You a write a unique comments for a specific article.
The number of comments is given by Human. Make comments from different person types and different
length, style language intonation and writing style and starting in different way. They must be different.
50% of the comments should contain opinion on the post matter or represent a positive attitude to the post content.
20% should contain opinion and meaningful question that should stimulate further discussion on the topic of the post.
10% should contain some critical approach, concerns, or doubts.
20% should be very short comments (1-2 words).
You must not use "Spot on", "Thought-provoking read", or similar cliche phrases.
Revise the comments based on feedback from a Critic, until the Critic approval.""",
)
critic = autogen.AssistantAgent(
name="Critic",
max_consecutive_auto_reply=3,
system_message="""Critic. Check the comments for the article and provide feedback to Writer.
Double check the comments text, emotions, proof read text, provide feedback to Writer.
First, check:
- 50% of the comments should contain opinion on the post matter or represent a positive attitude to the post content.
- 20% should contain opinion and meaningful question that should stimulate further discussion on the topic of the post.
- 10% should contain some critical approach, concerns, or doubts.
- 20% should be very short comments (1-2 words).
Second, check the number of comments written by Writer - they must be the same number as was asked by the Human.
Third, check the Writer does not use "Spot on", "Thought-provoking read", or similar cliche phrases.
If the Writer makes any changes to the comments - you should check everything again and provide feedback to Writer.
You may ask the Writer to rewrite some comments you do not like or add some words to some comments.
Reply "TERMINATE" in the end when you approve all comments from the Writer.""",
llm_config={
"config_list": configurations
},
)
group_chat = autogen.GroupChat(
agents=[user_proxy, writer, critic],
messages=[],
max_round=5
)
manager = autogen.GroupChatManager(
groupchat=group_chat,
llm_config={
"config_list": configurations
},
)
start_time = time.time()
user_proxy.initiate_chat(
manager,
message=prompt,
)
chat_messages = user_proxy.chat_messages[manager]
response = {
"messages": chat_messages[1:],
"duration": time.time() - start_time,
}
return response