Skip to content

Commit

Permalink
feat(v2): switch to generated fetch API stub
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Pilar committed Nov 27, 2023
1 parent 40871a8 commit 80effcc
Show file tree
Hide file tree
Showing 24 changed files with 4,241 additions and 2,461 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GENAI_DEFAULT_ENDPOINT=https://workbench-api.res.ibm.com
GENAI_DEFAULT_ENDPOINT=https://bam-api.res.ibm.com
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/dist/**/*
.eslintrc.cjs
tsup.config.ts

# generated files
/src/api/schema.d.ts
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/*
v18.12.0
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules/
.yarn/
yarn.lock
.pnp.*

/src/api/schema.d.ts
112 changes: 54 additions & 58 deletions examples/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,67 @@ const client = new Client({
const multipleInputs = loadGenerateInput();
const singleInput = multipleInputs[0];

{
// Use with a single input to get a promise
const output = await client.generate(singleInput);
console.log(output);
}
// {
// // Use with a single input to get a promise
// const output = await client.generate(singleInput);
// console.log(output);
// }

{
// Or supply a callback
client.generate(singleInput, (err, output) => {
if (err) console.error(err);
else console.log(output);
});
}
// {
// // Or supply a callback
// client.generate(singleInput, (err, output) => {
// if (err) console.error(err);
// else console.log(output);
// });
// }

{
// Use with multiple inputs to get a promise
const outputs = await Promise.all(client.generate(multipleInputs));
console.log(outputs);
// {
// // Use with multiple inputs to get a promise
// const outputs = await Promise.all(client.generate(multipleInputs));
// console.log(outputs);

// Or supply a callback which will be called for each output
// Callback is guaranteed to be called in the order of respective inputs
client.generate(multipleInputs, (err, output) => {
if (err) console.error(err);
else console.log(output);
});
// // Or supply a callback which will be called for each output
// // Callback is guaranteed to be called in the order of respective inputs
// client.generate(multipleInputs, (err, output) => {
// if (err) console.error(err);
// else console.log(output);
// });

// The method is optimized for sequential await, order the inputs accordingly
for (const outputPromise of client.generate(multipleInputs)) {
try {
console.log(await outputPromise);
} catch (err) {
console.error(err);
}
}
}
// // The method is optimized for sequential await, order the inputs accordingly
// for (const outputPromise of client.generate(multipleInputs)) {
// try {
// console.log(await outputPromise);
// } catch (err) {
// console.error(err);
// }
// }
// }

{
// Streaming (callback style)
client.generate(
singleInput,
{
stream: true,
},
(err, output) => {
if (err) {
console.error(err);
} else if (output === null) {
// END of stream
} else {
console.log(output.stop_reason);
console.log(output.generated_token_count);
console.log(output.input_token_count);
console.log(output.generated_text);
}
},
);
}
// {
// // Streaming (callback style)
// client.generate(
// singleInput,
// {
// stream: true,
// },
// (err, output) => {
// if (err) {
// console.error(err);
// } else if (output === null) {
// // END of stream
// } else {
// console.log(output.stop_reason);
// console.log(output.generated_token_count);
// console.log(output.input_token_count);
// console.log(output.generated_text);
// }
// },
// );
// }

{
// Streaming (async iterators)
const stream = client.generate(singleInput, {
stream: true,
});
const stream = client.generation_stream(singleInput);
for await (const chunk of stream) {
console.log(chunk.stop_reason);
console.log(chunk.generated_token_count);
Expand All @@ -82,9 +80,7 @@ const singleInput = multipleInputs[0];

{
// Streaming (built-in stream methods)
const stream = client.generate(singleInput, {
stream: true,
});
const stream = client.generation_stream(singleInput);
stream.on('data', (chunk) => {
console.log(chunk.stop_reason);
console.log(chunk.generated_token_count);
Expand Down
12 changes: 4 additions & 8 deletions examples/load_input.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { readFileSync } from 'fs';

import { GenerateInputSchema } from '../src/index.js';

export const loadGenerateInput = () =>
// Input files usually follow JSONL format
readFileSync('examples/assets/generate_input.jsonl', 'utf8')
.split('\n')
.map((line) => JSON.stringify(line))
.map((input) =>
GenerateInputSchema.parse({
model_id: 'default',
input,
}),
);
.map((input) => ({
model_id: 'google/flan-ul2',
input,
}));
8 changes: 8 additions & 0 deletions examples/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Client } from '../src/index.js';

const client = new Client({
apiKey: process.env.GENAI_API_KEY,
});

const models = await client.models({ limit: 100, offset: 0 });
console.log(models);
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@
"prepack": "yarn build && pinst --disable",
"postinstall": "husky install",
"postpack": "pinst --enable",
"generate": "npx openapi-typescript https://bam-api.res.ibm.com/docs/json -o ./src/api/schema.d.ts",
"example:run": "ts-node -r dotenv-flow/config",
"example:generate": "yarn run example:run examples/generate.ts",
"example:tune": "yarn run example:run examples/tune.ts",
"example:prompt-template": "yarn run example:run examples/prompt-templates.ts",
"example:file": "yarn run example:run examples/file.ts",
"example:chat": "yarn run example:run examples/chat.ts"
"example:chat": "yarn run example:run examples/chat.ts",
"example:models": "yarn run example:run examples/models.ts"
},
"peerDependencies": {
"langchain": ">=0.0.155"
Expand All @@ -71,7 +73,6 @@
"@commitlint/config-conventional": "^18.0.0",
"@types/lodash": "^4.14.200",
"@types/node": "^20.8.8",
"@types/promise-retry": "^1.1.5",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"@vitest/coverage-c8": "^0.31.2",
Expand All @@ -87,6 +88,7 @@
"lint-staged": "^15.0.2",
"lodash": "^4.17.21",
"msw": "^1.3.2",
"openapi-typescript": "^6.7.1",
"pinst": "^3.0.0",
"prettier": "^3.0.3",
"ts-node": "^10.9.1",
Expand All @@ -96,15 +98,15 @@
},
"dependencies": {
"@ai-zen/node-fetch-event-source": "^2.1.2",
"axios": "^1.5.1",
"axios-cache-interceptor": "^1.3.2",
"cross-fetch": "^4.0.0",
"fetch-retry": "^5.0.6",
"form-data": "^4.0.0",
"promise-retry": "^2.0.1",
"openapi-fetch": "^0.8.1",
"yaml": "^2.3.3",
"zod": "^3.22.4"
},
"engines": {
"node": ">=16.10.0"
"node": ">=18.12.0"
},
"lint-staged": {
"*.{cjs,js,jsx,ts,tsx}": [
Expand Down
Loading

0 comments on commit 80effcc

Please sign in to comment.