-
Notifications
You must be signed in to change notification settings - Fork 874
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
20 changed files
with
665 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,4 @@ | |
|
||
## Changes being requested | ||
|
||
|
||
## Additional context & links |
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 +1 @@ | ||
configured_endpoints: 23 | ||
configured_endpoints: 28 |
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,114 @@ | ||
#!/usr/bin/env -S npm run tsn -T | ||
|
||
import OpenAI from 'openai'; | ||
import { Stream } from 'openai/streaming'; | ||
|
||
// gets API Key from environment variable OPENAI_API_KEY | ||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
// ---------------- Explicit non-streaming params ------------ | ||
|
||
const params: OpenAI.Chat.CompletionCreateParams = { | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'Say this is a test!' }], | ||
}; | ||
const completion = await openai.chat.completions.create(params); | ||
console.log(completion.choices[0]?.message?.content); | ||
|
||
// ---------------- Explicit streaming params ---------------- | ||
|
||
const streamingParams: OpenAI.Chat.CompletionCreateParams = { | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'Say this is a test!' }], | ||
stream: true, | ||
}; | ||
|
||
const stream = await openai.chat.completions.create(streamingParams); | ||
for await (const chunk of stream) { | ||
process.stdout.write(chunk.choices[0]?.delta?.content || ''); | ||
} | ||
process.stdout.write('\n'); | ||
|
||
// ---------------- Explicit (non)streaming types ---------------- | ||
|
||
const params1: OpenAI.Chat.CompletionCreateParamsNonStreaming = { | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'Say this is a test!' }], | ||
}; | ||
|
||
const params2: OpenAI.Chat.CompletionCreateParamsStreaming = { | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'Say this is a test!' }], | ||
stream: true, | ||
}; | ||
|
||
// ---------------- Implicit params type ------------------- | ||
|
||
// Note: the `as const` is required here so that TS can properly infer | ||
// the right params type. | ||
// | ||
// If you didn't include it then you'd also get an error saying that | ||
// `role: string` is not assignable. | ||
const streamingParams2 = { | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'Say this is a test!' }], | ||
stream: true, | ||
} as const; | ||
|
||
// TS knows this is a Stream instance. | ||
const stream2 = await openai.chat.completions.create(streamingParams2); | ||
for await (const chunk of stream2) { | ||
process.stdout.write(chunk.choices[0]?.delta?.content || ''); | ||
} | ||
process.stdout.write('\n'); | ||
|
||
// Without the `as const` for `stream`. | ||
const streamingParams3 = { | ||
model: 'gpt-4', | ||
messages: [{ role: 'user' as const, content: 'Say this is a test!' }], | ||
stream: true, | ||
}; | ||
|
||
// TS doesn't know if this is a `Stream` or a direct response | ||
const response = await openai.chat.completions.create(streamingParams3); | ||
if (response instanceof Stream) { | ||
// here TS knows the response type is a `Stream` | ||
} else { | ||
// here TS knows the response type is a `ChatCompletion` | ||
} | ||
|
||
// ---------------- Dynamic params type ------------------- | ||
|
||
// TS knows this is a `Stream` | ||
const streamParamsFromFn = await createCompletionParams(true); | ||
const streamFromFn = await openai.chat.completions.create(streamParamsFromFn); | ||
console.log(streamFromFn); | ||
|
||
// TS knows this is a `ChatCompletion` | ||
const paramsFromFn = await createCompletionParams(false); | ||
const completionFromFn = await openai.chat.completions.create(paramsFromFn); | ||
console.log(completionFromFn); | ||
} | ||
|
||
// Dynamically construct the params object while retaining whether or | ||
// not the response will be streamed. | ||
export async function createCompletionParams( | ||
stream: true, | ||
): Promise<OpenAI.Chat.CompletionCreateParamsStreaming>; | ||
export async function createCompletionParams( | ||
stream: false, | ||
): Promise<OpenAI.Chat.CompletionCreateParamsNonStreaming>; | ||
export async function createCompletionParams(stream: boolean): Promise<OpenAI.Chat.CompletionCreateParams> { | ||
const params = { | ||
model: 'gpt-3.5-turbo', | ||
messages: [{ role: 'user' as const, content: 'Hello!' }], | ||
stream: stream, | ||
}; | ||
|
||
// <your logic here> | ||
|
||
return params; | ||
} | ||
|
||
main(); |
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,31 @@ | ||
#!/usr/bin/env -S npm run tsn -T | ||
|
||
import OpenAI from 'openai'; | ||
|
||
// gets API Key from environment variable OPENAI_API_KEY | ||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
// Explicit non streaming params type: | ||
const params: OpenAI.Chat.CompletionCreateParams = { | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'Say this is a test!' }], | ||
}; | ||
const completion = await openai.chat.completions.create(params); | ||
console.log(completion.choices[0]?.message?.content); | ||
|
||
// Explicit streaming params type: | ||
const streaming_params: OpenAI.Chat.CompletionCreateParams = { | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'Say this is a test!' }], | ||
stream: true, | ||
}; | ||
|
||
const stream = await openai.chat.completions.create(streaming_params); | ||
for await (const chunk of stream) { | ||
process.stdout.write(chunk.choices[0]?.delta?.content || ''); | ||
} | ||
process.stdout.write('\n'); | ||
} | ||
|
||
main(); |
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,6 +1,6 @@ | ||
{ | ||
"name": "openai", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Client library for the OpenAI API", | ||
"author": "OpenAI <[email protected]>", | ||
"types": "dist/index.d.ts", | ||
|
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
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
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.