Neil C. Obremski
Neil C. Obremski

Reputation: 20244

How to specify conversation history in Microsoft AutoGen?

Backstory: I'm integrating AutoGen as one method to further a conversation in my application (versus simply calling OpenAI's chat completion directly for example).

How do I inject my own messages into a user_proxy chat history?

Here's what I have thus far:

assistant = AssistantAgent(
    "assistant",
    human_input_mode="NEVER",
    llm_config={"config_list": config_list},
    system_message=str(system_message))

user_proxy = UserProxyAgent(
    "user_proxy",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=0,
)

# TODO: populate chat messages from existing conversation here

user_proxy.initiate_chat(
    assistant,
    clear_history=True,
    message=prompt,
)

last_message = user_proxy.last_message()
logger.info("AUTOGEN: last_message: %s", last_message)
return last_message['content']

NOTE: I have to set max_consecutive_auto_reply=0 otherwise AutoGen loops forever sending blank prompts to the assistant.

Upvotes: 0

Views: 1590

Answers (1)

Brian Chan
Brian Chan

Reputation: 36

I think a dirty but works solution is to alter the _oai_messages of both assistant and user_proxy.

If you look at the source code, you can see it just pass the self._oai_system_message + self._oai_messages to the Openai API.

def generate_oai_reply(
        self,
        messages: Optional[List[Dict]] = None,
        sender: Optional[Agent] = None,
        config: Optional[OpenAIWrapper] = None,
    ) -> Tuple[bool, Union[str, Dict, None]]:
        """Generate a reply using autogen.oai."""
        client = self.client if config is None else config
        if client is None:
            return False, None
        if messages is None:
            messages = self._oai_messages[sender]

        # TODO: #1143 handle token limit exceeded error
        response = client.create(
            context=messages[-1].pop("context", None), messages=self._oai_system_message + messages
        )

        # TODO: line 301, line 271 is converting messages to dict. Can be removed after ChatCompletionMessage_to_dict is merged.
        extracted_response = client.extract_text_or_completion_object(response)[0]
        if not isinstance(extracted_response, str):
            extracted_response = extracted_response.model_dump(mode="dict")
        return True, extracted_response

Upvotes: 1

Related Questions