Skip to content
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

feat: 🔧 add modelEncodings support and improve config loading #1056

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions docs/public/schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,35 @@
"description": "List of files to include in the project",
"type": "array",
"items": {
"type": "string"
"type": "string",
"description": "Path to a file or a glob pattern to include in the project"
}
},
"modelEncodings": {
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9_:]+$": {
"type": "string",
"description": "Encoding identifier",
"enum": [
"o1",
"gpt-4o",
"gpt-3.5-turbo",
"text-davinci-003",
"o200k_base",
"cl100k_base",
"p50k_base",
"r50k_base"
]
}
},
"additionalProperties": true,
"description": "Equivalent encoders for model identifiers"
},
"modelAliases": {
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9_]+$": {
"^[a-zA-Z0-9_:]+$": {
"oneOf": [
{
"type": "string",
Expand Down
39 changes: 34 additions & 5 deletions docs/src/content/docs/reference/configuration-files.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
```json title="genaiscript.config.json"
{
"$schema": "https://microsoft.github.io/genaiscript/schemas/config.json"
}

Check warning on line 16 in docs/src/content/docs/reference/configuration-files.mdx

View workflow job for this annotation

GitHub Actions / build

Header for section "File resolution" is missing.
```


## File resolution

GenAIScript will scan for the following configuration files
and merge their content into the final configuration.

- `~/genaiscript.config.yaml`
- `~/genaiscript.config.json`
- `./genaiscript.config.yaml`
- `./genaiscript.config.json`
- `~/genaiscript.config.yaml`
- `~/genaiscript.config.json`
- `./genaiscript.config.yaml`
- `./genaiscript.config.json`

The JSON files support the [JSON5](https://json5.org/) format (including comments, trailing commas, etc...).

Check failure on line 30 in docs/src/content/docs/reference/configuration-files.mdx

View workflow job for this annotation

GitHub Actions / build

JSON files support the [JSON5](https://json5.org/) format (including comments, trailing commas, etc...).
## Schema

The configuration schema is at https://microsoft.github.io/genaiscript/schemas/config.json .
Expand All @@ -46,3 +47,31 @@
include:
- "globalpath/*.genai.mjs"
```

## `modelAliases` property

The `modelAliases` property allows you to provide aliases for model names.

```js
{
"modelAliases": {
"llama32": "ollama:llama3.2:1b",
"llama32hot": {
"model": "ollama:llama3.2:1b",
"temperature": 2
}
}
}
```

## `modelEncodings` property

The `modelEncodings` property allows you to provide the encoding for the model.

```js
{
"modelEncodings": {
"azure:gpt__4o_random_name": "gpt-4o"
}
}
```
10 changes: 8 additions & 2 deletions packages/cli/src/nodehost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
ModelConfiguration,
} from "../../core/src/host"
import { TraceOptions } from "../../core/src/trace"
import { logError, logVerbose } from "../../core/src/util"
import { assert, logError, logVerbose } from "../../core/src/util"
import { parseModelIdentifier } from "../../core/src/models"
import { LanguageModel } from "../../core/src/chat"
import { errorMessage, NotSupportedError } from "../../core/src/error"
Expand Down Expand Up @@ -92,6 +92,7 @@ export class NodeHost implements RuntimeHost {
script: {},
config: {},
}
private _config: HostConfiguration
readonly userInputQueue = new PLimitPromiseQueue(1)
readonly azureToken: AzureTokenResolver
readonly azureServerlessToken: AzureTokenResolver
Expand Down Expand Up @@ -195,7 +196,12 @@ export class NodeHost implements RuntimeHost {
if (res.error) throw res.error
}
await parseDefaultsFromEnv(process.env)
return config
return (this._config = config)
}

get config() {
assert(!!this._config, "Host configuration not loaded")
return this._config
}

static async install(dotEnvPath?: string) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export async function resolveGlobalConfiguration(
config?.modelAliases || {},
parsed?.modelAliases || {}
),
modelEncodings: structuralMerge(
config?.modelEncodings || {},
parsed?.modelEncodings || {}
),
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/encoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export async function resolveTokenEncoder(
const { model } = parseModelIdentifier(modelId)
const module = model.toLowerCase() // Assign model to module for dynamic import path

const { modelEncodings } = runtimeHost?.config || {}
const encoding = modelEncodings?.[modelId] || module

const encoderOptions = {
disallowedSpecial: new Set<string>(),
} satisfies EncodeOptions
Expand All @@ -31,7 +34,7 @@ export async function resolveTokenEncoder(
encode,
decode,
default: api,
} = await import(`gpt-tokenizer/model/${module}`)
} = await import(`gpt-tokenizer/model/${encoding}`)
assert(!!encode)
const { modelName } = api
const size =
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,18 @@ export interface RuntimeHost extends Host {
value: string | Omit<ModelConfiguration, "source">
): void

/**
* Reloads the configuration
*/
readConfig(): Promise<HostConfiguration>
/**
* Gets the current loaded configuration
*/
get config(): HostConfiguration
/**
* Reads a secret
* @param name
*/
readSecret(name: string): Promise<string | undefined>
// executes a process
exec(
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/hostconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ export interface HostConfiguration {
* Configures a list of known aliases. Overriden by environment variables and CLI arguments
*/
modelAliases?: Record<string, string | ModelConfiguration>

/**
* Model identifier to encoding mapping
*/
modelEncodings?: Record<string, string>
}
4 changes: 4 additions & 0 deletions packages/core/src/testhost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export class TestHost implements RuntimeHost {
return {}
}

get config() {
return {}
}

contentSafety(
id?: "azure",
options?: TraceOptions
Expand Down
3 changes: 3 additions & 0 deletions packages/sample/genaiscript.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"model": "ollama:llama3.2:1b",
"temperature": 2
}
},
"modelEncodings": {
"azure:gpt__4o_random_name": "gpt-4o"
}
}
Loading