-
Notifications
You must be signed in to change notification settings - Fork 285
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
b15f2a4
commit 2d394a3
Showing
13 changed files
with
730 additions
and
19 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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from "react"; | ||
import { json, jsonLanguage, jsonParseLinter } from "@codemirror/lang-json"; | ||
import { linter } from "@codemirror/lint"; | ||
import { EditorView, hoverTooltip } from "@codemirror/view"; | ||
import { nord } from "@uiw/codemirror-theme-nord"; | ||
import CodeMirror, { ReactCodeMirrorProps } from "@uiw/react-codemirror"; | ||
import { | ||
handleRefresh, | ||
jsonCompletion, | ||
jsonSchemaHover, | ||
jsonSchemaLinter, | ||
stateExtensions, | ||
} from "codemirror-json-schema"; | ||
|
||
import { useTheme } from "@phoenix/contexts"; | ||
import { toolJSONSchema } from "@phoenix/schemas/toolSchema"; | ||
|
||
export type JSONToolEditorProps = Omit< | ||
ReactCodeMirrorProps, | ||
"theme" | "extensions" | "editable" | ||
>; | ||
|
||
export function JSONToolEditor(props: JSONToolEditorProps) { | ||
const { theme } = useTheme(); | ||
const codeMirrorTheme = theme === "light" ? undefined : nord; | ||
return ( | ||
<CodeMirror | ||
value={props.value} | ||
extensions={[ | ||
json(), | ||
EditorView.lineWrapping, | ||
linter(jsonParseLinter()), | ||
linter(jsonSchemaLinter(), { needsRefresh: handleRefresh }), | ||
jsonLanguage.data.of({ | ||
autocomplete: jsonCompletion(), | ||
}), | ||
hoverTooltip(jsonSchemaHover()), | ||
stateExtensions(toolJSONSchema as JSONSchema7), | ||
]} | ||
editable | ||
theme={codeMirrorTheme} | ||
{...props} | ||
/> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,26 +1,85 @@ | ||
import React from "react"; | ||
|
||
import { Card } from "@arizeai/components"; | ||
import { Button, Card, Flex, Icon, Icons } from "@arizeai/components"; | ||
|
||
import { JSONToolEditor } from "@phoenix/components/code"; | ||
import { usePlaygroundContext } from "@phoenix/contexts/PlaygroundContext"; | ||
|
||
import { TitleWithAlphabeticIndex } from "./TitleWithAlphabeticIndex"; | ||
import { PlaygroundInstanceProps } from "./types"; | ||
|
||
interface PlaygroundToolsProps extends PlaygroundInstanceProps {} | ||
|
||
export function PlaygroundTools(props: PlaygroundToolsProps) { | ||
const index = usePlaygroundContext((state) => | ||
state.instances.findIndex( | ||
const instanceId = props.playgroundInstanceId; | ||
const instance = usePlaygroundContext((state) => | ||
state.instances.find( | ||
(instance) => instance.id === props.playgroundInstanceId | ||
) | ||
); | ||
if (instance == null) { | ||
throw new Error(`Playground instance ${instanceId} not found`); | ||
} | ||
const updateInstance = usePlaygroundContext((state) => state.updateInstance); | ||
if (instance.tools == null) { | ||
throw new Error(`Playground instance ${instanceId} does not have tools`); | ||
} | ||
const index = usePlaygroundContext((state) => | ||
state.instances.findIndex((instance) => instance.id === instanceId) | ||
); | ||
|
||
return ( | ||
<Card | ||
title={<TitleWithAlphabeticIndex index={index} title="Tools" />} | ||
collapsible | ||
variant="compact" | ||
> | ||
Tools go here | ||
<Flex direction="column" gap="size-50"> | ||
{instance.tools.map((tool) => ( | ||
<Card | ||
collapsible | ||
variant="compact" | ||
key={tool.id} | ||
title={tool.definition.function.name} | ||
bodyStyle={{ padding: 0 }} | ||
extra={ | ||
<Button | ||
aria-label="Delete tool" | ||
icon={<Icon svg={<Icons.TrashOutline />} />} | ||
variant="default" | ||
size="compact" | ||
onClick={() => { | ||
updateInstance({ | ||
instanceId, | ||
patch: { | ||
tools: instance.tools.filter((t) => t.id !== tool.id), | ||
}, | ||
}); | ||
}} | ||
/> | ||
} | ||
> | ||
<JSONToolEditor | ||
value={JSON.stringify(tool.definition, null, 2)} | ||
onChange={(value) => { | ||
updateInstance({ | ||
instanceId, | ||
patch: { | ||
tools: instance.tools.map((t) => | ||
t.id === tool.id | ||
? { | ||
...t, | ||
definition: JSON.parse(value), | ||
} | ||
: t | ||
), | ||
}, | ||
}); | ||
}} | ||
/> | ||
</Card> | ||
))} | ||
</Flex> | ||
</Card> | ||
); | ||
} |
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 @@ | ||
export * from "./toolSchema"; |
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,70 @@ | ||
import { z } from "zod"; | ||
import zodToJsonSchema from "zod-to-json-schema"; | ||
|
||
/** | ||
* The schema for a tool definition | ||
* @see https://platform.openai.com/docs/guides/structured-outputs/supported-schemas | ||
*/ | ||
export const toolSchema = z.object({ | ||
type: z.literal("function"), | ||
function: z.object({ | ||
name: z.string().describe("The name of the function"), | ||
description: z | ||
.string() | ||
.optional() | ||
.describe("A description of the function"), | ||
parameters: z | ||
.object({ | ||
type: z.literal("object"), | ||
properties: z | ||
.record( | ||
z.object({ | ||
type: z | ||
.enum([ | ||
"string", | ||
"number", | ||
"boolean", | ||
"object", | ||
"array", | ||
"null", | ||
"integer", | ||
]) | ||
.describe("The type of the parameter"), | ||
description: z | ||
.string() | ||
.optional() | ||
.describe("A description of the parameter"), | ||
enum: z | ||
.array(z.string()) | ||
.optional() | ||
.describe("The allowed values"), | ||
}) | ||
) | ||
.describe("A map of parameter names to their definitions"), | ||
required: z | ||
.array(z.string()) | ||
.optional() | ||
.describe("The required parameters"), | ||
additionalProperties: z | ||
.boolean() | ||
.optional() | ||
.describe("Whether additional properties are allowed"), | ||
strict: z | ||
.boolean() | ||
.optional() | ||
.describe("Whether the object should be strict"), | ||
}) | ||
.describe("The parameters that the function accepts"), | ||
}), | ||
}); | ||
|
||
/** | ||
* The type of a tool definition | ||
* @see https://platform.openai.com/docs/guides/structured-outputs/supported-schemas | ||
*/ | ||
export type ToolDefinition = z.infer<typeof toolSchema>; | ||
|
||
/** | ||
* The JSON schema for a tool definition | ||
*/ | ||
export const toolJSONSchema = zodToJsonSchema(toolSchema); |
Oops, something went wrong.