Skip to content

Commit

Permalink
feat(runtimes/edge): dynamic model creator functions (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jul 13, 2024
1 parent 03c3672 commit bc77b4f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-clocks-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat(runtimes/edge): dynamic model creator functions
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { CoreMessage } from "../../types";
import { LanguageModelV1FunctionTool } from "@ai-sdk/provider";

export type EdgeRuntimeRequestOptions = {
export type LanguageModelConfig = {
apiKey?: string | undefined;
baseUrl?: string | undefined;
modelName?: string | undefined;
};

export type EdgeRuntimeRequestOptions = LanguageModelConfig & {
system?: string | undefined;
messages: CoreMessage[];
tools: LanguageModelV1FunctionTool[];
Expand Down
21 changes: 18 additions & 3 deletions packages/react/src/runtimes/edge/createEdgeRuntimeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
} from "@ai-sdk/provider";
import { CoreAssistantMessage, CoreMessage } from "../../types/AssistantTypes";
import { assistantEncoderStream } from "./streams/assistantEncoderStream";
import { EdgeRuntimeRequestOptions } from "./EdgeRuntimeRequestOptions";
import {
EdgeRuntimeRequestOptions,
LanguageModelConfig,
} from "./EdgeRuntimeRequestOptions";
import { toLanguageModelMessages } from "./converters/toLanguageModelMessages";
import { z } from "zod";
import { Tool } from "../../types";
Expand Down Expand Up @@ -53,8 +56,12 @@ type FinishResult = {
| undefined;
};

type LanguageModelCreator = (
config: LanguageModelConfig,
) => Promise<LanguageModelV1> | LanguageModelV1;

type CreateEdgeRuntimeAPIOptions = LanguageModelSettings & {
model: LanguageModelV1;
model: LanguageModelV1 | LanguageModelCreator;
system?: string;
tools?: Record<string, Tool<any, any>>;
toolChoice?: LanguageModelV1ToolChoice;
Expand All @@ -70,7 +77,7 @@ const voidStream = () => {
};

export const createEdgeRuntimeAPI = ({
model,
model: modelOrCreator,
system: serverSystem,
tools: serverTools = {},
toolChoice,
Expand All @@ -86,6 +93,9 @@ export const createEdgeRuntimeAPI = ({
system: clientSystem,
tools: clientTools,
messages,
apiKey,
baseUrl,
modelName,
} = (await request.json()) as EdgeRuntimeRequestOptions;

const systemMessages = [];
Expand All @@ -101,6 +111,11 @@ export const createEdgeRuntimeAPI = ({
}
}

const model =
typeof modelOrCreator === "function"
? await modelOrCreator({ apiKey, baseUrl, modelName })
: modelOrCreator;

let stream: ReadableStream<ToolResultStreamPart>;
const streamResult = await streamMessage({
...(settings as Partial<StreamMessageOptions>),
Expand Down

0 comments on commit bc77b4f

Please sign in to comment.