-
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.
- Loading branch information
Showing
16 changed files
with
618 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -18,6 +18,12 @@ Clone this repository: | |
git clone [email protected]:winglang/winglibs | ||
``` | ||
|
||
Change to the `winglibs` directory: | ||
|
||
```sh | ||
cd winglibs | ||
``` | ||
|
||
Use the fabulous `mklib.sh` script to scaffold your library: | ||
|
||
```sh | ||
|
@@ -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
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,45 @@ | ||
# openai | ||
|
||
## Prerequisites | ||
|
||
* [winglang](https://winglang.io). | ||
|
||
## Installation | ||
|
||
`sh | ||
npm i @winglibs/openai | ||
` | ||
|
||
## Example | ||
|
||
`js | ||
bring openai; | ||
|
||
let openAIService = new openai.OpenAI("your-api-key"); | ||
|
||
new cloud.Function(inflight () => { | ||
let joke = openAIService.createCompletion("tell me a short joke"); | ||
log(joke); | ||
}); | ||
` | ||
|
||
## Usage | ||
The `openai.OpenAI` resource provides the following inflight methods: | ||
|
||
* `createCompletion` - Gets an answer to a prompt. | ||
|
||
The preflight constructor requires an api key in the form of either a secret or a string. | ||
|
||
## Roadmap | ||
|
||
* [x] 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 extends std.IResource { | ||
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,42 @@ | ||
const openai = require('openai'); | ||
|
||
exports.createNewInflightClient = (apiKey, org) => { | ||
const config = { | ||
apiKey: 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 = { model: "gpt-3.5-turbo", max_tokens: 2048 }) => { | ||
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,21 @@ | ||
bring expect; | ||
bring "./openai.w" as openai; | ||
|
||
let client = new openai.OpenAI(); | ||
|
||
// This test currently doesn't pass because of issue https://github.com/winglang/wing/issues/5948 | ||
// test "cant create client without key" { | ||
// let var errorMessage = ""; | ||
// try { | ||
// let answer = client.createCompletion("tell me a short joke"); | ||
// } catch e { | ||
// errorMessage = e; | ||
// } | ||
// expect.equal(errorMessage, "OpenAI API key is required"); | ||
// } | ||
|
||
// This test currently cannot pass because we can't pass credentials to it in the test runner | ||
// test "basic completion" { | ||
// let answer = client.createCompletion("tell me a short joke"); | ||
// expect.notNil(answer); | ||
// } |
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,45 @@ | ||
bring util; | ||
bring "./api.w" as api; | ||
bring "./utils.w" as utils; | ||
bring cloud; | ||
|
||
pub class OpenAI impl api.IOpenAI { | ||
apiKey: cloud.Secret?; | ||
org: cloud.Secret?; | ||
keyOverride: str?; | ||
orgOverride: str?; | ||
|
||
inflight openai: api.IOpenAI; | ||
|
||
new (apiKey: str?, org: str?, apiKeySecret: cloud.Secret?, orgSecret: cloud.Secret?) { | ||
this.apiKey = apiKeySecret; | ||
this.org = orgSecret; | ||
this.keyOverride = apiKey; | ||
this.orgOverride = org; | ||
} | ||
|
||
inflight new() { | ||
// I wanted to write this thing like this: | ||
// let apiKey: str? = this.keyOverride ?? this.apiKey?.value(); | ||
// I was even willing to settle for this: | ||
// let apiKey: str? = this.keyOverride ?? this.apiKey?.value(); | ||
// But neither of those work. So I'm doing this instead after opening issue https://github.com/winglang/wing/issues/5944: | ||
let var apiKey = this.keyOverride; | ||
if (apiKey == nil){ | ||
apiKey = this.apiKey?.value(); | ||
} | ||
if (apiKey == nil || apiKey == ""){ | ||
throw "OpenAI API key is required"; | ||
} | ||
let var org: str? = this.orgOverride; | ||
if (org == nil){ | ||
org = this.org?.value(); | ||
} | ||
|
||
this.openai = utils.createNewInflightClient(apiKey!, org); | ||
} | ||
|
||
pub inflight createCompletion(prompt: str, params: api.CompletionParams?): str { | ||
return this.openai.createCompletion(prompt, params); | ||
} | ||
} |
Oops, something went wrong.