Can't find a way to integrate Autogen Groupchat and Datastream togather

61 Views Asked by At

I am working on an Autogen project group chat project (i.e. an investment firm). The firm has 5 members and I wanted to add the data-stream functionality (i.e. the analyst is able to gather data from the data-stream that is the 'https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers=AAPL&sort=LATEST&limit=5&apikey=demo').

Now to implement this data-stream thing there is a separate function call i.e. register_reply(analyst, add_data_reply, position=2, config={"news_stream": data})

And for group chat it is 'manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=gpt4_config)'

Other code:

(function to get data from the stream) def get_market_news(ind, ind_upper): import json

    import requests

    # replace the "demo" apikey below with your own key from https://www.alphavantage.co/support/#api-key
    url = 'https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers=AAPL&sort=LATEST&limit=5&apikey=KFGE5HXRO7G42KVK'
    r = requests.get(url)
    data = r.json()
    # with open('market_news_local.json', 'r') as file:
    #     # Load JSON data from file
    #     data = json.load(file)

    feeds = data["feed"][ind:ind_upper]
    feeds_summary = "\n".join(
        [
            f"News summary: {f['title']}. {f['summary']} overall_sentiment_score: {f['overall_sentiment_score']}"
            for f in feeds
        ]
    )
    return feeds_summary


data = asyncio.Future()


async def add_stock_price_data():
    # simulating the data stream
    for i in range(0, 5, 1):
        latest_news = get_market_news(i, i + 1)
        if data.done():
            data.result().append(latest_news)
        else:
            data.set_result([latest_news])
        # print(data.result())
        await asyncio.sleep(5)


data_task = asyncio.create_task(add_stock_price_data())


async def add_data_reply(recipient, messages, sender, config):
    await asyncio.sleep(0.1)
    data = config["news_stream"]
    if data.done():
        result = data.result()
        if result:
            news_str = "\n".join(result)
            result.clear()
            return (
                True,
                f"Just got some latest market news. Merge your new suggestion with previous ones.\n{news_str}",
            )
        return False, None

Analyst agent:

analyst = autogen.AssistantAgent(
    name="Investment_Analyst",
    llm_config=gpt4_config,
    system_message="""Investment_Analyst.You should create a structured report based upon your knowledge.Then pass this report to CEO for review. Go through the feedback provided by the CEO(if any).
                      If CEO asks for some improvements, do that and return it back to CEO.""",

)

Execution code:

# analyst.register_reply(autogen.AssistantAgent, add_data_reply, position=2, config={"news_stream": data})
groupchat = autogen.GroupChat(
    agents=[acc_manager,ceo, analyst, p_manager, op_manager,ceo], messages=[], max_round=50
)
firm_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=gpt4_config)

Now, HOW CAN I INTEGRATE THIS DATA STREAM THING WITH ANALYST SO THAT IT WORKS PERFECTLY IN THE GROUP CHAT?

0

There are 0 best solutions below