-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cli configure #1107
Merged
Merged
cli configure #1107
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
260f80b
towards configure support
pelikhan ca581ee
link to docs
pelikhan 643d3f0
more config
pelikhan 0fc53f1
twekas
pelikhan 4637f72
helper flag
pelikhan 6ba545c
configure mode
pelikhan dc54d74
configure docs
pelikhan 5909806
add responseType, responseSchema to cache key
pelikhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -6,3 +6,5 @@ package.json | |
package-lock.json | ||
yarn.lock | ||
**/package.lock | ||
docs/dist/ | ||
docs/distasw/ |
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,12 @@ | ||
--- | ||
title: configure | ||
descript: Configure and validate the LLM connections. | ||
sidebar: | ||
order: 4 | ||
--- | ||
|
||
Interactice command to configure and validate the LLM connections. | ||
|
||
```bash | ||
npx genaiscript configure | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Front matter is missing at the beginning of the file. Add front matter to provide metadata and structure for the document.
|
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,137 @@ | ||
import { select, input, confirm, password } from "@inquirer/prompts" | ||
import { MODEL_PROVIDERS } from "../../core/src/constants" | ||
import { resolveLanguageModelConfigurations } from "../../core/src/config" | ||
import { parse } from "dotenv" | ||
import { readFile } from "fs/promises" | ||
import { writeFile } from "fs/promises" | ||
import { runtimeHost } from "../../core/src/host" | ||
import { deleteUndefinedValues } from "../../core/src/cleaners" | ||
import { logInfo, logVerbose, logWarn } from "../../core/src/util" | ||
|
||
export async function configure(options: { provider?: string }) { | ||
while (true) { | ||
const provider = options?.provider | ||
? MODEL_PROVIDERS.find(({ id }) => options.provider === id) | ||
: await select({ | ||
message: "Select a LLM provider to configure", | ||
choices: MODEL_PROVIDERS.map((provider) => ({ | ||
name: provider.id, | ||
value: provider, | ||
description: provider.detail, | ||
})), | ||
}) | ||
if (!provider) break | ||
|
||
logInfo(`configurating ${provider.id} (${provider.detail})`) | ||
logVerbose( | ||
`- docs: https://microsoft.github.io/genaiscript/getting-started/configuration#${provider.id}` | ||
) | ||
while (true) { | ||
const config = await runtimeHost.readConfig() | ||
logVerbose(`- env file: ${config.envFile}`) | ||
const envText = await readFile(config.envFile, "utf-8") | ||
const env = parse(envText) | ||
const conn = ( | ||
await resolveLanguageModelConfigurations(provider.id, { | ||
token: false, | ||
error: true, | ||
models: true, | ||
}) | ||
)?.[0] | ||
if (conn) { | ||
const { error, models, ...rest } = conn | ||
logInfo("") | ||
logInfo( | ||
YAML.stringify( | ||
deleteUndefinedValues({ | ||
configuration: deleteUndefinedValues({ | ||
...rest, | ||
models: models?.length ?? undefined, | ||
}), | ||
}) | ||
) | ||
) | ||
if (error) logWarn(`error: ${error}`) | ||
else logInfo(`configured!`) | ||
} else { | ||
logWarn(`no configuration found`) | ||
} | ||
if (!provider.env) { | ||
logInfo( | ||
`sorry, this provider is not yet configurable through the cli` | ||
) | ||
break | ||
} | ||
const envVars = Object.entries(provider.env) | ||
if (!envVars.length) { | ||
logInfo(`this provider does not have configuration flags`) | ||
break | ||
} | ||
|
||
if (!conn?.error) { | ||
const edit = await confirm({ | ||
message: `do you want to edit the configuration?`, | ||
}) | ||
if (!edit) break | ||
} | ||
for (const ev of envVars) { | ||
const [name, info] = ev | ||
const oldValue = env[name] | ||
let value = oldValue | ||
if (value) { | ||
const edit = await confirm({ | ||
message: `found a value for ${name}, do you want to edit?`, | ||
}) | ||
if (!edit) continue | ||
} | ||
if (info.secret) { | ||
value = await password({ | ||
message: `enter a value for ${name}`, | ||
mask: false, | ||
}) | ||
} else { | ||
value = await input({ | ||
message: `enter a value for ${name} (optional, leave empty to skip)`, | ||
default: value, | ||
required: info.required, | ||
theme: { | ||
validationFailureMode: "keep", | ||
}, | ||
validate: (v) => { | ||
console.log(v) | ||
if (info.format === "url") { | ||
if (v && !URL.canParse(v)) return "invalid url" | ||
} | ||
return true | ||
}, | ||
}) | ||
} | ||
if (value === "") continue | ||
if (value !== oldValue) | ||
await patchEnvFile(config.envFile, name, value) | ||
} | ||
} | ||
} | ||
} | ||
|
||
async function patchEnvFile(filePath: string, key: string, value: string) { | ||
logVerbose(`patching ${filePath}, ${key}`) | ||
|
||
const fileContent = await readFile(filePath, "utf-8") | ||
const lines = fileContent.split("\n") | ||
let found = false | ||
|
||
const updatedLines = lines.map((line) => { | ||
if (line.startsWith(`${key}=`)) { | ||
found = true | ||
return `${key}=${value}` | ||
} | ||
return line | ||
}) | ||
|
||
if (!found) { | ||
updatedLines.push(`${key}=${value}`) | ||
} | ||
|
||
await writeFile(filePath, updatedLines.join("\n"), "utf-8") | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing header for new section. Consider adding a brief description or summary of the
configure
command.