-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: append assistant / system message support (#472)
- Loading branch information
Showing
21 changed files
with
125 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ChatModelAdapter, ChatModelRunOptions } from "../local"; | ||
import { ChatModelRunResult } from "../local/ChatModelAdapter"; | ||
import { toCoreMessage } from "./converters/toCoreMessage"; | ||
import { toLanguageModelTools } from "./converters/toLanguageModelTools"; | ||
import { EdgeRuntimeRequestOptions } from "./EdgeRuntimeRequestOptions"; | ||
import { assistantDecoderStream } from "./streams/assistantDecoderStream"; | ||
import { chunkByLineStream } from "./streams/chunkByLineStream"; | ||
import { runResultStream } from "./streams/runResultStream"; | ||
import { toolResultStream } from "./streams/toolResultStream"; | ||
|
||
export function asAsyncIterable<T>( | ||
source: ReadableStream<T>, | ||
): AsyncIterable<T> { | ||
return { | ||
[Symbol.asyncIterator]: () => { | ||
const reader = source.getReader(); | ||
return { | ||
async next(): Promise<IteratorResult<T, undefined>> { | ||
const { done, value } = await reader.read(); | ||
return done | ||
? { done: true, value: undefined } | ||
: { done: false, value }; | ||
}, | ||
}; | ||
}, | ||
}; | ||
} | ||
export type EdgeRuntimeOptions = { api: string }; | ||
|
||
export class EdgeChatAdapter implements ChatModelAdapter { | ||
constructor(private options: EdgeRuntimeOptions) {} | ||
|
||
async run({ messages, abortSignal, config, onUpdate }: ChatModelRunOptions) { | ||
const result = await fetch(this.options.api, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
system: config.system, | ||
messages: messages.map(toCoreMessage), | ||
tools: toLanguageModelTools( | ||
config.tools, | ||
) as EdgeRuntimeRequestOptions["tools"], | ||
} satisfies EdgeRuntimeRequestOptions), | ||
signal: abortSignal, | ||
}); | ||
|
||
const stream = result | ||
.body!.pipeThrough(new TextDecoderStream()) | ||
.pipeThrough(chunkByLineStream()) | ||
.pipeThrough(assistantDecoderStream()) | ||
.pipeThrough(toolResultStream(config.tools)) | ||
.pipeThrough(runResultStream()); | ||
|
||
let update: ChatModelRunResult | undefined; | ||
for await (update of asAsyncIterable(stream)) { | ||
onUpdate(update); | ||
} | ||
if (update === undefined) | ||
throw new Error("No data received from Edge Runtime"); | ||
return update; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { useEdgeRuntime } from "./useEdgeRuntime"; | ||
export { EdgeChatAdapter } from "./EdgeChatAdapter"; | ||
export type { EdgeRuntimeOptions } from "./EdgeChatAdapter"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,8 @@ | ||
import { assistantDecoderStream } from "./streams/assistantDecoderStream"; | ||
import { chunkByLineStream } from "./streams/chunkByLineStream"; | ||
import { | ||
ChatModelAdapter, | ||
ChatModelRunResult, | ||
} from "../local/ChatModelAdapter"; | ||
import { runResultStream } from "./streams/runResultStream"; | ||
import { useLocalRuntime } from ".."; | ||
import { useMemo } from "react"; | ||
import { toolResultStream } from "./streams/toolResultStream"; | ||
import { EdgeRuntimeRequestOptions } from "./EdgeRuntimeRequestOptions"; | ||
import { toLanguageModelTools } from "./converters/toLanguageModelTools"; | ||
import { toCoreMessage } from "./converters/toCoreMessage"; | ||
|
||
export function asAsyncIterable<T>( | ||
source: ReadableStream<T>, | ||
): AsyncIterable<T> { | ||
return { | ||
[Symbol.asyncIterator]: () => { | ||
const reader = source.getReader(); | ||
return { | ||
async next(): Promise<IteratorResult<T, undefined>> { | ||
const { done, value } = await reader.read(); | ||
return done | ||
? { done: true, value: undefined } | ||
: { done: false, value }; | ||
}, | ||
}; | ||
}, | ||
}; | ||
} | ||
|
||
type EdgeRuntimeOptions = { api: string }; | ||
|
||
const createEdgeChatAdapter = ({ | ||
api, | ||
}: EdgeRuntimeOptions): ChatModelAdapter => ({ | ||
run: async ({ messages, abortSignal, config, onUpdate }) => { | ||
const result = await fetch(api, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
system: config.system, | ||
messages: messages.map(toCoreMessage), | ||
tools: toLanguageModelTools( | ||
config.tools, | ||
) as EdgeRuntimeRequestOptions["tools"], | ||
} satisfies EdgeRuntimeRequestOptions), | ||
signal: abortSignal, | ||
}); | ||
|
||
const stream = result | ||
.body!.pipeThrough(new TextDecoderStream()) | ||
.pipeThrough(chunkByLineStream()) | ||
.pipeThrough(assistantDecoderStream()) | ||
.pipeThrough(toolResultStream(config.tools)) | ||
.pipeThrough(runResultStream()); | ||
|
||
let update: ChatModelRunResult | undefined; | ||
for await (update of asAsyncIterable(stream)) { | ||
onUpdate(update); | ||
} | ||
if (update === undefined) | ||
throw new Error("No data received from Edge Runtime"); | ||
return update; | ||
}, | ||
}); | ||
import { useState } from "react"; | ||
import { EdgeRuntimeOptions, EdgeChatAdapter } from "./EdgeChatAdapter"; | ||
|
||
export const useEdgeRuntime = (options: EdgeRuntimeOptions) => { | ||
const adapter = useMemo(() => createEdgeChatAdapter(options), [options]); | ||
const [adapter] = useState(() => new EdgeChatAdapter(options)); | ||
return useLocalRuntime(adapter); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
export { useLocalRuntime } from "./useLocalRuntime"; | ||
export type { ChatModelAdapter, ChatModelRunOptions } from "./ChatModelAdapter"; | ||
export type { | ||
ChatModelAdapter, | ||
ChatModelRunOptions, | ||
ChatModelRunResult, | ||
ChatModelRunUpdate, | ||
} from "./ChatModelAdapter"; |
Oops, something went wrong.