Skip to content

Instantly share code, notes, and snippets.

@john-adeojo
Last active October 25, 2023 17:33
Show Gist options
  • Select an option

  • Save john-adeojo/ee038ebb505d188f6cd0d51df609f72d to your computer and use it in GitHub Desktop.

Select an option

Save john-adeojo/ee038ebb505d188f6cd0d51df609f72d to your computer and use it in GitHub Desktop.
class CreateCandidateAnswers(dspy.Signature):
"""Generate a candidate answer"""
question = dspy.InputField(desc="Initial question")
candidate = dspy.OutputField(desc="Generate a candidate answer to the initial question")
class SelectCandidateResponse(dspy.Signature):
"""Select the best candidate response"""
question = dspy.InputField(desc="Initial question")
context = dspy.InputField(desc="The candidate answers")
answer = dspy.OutputField(desc="Use the candidate answers to generate a response to the initial question")
class MultiChainComp(dspy.Module):
def __init__(self, num_requests=3):
super().__init__()
# Declare modules
self.num_requests = num_requests
self.generate_candidate_answer = dspy.Predict(signature=CreateCandidateAnswers, n=num_requests)
self.answer = dspy.Predict(signature=SelectCandidateResponse)
def forward(self, question):
# Declare program to execute
context = []
candidate_answer = self.generate_candidate_answer(question=question)
for i in range(self.num_requests):
context.append(f"\ncandidate_answer_{i+1}: {candidate_answer.completions[i].candidate}")
context_str = ''.join(context)
return self.answer(question=question, context=context_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment