-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add openai lib * change git clone in the readme to the public url * add description to package.json * update mklib.sh to include a description in package.json * fix PR comment about peerDependencies * tweaks * remove author from template * Update openai.test.w * fix test * another fix to test --------- Co-authored-by: Elad Ben-Israel <[email protected]>
- Loading branch information
Showing
19 changed files
with
715 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
**/node_modules/** | ||
target/ | ||
**/.env | ||
.history/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target/ | ||
node_modules/ | ||
.history/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} | ||
})); | ||
} |
Oops, something went wrong.