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

Clarification about DSPy Modules #3004

Open
Devy99 opened this issue Jan 21, 2025 · 0 comments
Open

Clarification about DSPy Modules #3004

Devy99 opened this issue Jan 21, 2025 · 0 comments

Comments

@Devy99
Copy link

Devy99 commented Jan 21, 2025

Good morning,
First of all, thank you for your effort in creating this amazing library.

I would like to have more information about the use of Modules in DSPy.
In particular, I would like to use DSPy to create agents that can communicate with each other in an automated manner.
Let's take as an example my first attempt to create two agents that communicate with each other to discuss a topic (P.S. I am still new to DSPy. All suggestions are welcome :) ):

from typing import List
import os, dspy

lm = dspy.LM('openai/gpt-4o-mini', api_key=os.environ['OPENAI_API_KEY'], cache=False)
dspy.configure(lm=lm)

class Comment(dspy.Signature):
    """You are partecipating in a conversation. Comment on the given topic and on previous messages. Speak directly to the participants"""
    name = dspy.InputField(format=str, desc="Your name in the conversation")
    participants = dspy.InputField(format=List[str], desc="The participants in the conversation, excluding yourself")
    previous_messages = dspy.InputField(format=List[str], desc="The previous messages in the conversation")
    opinion = dspy.OutputField(format=str, desc="Your opinion.")

class Agent(dspy.Module):
    def __init__(self, name):
        self.name = name
        self.last_message = None
        self.comment = dspy.Predict(Comment)

    def forward(self, previous_messages: List[str]) -> List[str]:
        participants = set()
        for message in previous_messages:
            name = message.split(":")[0]
            if name != self.name and name != "Topic":
                participants.add(name)
        
        result = self.comment(name=self.name, participants=participants, previous_messages=previous_messages)
        self.last_message = result.opinion
        opinion = f"{self.name}: {result.opinion}"
        return dspy.Prediction(opinion=opinion)


messages = [] # The messages in the conversation
topic = '''Best programming language for beginners'''
messages.append(f"Topic: {topic}")

agent_1 = Agent('Bob')
agent_2 = Agent('Alice')

print(f"Topic: {topic}")
for i in range(3):
    # Bob turn
    last_message = agent_1(messages).opinion
    messages.append(last_message)
    print(f"- {last_message}")

    # Alice turn
    last_message = agent_2(messages).opinion
    print(f"- {last_message}")
    messages.append(last_message)


print("---------------------- HISTORY ---------------------------")

print(lm.inspect_history(n=1000))

Questions:

  • Is the above example a proper use of the Module class in an agent-based context? Based on what I have understood from the documentation, a module would be a class that contains more details, going beyond a single call to the LLM. So, in an agent-based context, the module would be used to save information such as "memory," "maximum number of iterations," "possible function calls," "tools," etc. Did I understand correctly?

  • Is it always necessary to encapsulate interactions between multiple agents in the same module? For example, in the example above, should I place the two agents within the same module if I later want to take advantage of prompt fine-tuning? I don't know if DSPy automatically understands that the Python program (which involves multiple separate modules) needs to be optimized separately across multiple modules or if it requires everything to be in a single module.

  • Extra question: How does memory management work in DSPy? I saw that lm.history maintains a list of API calls to the selected LLM. Does this history have an impact on future calls (is it used as context) or is it just for logging purposes? And is it possible to return all the logs without having to specify a predefined number?

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

1 participant