-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be925c0
commit b7a4b92
Showing
10 changed files
with
297 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
TODO.md | ||
TODO.md | ||
node_modules/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,51 +1,143 @@ | ||
import { Backend } from "@/editor/hooks/useRunner"; | ||
import { getMessagesOfAuthor } from "@/editor/lib/playMessages"; | ||
import { EditorState } from "@/editor/lib/types"; | ||
import { | ||
MessageOfAuthor, | ||
getMessagesOfAuthor, | ||
} from "@/editor/lib/playMessages"; | ||
import { EditorState, NamedVariable, SamplingParams } from "@/editor/lib/types"; | ||
import { assertIsNever } from "@/lib/utils"; | ||
import { FC } from "react"; | ||
import { atomFamily, useRecoilValueLoadable } from "recoil"; | ||
import { SGLangBackend } from "@lmscript/client/backends/sglang"; | ||
import { AbstractBackend } from "@lmscript/client/backends/abstract"; | ||
import { messagesToTasks } from "@/editor/lib/messageToTasks"; | ||
import { VllmBackend } from "@lmscript/client/backends/vllm"; | ||
import { RunpodServerlessBackend } from "@lmscript/client/backends/runpod-serverless-sglang"; | ||
const getBackendInstance = (backend: Backend): AbstractBackend => { | ||
switch (backend.tag) { | ||
case "runpod-serverless-sglang": { | ||
return new RunpodServerlessBackend(backend.url, backend.token); | ||
} | ||
case "runpod-serverless-vllm": { | ||
return new VllmBackend({ | ||
url: backend.url, | ||
auth: backend.token, | ||
model: backend.model, | ||
}); | ||
} | ||
case "sglang": { | ||
return new SGLangBackend(backend.url); | ||
} | ||
default: { | ||
return assertIsNever(backend); | ||
} | ||
} | ||
}; | ||
|
||
const PlayStream: FC<{ | ||
editorState: EditorState; | ||
captures: Record<string, string>; | ||
}> = ({ editorState }) => { | ||
const msgs = getMessagesOfAuthor(editorState); | ||
if (msgs.tag === "success") { | ||
return ( | ||
<> | ||
{msgs.value.map((msg, i) => { | ||
return ( | ||
<div key={i}> | ||
<pre>{JSON.stringify(msg, null, 2)}</pre> | ||
</div> | ||
); | ||
})} | ||
</> | ||
); | ||
type Captures = Record<string, string>; | ||
const generateAsyncAtom = atomFamily< | ||
{ | ||
captures: Captures; | ||
finalText: string | undefined; | ||
}, | ||
{ | ||
backend: Backend; | ||
messages: MessageOfAuthor[]; | ||
samplingParams: SamplingParams; | ||
variables: NamedVariable[]; | ||
} | ||
>({ | ||
key: "generateAsyncAtom", | ||
default: (_param) => { | ||
return { | ||
captures: {}, | ||
finalText: undefined, | ||
}; | ||
}, | ||
effects: (param) => [ | ||
(opts) => { | ||
const instance = getBackendInstance(param.backend); | ||
const tasks = messagesToTasks( | ||
param.messages, | ||
param.backend.template, | ||
param.variables, | ||
); | ||
|
||
instance | ||
.executeJSON( | ||
{ | ||
tasks, | ||
sampling_params: param.samplingParams, | ||
initial_state: { | ||
text: "", | ||
captured: {}, | ||
}, | ||
}, | ||
{ | ||
onCapture: (cap) => { | ||
opts.setSelf((prev) => { | ||
if ("captures" in prev) { | ||
return { | ||
captures: { | ||
...prev.captures, | ||
[cap.name]: cap.value, | ||
}, | ||
finalText: undefined, | ||
}; | ||
} | ||
return prev; | ||
}); | ||
}, | ||
}, | ||
) | ||
.then((out) => { | ||
opts.setSelf((prev) => { | ||
if ("captures" in prev) { | ||
return { | ||
captures: out.captured, | ||
finalText: out.text, | ||
}; | ||
} | ||
return prev; | ||
}); | ||
}); | ||
}, | ||
], | ||
}); | ||
|
||
return ( | ||
<> | ||
ERROR!!!!!! | ||
<pre>{JSON.stringify(msgs, null, 2)}</pre> | ||
</> | ||
const PlayStream: FC<{ | ||
backend: Backend; | ||
messages: MessageOfAuthor[]; | ||
samplingParams: SamplingParams; | ||
variables: NamedVariable[]; | ||
}> = ({ variables, backend, messages, samplingParams }) => { | ||
const generationAtom = useRecoilValueLoadable( | ||
generateAsyncAtom({ | ||
samplingParams, | ||
backend, | ||
messages, | ||
variables, | ||
}), | ||
); | ||
}; | ||
|
||
const useCaptures = (_backend: Backend, _editorState: EditorState) => { | ||
const captures: Record<string, string> = {}; | ||
console.log(generationAtom); | ||
|
||
return { | ||
captures, | ||
}; | ||
return <></>; | ||
}; | ||
|
||
export const Play: FC<{ | ||
backend: Backend; | ||
editorState: EditorState; | ||
}> = ({ editorState, backend }) => { | ||
const { captures } = useCaptures(backend, editorState); | ||
return ( | ||
<> | ||
<PlayStream editorState={editorState} captures={captures} /> | ||
</> | ||
); | ||
const msgs = getMessagesOfAuthor(editorState); | ||
if (msgs.tag === "success") { | ||
return ( | ||
<PlayStream | ||
backend={backend} | ||
messages={msgs.value} | ||
samplingParams={editorState.samplingParams} | ||
variables={editorState.variables} | ||
/> | ||
); | ||
} | ||
return <>TODO: error {JSON.stringify(msgs)}</>; | ||
}; |
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
Oops, something went wrong.