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: bring openai #126

Merged
merged 11 commits into from
Mar 19, 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
21 changes: 21 additions & 0 deletions .github/workflows/canary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ jobs:
- name: Test
run: wing test
working-directory: ngrok
canary-openai:
name: Test openai
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: openai
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: openai
- name: Test
run: wing test
working-directory: openai
canary-postgres:
name: Test postgres
runs-on: ubuntu-latest
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/openai-pull.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: openai-pull
on:
pull_request:
paths:
- openai/**
jobs:
build-openai:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: openai
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: openai
- name: Test
run: wing test
working-directory: openai
- name: Pack
run: wing pack
working-directory: openai
54 changes: 54 additions & 0 deletions .github/workflows/openai-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: openai-release
on:
push:
branches:
- main
paths:
- openai/**
- "!openai/package-lock.json"
jobs:
build-openai:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: openai
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: openai
- name: Test
run: wing test
working-directory: openai
- name: Pack
run: wing pack
working-directory: openai
- name: Get package version
run: echo WINGLIB_VERSION=$(node -p "require('./package.json').version") >>
"$GITHUB_ENV"
working-directory: openai
- name: Publish
run: npm publish --access=public --registry https://registry.npmjs.org --tag
latest *.tgz
working-directory: openai
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Tag commit
uses: tvdias/[email protected]
with:
repo-token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
tag: openai-v${{ env.WINGLIB_VERSION }}
- name: GitHub release
uses: softprops/action-gh-release@v1
with:
name: openai v${{ env.WINGLIB_VERSION }}
tag_name: openai-v${{ env.WINGLIB_VERSION }}
files: "*.tgz"
token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/node_modules/**
target/
**/.env
.history/
6 changes: 6 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pull_request_rules:
- -check-failure=build-ngrok
- -check-pending=build-ngrok
- -check-stale=build-ngrok
- -check-failure=build-openai
- -check-pending=build-openai
- -check-stale=build-openai
- -check-failure=build-postgres
- -check-pending=build-postgres
- -check-stale=build-postgres
Expand Down Expand Up @@ -119,6 +122,9 @@ pull_request_rules:
- -check-failure=build-ngrok
- -check-pending=build-ngrok
- -check-stale=build-ngrok
- -check-failure=build-openai
- -check-pending=build-openai
- -check-stale=build-openai
- -check-failure=build-postgres
- -check-pending=build-postgres
- -check-stale=build-postgres
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ It's so damn easy.
Clone this repository:

```sh
git clone [email protected]:winglang/winglibs
git clone https://github.com/winglang/winglibs.git
```

Change to the `winglibs` directory:

```sh
cd winglibs
```

Use the fabulous `mklib.sh` script to scaffold your library:
Expand Down Expand Up @@ -74,6 +80,9 @@ bring my-awesome-lib
## Writing Wing Libraries

See [docs](https://www.winglang.io/docs/libraries#creating-a-wing-library).

Please note that it refers to writing libraries that are not published here, so it includes instructions for things that you get here automatically when using the `mklib.sh` script to scaffold your library.

## License

This repository is licensed under the [MIT License](./LICENSE), unless otherwise specified in a
Expand Down
1 change: 1 addition & 0 deletions mklib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mkdir $1
cat > $1/package.json <<HERE
{
"name": "@winglibs/$1",
"description": "$1 library for Wing",
"version": "0.0.1",
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions openai/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target/
node_modules/
.history/
21 changes: 21 additions & 0 deletions openai/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Wing

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
79 changes: 79 additions & 0 deletions openai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# openai

An [OpenAI](https://openai.com) library for Winglang.

> This is an initial version of this library which currently exposes a very small subset of the
> OpenAI API.

## Prerequisites

* [winglang](https://winglang.io).

## Installation

```sh
npm i @winglibs/openai
```

## Example

```js
bring cloud;
bring openai;

let key = new cloud.Secret(name: "OAIApiKey");
let oai = new openai.OpenAI(apiKeySecret: key);

new cloud.Function(inflight () => {
let joke = oai.createCompletion("tell me a short joke", model: "gpt-3.5-turbo", max_tokens: 2048);
log(joke);
});
```

When running in a `test` context, the `createCompletion` method will return a JSON object which
echos the request under the `mock` key:

```js
bring expect;

test "create completion test" {
let r = oai.createCompletion("tell me a short joke");
expect.equal(r, Json.stringify({
mock: {
prompt:"tell me a short joke",
params:{"model":"gpt-3.5-turbo","max_tokens":2048}
}
}));
}
```

## Usage

```js
new openai.OpenAI();
```

* `apiKeySecret` - a `cloud.Secret` with the OpenAI API key (required).
* `orgSecret` - a `cloud.Secret` with the OpenAI organization ID (not required).

You can also specify clear text values through `apiKey` and `org`, but make sure not to commit these
values to a repository :warning:.

Methods:

* `inflight createCompletion()` - requests a completion from a model. Options are `model` (defaults
to `gpt-3.5.turbo`) and `max_tokens` (defaults to 2048).

## Roadmap

* [ ] Support the rest of the OpenAI API
* [ ] Add more examples
* [ ] Add more tests

## Maintainers

* [Shai Ber](https://github.com/shaiber)

## License

Licensed under the [MIT License](/LICENSE).
9 changes: 9 additions & 0 deletions openai/api.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub struct CompletionParams {
model: str;
max_tokens: num;
}

// TODO: need to recreate the openai interface with higher fidelity
pub interface IOpenAI {
inflight createCompletion(prompt: str, params: CompletionParams?): str;
}
11 changes: 11 additions & 0 deletions openai/example.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bring expect;
bring "./openai.w" as openai;

bring cloud;

let oai = new openai.OpenAI(apiKey: "my-openai-key");

new cloud.Function(inflight () => {
let answer = oai.createCompletion("tell me a short joke", model: "gpt-3.5-turbo", max_tokens: 2048);
log(answer);
}) as "tell me a joke";
10 changes: 10 additions & 0 deletions openai/openai.extern.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default interface extern {
createNewInflightClient: (apiKey: string, org?: (string) | undefined) => Promise<IOpenAI$Inflight>,
}
export interface CompletionParams {
readonly max_tokens: number;
readonly model: string;
}
export interface IOpenAI$Inflight {
readonly createCompletion: (prompt: string, params?: (CompletionParams) | undefined) => Promise<string>;
}
38 changes: 38 additions & 0 deletions openai/openai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const openai = require('openai');

exports.createNewInflightClient = (apiKey, org) => {
const config = { apiKey };

if (org) {
config.organization = org;
}

let client = new openai.OpenAI(config);

// TODO: this is a hack for now, we should model the openai api in the api.w file with more fidelity
// and then we can just return the client itself, like we do in redis
return {
createCompletion: async (prompt, params = {}) => {
if (!prompt) {
throw new Error("Prompt is required");
};

if (typeof prompt !== "string") {
throw new Error("Prompt must be a string");
}

if (!params.model) {
params.model = "gpt-3.5-turbo";
};

if (!params.max_tokens) {
params.max_tokens = 2048;
}

params.messages = [ { role: 'user', content: prompt } ];

const response = await client.chat.completions.create(params);
return response.choices[0]?.message?.content;
}
};
};
19 changes: 19 additions & 0 deletions openai/openai.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
bring expect;
bring "./openai.w" as openai;

bring cloud;

let key = "my-openai-key";
let oai = new openai.OpenAI(apiKey: key);

test "basic completion" {
let answer = oai.createCompletion("tell me a short joke", model :"gpt-3.5-turbo", max_tokens: 1024);

// in tests, the response is just an echo of the request
expect.equal(answer, Json.stringify({
mock: {
prompt:"tell me a short joke",
params:{"model":"gpt-3.5-turbo","max_tokens":1024}
}
}));
}
Loading
Loading