Skip to content

Commit

Permalink
fix: duplicate filter when querying openai
Browse files Browse the repository at this point in the history
  • Loading branch information
ogzhanolguncu committed Jan 15, 2025
1 parent c7f877d commit 9744826
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import { transformStructuredOutputToFilters } from "@/app/(app)/logs-v2/filters.schema";
import { useFilters } from "@/app/(app)/logs-v2/hooks/use-filters";
import { useKeyboardShortcut } from "@/app/(app)/logs-v2/hooks/use-keyboard-shortcut";
import { toast } from "@/components/ui/toaster";
import { trpc } from "@/lib/trpc/client";
import { cn } from "@/lib/utils";
import { CaretRightOutline, CircleInfoSparkle, Magnifier, Refresh3 } from "@unkey/icons";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@unkey/ui";
import { cn } from "@unkey/ui/src/lib/utils";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "components/ui/tooltip";
import { useRef, useState } from "react";

export const LogsSearch = () => {
const { filters, updateFilters } = useFilters();
const queryLLMForStructuredOutput = trpc.logs.llmSearch.useMutation({
onSuccess(data) {
console.info("OUTPUT", data);
if (data) {
const transformedFilters = transformStructuredOutputToFilters(data, filters);
updateFilters(transformedFilters);
} else {
toast.error("Try to be more descriptive about your query", {
duration: 8000,
important: true,
position: "top-right",
style: {
whiteSpace: "pre-line",
},
});
}
},
onError(error) {
toast.error(error.message, {
Expand Down
49 changes: 49 additions & 0 deletions apps/dashboard/app/(app)/logs-v2/filters.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
FieldConfig,
FilterField,
FilterFieldConfigs,
FilterValue,
HttpMethod,
NumberConfig,
StatusConfig,
Expand Down Expand Up @@ -52,6 +53,54 @@ export const filterOutputSchema = z.object({
),
});

// Required for transforming OpenAI structured outputs into our own Filter types
export const transformStructuredOutputToFilters = (
data: z.infer<typeof filterOutputSchema>,
existingFilters: FilterValue[] = [],
): FilterValue[] => {
const uniqueFilters = [...existingFilters];
const seenFilters = new Set(existingFilters.map((f) => `${f.field}-${f.operator}-${f.value}`));

for (const filterGroup of data.filters) {
filterGroup.filters.forEach((filter) => {
const baseFilter = {
field: filterGroup.field,
operator: filter.operator,
value: filter.value,
};

const filterKey = `${baseFilter.field}-${baseFilter.operator}-${baseFilter.value}`;

if (seenFilters.has(filterKey)) {
return;
}

if (filterGroup.field === "status") {
const numericValue =
typeof filter.value === "string" ? Number.parseInt(filter.value) : filter.value;

uniqueFilters.push({
id: crypto.randomUUID(),
...baseFilter,
value: numericValue,
metadata: {
colorClass: filterFieldConfig.status.getColorClass?.(numericValue),
},
});
} else {
uniqueFilters.push({
id: crypto.randomUUID(),
...baseFilter,
});
}

seenFilters.add(filterKey);
});
}

return uniqueFilters;
};

// Type guard for config types
function isStatusConfig(config: FieldConfig): config is StatusConfig {
return "validate" in config && config.type === "number";
Expand Down
40 changes: 1 addition & 39 deletions apps/dashboard/lib/trpc/routers/logs/llm-search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { METHODS } from "@/app/(app)/logs-v2/constants";
import { filterFieldConfig, filterOutputSchema } from "@/app/(app)/logs-v2/filters.schema";
import type { QuerySearchParams } from "@/app/(app)/logs-v2/filters.type";
import { db } from "@/lib/db";
import { env } from "@/lib/env";
import { rateLimitedProcedure, ratelimit } from "@/lib/trpc/ratelimitProcedure";
Expand Down Expand Up @@ -55,7 +54,7 @@ async function getStructuredSearchFromLLM(userSearchMsg: string) {
});
}

return transformFiltersToQuerySearchParams(completion.choices[0].message.parsed);
return completion.choices[0].message.parsed;
} catch (error) {
console.error(
`Something went wrong when querying OpenAI. Input: ${JSON.stringify(
Expand Down Expand Up @@ -108,43 +107,6 @@ export const llmSearch = rateLimitedProcedure(ratelimit.update)
});

// HELPERS
function transformFiltersToQuerySearchParams(
result: z.infer<typeof filterOutputSchema>,
): QuerySearchParams {
const output: QuerySearchParams = {
host: null,
requestId: null,
methods: null,
paths: null,
status: null,
};

for (const filter of result.filters) {
const filterValues = filter.filters.map((f) => ({
operator: f.operator,
value: f.value,
}));

switch (filter.field) {
case "host":
case "requestId":
if (filter.filters.length > 0) {
output[filter.field] = filterValues[0];
}
break;

case "methods":
case "paths":
case "status":
if (filter.filters.length > 0) {
output[filter.field] = filterValues;
}
break;
}
}

return output;
}

const getSystemPrompt = () => {
const operatorsByField = Object.entries(filterFieldConfig)
Expand Down

0 comments on commit 9744826

Please sign in to comment.