Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Nov 13, 2024
2 parents 30d2440 + 2c225ed commit 4266643
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 65 deletions.
1 change: 1 addition & 0 deletions docs/src/content/docs/reference/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,6 @@ Show .env information
Options:
-t, --token show token
-e, --error show errors
-h, --help display help for command
```
1 change: 1 addition & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 11 additions & 7 deletions packages/cli/src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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))
Expand Down
84 changes: 38 additions & 46 deletions packages/core/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
26 changes: 14 additions & 12 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
{
Expand Down Expand Up @@ -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",
Expand All @@ -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.\`
Expand Down Expand Up @@ -335,4 +336,5 @@ export const TOKEN_NO_ANSWER = "<NO_ANSWER>"

export const CHOICE_LOGIT_BIAS = 5

export const SANITIZED_PROMPT_INJECTION = "...prompt injection detected, content removed..."
export const SANITIZED_PROMPT_INJECTION =
"...prompt injection detected, content removed..."

0 comments on commit 4266643

Please sign in to comment.