Skip to content

Commit

Permalink
vscode + github models (#607)
Browse files Browse the repository at this point in the history
* move readSecret to runtimehost

* server/client config request

* lookup env from server
  • Loading branch information
pelikhan authored Aug 7, 2024
1 parent e9c809d commit 4636f8d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 33 deletions.
14 changes: 14 additions & 0 deletions packages/cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
ChatStart,
ChatChunk,
ChatCancel,
LanguageModelConfigurationResponse,
} from "../../core/src/server/messages"
import { envInfo } from "./info"
import { LanguageModel } from "../../core/src/chat"
Expand Down Expand Up @@ -189,6 +190,19 @@ export async function startServer(options: { port: string }) {
process.exit(0)
break
}
case "model.configuration": {
const { model, token } = data
console.log(`model: lookup configuration ${model}`)
const info = await host.getLanguageModelConfiguration(
model,
{ token }
)
response = <LanguageModelConfigurationResponse>{
ok: true,
info,
}
break
}
case "tests.run": {
console.log(
`tests: run ${data.scripts?.join(", ") || "*"}`
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export async function parseTokenFromEnv(
? "GITHUB_MODELS_TOKEN"
: "GITHUB_TOKEN"
const token = env[tokenVar]
// TODO: handle missing token
// if (!token) throw new Error("GITHUB_TOKEN must be set")
if (!token)
throw new Error("GITHUB_MODELS_TOKEN or GITHUB_TOKEN must be set")
const type = "openai"
const base = GITHUB_MODELS_BASE
return {
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ export const CLIENT_RECONNECT_DELAY = 3000
export const CLIENT_RECONNECT_MAX_ATTEMPTS = 20
export const RETRIEVAL_PERSIST_DIR = "retrieval"
export const HIGHLIGHT_LENGTH = 4000
export const DEFAULT_MODEL = "openai:gpt-4"
export const DEFAULT_MODEL_CANDIDATES = [
"openai:gpt-4o",
"azure:gpt-4o",
"github:gpt-4o",
]
export const DEFAULT_MODEL = "openai:gpt-4o"
export const DEFAULT_MODEL_CANDIDATES = ["azure:gpt-4o", "github:gpt-4o"]
export const DEFAULT_EMBEDDINGS_MODEL = "openai:text-embedding-ada-002"
export const DEFAULT_TEMPERATURE = 0.8
export const BUILTIN_PREFIX = "_builtin/"
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
GITHUB_TOKEN,
} from "./constants"
import { createFetch } from "./fetch"
import { host } from "./host"
import { runtimeHost } from "./host"
import { link, prettifyMarkdown } from "./markdown"
import { logError, logVerbose, normalizeInt } from "./util"

Expand Down Expand Up @@ -70,7 +70,7 @@ export async function githubUpdatePullRequestDescription(
assert(commentTag)

if (!issue) return { updated: false, statusText: "missing issue number" }
const token = await host.readSecret(GITHUB_TOKEN)
const token = await runtimeHost.readSecret(GITHUB_TOKEN)
if (!token) return { updated: false, statusText: "missing github token" }

text = prettifyMarkdown(text)
Expand Down Expand Up @@ -169,7 +169,7 @@ export async function githubCreateIssueComment(
const { apiUrl, repository, issue } = info

if (!issue) return { created: false, statusText: "missing issue number" }
const token = await host.readSecret(GITHUB_TOKEN)
const token = await runtimeHost.readSecret(GITHUB_TOKEN)
if (!token) return { created: false, statusText: "missing github token" }

const fetch = await createFetch({ retryOn: [] })
Expand Down Expand Up @@ -313,7 +313,7 @@ export async function githubCreatePullRequestReviews(
logError("missing commit sha")
return false
}
const token = await host.readSecret(GITHUB_TOKEN)
const token = await runtimeHost.readSecret(GITHUB_TOKEN)
if (!token) {
logError("missing github token")
return false
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export interface Host {
resolvePath(...segments: string[]): string

// read a secret from the environment or a .env file
readSecret(name: string): Promise<string | undefined>
defaultModelOptions: Required<Pick<ModelOptions, "model" | "temperature">>
defaultEmbeddingsModelOptions: Required<
Pick<EmbeddingsModelOptions, "embeddingsModel">
Expand Down Expand Up @@ -130,6 +129,7 @@ export interface RuntimeHost extends Host {
models: ModelService
workspace: Omit<WorkspaceFileSystem, "grep">

readSecret(name: string): Promise<string | undefined>
// executes a process
exec(
containerId: string,
Expand Down
15 changes: 14 additions & 1 deletion packages/core/src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CLIENT_RECONNECT_DELAY, OPEN, RECONNECT } from "../constants"
import { randomHex } from "../crypto"
import { errorMessage } from "../error"
import { GenerationResult } from "../generation"
import { ResponseStatus, host } from "../host"
import { LanguageModelConfiguration, ResponseStatus, host } from "../host"
import { MarkdownTrace } from "../trace"
import { assert, logError } from "../util"
import {
Expand All @@ -23,6 +23,7 @@ import {
ChatEvents,
ChatChunk,
ChatStart,
LanguageModelConfigurationRequest,
} from "./messages"

export type LanguageModelChatRequest = (
Expand Down Expand Up @@ -231,6 +232,18 @@ export class WebSocketClient extends EventTarget {
cancellers.forEach((a) => a.reject(reason || "cancelled"))
}

async getLanguageModelConfiguration(
modelId: string,
options?: { token?: boolean }
): Promise<LanguageModelConfiguration | undefined> {
const res = await this.queue<LanguageModelConfigurationRequest>({
type: "model.configuration",
model: modelId,
token: options?.token,
})
return res.response?.ok ? res.response.info : undefined
}

async version(): Promise<string> {
const res = await this.queue<ServerVersion>({ type: "server.version" })
return res.version
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/server/messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChatCompletionAssistantMessageParam } from "../chattypes"
import { GenerationResult } from "../generation"
import { ResponseStatus } from "../host"
import { LanguageModelConfiguration, ResponseStatus } from "../host"

export interface RequestMessage {
type: string
Expand Down Expand Up @@ -128,6 +128,17 @@ export interface ShellExec extends RequestMessage {
response?: ShellExecResponse
}

export interface LanguageModelConfigurationRequest extends RequestMessage {
type: "model.configuration"
model: string
token?: boolean
response?: LanguageModelConfigurationResponse
}

export interface LanguageModelConfigurationResponse extends ResponseStatus {
info?: LanguageModelConfiguration
}

export interface ChatStart {
type: "chat.start"
chatId: string
Expand Down Expand Up @@ -163,6 +174,7 @@ export type RequestMessages =
| PromptScriptStart
| PromptScriptAbort
| ChatChunk
| LanguageModelConfigurationRequest

export type PromptScriptResponseEvents =
| PromptScriptProgressResponseEvent
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/websearch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BING_SEARCH_ENDPOINT } from "./constants"
import { createFetch } from "./fetch"
import { host } from "./host"
import { runtimeHost } from "./host"
import { MarkdownTrace } from "./trace"

function toURLSearchParams(o: any) {
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function bingSearch(
} = options || {}
if (!q) return {}

const apiKey = await host.readSecret("BING_SEARCH_API_KEY")
const apiKey = await runtimeHost.readSecret("BING_SEARCH_API_KEY")
if (!apiKey)
throw new Error(
"BING_SEARCH_API_KEY secret is required to use bing search. See https://microsoft.github.io/genaiscript/reference/scripts/web-search/#bing-web-search-configuration."
Expand Down
25 changes: 9 additions & 16 deletions packages/vscode/src/vshost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ export class VSCodeHost extends EventTarget implements Host {
}

let files = Array.from(uris.values())
if (applyGitIgnore && (await checkFileExists(this.projectUri, ".gitignore"))) {
if (
applyGitIgnore &&
(await checkFileExists(this.projectUri, ".gitignore"))
) {
const gitignore = await readFileText(this.projectUri, ".gitignore")
files = await filterGitIgnore(gitignore, files)
}
Expand All @@ -186,26 +189,16 @@ export class VSCodeHost extends EventTarget implements Host {
await vscode.workspace.fs.delete(uri, { recursive: true })
}

async readSecret(name: string): Promise<string | undefined> {
try {
const dotenv = await readFileText(this.projectUri, DOT_ENV_FILENAME)
const env = dotEnvTryParse(dotenv)
return env?.[name]
} catch (e) {
return undefined
}
}

clientLanguageModel?: LanguageModel
async getLanguageModelConfiguration(
modelId: string,
options?: { token?: boolean } & AbortSignalOptions & TraceOptions
): Promise<LanguageModelConfiguration> {
const { signal, token: askToken } = options || {}
const dotenv = await readFileText(this.projectUri, DOT_ENV_FILENAME)
const env = dotEnvTryParse(dotenv) ?? {}
await parseDefaultsFromEnv(env)
const tok = await parseTokenFromEnv(env, modelId)
const tok = await this.server.client.getLanguageModelConfiguration(
modelId,
options
)
const { token: askToken } = options || {}
if (
askToken &&
tok &&
Expand Down

0 comments on commit 4636f8d

Please sign in to comment.