Skip to content

Commit

Permalink
fix: invalid CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Jul 1, 2024
1 parent e7c660c commit c31dfab
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/backend/src/api/v1/evaluations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export async function runEval({
const endedAt = new Date()

// Create virtual run to be able to run checks
const output = res.choices[0].message
const output = res?.choices[0].message

const promptTokens = res.usage?.prompt_tokens
const completionTokens = res.usage?.completion_tokens
const promptTokens = res?.usage?.prompt_tokens
const completionTokens = res?.usage?.completion_tokens
const duration = endedAt.getTime() - createdAt.getTime()

const virtualRun = {
Expand Down
40 changes: 18 additions & 22 deletions packages/frontend/components/evals/ResultsMatrix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import SmartViewer from "../SmartViewer"
import { MODELS, Provider } from "shared"
import { IconFileExport } from "@tabler/icons-react"

import { json2csv } from "json-2-csv"

// We create a matrix of results for each prompt, variable and model.
// The matrix is a 3D array, where each dimension represents a different variable, prompt and model.

Expand Down Expand Up @@ -178,15 +180,8 @@ export default function ResultsMatrix({ data, showTestIndicator }) {
...prompts.map((messages) => getVariableKeysForPrompt(messages).length),
)

function exportToCsv() {
const columns = [
"Prompt",
"Variable Variation",
"Model",
"Passed",
"Output",
]
const rows = []
async function exportToCsv() {
const rows = [] as any[]

prompts.forEach((messages) => {
const variableVariations = getVariableVariationsForPrompt(messages)
Expand All @@ -202,26 +197,27 @@ export default function ResultsMatrix({ data, showTestIndicator }) {
? JSON.stringify(result.error)
: result.output?.content

rows.push([
JSON.stringify(messages),
JSON.stringify(variables),
provider.model,
result.passed ? "Yes" : "No",
`"${textResult.replace(/"/g, '""')}"`, // Escape double quotes and wrap in double quotes
])
rows.push(
{
Prompt: JSON.stringify(messages),
Variables: JSON.stringify(variables),
Model: provider.model,
Passed: result.passed ? "Yes" : "No",
Output: textResult,
}, // Escape double quotes and wrap in double quotes
)
}
})
})
})

const csvContent = [
columns.join(","),
...rows.map((row) => row.join(",")),
].join("\n")
const csv = await json2csv(rows, {
arrayIndexesAsKeys: false,
})

const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" })
const link = document.createElement("a")
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" })
const url = URL.createObjectURL(blob)
const link = document.createElement("a")
link.setAttribute("href", url)
link.setAttribute("download", "results.csv")
link.style.visibility = "hidden"
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"load-env": "bash ./load-env.sh"
},
"dependencies": {
"@mantine/charts": "7.10.0",
"@mantine/core": "7.10.0",
"@mantine/dates": "^7.10.0",
"@mantine/form": "7.10.0",
"@mantine/hooks": "7.10.0",
"@mantine/modals": "7.10.0",
"@mantine/notifications": "7.10.0",
"@mantine/charts": "7.10.0",
"@sentry/nextjs": "^7.109.0",
"@tabler/icons-react": "^2.46.0",
"@tanstack/react-table": "^8.11.2",
Expand All @@ -24,6 +24,7 @@
"date-fns": "^3.6.0",
"dayjs": "^1.11.11",
"jose": "^5.2.0",
"json-2-csv": "^5.5.1",
"jsonrepair": "^3.5.1",
"next": "^14.1.0",
"next-seo": "^6.4.0",
Expand Down

0 comments on commit c31dfab

Please sign in to comment.