Skip to content

Commit 6e664ad

Browse files
committed
fix spacing issues
Signed-off-by: Amit Sharma <[email protected]>
1 parent 9184e56 commit 6e664ad

8 files changed

+105
-63
lines changed

pywhyllm/suggesters/identification_suggester.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import guidance
55
from guidance import system, user, assistant, gen
66
import re
7-
7+
import inspect
88

99
class IdentificationSuggester(IdentifierProtocol):
1010
CONTEXT: str = """causal mechanisms"""
@@ -199,14 +199,15 @@ def request_mediators(
199199
try:
200200
lm = self.llm
201201
with system():
202-
lm += f"""You are an expert in {domain_expertise} and are studying {analysis_context}. You are using your
202+
prompt_str = f"""You are an expert in {domain_expertise} and are studying {analysis_context}. You are using your
203203
knowledge to help build a causal model that contains all the assumptions about the factors that are directly
204204
influencing athe {outcome}. Where a causal model is a conceptual model that describes the causal mechanisms
205205
of a system. You will do this by by answering questions about cause and effect and using your domain knowledge
206206
in {domain_expertise}. Follow the next two steps, and complete the first one before moving on to the second:"""
207+
lm += inspect.cleandoc(prompt_str)
207208

208209
with user():
209-
lm += f"""(1) From your perspective as an expert in {domain_expertise}, think step by step as you consider the factors
210+
prompt_str = f"""(1) From your perspective as an expert in {domain_expertise}, think step by step as you consider the factors
210211
that may interact between the {treatment} and the {outcome}. Use your knowledge as an expert in
211212
{domain_expertise} to describe the mediators, if there are any at all, between {treatment} and the
212213
{outcome}. Be concise and keep your thinking within two paragraphs. Then provide your step by step chain
@@ -223,6 +224,7 @@ def request_mediators(
223224
the tags <mediating_factor>factor_name</mediating_factor>. Where factor_name is one of the items within the
224225
factor_names list. If a factor does not have a high likelihood of mediating, then do not wrap the factor with
225226
any tags. Your step by step answer as an in {domain_expertise}:"""
227+
lm += inspect.cleandoc(prompt_str)
226228

227229
with assistant():
228230
lm += gen("output")
@@ -314,15 +316,16 @@ def request_ivs(
314316
try:
315317
lm = self.llm
316318
with system():
317-
lm += f"""You are an expert in {domain_expertise} and are studying {analysis_context}.
319+
prompt_str = f"""You are an expert in {domain_expertise} and are studying {analysis_context}.
318320
You are using your knowledge to help build a causal model that contains all the assumptions about the factors
319321
that are directly influencing the {outcome}. Where a causal model is a conceptual model that describes the
320322
causal mechanisms of a system. You will do this by by answering questions about cause and effect and using
321323
your domain knowledge in {domain_expertise}. Follow the next two steps, and complete the first one before
322324
moving on to the second:"""
325+
lm += inspect.cleandoc(prompt_str)
323326

324327
with user():
325-
lm += f"""(1) From your perspective as an expert in {domain_expertise}, think step by step
328+
prompt_str = f"""(1) From your perspective as an expert in {domain_expertise}, think step by step
326329
as you consider the factors that may interact with the {treatment} and do not interact with {outcome}.
327330
Use your knowlegde as an expert in {domain_expertise} to describe the instrumental variable(s),
328331
if there are any at all, that both has/have a high likelihood of influecing and causing the {treatment} and
@@ -341,6 +344,7 @@ def request_ivs(
341344
Where factor_name is one of the items within the factor_names list. If a factor does not have a high
342345
likelihood of being an instrumental variable, then do not wrap the factor with any tags. Your step by step
343346
answer as an in {domain_expertise}:"""
347+
lm += inspect.cleandoc(prompt_str)
344348
with assistant():
345349
lm += gen("output")
346350

pywhyllm/suggesters/model_suggester.py

+23-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from guidance import system, user, assistant, gen
66
from ..helpers import RelationshipStrategy
77
import re
8-
8+
import inspect
99

1010
class ModelSuggester(ModelerProtocol):
1111
CONTEXT: str = """causal mechanisms"""
@@ -32,13 +32,14 @@ def suggest_domain_expertises(
3232
with system():
3333
lm += f"""You are a helpful assistant for recommending useful domain expertises."""
3434
with user():
35-
lm += f"""What domain expertises have the knowledge and experience needed to identify causal
35+
prompt_str = f"""What domain expertises have the knowledge and experience needed to identify causal
3636
relationships and causal influences between the {analysis_context}? What domain expertises are needed
3737
to work with and reason about the causal influence between {factors_list}? What domain expertises
3838
have the knowledge and experience to reason and answer questions about influence and cause between
3939
such factors? Think about this in a step by step manner and recommend {n_experts} expertises and
4040
provide each one wrapped within the tags, <domain_expertise></domain_expertise>, along with the
4141
reasoning and explanation wrapped between the tags <explanation></explanation>."""
42+
lm += inspect.cleandoc(prompt_str)
4243
with assistant():
4344
lm += gen("output")
4445

@@ -74,13 +75,14 @@ def suggest_domain_experts(
7475
with system():
7576
lm += f"""You are a helpful assistant for recommending useful domain experts."""
7677
with user():
77-
lm += f"""What domain experts have the knowledge and experience needed to identify causal relationships
78+
prompt_str = f"""What domain experts have the knowledge and experience needed to identify causal relationships
7879
and causal influences between the {analysis_context}? What experts are needed to work with and
7980
reason about the causal influence between {factors_list}? What domain experts have the knowledge
8081
and experience to reason and answer questions about influence and cause between such factors? Think
8182
about this in a step by step manner and recommend {n_experts} domain experts and provide each one
8283
wrapped within the tags, <domain_expert></domain_expert>, along with the reasoning and explanation
8384
wrapped between the tags <explanation></explanation>."""
85+
lm += inspect.cleandoc(prompt_str)
8486
with assistant():
8587
lm += gen("output")
8688

@@ -118,12 +120,13 @@ def suggest_stakeholders(
118120
lm += "You are a helpful assistant for recommending useful primary and secondary stakeholders."
119121

120122
with user():
121-
lm += f"""What stakeholders have knowledge and experience in and about {analysis_context}?
122-
What stakeholders can work best with and reason well about the causal influence between
123+
prompt_str = f"""What stakeholders have knowledge and experience in and about {analysis_context}?
124+
What stakeholders can work best with and reason well about the causal influence between
123125
{factors_list}? What stakeholders have the knowledge and experience useful to reason within this context? Think about
124126
this in a step by step manner and recommend {n_stakeholders} stakeholders. Then provide each useful stakeholder
125127
wrapped within the tags, <stakeholder></stakeholder>, along with the reasoning and explanation wrapped between the tags
126128
<explanation></explanation>."""
129+
lm += inspect.cleandoc(prompt_str)
127130
with assistant():
128131
lm += gen("output")
129132

@@ -204,13 +207,14 @@ def request_confounders(
204207
try:
205208
lm = self.llm
206209
with system():
207-
lm += f"""You are an expert in {domain_expertise} and are studying {analysis_context}.
208-
You are using your knowledge to help build a causal model that contains all the assumptions about {
210+
prompt_str = f"""You are an expert in {domain_expertise} and are studying {analysis_context}.
211+
You are using your knowledge to help build a causal model that contains all the assumptions about {
209212
analysis_context}. Where a causal model is a conceptual model that describes the causal mechanisms of a
210-
system. You
211-
will do this by by answering questions about cause and effect and using your domain knowledge in {domain_expertise}."""
213+
system. You
214+
will do this by answering questions about cause and effect and using your domain knowledge in {domain_expertise}."""
215+
lm += inspect.cleandoc(prompt_str)
212216
with user():
213-
lm += f"""Follow the next two steps, and complete the first one before moving on to the second: (1)
217+
prompt_str = f"""Follow the next two steps, and complete the first one before moving on to the second: (1)
214218
From your perspective as an
215219
expert in {domain_expertise}, think step by step as you consider the factors that may interact between the {treatment}
216220
and the {outcome}. Use your knowlegde as an expert in {domain_expertise} to describe the confounders, if there are any
@@ -230,6 +234,7 @@ def request_confounders(
230234
<confounding_factor>factor_name</confounding_factor> where
231235
factor_name is one of the items within the factor_names list. If a factor does not have a high likelihood of directly
232236
confounding, then do not wrap the factor with any tags."""
237+
lm += inspect.cleandoc(prompt_str)
233238
with assistant():
234239
lm += gen("output")
235240

@@ -284,7 +289,7 @@ def suggest_parents(
284289
lm += f"""You are an expert in {domain_expertise} and are studying {analysis_context}"""
285290

286291
with user():
287-
lm += f"""You are using your knowledge to help build a causal model that
292+
prompt_str = f"""You are using your knowledge to help build a causal model that
288293
contains all the assumptions about the factors that are directly influencing
289294
and causing the {factor}. Where a causal model is a conceptual model that describes the
290295
causal mechanisms of a system. You will do this by by answering questions about cause and
@@ -306,6 +311,7 @@ def suggest_parents(
306311
{factor},
307312
then do not wrap the factor with any tags. Your answer as an expert in
308313
{domain_expertise}:"""
314+
lm += inspect.cleandoc(prompt_str)
309315

310316
with assistant():
311317
lm += gen("output")
@@ -349,7 +355,7 @@ def suggest_children(
349355
with system():
350356
lm += f"""You are an expert in {domain_expertise} and are studying {analysis_context}"""
351357
with user():
352-
lm += f"""You are using your knowledge to help build a causal model that
358+
prompt_str = f"""You are using your knowledge to help build a causal model that
353359
contains all the assumptions about the factors that are directly influencing and causing the {factor}.
354360
Where a
355361
causal model is a conceptual model that describes the causal mechanisms of a system. You will do this by by
@@ -377,7 +383,7 @@ def suggest_children(
377383
factor_names list. If a factor does not have a high likelihood of directly influencing and causing the {
378384
factor}, then do not wrap the factor with any tags. Your answer as an expert in
379385
{domain_expertise}:"""
380-
386+
lm += inspect.cleandoc(prompt_str)
381387
with assistant():
382388
lm += gen("output")
383389

@@ -410,16 +416,17 @@ def suggest_pairwise_relationship(
410416
try:
411417
lm = self.llm
412418
with system():
413-
lm += f"""You are an expert in {domain_expertise} and are
419+
prompt_str = f"""You are an expert in {domain_expertise} and are
414420
studying {analysis_context}. You are using your knowledge to help build a causal model that contains
415421
all the
416422
assumptions about {analysis_context}. Where a causal model is a conceptual model that describes the
417423
causal
418424
mechanisms of a system. You will do this by by answering questions about cause and effect and using your
419425
domain
420426
knowledge as an expert in {domain_expertise}."""
427+
lm += inspect.cleandoc(prompt_str)
421428
with user():
422-
lm += f"""From your perspective as an expert in {domain_expertise}, which of the following is
429+
prompt_str = f"""From your perspective as an expert in {domain_expertise}, which of the following is
423430
most likely true? (A) {factor_a} affects {factor_b}; {factor_a} has a high likelihood of directly
424431
influencing {factor_b}; and {factor_a} causes {factor_b}. (B) {factor_b} affects {factor_a};
425432
{factor_b} has a high likelihood of directly influencing {factor_a}; and {factor_b} causes {factor_a}. (C) Neither A
@@ -429,6 +436,7 @@ def suggest_pairwise_relationship(
429436
you reach a conclusion, wrap your answer within the tags <answer></answer>. If you are done thinking, provide your
430437
answer wrapped within the tags <answer></answer>. e.g. <answer>A</answer>, <answer>B</answer>, or <answer>C</answer>.
431438
Your answer as an expert in {domain_expertise}:"""
439+
lm += inspect.cleandoc(prompt_str)
432440

433441
with assistant():
434442
lm += gen("output")

pywhyllm/suggesters/simple_identification_suggester.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import guidance
22
from guidance import system, user, assistant, gen
33
import re
4-
4+
from inspect import cleandoc
55

66
class SimpleIdentificationSuggester:
77

@@ -16,9 +16,10 @@ def suggest_iv(self, factors, treatment, outcome):
1616
lm += "You are a helpful assistant for causal reasoning."
1717

1818
with user():
19-
lm += f"""Which factors in {factors} might be valid instrumental variables for identifying the effect of {treatment} on {outcome}?
19+
prompt_str = f"""Which factors in {factors} might be valid instrumental variables for identifying the effect of {treatment} on {outcome}?
2020
2121
List the factors that are possible instrumental variables in <iv> </iv> tags."""
22+
lm += cleandoc(prompt_str)
2223
with assistant():
2324
lm += gen("iv")
2425

@@ -33,10 +34,11 @@ def suggest_backdoor(self, factors, treatment, outcome):
3334
lm += "You are a helpful assistant for causal reasoning."
3435

3536
with user():
36-
lm += f"""Which set or subset of factors in {factors} might satisfy the backdoor criteria for identifying the effect of {treatment} on {outcome}?
37+
prompt_str = f"""Which set or subset of factors in {factors} might satisfy the backdoor criteria for identifying the effect of {treatment} on {outcome}?
3738
3839
List the factors satisfying the backdoor criteria enclosing the name of each factor in <backdoor> </backdoor> tags.
3940
"""
41+
lm += cleandoc(prompt_str)
4042
with assistant():
4143
lm += gen("backdoors")
4244

@@ -51,10 +53,11 @@ def suggest_frontdoor(self, factors, treatment, outcome):
5153
lm += "You are a helpful assistant for causal reasoning."
5254

5355
with user():
54-
lm += f"""Which set or subset of factors in {factors} might satisfy the frontdoor criteria for identifying the effect of {treatment} on {outcome}?
56+
prompt_str = f"""Which set or subset of factors in {factors} might satisfy the frontdoor criteria for identifying the effect of {treatment} on {outcome}?
5557
5658
List the factors satisfying the frontdoor criteria enclosing the name of each factor in <frontdoor> </frontdoor> tags.
5759
"""
60+
lm += cleandoc(prompt_str)
5861
with assistant():
5962
lm += gen("frontdoors")
6063

pywhyllm/suggesters/simple_model_suggester.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44
import itertools
55
from guidance import system, user, assistant, gen
6-
6+
from inspect import cleandoc
77

88
class SimpleModelSuggester:
99
"""
@@ -43,8 +43,9 @@ def suggest_pairwise_relationship(self, variable1: str, variable2: str):
4343
lm += "You are a helpful assistant for causal reasoning."
4444

4545
with user():
46-
lm += f"""Which cause-and-effect-relationship is more likely? Provide reasoning and give your final answer (A, B, or C) in <answer> </answer> tags with the letter only and no whitespaces.
46+
prompt_str = f"""Which cause-and-effect-relationship is more likely? Provide reasoning and give your final answer (A, B, or C) in <answer> </answer> tags with the letter only and no whitespaces.
4747
A. {variable1} causes {variable2} B. {variable2} causes {variable1} C. neither {variable1} nor {variable2} cause each other."""
48+
lm += cleandoc(prompt_str)
4849

4950
with assistant():
5051
lm += gen("description")
@@ -110,12 +111,13 @@ def suggest_confounders(self, variables: List[str], treatment: str, outcome: str
110111
lm += "You are a helpful assistant for causal reasoning."
111112

112113
with user():
113-
lm += f"""What latent confounding factors might influence the relationship between {treatment} and {outcome}?
114+
prompt_str = f"""What latent confounding factors might influence the relationship between {treatment} and {outcome}?
114115
115116
We have already considered the following factors {variables}. Please do not repeat them.
116117
117118
List the confounding factors between {treatment} and {outcome} enclosing the name of each factor in <conf> </conf> tags.
118119
"""
120+
lm += cleandoc(prompt_str)
119121
with assistant():
120122
lm += gen("latents")
121123

0 commit comments

Comments
 (0)