From 03978e7d7a452f59f3965093aaee7b57f8b3b8ff Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 13 Nov 2024 19:30:55 +0000 Subject: [PATCH] filter errors out of info env --- .../content/docs/reference/cli/commands.md | 1 + packages/cli/src/cli.ts | 1 + packages/cli/src/info.ts | 18 ++-- packages/core/src/connection.ts | 84 +++++++++---------- packages/core/src/constants.ts | 26 +++--- 5 files changed, 65 insertions(+), 65 deletions(-) diff --git a/docs/src/content/docs/reference/cli/commands.md b/docs/src/content/docs/reference/cli/commands.md index d0c7c4ea4a..5aa61c687e 100644 --- a/docs/src/content/docs/reference/cli/commands.md +++ b/docs/src/content/docs/reference/cli/commands.md @@ -492,5 +492,6 @@ Show .env information Options: -t, --token show token + -e, --error show errors -h, --help display help for command ``` diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 0c280ef597..5c083b4a8c 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -385,6 +385,7 @@ export async function cli() { .description("Show .env information") .arguments("[provider]") .option("-t, --token", "show token") + .option("-e, --error", "show errors") .action(envInfo) // Action to show environment information program.parse() // Parse command-line arguments } diff --git a/packages/cli/src/info.ts b/packages/cli/src/info.ts index ef37408711..6c849ebae3 100644 --- a/packages/cli/src/info.ts +++ b/packages/cli/src/info.ts @@ -32,8 +32,11 @@ export async function systemInfo() { * @param provider - The specific provider to filter by (optional). * @param options - Configuration options, including whether to show tokens. */ -export async function envInfo(provider: string, options?: { token?: boolean }) { - const { token } = options || {} +export async function envInfo( + provider: string, + options?: { token?: boolean; error?: boolean } +) { + const { token, error } = options || {} const res: any = {} res[".env"] = host.dotEnvPath ?? "" res.providers = [] @@ -52,11 +55,12 @@ export async function envInfo(provider: string, options?: { token?: boolean }) { res.providers.push(conn) } } catch (e) { - // Capture and store any errors encountered - res.providers.push({ - provider: modelProvider.id, - error: errorMessage(e), - }) + if (error) + // Capture and store any errors encountered + res.providers.push({ + provider: modelProvider.id, + error: errorMessage(e), + }) } } console.log(YAMLStringify(res)) diff --git a/packages/core/src/connection.ts b/packages/core/src/connection.ts index 1b9c07f0cc..65e6063313 100644 --- a/packages/core/src/connection.ts +++ b/packages/core/src/connection.ts @@ -87,54 +87,46 @@ export async function parseTokenFromEnv( const BASE_SUFFIX = ["_API_BASE", "_API_ENDPOINT", "_BASE", "_ENDPOINT"] if (provider === MODEL_PROVIDER_OPENAI) { - if (env.OPENAI_API_KEY || env.OPENAI_API_BASE || env.OPENAI_API_TYPE) { - const token = env.OPENAI_API_KEY ?? "" - let base = env.OPENAI_API_BASE - let type = (env.OPENAI_API_TYPE as OpenAIAPIType) || "openai" - const version = env.OPENAI_API_VERSION - if ( - type !== "azure" && - type !== "openai" && - type !== "localai" && - type !== "azure_serverless" && - type !== "azure_serverless_models" + const token = env.OPENAI_API_KEY ?? "" + let base = env.OPENAI_API_BASE + let type = (env.OPENAI_API_TYPE as OpenAIAPIType) || "openai" + const version = env.OPENAI_API_VERSION + if ( + type !== "azure" && + type !== "openai" && + type !== "localai" && + type !== "azure_serverless" && + type !== "azure_serverless_models" + ) + throw new Error( + "OPENAI_API_TYPE must be 'azure', 'azure_serverless', 'azure_serverless_models' or 'openai' or 'localai'" ) - throw new Error( - "OPENAI_API_TYPE must be 'azure', 'azure_serverless', 'azure_serverless_models' or 'openai' or 'localai'" - ) - if (type === "openai" && !base) base = OPENAI_API_BASE - if (type === "localai" && !base) base = LOCALAI_API_BASE - if ((type === "azure" || type === "azure_serverless") && !base) - throw new Error( - "OPENAI_API_BASE must be set when type is 'azure'" - ) - if (type === "azure") base = cleanAzureBase(base) - if ( - type === "azure" && - version && - version !== AZURE_OPENAI_API_VERSION + if (type === "openai" && !base) base = OPENAI_API_BASE + if (type === "localai" && !base) base = LOCALAI_API_BASE + if ((type === "azure" || type === "azure_serverless") && !base) + throw new Error("OPENAI_API_BASE must be set when type is 'azure'") + if (type === "azure") base = cleanAzureBase(base) + if (type === "azure" && version && version !== AZURE_OPENAI_API_VERSION) + throw new Error( + `OPENAI_API_VERSION must be '${AZURE_OPENAI_API_VERSION}'` ) - throw new Error( - `OPENAI_API_VERSION must be '${AZURE_OPENAI_API_VERSION}'` - ) - if (!token && !/^http:\/\//i.test(base)) - // localhost typically requires no key - throw new Error("OPENAI_API_KEY missing") - if (token === PLACEHOLDER_API_KEY) - throw new Error("OPENAI_API_KEY not configured") - if (base === PLACEHOLDER_API_BASE) - throw new Error("OPENAI_API_BASE not configured") - if (base && !URL.canParse(base)) - throw new Error("OPENAI_API_BASE must be a valid URL") - return { - provider, - model, - base, - type, - token, - source: "env: OPENAI_API_...", - version, - } + if (!token && !/^http:\/\//i.test(base)) + // localhost typically requires no key + throw new Error("OPENAI_API_KEY missing") + if (token === PLACEHOLDER_API_KEY) + throw new Error("OPENAI_API_KEY not configured") + if (base === PLACEHOLDER_API_BASE) + throw new Error("OPENAI_API_BASE not configured") + if (base && !URL.canParse(base)) + throw new Error("OPENAI_API_BASE must be a valid URL") + return { + provider, + model, + base, + type, + token, + source: "env: OPENAI_API_...", + version, } } diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index 435382ff37..7fa822b074 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -194,7 +194,8 @@ export const DOCS_CONFIGURATION_HUGGINGFACE_URL = "https://microsoft.github.io/genaiscript/getting-started/configuration/#huggingface" export const DOCS_CONFIGURATION_CONTENT_SAFETY_URL = "https://microsoft.github.io/genaiscript/reference/scripts/content-safety" -export const DOCS_DEF_FILES_IS_EMPTY_URL = "https://microsoft.github.io/genaiscript/reference/scripts/context/#empty-files" +export const DOCS_DEF_FILES_IS_EMPTY_URL = + "https://microsoft.github.io/genaiscript/reference/scripts/context/#empty-files" export const MODEL_PROVIDERS = Object.freeze([ { @@ -222,6 +223,16 @@ export const MODEL_PROVIDERS = Object.freeze([ detail: "Azure AI Models (serverless deployments, not OpenAI)", url: DOCS_CONFIGURATION_AZURE_MODELS_SERVERLESS_URL, }, + { + id: MODEL_PROVIDER_ANTHROPIC, + detail: "Anthropic models", + url: DOCS_CONFIGURATION_ANTHROPIC_URL, + }, + { + id: MODEL_PROVIDER_HUGGINGFACE, + detail: "Hugging Face models", + url: DOCS_CONFIGURATION_HUGGINGFACE_URL, + }, { id: MODEL_PROVIDER_OLLAMA, detail: "Ollama local model", @@ -237,16 +248,6 @@ export const MODEL_PROVIDERS = Object.freeze([ detail: "LiteLLM proxy", url: DOCS_CONFIGURATION_LITELLM_URL, }, - { - id: MODEL_PROVIDER_ANTHROPIC, - detail: "Anthropic models", - url: DOCS_CONFIGURATION_ANTHROPIC_URL, - }, - { - id: MODEL_PROVIDER_HUGGINGFACE, - detail: "Hugging Face models", - url: DOCS_CONFIGURATION_HUGGINGFACE_URL, - }, ]) export const NEW_SCRIPT_TEMPLATE = `$\`Write a short poem in code.\` @@ -335,4 +336,5 @@ export const TOKEN_NO_ANSWER = "" export const CHOICE_LOGIT_BIAS = 5 -export const SANITIZED_PROMPT_INJECTION = "...prompt injection detected, content removed..." \ No newline at end of file +export const SANITIZED_PROMPT_INJECTION = + "...prompt injection detected, content removed..."