Skip to content

Commit

Permalink
feat: add data fact description to search
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbrgl committed Jan 20, 2025
1 parent 67d9b94 commit a1db318
Show file tree
Hide file tree
Showing 6 changed files with 416 additions and 4 deletions.
10 changes: 9 additions & 1 deletion adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ import {
handleDeleteChartRedirect,
} from "./apiRoutes/redirects.js"
import { triggerStaticBuild } from "./apiRoutes/routeUtils.js"
import { suggestGptTopics, suggestGptAltText } from "./apiRoutes/suggest.js"
import {
suggestGptTopics,
suggestGptAltText,
suggestDataPointDescription,
} from "./apiRoutes/suggest.js"
import {
handleGetFlatTagGraph,
handlePostTagGraph,
Expand Down Expand Up @@ -332,6 +336,10 @@ getRouteWithROTransaction(
suggestGptAltText
)

apiRouter.post("/gpt/suggest-data-point-description", async (req) => ({
description: await suggestDataPointDescription(req),
}))

// Tag graph routes
getRouteWithROTransaction(
apiRouter,
Expand Down
46 changes: 46 additions & 0 deletions adminSiteServer/apiRoutes/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { fetchGptGeneratedAltText } from "../imagesHelpers.js"
import * as db from "../../db/db.js"
import e from "express"
import { Request } from "../authentication.js"
import OpenAI from "openai"
import { OPENAI_API_KEY } from "../../settings/serverSettings.js"

export async function suggestGptTopics(
req: Request,
Expand Down Expand Up @@ -62,3 +64,47 @@ export async function suggestGptAltText(

return { success: true, altText }
}

export async function suggestDataPointDescription(
req: Request
): Promise<string> {
const { dataPoint, metadata } = req.body
if (!dataPoint || !metadata) throw new JsonError(`Invalid input data`, 400)

let description: string | null = ""
try {
description = await fetchGptDataPointDescription(dataPoint, metadata)
} catch (error) {
console.error(`Error fetching GPT sentence`, error)
throw new JsonError(`Error fetching GPT sentence: ${error}`, 500)
}

if (!description) {
throw new JsonError(`Unable to generate sentence`, 404)
}

return description
}

export async function fetchGptDataPointDescription(
dataPoint: any,
metadata: any
) {
const prompt = `Given the data point ${JSON.stringify(dataPoint)} and metadata ${JSON.stringify(metadata)},
generate a data point fact.
- Do not add any information that is not directly supported by the data point and metadata.
- Do not prefix the fact with "Data point:" or similar.`
// console.log(prompt)
const openai = new OpenAI({
apiKey: OPENAI_API_KEY,
})
const completion = await openai.chat.completions.create({
messages: [{ role: "user", content: prompt }],
model: "gpt-4o-mini",
})

const description = completion.choices[0]?.message?.content
if (!description) throw new JsonError("No response from GPT", 500)
// console.log("Generated Description:", description)
return description
}
2 changes: 1 addition & 1 deletion packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ export const fetchText = async (url: string): Promise<string> => {
const _getUserCountryInformation = async (): Promise<
UserCountryInformation | undefined
> =>
await fetchWithRetry("/detect-country")
await fetchWithRetry("https://detect-country.owid.io")
.then((res) => res.json())
.then((res) => res.country)
.catch(() => undefined)
Expand Down
45 changes: 45 additions & 0 deletions site/search/ChartHit.scss
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,48 @@ svg.chart-hit-icon {
}
}
}

.chart-hit-textual {
// border-bottom: 1px solid $gray-20;
padding-bottom: 24px;
}

.chart-hit-textual__description {
margin-top: 32px;
font-size: 1.5rem;
a {
@include owid-link-90;
text-decoration: none;
&:visited {
color: $blue-90;
}
&:hover {
text-decoration: underline;
}
}
}

.chart-hit-textual__debug {
display: flex;
gap: 0.5em;
margin-top: 4px;
justify-content: end;
}

.chart-hit-textual__get-data {
a {
@include owid-link-60;
&:visited {
color: $blue-60;
}
}
}

.chart-hit-textual__data {
color: $gray-60;
font-size: 0.8rem;

a {
@include owid-link-60;
}
}
Loading

0 comments on commit a1db318

Please sign in to comment.