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

Testing #7

Merged
merged 1 commit into from
Apr 6, 2024
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
84 changes: 84 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Tests

on:
push:
branches:
- main
pull_request:

jobs:
setup-node-modules:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Git Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: Install Dependencies
run: npm install

lint:
name: Linting
runs-on: ubuntu-latest
needs: setup-node-modules
steps:
- name: Git Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: Install Dependencies
run: npm install

- name: Run Linting
run: npm run lint

unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
needs: setup-node-modules
steps:
- name: Git Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: Install Dependencies
run: npm install

- name: Run Tests
run: npm run test:unit

e2e-tests:
name: E2E Tests
runs-on: ubuntu-latest
needs: setup-node-modules
steps:
- name: Git Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: Install Dependencies
run: npm install

- name: Run Tests
run: npm run test:e2e
6 changes: 4 additions & 2 deletions ai-providers/open-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ReadableStream, UnderlyingByteSource, ReadableByteStreamController } fr
import OpenAI from 'openai'
import { AiProvider, NoContentError, StreamChunkCallback } from './provider'
import { ReadableStream as ReadableStreamPolyfill } from 'web-streams-polyfill'
import { fetch } from 'undici'
import { ChatCompletionChunk } from 'openai/resources/index.mjs'
import { AiStreamEvent, encodeEvent } from './event'
import createError from '@fastify/error'
Expand Down Expand Up @@ -86,7 +87,8 @@ export class OpenAiProvider implements AiProvider {

constructor (model: string, apiKey: string) {
this.model = model
this.client = new OpenAI({ apiKey })
// @ts-expect-error
this.client = new OpenAI({ apiKey, fetch })
}

async ask (prompt: string): Promise<string> {
Expand Down Expand Up @@ -118,6 +120,6 @@ export class OpenAiProvider implements AiProvider {
],
stream: true
})
return new ReadableStream(new OpenAiByteSource(response.toReadableStream()))
return new ReadableStream(new OpenAiByteSource(response.toReadableStream(), chunkCallback))
}
}
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ReadableStream } from 'node:stream/web'
import { PlatformaticApp } from '@platformatic/service'
import { errorResponseBuilderContext } from '@fastify/rate-limit'
import { AiWarpConfig } from './config'

declare module 'fastify' {
Expand Down
8 changes: 6 additions & 2 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class AiWarpGenerator extends ServiceGenerator {
const defaultBaseConfig = super.getDefaultConfig()
const defaultConfig = {
aiProvider: 'openai',
aiModel: 'gpt-3.5-turbo'
aiModel: 'gpt-3.5-turbo',
// TODO: temporary fix, when running the typescript files directly
// (in tests) this goes a directory above the actual project. Exposing
// temporarily until I come up with something better
aiWarpPackageJsonPath: join(__dirname, '..', '..', 'package.json')
}
return Object.assign({}, defaultBaseConfig, defaultConfig)
}
Expand Down Expand Up @@ -132,7 +136,7 @@ class AiWarpGenerator extends ServiceGenerator {

async getStackablePackageJson (): Promise<PackageJson> {
if (this._packageJson == null) {
const packageJsonPath = join(__dirname, '..', '..', 'package.json')
const packageJsonPath = this.config.aiWarpPackageJsonPath
const packageJsonFile = await readFile(packageJsonPath, 'utf8')
const packageJson: Partial<PackageJson> = JSON.parse(packageJsonFile)

Expand Down
Loading
Loading