Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): switch to generated fetch API stub #64

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.18.2
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);
20 changes: 9 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"genai",
"ibm"
],
"homepage": "https://workbench.res.ibm.com",
"homepage": "https://bam.res.ibm.com",
"license": "MIT",
"repository": {
"type": "git",
Expand Down Expand Up @@ -56,12 +56,14 @@
"prepack": "yarn build && pinst --disable",
"postinstall": "husky install",
"postpack": "pinst --enable",
"generate": "./scripts/generate.sh",
"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/core": ">=0.1.11"
Expand All @@ -71,8 +73,7 @@
"@commitlint/config-conventional": "^18.0.0",
"@langchain/core": "^0.1.11",
"@types/lodash": "^4.14.200",
"@types/node": "^20.8.8",
"@types/promise-retry": "^1.1.5",
"@types/node": "18.18.2",
"@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,16 +98,12 @@
},
"dependencies": {
"@ai-zen/node-fetch-event-source": "^2.1.2",
"axios": "^1.5.1",
"axios-cache-interceptor": "^1.3.2",
"form-data": "^4.0.0",
"promise-retry": "^2.0.1",
"cross-fetch": "^4.0.0",
"fetch-retry": "^5.0.6",
"openapi-fetch": "^0.8.1",
"yaml": "^2.3.3",
"zod": "^3.22.4"
},
"engines": {
"node": ">=16.10.0"
},
"lint-staged": {
"*.{cjs,js,jsx,ts,tsx}": [
"eslint --fix"
Expand Down
4 changes: 4 additions & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

source .env
openapi-typescript $GENAI_DEFAULT_ENDPOINT/docs/json -o ./src/api/schema.d.ts
Loading
Loading