-
Notifications
You must be signed in to change notification settings - Fork 900
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
23 changed files
with
449 additions
and
184 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,48 @@ | ||
#!/usr/bin/env yarn tsn -T | ||
|
||
import OpenAI from 'openai'; | ||
|
||
// The name of your Azure OpenAI Resource. | ||
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource | ||
const resource = '<your resource name>'; | ||
|
||
// Corresponds to your Model deployment within your OpenAI resource, e.g. my-gpt35-16k-deployment | ||
// Navigate to the Azure OpenAI Studio to deploy a model. | ||
const model = '<your model>'; | ||
|
||
const apiKey = process.env['AZURE_OPENAI_API_KEY']; | ||
if (!apiKey) { | ||
throw new Error('The AZURE_OPENAI_API_KEY environment variable is missing or empty.'); | ||
} | ||
|
||
// Azure OpenAI requires a custom baseURL, api-version query param, and api-key header. | ||
const openai = new OpenAI({ | ||
apiKey, | ||
baseURL: `https://${resource}.openai.azure.com/openai/deployments/${model}`, | ||
defaultQuery: { 'api-version': '2023-06-01-preview' }, | ||
defaultHeaders: { 'api-key': apiKey }, | ||
}); | ||
|
||
async function main() { | ||
console.log('Non-streaming:'); | ||
const result = await openai.chat.completions.create({ | ||
model, | ||
messages: [{ role: 'user', content: 'Say hello!' }], | ||
}); | ||
console.log(result.choices[0]!.message?.content); | ||
|
||
console.log(); | ||
console.log('Streaming:'); | ||
const stream = await openai.chat.completions.create({ | ||
model, | ||
messages: [{ role: 'user', content: 'Say hello!' }], | ||
stream: true, | ||
}); | ||
|
||
for await (const part of stream) { | ||
process.stdout.write(part.choices[0]?.delta?.content ?? ''); | ||
} | ||
process.stdout.write('\n'); | ||
} | ||
|
||
main().catch(console.error); |
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,21 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
const path = require('path'); | ||
const distSrcDir = path.resolve(__dirname, '..', 'dist', 'src'); | ||
|
||
function replaceSelfReferencingImports({ orig, file, config }) { | ||
// replace self-referencing imports in source files to reduce errors users will | ||
// see if they go to definition | ||
if (!file.startsWith(distSrcDir)) return orig; | ||
return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => { | ||
if (!importPath.startsWith('openai/')) return match; | ||
let relativePath = path.relative( | ||
path.dirname(file), | ||
path.join(distSrcDir, importPath.substring('openai/'.length)), | ||
); | ||
if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`; | ||
return JSON.stringify(relativePath); | ||
}); | ||
} | ||
exports.default = replaceSelfReferencingImports; |
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
Oops, something went wrong.