Skip to content

Commit eb3e954

Browse files
authored
Merge pull request #337 from grafana/grafana-frontend-add-model-abstraction
llm-frontend: add model abstraction
2 parents 3ad592c + 568ee0c commit eb3e954

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
"scripts": {
33
"build": "npm run build --workspaces",
44
"build:all": "npm run build --workspaces && npm run backend:build --workspace=@grafana/llm-app",
5-
"dev": "npm run backend:build && npm run build -w @grafana/llm && concurrently --names 'llm-frontend,llm-app' 'npm run dev -w @grafana/llm' 'npm run dev -w @grafana/llm-app' -c 'bgBlue.bold,bgMagenta.bold'",
5+
"dev": "npm run frontend:build && npm run backend:build && npm run build -w @grafana/llm && concurrently --names 'llm-frontend,llm-app' 'npm run dev -w @grafana/llm' 'npm run dev -w @grafana/llm-app' -c 'bgBlue.bold,bgMagenta.bold'",
66
"e2e:ci": "npm run e2e:ci --workspace=@grafana/llm-app",
77
"backend:update-sdk": "npm run backend:update-sdk --workspace=@grafana/llm-app",
88
"backend:build": "npm run backend:build --workspace=@grafana/llm-app",
99
"backend:test": "npm run backend:test --workspace=@grafana/llm-app",
1010
"backend:restart": "npm run backend:restart --workspace=@grafana/llm-app",
11+
"frontend:build": "npm run build --workspace=@grafana/llm",
1112
"lint": "npm run lint --workspaces",
1213
"lint:fix": "npm run lint:fix --workspaces",
1314
"server": "npm run server --workspace=@grafana/llm-app",

packages/grafana-llm-app/src/components/AppConfig/DevSandbox.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const BasicChatTest = () => {
3737
if (!useStream) {
3838
// Make a single request to the LLM.
3939
const response = await openai.chatCompletions({
40-
model: 'base',
40+
model: openai.Model.BASE,
4141
messages: [
4242
{ role: 'system', content: 'You are a cynical assistant.' },
4343
{ role: 'user', content: message },
@@ -50,7 +50,7 @@ const BasicChatTest = () => {
5050
} else {
5151
// Stream the completions. Each element is the next stream chunk.
5252
const stream = openai.streamChatCompletions({
53-
model: 'base',
53+
model: openai.Model.BASE,
5454
messages: [
5555
{ role: 'system', content: 'You are a cynical assistant.' },
5656
{ role: 'user', content: message },
@@ -92,8 +92,8 @@ const BasicChatTest = () => {
9292
placeholder="Enter a message"
9393
/>
9494
<br />
95-
<Button type="submit" onClick={() => {setMessage(input); setUseStream(true);}}>Submit Stream</Button>
96-
<Button type="submit" onClick={() => {setMessage(input); setUseStream(false);}}>Submit Request</Button>
95+
<Button type="submit" onClick={() => { setMessage(input); setUseStream(true); }}>Submit Stream</Button>
96+
<Button type="submit" onClick={() => { setMessage(input); setUseStream(false); }}>Submit Request</Button>
9797
<br />
9898
<div>{loading ? <Spinner /> : reply}</div>
9999
<div>{started ? "Response is started" : "Response is not started"}</div>

packages/grafana-llm-frontend/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const MyComponent = (): JSX.Element => {
4040
// Stream the completions. Each element is the next stream chunk.
4141
const stream = llms.openai
4242
.streamChatCompletions({
43-
model: 'gpt-3.5-turbo',
43+
model: llms.openai.Model.BASE,
4444
messages: [
4545
{ role: 'system', content: 'You are a cynical assistant.' },
4646
{ role: 'user', content: message },

packages/grafana-llm-frontend/src/openai.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,25 @@ export interface Function {
7272
parameters: Object;
7373
}
7474

75+
/**
76+
* Enum representing abstracted models used by the backend app.
77+
* @enum {string}
78+
*/
79+
export enum Model {
80+
BASE = 'base',
81+
LARGE = 'large',
82+
}
83+
84+
/**
85+
* @deprecated Use {@link Model} instead.
86+
*/
87+
type DeprecatedString = string;
88+
7589
export interface ChatCompletionsRequest {
7690
/**
77-
* ID of the model to use.
78-
*
79-
* See the model endpoint compatibility table for details on which models work with the Chat Completions API.
91+
* Model abstraction to use. These abstractions are then translated back into specific models based on the users settings.
8092
*/
81-
model: string;
93+
model: Model | DeprecatedString;
8294
/** A list of messages comprising the conversation so far. */
8395
messages: Message[];
8496
/** A list of functions the model may generate JSON inputs for. */
@@ -255,7 +267,7 @@ export function isErrorResponse<T>(
255267
* @returns An observable that emits the content messages. Each emission will be a string containing the
256268
* token emitted by the model.
257269
* @example <caption>Example of reading all tokens in a stream.</caption>
258-
* const stream = streamChatCompletions({ model: 'gpt-3.5-turbo', messages: [
270+
* const stream = streamChatCompletions({ model: Model.BASE, messages: [
259271
* { role: 'system', content: 'You are a great bot.' },
260272
* { role: 'user', content: 'Hello, bot.' },
261273
* ]}).pipe(extractContent());
@@ -282,7 +294,7 @@ export function extractContent(): UnaryFunction<
282294
* @returns An observable that emits the accumulated content messages. Each emission will be a string containing the
283295
* content of all messages received so far.
284296
* @example
285-
* const stream = streamChatCompletions({ model: 'gpt-3.5-turbo', messages: [
297+
* const stream = streamChatCompletions({ model: Model.BASE, messages: [
286298
* { role: 'system', content: 'You are a great bot.' },
287299
* { role: 'user', content: 'Hello, bot.' },
288300
* ]}).pipe(accumulateContent());
@@ -324,7 +336,7 @@ export async function chatCompletions(request: ChatCompletionsRequest): Promise<
324336
* The 'done' message will not be emitted; the stream will simply end when this message is encountered.
325337
*
326338
* @example <caption>Example of reading all tokens in a stream.</caption>
327-
* const stream = streamChatCompletions({ model: 'gpt-3.5-turbo', messages: [
339+
* const stream = streamChatCompletions({ model: Model.BASE, messages: [
328340
* { role: 'system', content: 'You are a great bot.' },
329341
* { role: 'user', content: 'Hello, bot.' },
330342
* ]}).pipe(extractContent());
@@ -333,7 +345,7 @@ export async function chatCompletions(request: ChatCompletionsRequest): Promise<
333345
* // ['Hello', '? ', 'How ', 'are ', 'you', '?']
334346
*
335347
* @example <caption>Example of accumulating tokens in a stream.</caption>
336-
* const stream = streamChatCompletions({ model: 'gpt-3.5-turbo', messages: [
348+
* const stream = streamChatCompletions({ model: Model.BASE, messages: [
337349
* { role: 'system', content: 'You are a great bot.' },
338350
* { role: 'user', content: 'Hello, bot.' },
339351
* ]}).pipe(accumulateContent());
@@ -487,7 +499,7 @@ export type OpenAIStreamState = {
487499
* @property {Subscription|undefined} value.stream - The stream subscription object if the stream is active, or undefined if not.
488500
*/
489501
export function useOpenAIStream(
490-
model = 'gpt-4',
502+
model = Model.LARGE,
491503
temperature = 1,
492504
notifyError: (title: string, text?: string, traceId?: string) => void = () => {}
493505
): OpenAIStreamState {

0 commit comments

Comments
 (0)