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

How to add RAG into Data Interpreter #1635

Open
cjl0222 opened this issue Dec 9, 2024 · 1 comment
Open

How to add RAG into Data Interpreter #1635

cjl0222 opened this issue Dec 9, 2024 · 1 comment
Assignees

Comments

@cjl0222
Copy link

cjl0222 commented Dec 9, 2024

Hi! Thanks for your great work. I'm wondering whether you could add an example or documentation for integrating RAG into Data Interpreter. It seems that there are only examples for RAG Retrieval and Rerank, but I have no idea how to utilize this ability into the current Data Interpreter Machine learning modeling procedure. Thanks!

@iorisa
Copy link
Collaborator

iorisa commented Jan 9, 2025

When the Data Interpreter object's rc.memory content is too large, you can use the RAG approach to retrieve this memory, which reduces the cost of a single interaction and avoids exceeding the maximum token limit of the LLM.

For example, using RAG to override the self.get_memories() function used in metagpt/roles/di/data_interpreter.py:

    async def _think(self) -> bool:
        """Useful in 'react' mode. Use LLM to decide whether and what to do next."""
        user_requirement = self.get_memories()[0].content
        context = self.working_memory.get()

        if not context:
            # just started the run, we need action certainly
            self.working_memory.add(self.get_memories()[0])  # add user requirement to working memory
            self._set_state(0)
            return True

        prompt = REACT_THINK_PROMPT.format(user_requirement=user_requirement, context=context)
        rsp = await self.llm.aask(prompt)
        rsp_dict = json.loads(CodeParser.parse_code(block=None, text=rsp))
        self.working_memory.add(Message(content=rsp_dict["thoughts"], role="assistant"))
        need_action = rsp_dict["state"]
        self._set_state(0) if need_action else self._set_state(-1)

        return need_action

The original definition of the get_memories function in metagpt/roles/role.py is as follows:

    def get_memories(self, k=0) -> list[Message]:
        """A wrapper to return the most recent k memories of this role, return all when k=0"""
        return self.rc.memory.get(k=k)

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

No branches or pull requests

3 participants