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
18 changed files
with
1,197 additions
and
883 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@paperdave/openai': minor | ||
--- | ||
|
||
Add Text Edit |
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,5 @@ | ||
--- | ||
'@paperdave/openai': minor | ||
--- | ||
|
||
Implement Retry Count |
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,5 @@ | ||
--- | ||
'@paperdave/openai': minor | ||
--- | ||
|
||
Move to using camel case everywhere |
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,8 @@ | ||
import { generateTextEdit } from '../src/text-edit'; | ||
|
||
const generation = await generateTextEdit({ | ||
model: 'text-davinci-edit-001', | ||
input: 'The quick brown fox jumps over the lazy dog.', | ||
instruction: 'Convert to leet speak.', | ||
}); | ||
console.log(generation); |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { delay } from '@paperdave/utils'; | ||
import { AuthOverride, getAuthHeaders } from './api-key'; | ||
|
||
export interface FetchOptions { | ||
/** Number of retries before giving up. Defaults to 3. */ | ||
retry?: number; | ||
/** Override authentication settings. */ | ||
auth?: AuthOverride; | ||
} | ||
|
||
export interface InternalFetchOptions extends FetchOptions { | ||
endpoint: string; | ||
method: 'GET' | 'POST' | 'PUT' | 'DELETE'; | ||
body?: any; | ||
init?: RequestInit; | ||
} | ||
|
||
const base = 'https://api.openai.com/v1'; | ||
|
||
export async function fetchOpenAI(options: InternalFetchOptions) { | ||
const { endpoint, method, body, init, retry = 3, auth } = options; | ||
const url = `${base}${endpoint}`; | ||
const headers = { | ||
...(body ? { 'Content-Type': 'application/json' } : {}), | ||
...getAuthHeaders(auth), | ||
}; | ||
const fullInit = { | ||
...init, | ||
method, | ||
headers: { | ||
...headers, | ||
...init?.headers, | ||
}, | ||
body: body ? JSON.stringify(body) : undefined, | ||
}; | ||
let tries = 0; | ||
while (true) { | ||
try { | ||
const response = await fetch(url, fullInit); | ||
if (response.status === 429) { | ||
const retryAfter = response.headers.get('Retry-After'); | ||
if (retryAfter) { | ||
await delay(parseInt(retryAfter, 10) * 1000); | ||
} else { | ||
await delay(1000); | ||
} | ||
continue; | ||
} | ||
if (response.status >= 400) { | ||
const error = await response.json(); | ||
throw new Error( | ||
(error as any)?.error.message ?? | ||
(typeof error === 'string' ? error : JSON.stringify(error)) | ||
); | ||
} | ||
return response; | ||
} catch (error) { | ||
if (tries >= retry) { | ||
throw error; | ||
} | ||
tries++; | ||
} | ||
} | ||
} |
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,8 @@ | ||
export * from './api-key'; | ||
export type { FetchOptions } from './fetch'; | ||
export * from './chat'; | ||
export * from './log-probs'; | ||
export * from './models'; | ||
export * from './text'; | ||
export * from './text-edit'; | ||
export * from './tokenization'; |
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.