From 24974aa6a2d943308f0f00e902680d2782ab319f Mon Sep 17 00:00:00 2001 From: Akrem Abayed Date: Mon, 27 Nov 2023 14:21:49 +0100 Subject: [PATCH 1/5] format --- .../components/AppSelector/modals/CreateAppStatusModal.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agenta-web/src/components/AppSelector/modals/CreateAppStatusModal.tsx b/agenta-web/src/components/AppSelector/modals/CreateAppStatusModal.tsx index cb165d4b04..3a7d655407 100644 --- a/agenta-web/src/components/AppSelector/modals/CreateAppStatusModal.tsx +++ b/agenta-web/src/components/AppSelector/modals/CreateAppStatusModal.tsx @@ -196,8 +196,8 @@ const CreateAppStatusModal: React.FC> type === "success" ? "success" : type === "error" - ? "danger" - : "secondary" + ? "danger" + : "secondary" } strong={Object.keys(messages)[ix] === "success"} > From 710ad20f2cbc4b20b563aa7cc7bdbe13b92c53a8 Mon Sep 17 00:00:00 2001 From: Kaosiso Ezealigo Date: Mon, 27 Nov 2023 14:47:51 +0100 Subject: [PATCH 2/5] fixes cypress error --- agenta-web/cypress/support/commands/evaluations.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agenta-web/cypress/support/commands/evaluations.ts b/agenta-web/cypress/support/commands/evaluations.ts index 38c859310d..ed16f99c4c 100644 --- a/agenta-web/cypress/support/commands/evaluations.ts +++ b/agenta-web/cypress/support/commands/evaluations.ts @@ -103,8 +103,8 @@ Cypress.Commands.add("cleanupVariantAndTestset", () => { Cypress.Commands.add("addingOpenaiKey", () => { cy.visit("/settings") - cy.get('[data-cy="openai-api-input"]').type(apiKey) - cy.get('[data-cy="openai-api-save"]').click() + cy.get('[data-cy="openai-api-input"]').eq(0).type(apiKey) + cy.get('[data-cy="openai-api-save"]').eq(0).click() }) Cypress.Commands.add("removeOpenAiKey", () => { From 6bb2880e8d97816de8375defcbd800a6ec14c939 Mon Sep 17 00:00:00 2001 From: Kaosiso Ezealigo Date: Mon, 27 Nov 2023 16:15:50 +0100 Subject: [PATCH 3/5] adds removeLlmProviderKey func --- agenta-web/cypress/e2e/ai-critic-evaluation.cy.ts | 4 ++-- agenta-web/cypress/support/commands/evaluations.ts | 10 +++++----- agenta-web/cypress/support/commands/index.ts | 2 +- agenta-web/src/lib/helpers/utils.ts | 6 ++++++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/agenta-web/cypress/e2e/ai-critic-evaluation.cy.ts b/agenta-web/cypress/e2e/ai-critic-evaluation.cy.ts index 01f64f965c..f53b455fc4 100644 --- a/agenta-web/cypress/e2e/ai-critic-evaluation.cy.ts +++ b/agenta-web/cypress/e2e/ai-critic-evaluation.cy.ts @@ -14,7 +14,7 @@ describe("AI Critics Evaluation workflow", () => { context("When you select evaluation without an API key", () => { beforeEach(() => { cy.visit(`/apps/${app_id}/evaluations`) - cy.clearLocalStorage("openAiToken") + cy.clearLocalStorage("llmAvailableProvidersToken") cy.get('[data-cy="evaluation-error-modal"]').should("not.exist") cy.get('[data-cy="ai-critic-button"]').click() @@ -81,7 +81,7 @@ describe("AI Critics Evaluation workflow", () => { }) it("Should execute evaluation workflow with error", () => { - cy.clearLocalStorage("openAiToken") + cy.clearLocalStorage("llmAvailableProvidersToken") cy.wait(1000) cy.clickLinkAndWait('[data-cy="ai-critic-run-evaluation"]') diff --git a/agenta-web/cypress/support/commands/evaluations.ts b/agenta-web/cypress/support/commands/evaluations.ts index ed16f99c4c..8959fdf7d4 100644 --- a/agenta-web/cypress/support/commands/evaluations.ts +++ b/agenta-web/cypress/support/commands/evaluations.ts @@ -1,4 +1,4 @@ -import {randString, removeOpenAIKey} from "../../../src/lib/helpers/utils" +import {randString, removeLlmProviderKey} from "../../../src/lib/helpers/utils" let app_id @@ -51,7 +51,7 @@ Cypress.Commands.add("createVariant", () => { cy.wrap(app_id).as("app_id") }) cy.contains(/modify parameters/i) - cy.removeOpenAiKey() + cy.removeLlmProviderKey() }) Cypress.Commands.add("createVariantsAndTestsets", () => { @@ -98,7 +98,7 @@ Cypress.Commands.add("cleanupVariantAndTestset", () => { }, }) - cy.removeOpenAiKey() + cy.removeLlmProviderKey() }) Cypress.Commands.add("addingOpenaiKey", () => { @@ -107,6 +107,6 @@ Cypress.Commands.add("addingOpenaiKey", () => { cy.get('[data-cy="openai-api-save"]').eq(0).click() }) -Cypress.Commands.add("removeOpenAiKey", () => { - removeOpenAIKey() +Cypress.Commands.add("removeLlmProviderKey", () => { + removeLlmProviderKey() }) diff --git a/agenta-web/cypress/support/commands/index.ts b/agenta-web/cypress/support/commands/index.ts index 83e4041295..b1da828cac 100644 --- a/agenta-web/cypress/support/commands/index.ts +++ b/agenta-web/cypress/support/commands/index.ts @@ -11,7 +11,7 @@ declare global { cleanupVariantAndTestset(): Chainable createVariant(): Chainable saveOpenAiKey(): Chainable - removeOpenAiKey(): Chainable + removeLlmProviderKey(): Chainable addingOpenaiKey(): Chainable } } diff --git a/agenta-web/src/lib/helpers/utils.ts b/agenta-web/src/lib/helpers/utils.ts index 1b8c7782f8..a0c43b8b55 100644 --- a/agenta-web/src/lib/helpers/utils.ts +++ b/agenta-web/src/lib/helpers/utils.ts @@ -86,6 +86,12 @@ export const removeSingleLlmProviderKey = (providerIdx: number) => { } } +export const removeLlmProviderKey = () => { + if (typeof window !== "undefined") { + localStorage.removeItem(llmAvailableProvidersToken) + } +} + export const capitalize = (s: string) => { if (typeof s !== "string") return "" return s From 3fc6d16d56083520b75d3d5fbc6e9f93255a0742 Mon Sep 17 00:00:00 2001 From: Kaosiso Ezealigo Date: Mon, 27 Nov 2023 17:30:15 +0100 Subject: [PATCH 4/5] feat: Add function for retrieving api key --- .../components/AppSelector/AppSelector.tsx | 15 ++++++++----- .../AICritiqueEvaluationTable.tsx | 4 ++-- .../CustomCodeRunEvaluationTable.tsx | 4 ++-- .../components/Evaluations/Evaluations.tsx | 7 ++---- agenta-web/src/lib/Types.ts | 4 +++- agenta-web/src/lib/helpers/utils.ts | 22 +++++++++++++++++++ agenta-web/src/lib/services/api.ts | 14 +++--------- 7 files changed, 44 insertions(+), 26 deletions(-) diff --git a/agenta-web/src/components/AppSelector/AppSelector.tsx b/agenta-web/src/components/AppSelector/AppSelector.tsx index 0068dfaaa8..73886d75f4 100644 --- a/agenta-web/src/components/AppSelector/AppSelector.tsx +++ b/agenta-web/src/components/AppSelector/AppSelector.tsx @@ -9,7 +9,14 @@ import {useAppTheme} from "../Layout/ThemeContextProvider" import {CloseCircleFilled} from "@ant-design/icons" import TipsAndFeatures from "./TipsAndFeatures" import Welcome from "./Welcome" -import {getAllLlmProviderKeysAsEnvVariable, isAppNameInputValid, isDemo} from "@/lib/helpers/utils" +import { + getAllLlmProviderKeysAsEnvVariable, + getAllProviderLlmKeys, + getApikeys, + getLlmProviderKey, + isAppNameInputValid, + isDemo, +} from "@/lib/helpers/utils" import { createAndStartTemplate, getProfile, @@ -183,7 +190,7 @@ const AppSelector: React.FC = () => { // warn the user and redirect if openAI key is not present // TODO: must be changed for multiples LLM keys - const providerKeys = getAllLlmProviderKeysAsEnvVariable() + const providerKeys = getApikeys() if (!providerKeys && !isDemo()) { notification.error({ message: "OpenAI API Key Missing", @@ -202,9 +209,7 @@ const AppSelector: React.FC = () => { appName: newApp, templateId: template_id, orgId: selectedOrg?.id!, - env_vars: isDemo() - ? getAllLlmProviderKeysAsEnvVariable() - : (providerKeys as LlmProvidersKeys), + providerKey: isDemo() ? "" : (providerKeys as string), timeout, onStatusChange: (status, details, appId) => { setStatusData((prev) => ({status, details, appId: appId || prev.appId})) diff --git a/agenta-web/src/components/EvaluationTable/AICritiqueEvaluationTable.tsx b/agenta-web/src/components/EvaluationTable/AICritiqueEvaluationTable.tsx index d569000b6e..dc5ba4f4e0 100644 --- a/agenta-web/src/components/EvaluationTable/AICritiqueEvaluationTable.tsx +++ b/agenta-web/src/components/EvaluationTable/AICritiqueEvaluationTable.tsx @@ -26,7 +26,7 @@ import { import {useVariants} from "@/lib/hooks/useVariant" import {useRouter} from "next/router" import {EvaluationFlow, EvaluationType} from "@/lib/enums" -import {getLlmProviderKey} from "@/lib/helpers/utils" +import {getApikeys, getLlmProviderKey} from "@/lib/helpers/utils" import {createUseStyles} from "react-jss" import {exportAICritiqueEvaluationData} from "@/lib/helpers/evaluate" import SecondaryButton from "../SecondaryButton/SecondaryButton" @@ -279,7 +279,7 @@ Answer ONLY with one of the given grading or evaluation options. inputs: rows[rowNumber].inputs, outputs: data.outputs, evaluation_prompt_template: evaluationPromptTemplate, - open_ai_key: getLlmProviderKey("OpenAI"), + open_ai_key: getApikeys(), }) try { diff --git a/agenta-web/src/components/EvaluationTable/CustomCodeRunEvaluationTable.tsx b/agenta-web/src/components/EvaluationTable/CustomCodeRunEvaluationTable.tsx index 8483d479fe..76b40dccbb 100644 --- a/agenta-web/src/components/EvaluationTable/CustomCodeRunEvaluationTable.tsx +++ b/agenta-web/src/components/EvaluationTable/CustomCodeRunEvaluationTable.tsx @@ -30,7 +30,7 @@ import { import {useVariants} from "@/lib/hooks/useVariant" import {useRouter} from "next/router" import {EvaluationFlow, EvaluationType} from "@/lib/enums" -import {getLlmProviderKey} from "@/lib/helpers/utils" +import {getApikeys, getLlmProviderKey} from "@/lib/helpers/utils" import {createUseStyles} from "react-jss" import SecondaryButton from "../SecondaryButton/SecondaryButton" import {exportCustomCodeEvaluationData} from "@/lib/helpers/evaluate" @@ -292,7 +292,7 @@ const CustomCodeRunEvaluationTable: React.FC = ( outputs: [{variant_id: variants[0].variantId, variant_output: outputVariantX}], inputs: rows[rowNumber].inputs, correct_answer: correctAnswer(rows[rowNumber].inputs), - open_ai_key: getLlmProviderKey("OpenAI"), + open_ai_key: getApikeys(), } try { diff --git a/agenta-web/src/components/Evaluations/Evaluations.tsx b/agenta-web/src/components/Evaluations/Evaluations.tsx index b3d4b69764..3e35b0d003 100644 --- a/agenta-web/src/components/Evaluations/Evaluations.tsx +++ b/agenta-web/src/components/Evaluations/Evaluations.tsx @@ -20,7 +20,7 @@ import { useLoadTestsetsList, fetchCustomEvaluations, } from "@/lib/services/api" -import {dynamicComponent, getLlmProviderKey, isDemo} from "@/lib/helpers/utils" +import {dynamicComponent, getApikeys, getLlmProviderKey, isDemo} from "@/lib/helpers/utils" import {useRouter} from "next/router" import {Variant, Parameter, GenericObject, SingleCustomEvaluation} from "@/lib/Types" import {EvaluationType} from "@/lib/enums" @@ -337,10 +337,7 @@ export default function Evaluations() { } else if (selectedTestset?.name === "Select a Test set") { message.error("Please select a testset") return - } else if ( - !getLlmProviderKey("OpenAI") && - selectedEvaluationType === EvaluationType.auto_ai_critique - ) { + } else if (!getApikeys() && selectedEvaluationType === EvaluationType.auto_ai_critique) { setError({ message: "In order to run an AI Critique evaluation, please set your OpenAI API key in the API Keys page.", diff --git a/agenta-web/src/lib/Types.ts b/agenta-web/src/lib/Types.ts index 9eff5f17e0..7cbcfd0447 100644 --- a/agenta-web/src/lib/Types.ts +++ b/agenta-web/src/lib/Types.ts @@ -229,7 +229,9 @@ export interface LlmProvidersKeys { export interface AppTemplate { app_name: string template_id: string - env_vars?: LlmProvidersKeys + env_vars?: { + OPENAI_API_KEY: string | null + } organization_id?: string } diff --git a/agenta-web/src/lib/helpers/utils.ts b/agenta-web/src/lib/helpers/utils.ts index a0c43b8b55..918952fd66 100644 --- a/agenta-web/src/lib/helpers/utils.ts +++ b/agenta-web/src/lib/helpers/utils.ts @@ -53,6 +53,28 @@ export const EvaluationTypeLabels: Record = { [EvaluationType.auto_webhook_test]: "Webhook Test", } +export const getApikeys = () => { + if (typeof window !== "undefined") { + const llmAvailableProvidersTokenString = localStorage.getItem(llmAvailableProvidersToken) + + if (llmAvailableProvidersTokenString !== null) { + const llmAvailableProvidersTokenArray = JSON.parse(llmAvailableProvidersTokenString) + + if ( + Array.isArray(llmAvailableProvidersTokenArray) && + llmAvailableProvidersTokenArray.length > 0 + ) { + for (let i = 0; i < llmAvailableProvidersTokenArray.length; i++) { + if (llmAvailableProvidersTokenArray[i].key !== "") { + return llmAvailableProvidersTokenArray[i].key + } + } + } + } + return "" + } +} + export const saveLlmProviderKey = (providerIdx: number, keyValue: string) => { if (typeof window !== "undefined") { // TODO: add encryption here diff --git a/agenta-web/src/lib/services/api.ts b/agenta-web/src/lib/services/api.ts index 11fd40e8a9..29f6f866ad 100644 --- a/agenta-web/src/lib/services/api.ts +++ b/agenta-web/src/lib/services/api.ts @@ -614,16 +614,14 @@ export const waitForAppToStart = async ({ export const createAndStartTemplate = async ({ appName, - env_vars, + providerKey, templateId, orgId, timeout, onStatusChange, }: { appName: string - env_vars: { - [key in keyof LlmProvidersKeys]: {title: string; key: string} - } + providerKey: string templateId: string orgId: string timeout?: number @@ -642,13 +640,7 @@ export const createAndStartTemplate = async ({ app_name: appName, template_id: templateId, env_vars: { - OPENAI_API_KEY: env_vars.OPENAI_API_KEY.key, - COHERE_API_KEY: env_vars.COHERE_API_KEY.key, - ANTHROPIC_API_KEY: env_vars.ANTHROPIC_API_KEY.key, - AZURE_API_KEY: env_vars.AZURE_API_KEY.key, - REPLICATE_API_KEY: env_vars.REPLICATE_API_KEY.key, - TOGETHERAI_API_KEY: env_vars.TOGETHERAI_API_KEY.key, - HUGGING_FACE_API_KEY: env_vars.HUGGING_FACE_API_KEY.key, + OPENAI_API_KEY: providerKey, }, organization_id: orgId, }, From 3841e706b9b8d0428bf0e222b4a99eeb68b58383 Mon Sep 17 00:00:00 2001 From: Kaosiso Ezealigo Date: Mon, 27 Nov 2023 17:31:37 +0100 Subject: [PATCH 5/5] removes unused imports --- agenta-web/src/components/AppSelector/AppSelector.tsx | 11 ++--------- .../EvaluationTable/AICritiqueEvaluationTable.tsx | 2 +- .../EvaluationTable/CustomCodeRunEvaluationTable.tsx | 2 +- agenta-web/src/components/Evaluations/Evaluations.tsx | 2 +- agenta-web/src/lib/services/api.ts | 2 -- 5 files changed, 5 insertions(+), 14 deletions(-) diff --git a/agenta-web/src/components/AppSelector/AppSelector.tsx b/agenta-web/src/components/AppSelector/AppSelector.tsx index 73886d75f4..e006780947 100644 --- a/agenta-web/src/components/AppSelector/AppSelector.tsx +++ b/agenta-web/src/components/AppSelector/AppSelector.tsx @@ -4,19 +4,12 @@ import {usePostHog} from "posthog-js/react" import {PlusOutlined} from "@ant-design/icons" import {Input, Modal, ConfigProvider, theme, Spin, Card, Button, notification, Divider} from "antd" import AppCard from "./AppCard" -import {Template, GenericObject, LlmProvidersKeys} from "@/lib/Types" +import {Template, GenericObject} from "@/lib/Types" import {useAppTheme} from "../Layout/ThemeContextProvider" import {CloseCircleFilled} from "@ant-design/icons" import TipsAndFeatures from "./TipsAndFeatures" import Welcome from "./Welcome" -import { - getAllLlmProviderKeysAsEnvVariable, - getAllProviderLlmKeys, - getApikeys, - getLlmProviderKey, - isAppNameInputValid, - isDemo, -} from "@/lib/helpers/utils" +import {getApikeys, isAppNameInputValid, isDemo} from "@/lib/helpers/utils" import { createAndStartTemplate, getProfile, diff --git a/agenta-web/src/components/EvaluationTable/AICritiqueEvaluationTable.tsx b/agenta-web/src/components/EvaluationTable/AICritiqueEvaluationTable.tsx index dc5ba4f4e0..950a028895 100644 --- a/agenta-web/src/components/EvaluationTable/AICritiqueEvaluationTable.tsx +++ b/agenta-web/src/components/EvaluationTable/AICritiqueEvaluationTable.tsx @@ -26,7 +26,7 @@ import { import {useVariants} from "@/lib/hooks/useVariant" import {useRouter} from "next/router" import {EvaluationFlow, EvaluationType} from "@/lib/enums" -import {getApikeys, getLlmProviderKey} from "@/lib/helpers/utils" +import {getApikeys} from "@/lib/helpers/utils" import {createUseStyles} from "react-jss" import {exportAICritiqueEvaluationData} from "@/lib/helpers/evaluate" import SecondaryButton from "../SecondaryButton/SecondaryButton" diff --git a/agenta-web/src/components/EvaluationTable/CustomCodeRunEvaluationTable.tsx b/agenta-web/src/components/EvaluationTable/CustomCodeRunEvaluationTable.tsx index 76b40dccbb..51bff29f03 100644 --- a/agenta-web/src/components/EvaluationTable/CustomCodeRunEvaluationTable.tsx +++ b/agenta-web/src/components/EvaluationTable/CustomCodeRunEvaluationTable.tsx @@ -30,7 +30,7 @@ import { import {useVariants} from "@/lib/hooks/useVariant" import {useRouter} from "next/router" import {EvaluationFlow, EvaluationType} from "@/lib/enums" -import {getApikeys, getLlmProviderKey} from "@/lib/helpers/utils" +import {getApikeys} from "@/lib/helpers/utils" import {createUseStyles} from "react-jss" import SecondaryButton from "../SecondaryButton/SecondaryButton" import {exportCustomCodeEvaluationData} from "@/lib/helpers/evaluate" diff --git a/agenta-web/src/components/Evaluations/Evaluations.tsx b/agenta-web/src/components/Evaluations/Evaluations.tsx index 3e35b0d003..0d250353b9 100644 --- a/agenta-web/src/components/Evaluations/Evaluations.tsx +++ b/agenta-web/src/components/Evaluations/Evaluations.tsx @@ -20,7 +20,7 @@ import { useLoadTestsetsList, fetchCustomEvaluations, } from "@/lib/services/api" -import {dynamicComponent, getApikeys, getLlmProviderKey, isDemo} from "@/lib/helpers/utils" +import {dynamicComponent, getApikeys, isDemo} from "@/lib/helpers/utils" import {useRouter} from "next/router" import {Variant, Parameter, GenericObject, SingleCustomEvaluation} from "@/lib/Types" import {EvaluationType} from "@/lib/enums" diff --git a/agenta-web/src/lib/services/api.ts b/agenta-web/src/lib/services/api.ts index 29f6f866ad..c95dcc996a 100644 --- a/agenta-web/src/lib/services/api.ts +++ b/agenta-web/src/lib/services/api.ts @@ -8,13 +8,11 @@ import { Evaluation, AppTemplate, GenericObject, - TemplateImage, Environment, CreateCustomEvaluation, ExecuteCustomEvalCode, ListAppsItem, AICritiqueCreate, - LlmProvidersKeys, } from "@/lib/Types" import { fromEvaluationResponseToEvaluation,