Skip to content

Commit

Permalink
Merge branch 'main' into feat/ai-service/sql-pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
cyyeh committed Dec 26, 2024
2 parents 1e21fcb + 81e4f2a commit 7d96661
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 105 deletions.
2 changes: 1 addition & 1 deletion wren-ai-service/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "wren-ai-service"
version = "0.13.7"
version = "0.13.9"
description = ""
authors = ["Jimmy Yeh <jimmy.yeh@cannerdata.com>", "Pao Sheng Wang <david.wang@cannerdata.com>", "Aster Sun <aster.sun@cannerdata.com>"]
license = "AGPL-3.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Steps to rephrase the user's question:
- First, try to recognize adjectives in the user's question that are important to the user's intent.
- Second, change the adjectives to more specific and clear ones that can be matched to columns in the database schema.
- Third, if the user's question is related to time/date, add time/date format(such as YYYY-MM-DD) in the rephrased_question output.
- MUST use the rephrased user's question to make the intent classification.
- MUST put the rephrased user's question in the rephrased_question output.
- REASONING MUST be within 20 words.
Expand Down
26 changes: 26 additions & 0 deletions wren-ui/migrations/20241226135712_remove_thread_sql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
// drop foreign key constraint before altering column type to prevent data loss
await knex.schema.alterTable('thread_response', (table) => {
table.dropForeign('thread_id');
});
await knex.schema.alterTable('thread', (table) => {
table.dropColumn('sql');
});
await knex.schema.alterTable('thread_response', (table) => {
table.foreign('thread_id').references('thread.id').onDelete('CASCADE');
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
await knex.schema.alterTable('thread', (table) => {
table.text('sql').nullable();
});
};
2 changes: 1 addition & 1 deletion wren-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wren-ui",
"version": "0.18.9",
"version": "0.18.10",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
5 changes: 1 addition & 4 deletions wren-ui/src/apollo/client/graphql/__types__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ export type DetailedThread = {
__typename?: 'DetailedThread';
id: Scalars['Int'];
responses: Array<ThreadResponse>;
/** @deprecated Doesn't seem to be reasonable to put a sql in a thread */
sql: Scalars['String'];
};

export type Diagram = {
Expand Down Expand Up @@ -997,8 +995,6 @@ export type Task = {
export type Thread = {
__typename?: 'Thread';
id: Scalars['Int'];
/** @deprecated Doesn't seem to be reasonable to put a sql in a thread */
sql: Scalars['String'];
summary: Scalars['String'];
};

Expand Down Expand Up @@ -1044,6 +1040,7 @@ export type ThreadResponseBreakdownDetail = {

export type ThreadResponseChartDetail = {
__typename?: 'ThreadResponseChartDetail';
adjustment?: Maybe<Scalars['Boolean']>;
chartSchema?: Maybe<Scalars['JSON']>;
description?: Maybe<Scalars['String']>;
error?: Maybe<Error>;
Expand Down
26 changes: 12 additions & 14 deletions wren-ui/src/apollo/client/graphql/home.generated.ts

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions wren-ui/src/apollo/client/graphql/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const COMMON_CHART_DETAIL = gql`
error {
...CommonError
}
adjustment
}
`;

Expand Down Expand Up @@ -144,7 +145,6 @@ export const THREAD = gql`
query Thread($threadId: Int!) {
thread(threadId: $threadId) {
id
sql
responses {
...CommonResponse
}
Expand Down Expand Up @@ -180,7 +180,6 @@ export const CREATE_THREAD = gql`
mutation CreateThread($data: CreateThreadInput!) {
createThread(data: $data) {
id
sql
}
}
`;
Expand All @@ -204,7 +203,6 @@ export const UPDATE_THREAD = gql`
) {
updateThread(where: $where, data: $data) {
id
sql
summary
}
}
Expand Down
1 change: 1 addition & 0 deletions wren-ui/src/apollo/server/backgrounds/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export class ChartAdjustmentBackgroundTracker {
error: result?.error,
description: result?.response?.reasoning,
chartSchema: result?.response?.chartSchema,
adjustment: true,
};
logger.debug(
`Job ${threadResponse.id} chart status changed, updating`,
Expand Down
1 change: 0 additions & 1 deletion wren-ui/src/apollo/server/repositories/threadRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface ThreadRecommendationQuestionResult {
export interface Thread {
id: number; // ID
projectId: number; // Reference to project.id
sql: string; // SQL
summary: string; // Thread summary

// recommend question
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface ThreadResponseChartDetail {
error?: object;
description?: string;
chartSchema?: Record<string, any>;
adjustment?: boolean;
}

export interface ThreadResponse {
Expand Down
9 changes: 1 addition & 8 deletions wren-ui/src/apollo/server/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ export const typeDefs = gql`
error: Error
description: String
chartSchema: JSON
adjustment: Boolean
}
type ThreadResponse {
Expand All @@ -700,20 +701,12 @@ export const typeDefs = gql`
# Thread only consists of basic information of a thread
type Thread {
id: Int!
sql: String!
@deprecated(
reason: "Doesn't seem to be reasonable to put a sql in a thread"
)
summary: String!
}
# Detailed thread consists of thread and thread responses
type DetailedThread {
id: Int!
sql: String!
@deprecated(
reason: "Doesn't seem to be reasonable to put a sql in a thread"
)
responses: [ThreadResponse!]!
}
Expand Down
4 changes: 1 addition & 3 deletions wren-ui/src/apollo/server/services/askingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,6 @@ export class AskingService implements IAskingService {
const { id } = await this.projectService.getCurrentProject();
const thread = await this.threadRepository.createOne({
projectId: id,
sql: input.sql,
summary: input.question,
});

Expand Down Expand Up @@ -780,6 +779,7 @@ export class AskingService implements IAskingService {
chartDetail: {
queryId: response.queryId,
status: ChartStatus.FETCHING,
adjustment: true,
},
},
);
Expand Down Expand Up @@ -969,8 +969,6 @@ export class AskingService implements IAskingService {
const { id } = await this.projectService.getCurrentProject();
const thread = await this.threadRepository.createOne({
projectId: id,
// todo: remove sql from thread
sql: view.statement,
summary: input.question,
});

Expand Down
61 changes: 0 additions & 61 deletions wren-ui/src/components/chart/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChartType } from '@/apollo/client/graphql/__types__';
import { isNil, cloneDeep, uniq, sortBy, omit, isNumber } from 'lodash';
import { Config, TopLevelSpec } from 'vega-lite';
import { PositionFieldDef } from 'vega-lite/build/src/channeldef';

enum MarkType {
ARC = 'arc',
Expand Down Expand Up @@ -234,7 +233,6 @@ export default class ChartSpecHandler {

private addEncoding(encoding: EncodingSpec) {
this.encoding = encoding;
const { x, y } = this.getAxisDomain();

// fill color by x field if AI not provide color(category) field
if (isNil(this.encoding.color)) {
Expand All @@ -253,26 +251,6 @@ export default class ChartSpecHandler {

// handle scale on bar chart
if (this.mark.type === MarkType.BAR) {
if (y) {
this.encoding.y = {
...this.encoding.y,
scale: {
domain: y,
nice: false,
},
};
}

if (x) {
this.encoding.x = {
...this.encoding.x,
scale: {
domain: x,
nice: false,
},
};
}

if ('stack' in this.encoding.y) {
this.encoding.y.stack = this.options.stack;
}
Expand All @@ -291,45 +269,6 @@ export default class ChartSpecHandler {
this.addHoverHighlight(this.encoding);
}

private getAxisDomain() {
const xField = this.encoding.x as PositionFieldDef<any>;
const yField = this.encoding.y as PositionFieldDef<any>;
const calculateMaxDomain = (field: PositionFieldDef<any>) => {
if (field?.type !== 'quantitative') return null;
const fieldValue = field.field;
const values = (this.data as any).values.map((d) => d[fieldValue]);

const maxValue = Math.max(...values);

// Get the magnitude (e.g., 1, 10, 100, 1000)
const magnitude = Math.pow(10, Math.floor(Math.log10(maxValue)));

// Get number between 1-10
const normalizedValue = maxValue / magnitude;
let niceNumber;

if (normalizedValue <= 1.2) niceNumber = 1.2;
else if (normalizedValue <= 1.5) niceNumber = 1.5;
else if (normalizedValue <= 2) niceNumber = 2;
else if (normalizedValue <= 2.5) niceNumber = 2.5;
else if (normalizedValue <= 3) niceNumber = 3;
else if (normalizedValue <= 4) niceNumber = 4;
else if (normalizedValue <= 5) niceNumber = 5;
else if (normalizedValue <= 7.5) niceNumber = 7.5;
else if (normalizedValue <= 8) niceNumber = 8;
else niceNumber = 10;

const domainMax = niceNumber * magnitude;
return [0, domainMax];
};
const xDomain = calculateMaxDomain(xField);
const yDomain = calculateMaxDomain(yField);
return {
x: xDomain,
y: yDomain,
};
}

private addHoverHighlight(encoding: EncodingSpec) {
const category = (
encoding.color?.condition ? encoding.color.condition : encoding.color
Expand Down
22 changes: 15 additions & 7 deletions wren-ui/src/components/pages/home/promptThread/ChartAnswer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default function ChartAnswer(props: Props) {

const [form] = Form.useForm();
const { chartDetail } = threadResponse;
const { error, status } = chartDetail || {};
const { error, status, adjustment } = chartDetail || {};

const [previewData, previewDataResult] = usePreviewDataMutation({
onError: (error) => console.error(error),
Expand Down Expand Up @@ -203,6 +203,14 @@ export default function ChartAnswer(props: Props) {
onResetState();
};

const regenerateBtn = (
<div className="text-center mt-4">
<Button icon={<ReloadOutlined />} onClick={onReload}>
Regenerate
</Button>
</div>
);

if (error) {
return (
<div className="py-6 px-4">
Expand All @@ -212,15 +220,13 @@ export default function ChartAnswer(props: Props) {
type="error"
showIcon
/>
<div className="text-center mt-4">
<Button icon={<ReloadOutlined />} onClick={onRegenerate}>
Regenerate
</Button>
</div>
{regenerateBtn}
</div>
);
}

const chartRegenerateBtn = adjustment ? regenerateBtn : null;

return (
<StyledSkeleton
active
Expand All @@ -230,7 +236,7 @@ export default function ChartAnswer(props: Props) {
>
<div className="text-md gray-10 py-6 px-4">
{chartDetail?.description}
{chartSpec && (
{chartSpec ? (
<ChartWrapper
className={clsx(
'border border-gray-4 rounded mt-4 pb-3 overflow-hidden',
Expand Down Expand Up @@ -277,6 +283,8 @@ export default function ChartAnswer(props: Props) {
onReload={onReload}
/>
</ChartWrapper>
) : (
chartRegenerateBtn
)}
</div>
</StyledSkeleton>
Expand Down
6 changes: 4 additions & 2 deletions wren-ui/src/styles/components/chart.less
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
cursor: pointer;
width: 28px;
height: 28px;
opacity: 0.2;
opacity: 0.4;
color: @gray-8;
transition: all 0.4s ease-in;

&:hover {
Expand All @@ -33,7 +34,7 @@
.vega-embed {
&:hover {
summary {
opacity: 0.2 !important;
opacity: 0.4 !important;
}
}

Expand All @@ -43,6 +44,7 @@
box-shadow: none;
transition: all 0.4s ease-in;
color: @gray-8;
opacity: 0.4 !important;

&:hover {
opacity: 1 !important;
Expand Down

0 comments on commit 7d96661

Please sign in to comment.