Skip to content

Commit

Permalink
refactor: improve code readability and consistency across components
Browse files Browse the repository at this point in the history
  • Loading branch information
actualwitch committed Dec 16, 2024
1 parent 4aa45e9 commit f735a6b
Show file tree
Hide file tree
Showing 25 changed files with 374 additions and 78 deletions.
Binary file removed .github/Screenshot.png
Binary file not shown.
Binary file added .github/screenshots/home-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/screenshots/home-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Playwright Tests
# on:
on:
workflow_dispatch:
# push:
# branches: [ main ]
# pull_request:
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[<img alt="Experiment screenshot" src="https://raw.githubusercontent.com/actualwitch/experiment/main/.github/Screenshot.png" />](https://actualwitch.github.io/experiment/)
<a href="https://actualwitch.github.io/experiment/">
<picture>
<source media="(prefers-color-scheme: dark)" srcset=".github/screenshots/home-dark.png">
<img alt="Experiment screenshot" src=".github/screenshots/home-light.png">
</picture>
</a>

## 🔬 Let's start the <ins>Experiment</ins>

Expand All @@ -14,4 +19,4 @@ Shall you want to run this repo locally, you can do so by running `dev` script f

```sh
bun dev
```
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dev:spa": "REALM=spa bun --watch run src/entry/server.spa.tsx",
"format": "bunx biome format --write ./src/*",
"format:prettier": "prettier ./src/* --write",
"lint": "bunx biome lint --write .",
"test": "bunx playwright test",
"playwright:update": "bunx playwright install"
},
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
Tool,
} from "@anthropic-ai/sdk/resources/index.mjs";
import type { Message } from "../state/common";
import { makeRequestTool } from "../state/inference";
import type { makeRequestTool } from "../state/inference";

export const experimentToAnthropic = (experiment: Message[]): MessageCreateParams | MessageCreateParamsNonStreaming => {
let system = "";
Expand All @@ -24,7 +24,7 @@ export const experimentToAnthropic = (experiment: Message[]): MessageCreateParam
}
}
if (role === "tool") {
let thisTool = content;
const thisTool = content;

if (typeof thisTool === "object") {
const tool = thisTool as typeof makeRequestTool;
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/mistral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const experimentToMistral = (experiment: Message[]): ChatCompletionStream
const tools: ChatCompletionStreamRequest["tools"] = [];
for (const { role, content } of experiment) {
if (role === "tool") {
let thisTool = content;
const thisTool = content;

if (typeof thisTool === "object") {
tools.push(thisTool as any);
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const experimentToOpenai = (experiment: Message[]): ChatCompletionCreateP
const tools: ChatCompletionTool[] = [];
for (const { role, content } of experiment) {
if (role === "tool") {
let thisTool = content;
const thisTool = content;

if (typeof thisTool === "object") {
tools.push(thisTool as any);
Expand Down
5 changes: 2 additions & 3 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ export const ForkButton = ({ experiment, parent }: { experiment?: Message[]; par
return (
<Button
type="submit"
onClick={(e) => {
onClick={() => {
if (!experiment) return;
e.preventDefault();
setExoeriment(experiment);
if (parent) setParent(parent);
navigate("/");
}}
>
fork experiment
Fork Experiment
</Button>
);
};
4 changes: 2 additions & 2 deletions src/components/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ function asTreeNodes(
function* asTextTreeNodes(input: string) {
const stack = [];
let buffer = "";
let currentBlock = "p";
let level = 0;
const currentBlock = "p";
const level = 0;
let index = 0;
for (const char of input) {
if (char === "\n" && buffer) {
Expand Down
179 changes: 179 additions & 0 deletions src/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,182 @@ export const FIXTURES = {
export function isFixture(value: unknown): value is keyof typeof FIXTURES {
return value ? typeof value === "string" && Object.keys(FIXTURES).includes(value) : false;
}
const openaiEventStream = [
{
index: 0,
delta: {
role: "assistant",
content: null,
tool_calls: [
{
index: 0,
id: "call_psZOuVcEAR1Qqvdx3nl1nq7I",
type: "function",
function: {
name: "bio",
arguments: "",
},
},
],
refusal: null,
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: '{"',
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: "memory",
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: '":"',
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: "User",
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: "'s",
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: " name",
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: " is",
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: " Ade",
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: '."',
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
function: {
arguments: "}",
},
},
],
},
logprobs: null,
finish_reason: null,
},
{
index: 0,
delta: {},
logprobs: null,
finish_reason: "stop",
},
];
2 changes: 1 addition & 1 deletion src/pages/Import.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default function () {
navigator.clipboard.writeText(JSON.stringify(chat));
}}
>
Copy as json
Copy JSON
</button>
</div>
)}
Expand Down
49 changes: 28 additions & 21 deletions src/pages/NewExperiment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ChatPreview, selectionAtom } from "../components/chat";
import { ExperimentsSidebar } from "../sidebars/experiments";
import { type Message, type Role, experimentAtom, isDarkModeAtom, parentAtom, templatesAtom } from "../state/common";
import {
availableProvidersAtom,
availableProviderOptionsAtom,
isRunningAtom,
modelAtom,
modelOptions,
Expand Down Expand Up @@ -127,8 +127,7 @@ export default function NewExperiment() {
const [role, setRole] = useState<Role>("user");
const isRunning = useAtomValue(isRunningAtom);

const [tokenProviders] = useAtom(availableProvidersAtom);
const providerOptions = withIds(tokenProviders);
const [providerOptions] = useAtom(availableProviderOptionsAtom);
const [provider, setProvider] = useAtom(selectedProviderAtom);

useEffect(() => {
Expand Down Expand Up @@ -281,7 +280,7 @@ export default function NewExperiment() {
</Column>
<Sidebar>
<h3>Actions</h3>
{tokenProviders.length > 1 && (
{providerOptions.length > 1 && (
<Select
label="Provider"
items={providerOptions}
Expand Down Expand Up @@ -322,48 +321,56 @@ export default function NewExperiment() {
formatOptions={{ minimumFractionDigits: 2 }}
/>
)}
<Button
type="submit"
disabled={isRunning || experiment.length === 0}
onClick={(e) => {
e.preventDefault();
runExperiment();
}}
>
start experiment
</Button>
<div>
<Button
type="submit"
disabled={isRunning || experiment.length === 0}
onClick={() => {
runExperiment();
}}
>
Start Experiment
</Button>

<Button
type="submit"
onClick={() => {
setExperiment([]);
}}
disabled={isRunning || experiment.length === 0}
>
Reset
</Button>
</div>
{selection !== null && selection.length === 1 && (
<>
<h4>This message</h4>
<div>
<Button
type="submit"
onClick={(e) => {
e.preventDefault();
setSelection([selection[0], "content"]);
}}
>
edit
Edit
</Button>
<Button
type="submit"
onClick={(e) => {
e.preventDefault();
deleteSelection();
}}
>
delete
Delete
</Button>
<Button
type="submit"
onClick={async (e) => {
e.preventDefault();
onClick={async () => {
const name = prompt("Name of the template");
if (!name) return;
setTemplates({ ...templates, [name]: experiment[selection[0]] });
}}
>
template
Template
</Button>
</div>
</>
Expand Down
Loading

0 comments on commit f735a6b

Please sign in to comment.