-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(playground): add credential storage (#4970)
* feat(playground): add credential storage * organize * reorganize store files * add credentials dropdown * remove unused icon * make credentials inputs unique per provider * update unique logic for providers * migrate to instance.model.provider from instance.provider * move provider in tests
- Loading branch information
1 parent
e20716f
commit b15f2a4
Showing
18 changed files
with
373 additions
and
234 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
declare type ModelProvider = "OPENAI" | "AZURE_OPENAI" | "ANTHROPIC"; | ||
|
||
/** | ||
* The role of a chat message | ||
*/ | ||
declare type ChatMessageRole = "user" | "system" | "ai" | "tool"; |
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,35 @@ | ||
import React, { createContext, PropsWithChildren, useState } from "react"; | ||
import { useZustand } from "use-zustand"; | ||
|
||
import { | ||
createCredentialsStore, | ||
CredentialsProps, | ||
CredentialsState, | ||
CredentialsStore, | ||
} from "@phoenix/store"; | ||
|
||
export const CredentialsContext = createContext<CredentialsStore | null>(null); | ||
|
||
export function CredentialsProvider({ | ||
children, | ||
...props | ||
}: PropsWithChildren<Partial<CredentialsProps>>) { | ||
const [store] = useState<CredentialsStore>(() => | ||
createCredentialsStore(props) | ||
); | ||
return ( | ||
<CredentialsContext.Provider value={store}> | ||
{children} | ||
</CredentialsContext.Provider> | ||
); | ||
} | ||
|
||
export function useCredentialsContext<T>( | ||
selector: (state: CredentialsState) => T, | ||
equalityFn?: (left: T, right: T) => boolean | ||
): T { | ||
const store = React.useContext(CredentialsContext); | ||
if (!store) | ||
throw new Error("Missing CredentialsContext.Provider in the tree"); | ||
return useZustand(store, selector, equalityFn); | ||
} |
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
69 changes: 69 additions & 0 deletions
69
app/src/pages/playground/PlaygroundCredentialsDropdown.tsx
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,69 @@ | ||
import React from "react"; | ||
|
||
import { | ||
DropdownButton, | ||
DropdownMenu, | ||
DropdownTrigger, | ||
Flex, | ||
Form, | ||
Heading, | ||
Text, | ||
TextField, | ||
View, | ||
} from "@arizeai/components"; | ||
|
||
import { useCredentialsContext } from "@phoenix/contexts/CredentialsContext"; | ||
import { usePlaygroundContext } from "@phoenix/contexts/PlaygroundContext"; | ||
import { CredentialKey } from "@phoenix/store"; | ||
|
||
export const ProviderToCredentialKeyMap: Record<ModelProvider, CredentialKey> = | ||
{ | ||
OPENAI: "OPENAI_API_KEY", | ||
ANTHROPIC: "ANTHROPIC_API_KEY", | ||
AZURE_OPENAI: "AZURE_OPENAI_API_KEY", | ||
}; | ||
|
||
export function PlaygroundCredentialsDropdown() { | ||
const currentProviders = usePlaygroundContext((state) => | ||
Array.from( | ||
new Set(state.instances.map((instance) => instance.model.provider)) | ||
) | ||
); | ||
const setCredential = useCredentialsContext((state) => state.setCredential); | ||
const credentials = useCredentialsContext((state) => state); | ||
return ( | ||
<DropdownTrigger placement="bottom"> | ||
<DropdownButton size="compact">API Keys</DropdownButton> | ||
<DropdownMenu> | ||
<View padding="size-200"> | ||
<Flex direction={"column"} gap={"size-100"}> | ||
<Heading level={2} weight="heavy"> | ||
API Keys | ||
</Heading> | ||
<Text color="white70"> | ||
API keys are stored in your browser and used to communicate with | ||
their respective API's. | ||
</Text> | ||
<Form> | ||
{currentProviders.map((provider) => { | ||
const credentialKey = ProviderToCredentialKeyMap[provider]; | ||
return ( | ||
<TextField | ||
key={provider} | ||
label={credentialKey} | ||
type="password" | ||
isRequired | ||
onChange={(value) => { | ||
setCredential({ credential: credentialKey, value }); | ||
}} | ||
value={credentials[credentialKey]} | ||
/> | ||
); | ||
})} | ||
</Form> | ||
</Flex> | ||
</View> | ||
</DropdownMenu> | ||
</DropdownTrigger> | ||
); | ||
} |
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,49 @@ | ||
import { create, StateCreator } from "zustand"; | ||
import { devtools, persist } from "zustand/middleware"; | ||
|
||
export interface CredentialsProps { | ||
/** | ||
* The API key for the OpenAI API. | ||
*/ | ||
OPENAI_API_KEY?: string; | ||
/** | ||
* The API key for the Azure OpenAI API. | ||
*/ | ||
AZURE_OPENAI_API_KEY?: string; | ||
/** | ||
* The API key for the Anthropic API. | ||
*/ | ||
ANTHROPIC_API_KEY?: string; | ||
} | ||
|
||
export type CredentialKey = keyof CredentialsProps; | ||
|
||
export interface CredentialsState extends CredentialsProps { | ||
/** | ||
* Setter for a given credential | ||
* @param credential the name of the credential to set | ||
* @param value the value of the credential | ||
*/ | ||
setCredential: (params: { | ||
credential: keyof CredentialsProps; | ||
value: string; | ||
}) => void; | ||
} | ||
|
||
export const createCredentialsStore = ( | ||
initialProps: Partial<CredentialsProps> | ||
) => { | ||
const credentialsStore: StateCreator<CredentialsState> = (set) => ({ | ||
setCredential: ({ credential, value }) => { | ||
set({ [credential]: value }); | ||
}, | ||
...initialProps, | ||
}); | ||
return create<CredentialsState>()( | ||
persist(devtools(credentialsStore), { | ||
name: "arize-phoenix-credentials", | ||
}) | ||
); | ||
}; | ||
|
||
export type CredentialsStore = ReturnType<typeof createCredentialsStore>; |
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,3 +1,4 @@ | ||
export * from "./pointCloudStore"; | ||
export * from "./tracingStore"; | ||
export * from "./playgroundStore"; | ||
export * from "./playground"; | ||
export * from "./credentialsStore"; |
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,2 @@ | ||
export * from "./playgroundStore"; | ||
export * from "./types"; |
Oops, something went wrong.