Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hackathon] Agent #47993

Draft
wants to merge 52 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
fa1a978
wip: edit viz with LLMs
rafpaf Aug 31, 2024
4a4fa01
wip
rafpaf Sep 16, 2024
c2e2414
wip
rafpaf Sep 16, 2024
bfd9e29
wip: teaching AI more about mbql
rafpaf Sep 17, 2024
dc956b5
wip - creating filters by typing prose
rafpaf Sep 17, 2024
69b2f73
metabot section
heypoom Sep 18, 2024
29b8a06
wip
rafpaf Sep 18, 2024
9a0448a
wip
rafpaf Sep 18, 2024
df140f2
wip
rafpaf Sep 18, 2024
d610a21
wip
rafpaf Sep 18, 2024
55f595d
wip
rafpaf Sep 18, 2024
aa43eb6
wip
rafpaf Sep 18, 2024
e1d23c1
metabot agent
heypoom Sep 18, 2024
7b90cfa
wip
rafpaf Sep 18, 2024
0b2e204
Improve look of chat widget
rafpaf Sep 18, 2024
65b447f
Fix menu
rafpaf Sep 18, 2024
d80b5db
show/hide columns
heypoom Sep 18, 2024
d291dc6
Fix bug
rafpaf Sep 18, 2024
38291d2
Rename menu item
rafpaf Sep 18, 2024
fdf1d38
ai
heypoom Sep 18, 2024
bb644f3
pass messages
heypoom Sep 18, 2024
ba1c9ee
metabot section
heypoom Sep 18, 2024
1dd11d5
metabot agent
rafpaf Sep 18, 2024
13b9a70
show/hide columns
rafpaf Sep 18, 2024
157e747
ai
heypoom Sep 18, 2024
fd97647
pass messages
heypoom Sep 18, 2024
b06fb87
Reset change to sidebar
rafpaf Sep 18, 2024
bd8ab56
Reset question info sidebar entirely
rafpaf Sep 18, 2024
0ba458f
types
heypoom Sep 18, 2024
e1a05d0
wip
rafpaf Sep 18, 2024
d28c555
wip: adding filters
rafpaf Sep 18, 2024
8089f6a
summ/breakout
heypoom Sep 18, 2024
c2925d6
tool spec
heypoom Sep 18, 2024
125bec0
Merge remote-tracking branch 'origin/2024-hackathon-ai-agent' into 20…
rafpaf Sep 18, 2024
c2f8b1f
wip
rafpaf Sep 18, 2024
8671cd9
wip
rafpaf Sep 18, 2024
044374e
Merge branch 'sept2024-agent' into 2024-hackathon-ai-agent
rafpaf Sep 18, 2024
84c23d6
wip
rafpaf Sep 18, 2024
15de437
wip
rafpaf Sep 18, 2024
2bd893b
wip
rafpaf Sep 18, 2024
ad121f8
Add some metabot moods
rafpaf Sep 18, 2024
f104f00
Change visualization appropriately when summarization is applied.
maxzheng Sep 18, 2024
d897f8f
tool spec
heypoom Sep 18, 2024
435157a
Use new image for 'Metabot is thinking'
rafpaf Sep 18, 2024
fc356ef
Include distinct values for each field in the context
rafpaf Sep 18, 2024
f09a0bf
Merge remote-tracking branch 'origin/2024-hackathon-ai-agent' into 20…
rafpaf Sep 18, 2024
4c6bc92
Merge branch '2024-hackathon-ai-agent' of github.com:metabase/metabas…
heypoom Sep 18, 2024
41d1ab2
null tool call
heypoom Sep 18, 2024
0a57c2d
avg
heypoom Sep 18, 2024
ec9ff92
simpler filter
heypoom Sep 18, 2024
90527aa
fff
heypoom Sep 18, 2024
a7c4ff8
fff
heypoom Sep 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
rafpaf committed Sep 18, 2024
commit e1a05d028a635401771fcfe443b1bc08262ddcf1
21 changes: 12 additions & 9 deletions frontend/src/metabase/chat/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ import type Question from "metabase-lib/v1/Question";
import Styles from "./Chat.module.css";
import { useMessages } from "./hooks/use-messages";
import type { Message } from "./types";
import { getColumnsWithSampleValues } from "./utils";
import { getColumnsWithSampleValues, getJsonSchemaForFilters } from "./utils";
import { useMetabotAgentTool } from "./hooks/useMetabotAgentTool";
import { METABOT_AGENT_TOOLS_SPEC } from "metabase/query_builder/components/view/sidebars/QuestionInfoSidebar/constants/agent-tools-spec";
import { Field } from "metabase-types/api";

export const Chat = ({
scrollableStackRef,
Expand Down Expand Up @@ -74,14 +75,6 @@ export const Chat = ({
return getColumnsWithSampleValues(cols, rows);
}, [cols, rows]);

// const fieldsWithSampleValues = (() => {
// if (!rows || !cols) {
// return [];
// }
//
// return getColumnsWithSampleValues(cols, rows);
// })();

// So that the LLM has better information, we're going to concatenate the id
// with the field name and remove this name later.

Expand Down Expand Up @@ -109,6 +102,12 @@ export const Chat = ({
// use the field's name field.
// </system>;

const jsonSchemaForFilters = getJsonSchemaForFilters(
table.fields as Field[],
);
const jsonSchemaForFiltersStringified =
JSON.stringify(jsonSchemaForFilters);

const prompt = `
<context>
table id: ${question._card.dataset_query.database}.
Expand All @@ -117,6 +116,10 @@ export const Chat = ({
available fields: ${JSON.stringify(fields)}.
</context>

<json-schema-for-filters>
${jsonSchemaForFiltersStringified}
</json-schema-for-filters>

<user_ask>
${userMessage}
</user_ask>
Expand Down
76 changes: 74 additions & 2 deletions frontend/src/metabase/chat/components/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
METABOT_AGENT_TOOLS_SPEC,
Tool,
} from "metabase/query_builder/components/view/sidebars/QuestionInfoSidebar/constants/agent-tools-spec";
import { Field, RowValues } from "metabase-types/api";

const getBody = ({
content,
Expand Down Expand Up @@ -117,11 +118,11 @@ type Column = { display_name: string };

export const getColumnsWithSampleValues = (
columns: Column[],
rows: (string | number)[][],
rows: RowValues[],
) => {
return columns
.map((col, index) => {
const values = rows.map(row => row[index]);
const values = rows.map(row => row?.[index]);
const uniqueValues = _.uniq(values)
.slice(0, 10)
.filter(
Expand All @@ -141,3 +142,74 @@ export const getColumnsWithSampleValues = (
})
.join("\n");
};

export const getJsonSchemaForFilters = (fields: Field[]) => {
const fieldsForSchema = getFieldsForJsonSchema(fields);
const schema = {
$defs: {
Query: {
properties: {
filter: { $ref: "#/$defs/FilterCombo" },
},
title: "Query",
type: "object",
additionalProperties: false,
required: ["filter"],
},
Field: { enum: fieldsForSchema },
Filter: {
prefixItems: [
{
enum: [
"=",
"!=",
"<",
">",
">=",
"<=",
// These seem to be causing more trouble than they're worth right now:
// "is-null",
// "not-null"
],
},
{
$ref: "#/$defs/Field",
},
],
items: { anyOf: [{ type: "string" }, { type: "number" }] },
type: "array",
additionalItems: false,
},
FilterCombo: {
type: "array",
prefixItems: [{ enum: ["and", "or"] }],
items: { $ref: "#/$defs/Filter" },
additionalItems: false,
},
},
properties: {
display: {
enum: ["bar", "line", "pie", "scatter", "table", "map"],
title: "Display",
type: "string",
},
// "visualization_settings": {"$ref": "#/$defs/VisualizationSettings"},
query: {
anyOf: [{ $ref: "#/$defs/Query" }, { type: "null" }],
},
},
required: [
"display",
"query",
// "visualization_settings"
],
additionalProperties: false,
title: "QueryWithViz",
type: "object",
};
return schema;
};

export const getFieldsForJsonSchema = (fields: Field[]) => {
return fields.map(field => `${field.display_name}/${field.id}`);
};