2121dspy .settings .configure (rm = colbertv2_wiki17_abstracts )
2222
2323
24- #Define a simple signature for basic question answering
24+ # Define a simple signature for basic question answering
2525class BasicQA (dspy .Signature ):
2626 """Answer questions with short factoid answers."""
27+
2728 question = dspy .InputField ()
2829 answer = dspy .OutputField (desc = "often between 1 and 5 words" )
2930
31+
3032# Pass signature to Predict module
3133generate_answer = dspy .Predict (BasicQA )
3234
3335# Call the predictor on a particular input.
34- question = ' What is the color of the sky?'
36+ question = " What is the color of the sky?"
3537pred = generate_answer (question = question )
3638
3739print (f"Question: { question } " )
3840print (f"Predicted Answer: { pred .answer } " )
3941question = "What's something great about the ColBERT retrieval model ?!?abc"
4042
4143# 1) Declare with a signature, and pass some config.
42- classify = dspy .ChainOfThought (' question -> answer' , n = 1 )
44+ classify = dspy .ChainOfThought (" question -> answer" , n = 1 )
4345
4446# 2) Call with input argument.
4547response = classify (question = question )
@@ -51,14 +53,16 @@ class BasicQA(dspy.Signature):
5153# Define a simple signature for basic question answering
5254class BasicQA (dspy .Signature ):
5355 """Answer questions with short factoid answers."""
56+
5457 question = dspy .InputField ()
5558 answer = dspy .OutputField (desc = "often between 1 and 5 words" )
5659
57- #Pass signature to ChainOfThought module
60+
61+ # Pass signature to ChainOfThought module
5862generate_answer = dspy .ChainOfThought (BasicQA )
5963
6064# Call the predictor on a particular input.
61- question = ' What is the color of the sky?12'
65+ question = " What is the color of the sky?12"
6266pred = generate_answer (question = question )
6367
6468print (f"Question: { question } " )
@@ -67,9 +71,11 @@ class BasicQA(dspy.Signature):
6771
6872class BasicQA (dspy .Signature ):
6973 """Answer questions with short factoid answers."""
74+
7075 question = dspy .InputField ()
7176 answer = dspy .OutputField (desc = "often between 1 and 5 words" )
7277
78+
7379# Example completions generated by a model for reference
7480completions = [
7581 dspy .Prediction (rationale = " I recall that during clear days, the sky often appears this colo12r" , answer = "blue" ),
@@ -81,25 +87,27 @@ class BasicQA(dspy.Signature):
8187compare_answers = dspy .MultiChainComparison (BasicQA )
8288
8389# Call the MultiChainComparison on the completions
84- question = ' What is the color of th e sky14?'
90+ question = " What is the color of th e sky14?"
8591final_pred = compare_answers (completions , question = question )
8692
8793print (f"Question: { question } " )
8894print (f"Final Predicted Answer (after comparison): { final_pred .answer } " )
8995print (f"Final Rationale: { final_pred .rationale } " )
9096
9197
92- #Define a simple signature for basic question answering
98+ # Define a simple signature for basic question answering
9399class GenerateAnswer (dspy .Signature ):
94100 """Answer questions with short factoid answers."""
101+
95102 question = dspy .InputField ()
96103 answer = dspy .OutputField (desc = "often between 1 and 5 words" )
97104
105+
98106# Pass signature to ProgramOfThought Module
99107pot = dspy .ProgramOfThought (GenerateAnswer )
100108
101- #Call the ProgramOfThought module on a particular input
102- question = ' Sarah has 5 applez. She buys 123 more apples from the store. How many apples does Sarah have now?'
109+ # Call the ProgramOfThought module on a particular input
110+ question = " Sarah has 5 applez. She buys 123 more apples from the store. How many apples does Sarah have now?"
103111result = pot (question = question )
104112
105113print (f"Question: { question } " )
@@ -125,14 +133,14 @@ class BasicQA(dspy.Signature):
125133print (f"Final Predicted Answer (after ReAct process): { result .answer } " )
126134
127135
128- query = ' Where was the first FIFA World Cup held?12'
136+ query = " Where was the first FIFA World Cup held?12"
129137
130138
131139# Call the retriever on a particular query.
132140retrieve = dspy .Retrieve (k = 3 )
133141topK_passages = retrieve (query ).passages
134142
135- print (f"Top { retrieve .k } passages for question: { query } \n " , '-' * 30 , ' \n ' )
143+ print (f"Top { retrieve .k } passages for question: { query } \n " , "-" * 30 , " \n " )
136144
137145for idx , passage in enumerate (topK_passages ):
138- print (f' { idx + 1 } ]' , passage , ' \n ' )
146+ print (f" { idx + 1 } ]" , passage , " \n " )
0 commit comments