-
Notifications
You must be signed in to change notification settings - Fork 1
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
11 changed files
with
4,947 additions
and
20 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,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 |
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,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/). |
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,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> |
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,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> |
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,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\")"} |
Oops, something went wrong.