Skip to content

Commit

Permalink
deploy t2vega model with ml-commons
Browse files Browse the repository at this point in the history
Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
  • Loading branch information
ruanyl committed May 17, 2024
1 parent c82118d commit 5ffaf3c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
37 changes: 20 additions & 17 deletions src/plugins/vis_type_vega/public/text_to_vega.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { BehaviorSubject, Observable, of } from 'rxjs';
import {
takeWhile,
debounceTime,
distinctUntilChanged,
switchMap,
tap,
filter,
catchError,
} from 'rxjs/operators';
import { BehaviorSubject, Observable } from 'rxjs';

Check failure on line 1 in src/plugins/vis_type_vega/public/text_to_vega.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

File must start with a license header
import { debounceTime, distinctUntilChanged, switchMap, tap, filter } from 'rxjs/operators';
import { HttpSetup } from 'opensearch-dashboards/public';

const topN = (ppl: string, n: number) => `${ppl} | head ${n}`;
Expand All @@ -33,6 +25,10 @@ Just reply with the json based Vega-Lite object, do not include any other conten
`;
};

const llmRunning$ = new BehaviorSubject(false);
// @ts-ignore
window['llmRunning$'] = llmRunning$;

Check failure on line 30 in src/plugins/vis_type_vega/public/text_to_vega.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

["llmRunning$"] is better written in dot notation

export class Text2Vega {
input$: BehaviorSubject<string>;
vega$: Observable<string | Error>;
Expand All @@ -42,10 +38,10 @@ export class Text2Vega {
this.http = http;
this.input$ = new BehaviorSubject('');
this.vega$ = this.input$.pipe(
tap((v) => console.log(v)),
filter((v) => v.length > 0),
debounceTime(200),
distinctUntilChanged(),
tap((v) => llmRunning$.next(true)),
// text to ppl
switchMap(async (value) => {
const pplQuestion = value.split('//')[0];
Expand Down Expand Up @@ -83,16 +79,23 @@ export class Text2Vega {
query: value.ppl,
};
return result;
})
}),
tap(() => llmRunning$.next(false))
);
}

async text2vega(query: string) {
const res = await this.http.post('/api/llm/text2vega', {
body: JSON.stringify({ query }),
});
console.log('llm res: ', res);
return res;
try {
const res = await this.http.post('/api/llm/text2vega', {
body: JSON.stringify({ query }),
});
console.log('llm res: ', res);

Check failure on line 92 in src/plugins/vis_type_vega/public/text_to_vega.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Unexpected console statement
// return res;
const result = res.body.inference_results[0].output[0].dataAsMap;
return result;
} catch (e) {
console.log(e);

Check failure on line 97 in src/plugins/vis_type_vega/public/text_to_vega.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Unexpected console statement
}
}

async text2ppl(query: string) {
Expand Down
14 changes: 12 additions & 2 deletions src/plugins/vis_type_vega/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
} from './vega_visualization_client_wrapper';
import { setDataSourceEnabled } from './services';

export const invokeText2Vega = async (
const invokeText2Vega = async (
prompt: string,
modelId = 'anthropic.claude-3-sonnet-20240229-v1:0'
// modelId = 'anthropic.claude-3-haiku-20240307-v1:0'
Expand Down Expand Up @@ -117,7 +117,17 @@ export class VisTypeVegaPlugin implements Plugin<VisTypeVegaPluginSetup, VisType
},
},
router.handleLegacyErrors(async (context, req, res) => {
const result = await invokeText2Vega(req.body.query);
// const result = await invokeText2Vega(req.body.query);
// return res.ok({ body: result });
const result = await context.core.opensearch.client.asCurrentUser.transport.request({
method: 'POST',
path: '/_plugins/_ml/models/_yV0hY8B8ef_5QXJp6Xd/_predict',
body: {
parameters: {
prompt: req.body.query,
},
},
});
return res.ok({ body: result });
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ const TopNav = ({
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
window['llmRunning$'].subscribe((running) => {
setGenerating(!!running);
});
}, []);

const HARDCODED_SUGGESTIONS: string[] = [
`what's the revenue for past week and group by day?`,
'how many orders per day for past week?',
Expand Down

0 comments on commit 5ffaf3c

Please sign in to comment.