Skip to content

Commit

Permalink
Merge pull request #1772 from anmolchhabra21/fixes1751refactorAnalysi…
Browse files Browse the repository at this point in the history
…s.jsx

refactor Analysis.jsx component
  • Loading branch information
anth-volk authored May 13, 2024
2 parents a43c37e + 2913358 commit 158a27f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 91 deletions.
106 changes: 15 additions & 91 deletions src/pages/policy/output/Analysis.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { useSearchParams } from "react-router-dom";
import { useState } from "react";
import Spinner from "../../../layout/Spinner";
import Button from "../../../controls/Button";
// import CodeBlock from "layout/CodeBlock";
import CodeBlock from "../../../layout/CodeBlock";
import colors from "../../../style/colors";
import { getParameterAtInstant } from "../../../api/parameters";
import { MarkdownFormatter } from "../../../layout/MarkdownFormatter";
import { asyncApiCall, countryApiCall } from "../../../api/call";
import { getImpactReps } from "./ImpactTypes";
import { promptContent } from "./promptContent";

export default function Analysis(props) {
const { impact, policyLabel, metadata, policy, region, timePeriod } = props;
Expand Down Expand Up @@ -56,103 +56,27 @@ export default function Analysis(props) {
},
{},
);
const policyDetails = `I'm using PolicyEngine, a free, open source tool to compute the impact of public policy. I'm writing up an economic analysis of a hypothetical tax-benefit policy reform. Please write the analysis for me using the details below, in their order. You should:
* First explain each provision of the reform, noting that it's hypothetical and won't represents policy reforms for ${timePeriod} and ${
regionKeyToLabel[region]
}. Explain how the parameters are changing from the baseline to the reform values using the given data.
* Round large numbers like: ${metadata.currency}3.1 billion, ${
metadata.currency
}300 million, ${metadata.currency}106,000, ${metadata.currency}1.50 (never ${
metadata.currency
}1.5).
* Round percentages to one decimal place.
* Avoid normative language like 'requires', 'should', 'must', and use quantitative statements over general adjectives and adverbs. If you don't know what something is, don't make it up.
* Avoid speculating about the intent of the policy or inferring any motives; only describe the observable effects and impacts of the policy. Refrain from using subjective language or making assumptions about the recipients and their needs.
* Use the active voice where possible; for example, write phrases where the reform is the subject, such as "the reform [or a description of the reform] reduces poverty by x%".
* Use ${
metadata.countryId === "uk" ? "British" : "American"
} English spelling and grammar.
* Cite PolicyEngine ${metadata.countryId.toUpperCase()} v${selectedVersion} and the ${
metadata.countryId === "uk"
? "PolicyEngine-enhanced 2019 Family Resources Survey"
: "2021 Current Population Survey March Supplement"
} microdata when describing policy impacts.
* When describing poverty impacts, note that the poverty measure reported is ${
metadata.countryId === "uk"
? "absolute poverty before housing costs"
: "the Supplemental Poverty Measure"
}.
* Don't use headers, but do use Markdown formatting. Use - for bullets, and include a newline after each bullet.
* Include the following embeds inline, without a header so it flows.
* Immediately after you describe the changes by decile, include the text: {{decileRelativeImpact}}
* And after the poverty rate changes, include the text: {{povertyImpact}}
${
metadata.countryId === "us"
? "* After the racial breakdown of poverty rate changes, include the text: {{racialPovertyImpact}}"
: ""
}
* And after the inequality changes, include the text: {{inequalityImpact}}
* Make sure to accurately represent the changes observed in the data.
This JSON snippet describes the default parameter values: ${JSON.stringify(
relevantParameterBaselineValues,
)}\n
This JSON snippet describes the baseline and reform policies being compared: ${JSON.stringify(
policy,
)}\n`;
const description = `${policyLabel} has the following impacts from the PolicyEngine microsimulation model:
This JSON snippet describes the relevant parameters with more details: ${JSON.stringify(
relevantParameters,
)}
This JSON describes the total budgetary impact, the change to tax revenues and benefit spending (ignore 'households' and 'baseline_net_income': ${JSON.stringify(
impact.budget,
)}
This JSON describes how common different outcomes were at each income decile: ${JSON.stringify(
impact.intra_decile,
)}
This JSON describes the average and relative changes to income by each income decile: ${JSON.stringify(
impact.decile,
)}
This JSON describes the baseline and reform poverty rates by age group (describe the relative changes): ${JSON.stringify(
impact.poverty.poverty,
)}
This JSON describes the baseline and reform deep poverty rates by age group (describe the relative changes): ${JSON.stringify(
impact.poverty.deep_poverty,
)}
This JSON describes the baseline and reform poverty and deep poverty rates by gender (briefly describe the relative changes): ${JSON.stringify(
impact.poverty_by_gender,
)}
${
metadata.countryId === "us" &&
"This JSON describes the baseline and reform poverty impacts by racial group (briefly describe the relative changes): " +
JSON.stringify(impact.poverty_by_race.poverty)
}
This JSON describes three inequality metrics in the baseline and reform, the Gini coefficient of income inequality, the share of income held by the top 10% of households and the share held by the top 1% (describe the relative changes): ${JSON.stringify(
impact.inequality,
)}
`;

const [audience, setAudience] = useState("Normal");

const audienceDescriptions = {
ELI5: "Write this for a five-year-old who doesn't know anything about economics or policy. Explain fundamental concepts like taxes, poverty rates, and inequality as needed.",
Normal:
"Write this for a policy analyst who knows a bit about economics and policy.",
Wonk: "Write this for a policy analyst who knows a lot about economics and policy. Use acronyms and jargon if it makes the content more concise and informative.",
};

const prompt = policyDetails + description + audienceDescriptions[audience];
const prompt =
promptContent(
metadata,
selectedVersion,
timePeriod,
regionKeyToLabel,
impact,
policyLabel,
policy,
region,
relevantParameterBaselineValues,
relevantParameters,
) + audienceDescriptions[audience];

const [analysis, setAnalysis] = useState("");
const [loading, setLoading] = useState(false);
Expand Down Expand Up @@ -271,7 +195,7 @@ export default function Analysis(props) {
<h2>Analysis</h2>
<p>
<a href="/us/research/gpt-analysis">
Read more about PolicyEngine&apos;s automatic GPT4-powered policy
Read more about PolicyEn e&apos;s automatic GPT4-powered policy
analysis.
</a>{" "}
Generation usually takes around 60 seconds. Please verify any results of
Expand Down
102 changes: 102 additions & 0 deletions src/pages/policy/output/promptContent.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
export const promptContent = (
metadata,
selectedVersion,
timePeriod,
regionKeyToLabel,
impact,
policyLabel,
policy,
region,
relevantParameterBaselineValues,
relevantParameters,
) => {
const policyDetails = `I'm using PolicyEngine, a free, open source tool to compute the impact of public policy. I'm writing up an economic analysis of a hypothetical tax-benefit policy reform. Please write the analysis for me using the details below, in their order. You should:
* First explain each provision of the reform, noting that it's hypothetical and won't represents policy reforms for ${timePeriod} and ${
regionKeyToLabel[region]
}. Explain how the parameters are changing from the baseline to the reform values using the given data.
* Round large numbers like: ${metadata.currency}3.1 billion, ${
metadata.currency
}300 million, ${metadata.currency}106,000, ${metadata.currency}1.50 (never ${
metadata.currency
}1.5).
* Round percentages to one decimal place.
* Avoid normative language like 'requires', 'should', 'must', and use quantitative statements over general adjectives and adverbs. If you don't know what something is, don't make it up.
* Avoid speculating about the intent of the policy or inferring any motives; only describe the observable effects and impacts of the policy. Refrain from using subjective language or making assumptions about the recipients and their needs.
* Use the active voice where possible; for example, write phrases where the reform is the subject, such as "the reform [or a description of the reform] reduces poverty by x%".
* Use ${
metadata.countryId === "uk" ? "British" : "American"
} English spelling and grammar.
* Cite PolicyEngine ${metadata.countryId.toUpperCase()} v${selectedVersion} and the ${
metadata.countryId === "uk"
? "PolicyEngine-enhanced 2019 Family Resources Survey"
: "2021 Current Population Survey March Supplement"
} microdata when describing policy impacts.
* When describing poverty impacts, note that the poverty measure reported is ${
metadata.countryId === "uk"
? "absolute poverty before housing costs"
: "the Supplemental Poverty Measure"
}.
* Don't use headers, but do use Markdown formatting. Use - for bullets, and include a newline after each bullet.
* Include the following embeds inline, without a header so it flows.
* Immediately after you describe the changes by decile, include the text: {{decileRelativeImpact}}
* And after the poverty rate changes, include the text: {{povertyImpact}}
${
metadata.countryId === "us"
? "* After the racial breakdown of poverty rate changes, include the text: {{racialPovertyImpact}}"
: ""
}
* And after the inequality changes, include the text: {{inequalityImpact}}
* Make sure to accurately represent the changes observed in the data.
This JSON snippet describes the default parameter values: ${JSON.stringify(
relevantParameterBaselineValues,
)}\n
This JSON snippet describes the baseline and reform policies being compared: ${JSON.stringify(
policy,
)}\n`;

const description = `${policyLabel} has the following impacts from the PolicyEngine microsimulation model:
This JSON snippet describes the relevant parameters with more details: ${JSON.stringify(
relevantParameters,
)}
This JSON describes the total budgetary impact, the change to tax revenues and benefit spending (ignore 'households' and 'baseline_net_income': ${JSON.stringify(
impact.budget,
)}
This JSON describes how common different outcomes were at each income decile: ${JSON.stringify(
impact.intra_decile,
)}
This JSON describes the average and relative changes to income by each income decile: ${JSON.stringify(
impact.decile,
)}
This JSON describes the baseline and reform poverty rates by age group (describe the relative changes): ${JSON.stringify(
impact.poverty.poverty,
)}
This JSON describes the baseline and reform deep poverty rates by age group (describe the relative changes): ${JSON.stringify(
impact.poverty.deep_poverty,
)}
This JSON describes the baseline and reform poverty and deep poverty rates by gender (briefly describe the relative changes): ${JSON.stringify(
impact.poverty_by_gender,
)}
${
metadata.countryId === "us" &&
"This JSON describes the baseline and reform poverty impacts by racial group (briefly describe the relative changes): " +
JSON.stringify(impact.poverty_by_race.poverty)
}
This JSON describes three inequality metrics in the baseline and reform, the Gini coefficient of income inequality, the share of income held by the top 10% of households and the share held by the top 1% (describe the relative changes): ${JSON.stringify(
impact.inequality,
)}
`;

return policyDetails + description;
};

0 comments on commit 158a27f

Please sign in to comment.