Skip to content

Commit

Permalink
ADD github CI config & examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
karminski committed Jun 27, 2024
1 parent bbcd0fd commit 66c0973
Show file tree
Hide file tree
Showing 11 changed files with 4,947 additions and 20 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: CI

on:
push:
branches: ['main']
pull_request:
branches: ['main']

jobs:
commitlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: wagoid/commitlint-github-action@v4

lint:
needs: commitlint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: '18.x'
cache: 'npm'
- run: npm ci
- run: npm run lint

test:
needs: lint
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm i
- run: npm test
- run: npm run build --if-present
- run: npm run test:coverage
- name: Upload coverage to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage/lcov.info
flags: unittests
fail_ci_if_error: true
9 changes: 9 additions & 0 deletions DOCUMENTS/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

DOCUMENTS
---------

The use of the streamingjson library is very simple. There is only one object exported by the library, called ```Lexer```. ```Lexer``` has two public methods, which are ```AppendJSONString``` and ```CompleteJSON```:
- ```AppendJSONString``` takes a string as an argument and is responsible for receiving streaming JSON fragments.
- ```CompleteJSON``` has no arguments and returns a string. It is responsible for completing the JSON fragments received by ```AppendJSONString``` into a complete, syntactically valid JSON string.

For detailed usage, see [examples](../examples/).
74 changes: 74 additions & 0 deletions examples/gpt-function-call/demo-amd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<script src="../../node_modules/es5-shim/es5-shim.js"></script>
<script src="../../node_modules/es5-shim/es5-sham.js"></script>

<script src="./js/require.js"></script>
<script>
requirejs(['../../dist/index.aio.js'], function (streamingjson) {
// In GPT's chat completion stream mode, the request for tool_calls returns a structure as follows:
//
// {
// "id": "chatcmpl-?",
// "object": "chat.completion.chunk",
// "created": 1712000001,
// "model": "gpt-4-0125-preview",
// "system_fingerprint": "fp_?",
// "choices": [
// {
// "index": 0,
// "delta": {
// "tool_calls": [
// {
// "index": 0,
// "function": {
// "arguments": "{\"fi"
// }
// }
// ]
// },
// "logprobs": null,
// "finish_reason": null
// }
// ]
// }
//
// We need extract data.choices[0].delta.tool_calls[0].function.arguments.
// The arguments fiels is a JSON fragment, we can use steaming-json-go complete it to a syntactically correct JSON and Unmarshal it.

// We use string slice to simulate the arguments field in the return of GPT.
arguments = ['{"fu', 'nction', '_name', '"', ':', '"run', '_code', '", ', '"argu', 'ments"', ': ', '"print(', '\\"hello', ' world', '\\"', ')"'];

lexer = new streamingjson.Lexer();
arguments.forEach(jsonFragment => {
lexer.AppendJSONString(jsonFragment);
console.log(lexer.CompleteJSON());
});

// will print:
// {"fu":null}
// {"function":null}
// {"function_name":null}
// {"function_name":null}
// {"function_name":null}
// {"function_name":"run"}
// {"function_name":"run_code"}
// {"function_name":"run_code"}
// {"function_name":"run_code", "argu":null}
// {"function_name":"run_code", "arguments":null}
// {"function_name":"run_code", "arguments":null}
// {"function_name":"run_code", "arguments": "print("}
// {"function_name":"run_code", "arguments": "print(\"hello"}
// {"function_name":"run_code", "arguments": "print(\"hello world"}
// {"function_name":"run_code", "arguments": "print(\"hello world\""}
// {"function_name":"run_code", "arguments": "print(\"hello world\")"}

});
</script>
</body>
</html>
72 changes: 72 additions & 0 deletions examples/gpt-function-call/demo-global.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<script src="../../node_modules/es5-shim/es5-shim.js"></script>
<script src="../../node_modules/es5-shim/es5-sham.js"></script>

<script src="../../dist/index.aio.js"></script>
<script>
// In GPT's chat completion stream mode, the request for tool_calls returns a structure as follows:
//
// {
// "id": "chatcmpl-?",
// "object": "chat.completion.chunk",
// "created": 1712000001,
// "model": "gpt-4-0125-preview",
// "system_fingerprint": "fp_?",
// "choices": [
// {
// "index": 0,
// "delta": {
// "tool_calls": [
// {
// "index": 0,
// "function": {
// "arguments": "{\"fi"
// }
// }
// ]
// },
// "logprobs": null,
// "finish_reason": null
// }
// ]
// }
//
// We need extract data.choices[0].delta.tool_calls[0].function.arguments.
// The arguments fiels is a JSON fragment, we can use steaming-json-go complete it to a syntactically correct JSON and Unmarshal it.


// We use string slice to simulate the arguments field in the return of GPT.
arguments = ['{"fu', 'nction', '_name', '"', ':', '"run', '_code', '", ', '"argu', 'ments"', ': ', '"print(', '\\"hello', ' world', '\\"', ')"'];

lexer = new window['streaming-json'].Lexer()
arguments.forEach(jsonFragment => {
lexer.AppendJSONString(jsonFragment);
console.log(lexer.CompleteJSON());
});

// will print:
// {"fu":null}
// {"function":null}
// {"function_name":null}
// {"function_name":null}
// {"function_name":null}
// {"function_name":"run"}
// {"function_name":"run_code"}
// {"function_name":"run_code"}
// {"function_name":"run_code", "argu":null}
// {"function_name":"run_code", "arguments":null}
// {"function_name":"run_code", "arguments":null}
// {"function_name":"run_code", "arguments": "print("}
// {"function_name":"run_code", "arguments": "print(\"hello"}
// {"function_name":"run_code", "arguments": "print(\"hello world"}
// {"function_name":"run_code", "arguments": "print(\"hello world\""}
// {"function_name":"run_code", "arguments": "print(\"hello world\")"}
</script>
</body>
</html>
59 changes: 59 additions & 0 deletions examples/gpt-function-call/demo-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var streamingjson = require('../../dist/index.js');


// In GPT's chat completion stream mode, the request for tool_calls returns a structure as follows:
//
// {
// "id": "chatcmpl-?",
// "object": "chat.completion.chunk",
// "created": 1712000001,
// "model": "gpt-4-0125-preview",
// "system_fingerprint": "fp_?",
// "choices": [
// {
// "index": 0,
// "delta": {
// "tool_calls": [
// {
// "index": 0,
// "function": {
// "arguments": "{\"fi"
// }
// }
// ]
// },
// "logprobs": null,
// "finish_reason": null
// }
// ]
// }
//
// We need extract data.choices[0].delta.tool_calls[0].function.arguments.
// The arguments fiels is a JSON fragment, we can use steaming-json-go complete it to a syntactically correct JSON and Unmarshal it.

// We use string slice to simulate the arguments field in the return of GPT.
arguments = ['{"fu', 'nction', '_name', '"', ':', '"run', '_code', '", ', '"argu', 'ments"', ': ', '"print(', '\\"hello', ' world', '\\"', ')"'];

lexer = new streamingjson.Lexer();
arguments.forEach(jsonFragment => {
lexer.AppendJSONString(jsonFragment);
console.log(lexer.CompleteJSON());
});

// will print:
// {"fu":null}
// {"function":null}
// {"function_name":null}
// {"function_name":null}
// {"function_name":null}
// {"function_name":"run"}
// {"function_name":"run_code"}
// {"function_name":"run_code"}
// {"function_name":"run_code", "argu":null}
// {"function_name":"run_code", "arguments":null}
// {"function_name":"run_code", "arguments":null}
// {"function_name":"run_code", "arguments": "print("}
// {"function_name":"run_code", "arguments": "print(\"hello"}
// {"function_name":"run_code", "arguments": "print(\"hello world"}
// {"function_name":"run_code", "arguments": "print(\"hello world\""}
// {"function_name":"run_code", "arguments": "print(\"hello world\")"}
Loading

0 comments on commit 66c0973

Please sign in to comment.