Skip to content

Commit

Permalink
chore: Add CI with GitHub Actions.
Browse files Browse the repository at this point in the history
Also, files have been reformatted so that the formatting
check in CI will pass.
  • Loading branch information
jemc committed May 3, 2024
1 parent 4d0da8e commit 2e92b81
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 65 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: ci

on:
workflow_dispatch: # allow manual trigger
pull_request: # on pull request changes
push:
branches: [main] # on commits to the main branch

jobs:
ci:
runs-on: ubuntu-latest
permissions:
contents: write # to publish releases
issues: write # to comment on released issues
pull-requests: write # to comment on released PRs
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
steps:
- uses: actions/checkout@v3
- uses: actions/[email protected]
with: { node-version: 20.x }
- run: npm install -g pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm run -r format:check
- run: pnpm run -r test
- run: pnpm run -r build
# - run: pnpm run -r release
# env:
# GITHUB_TOKEN: ${{ github.token }}
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
62 changes: 33 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
{
"name": "@formula-monks/kurt",
"description": "A wrapper for AI SDKs, for building LLM-agnostic structured AI applications",
"license": "MIT",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "jest",
"prepack": "tsc"
},
"prettier": {
"semi": false
},
"dependencies": {
"@google-cloud/vertexai": "1.1.0",
"openai": "^4.40.0",
"zod": "^3.23.5",
"zod-to-json-schema": "^3.23.0"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"type-fest": "^4.18.1",
"typescript": "^5.4.5"
}
"name": "@formula-monks/kurt",
"description": "A wrapper for AI SDKs, for building LLM-agnostic structured AI applications",
"license": "MIT",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "jest",
"format": "prettier --write ./**/*.{ts,js,json,md}",
"format:check": "prettier --check ./**/*.{ts,js,json,md}",
"build": "tsc",
"prepack": "pnpm run build"
},
"prettier": {
"semi": false
},
"dependencies": {
"@google-cloud/vertexai": "1.1.0",
"openai": "^4.40.0",
"zod": "^3.23.5",
"zod-to-json-schema": "^3.23.0"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"type-fest": "^4.18.1",
"typescript": "^5.4.5"
}
}
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions spec/KurtOpenAI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const USE_REAL_API = false // set to true to validate against actual OpenAI

function setupExpectingCall(
expectedRequest: OpenAIRequest,
responseChunks: OpenAIResponseChunk["choices"][number][]
responseChunks: OpenAIResponseChunk["choices"][number][],
) {
const openAI: OpenAI = USE_REAL_API
? new RealOpenAI()
Expand Down Expand Up @@ -68,7 +68,7 @@ describe("KurtOpenAI", () => {
{ delta: { content: " today" }, finish_reason: null },
{ delta: { content: "?" }, finish_reason: null },
{ delta: { content: "" }, finish_reason: "stop" },
]
],
)

expect(await arrayFromAsync(kurt.generateNaturalLanguage(req))).toEqual([
Expand Down Expand Up @@ -170,7 +170,7 @@ describe("KurtOpenAI", () => {
finish_reason: null,
},
{ delta: {}, finish_reason: "stop" },
]
],
)

expect(await arrayFromAsync(kurt.generateStructuredData(req))).toEqual([
Expand Down
10 changes: 5 additions & 5 deletions spec/KurtResult.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function kurtSayHelloFinalEvent() {
function kurtResultSayHello(
opts: {
errorBeforeFinish?: boolean
} = {}
} = {},
) {
const schema = z.object({ say: z.string() })
return new KurtResult<(typeof schema)["shape"]>(
Expand All @@ -42,7 +42,7 @@ function kurtResultSayHello(
// Send the event.
yield event as KurtResultEvent<(typeof schema)["shape"]>
}
})()
})(),
)
}

Expand Down Expand Up @@ -207,8 +207,8 @@ describe("KurtResult", () => {
awaits.push(
(async () =>
expect(await result.finalEvent).toEqual(
kurtSayHelloFinalEvent()
))()
kurtSayHelloFinalEvent(),
))(),
)
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ describe("KurtResult", () => {
listeners.push(listener)
errorers.push(expectThrow("Whoops!", async () => await listener))
errorers.push(
expectThrow("Whoops!", async () => await result.finalEvent)
expectThrow("Whoops!", async () => await result.finalEvent),
)
}
}
Expand Down
12 changes: 6 additions & 6 deletions spec/KurtVertexAI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const USE_REAL_API = false // set to true to validate against actual VertexAI

function setupExpectingCall(
expectedRequest: VertexAIRequest,
responseChunks: VertexAIResponseChunkCandidate[]
responseChunks: VertexAIResponseChunkCandidate[],
) {
function requiredEnvVar(name: string) {
const value = process.env[name]
Expand All @@ -33,7 +33,7 @@ function setupExpectingCall(
getGenerativeModel(...args: unknown[]) {
return {
generateContentStreamPATCHED(
req: VertexAIRequest
req: VertexAIRequest,
): VertexAIResponse {
expect(req).toEqual(expectedRequest)

Expand Down Expand Up @@ -79,7 +79,7 @@ describe("KurtVertexAI", () => {
},
finishReason: "STOP" as any, // TODO: no any
},
]
],
)

expect(await arrayFromAsync(kurt.generateNaturalLanguage(req))).toEqual([
Expand Down Expand Up @@ -139,7 +139,7 @@ describe("KurtVertexAI", () => {
},
finishReason: "STOP" as any, // TODO: no any
},
]
],
)

expect(await arrayFromAsync(kurt.generateStructuredData(req))).toEqual([
Expand Down Expand Up @@ -206,7 +206,7 @@ describe("KurtVertexAI", () => {
},
finishReason: "STOP" as any, // TODO: no any
},
]
],
)

expect(await arrayFromAsync(kurt.generateStructuredData(req))).toEqual([
Expand Down Expand Up @@ -273,7 +273,7 @@ describe("KurtVertexAI", () => {
},
finishReason: "STOP" as any, // TODO: no any
},
]
],
)

expect(await arrayFromAsync(kurt.generateStructuredData(req))).toEqual([
Expand Down
4 changes: 2 additions & 2 deletions src/Kurt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { KurtSchema, KurtSchemaInner } from "./KurtSchema"

export interface Kurt {
generateNaturalLanguage(
options: KurtGenerateNaturalLanguageOptions
options: KurtGenerateNaturalLanguageOptions,
): KurtResult

generateStructuredData<T extends KurtSchemaInner>(
options: KurtGenerateStructuredDataOptions<T>
options: KurtGenerateStructuredDataOptions<T>,
): KurtResult<T>
}

Expand Down
10 changes: 5 additions & 5 deletions src/KurtOpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class KurtOpenAI implements Kurt {
constructor(private options: KurtOpenAICreateOptions) {}

generateNaturalLanguage(
options: KurtGenerateNaturalLanguageOptions
options: KurtGenerateNaturalLanguageOptions,
): KurtResult {
const systemPrompt = options.systemPrompt ?? this.options.systemPrompt
const prompt = options.prompt
Expand All @@ -54,12 +54,12 @@ export class KurtOpenAI implements Kurt {
{ role: "user", content: prompt },
...toOpenAIMessages(extraMessages),
],
})
}),
)
}

generateStructuredData<T extends KurtSchemaInner>(
options: KurtGenerateStructuredDataOptions<T>
options: KurtGenerateStructuredDataOptions<T>,
): KurtResult<T> {
const systemPrompt = options.systemPrompt ?? this.options.systemPrompt
const prompt = options.prompt
Expand Down Expand Up @@ -92,13 +92,13 @@ export class KurtOpenAI implements Kurt {
},
},
],
})
}),
)
}

private handleStream<T extends KurtSchemaInnerMaybe>(
schema: KurtSchemaMaybe<T>,
response: OpenAIResponse
response: OpenAIResponse,
): KurtResult<T> {
async function* generator<T extends KurtSchemaInnerMaybe>() {
const stream = await response
Expand Down
2 changes: 1 addition & 1 deletion src/KurtResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type KurtResultEvent<T extends KurtSchemaInnerMaybe = undefined> =
| KurtResultEventFinal<T>

type _AdditionalListener<T extends KurtSchemaInnerMaybe = undefined> = (
event: KurtResultEvent<T> | { uncaughtError: unknown }
event: KurtResultEvent<T> | { uncaughtError: unknown },
) => void

// This class represents the result of a call to an LLM.
Expand Down
14 changes: 7 additions & 7 deletions src/KurtVertexAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class KurtVertexAI implements Kurt {
constructor(private options: KurtVertexAICreateOptions) {}

generateNaturalLanguage(
options: KurtGenerateNaturalLanguageOptions
options: KurtGenerateNaturalLanguageOptions,
): KurtResult {
const systemPrompt = options.systemPrompt ?? this.options.systemPrompt
const prompt = options.prompt
Expand All @@ -57,12 +57,12 @@ export class KurtVertexAI implements Kurt {
{ role: "user", parts: [{ text: prompt }] },
...toVertexAIMessages(extraMessages),
],
})
}),
)
}

generateStructuredData<T extends KurtSchemaInner>(
options: KurtGenerateStructuredDataOptions<T>
options: KurtGenerateStructuredDataOptions<T>,
): KurtResult<T> {
const systemPrompt = options.systemPrompt ?? this.options.systemPrompt
const prompt = options.prompt
Expand Down Expand Up @@ -95,13 +95,13 @@ export class KurtVertexAI implements Kurt {
],
},
],
})
}),
)
}

private handleStream<T extends KurtSchemaInnerMaybe>(
schema: KurtSchemaMaybe<T>,
response: VertexAIResponse
response: VertexAIResponse,
): KurtResult<T> {
async function* generator<T extends KurtSchemaInnerMaybe>() {
const { stream } = await response
Expand Down Expand Up @@ -152,7 +152,7 @@ function toVertexAIMessages(messages: KurtMessage[]): VertexAIMessage[] {
}

function jsonSchemaForVertexAI<T extends KurtSchemaInner>(
zodSchema: KurtSchema<T>
zodSchema: KurtSchema<T>,
) {
// Vertex AI supports only a limited version of JSON schema,
// so we need to make modifications here to make it work properly.
Expand Down Expand Up @@ -192,7 +192,7 @@ function jsonSchemaForVertexAI<T extends KurtSchemaInner>(
// and then add new logic here as needed to handle the new scenario.
function applySchemaToFuzzyStructure<T extends KurtSchemaInnerMaybe>(
schema: KurtSchemaMaybe<T>,
input: { name: string; args: object } | undefined
input: { name: string; args: object } | undefined,
): any {
if (schema === undefined || input === undefined) return undefined

Expand Down
Loading

0 comments on commit 2e92b81

Please sign in to comment.