Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LlamaIndexTS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
run-llama
LlamaIndexTS
Commits
26a90435
Commit
26a90435
authored
1 year ago
by
Elliot Kang
Browse files
Options
Downloads
Patches
Plain Diff
Revert "Simplified OutputParser"
This reverts commit
ff0e831d
.
parent
f6efaba9
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.husky/pre-commit
+4
-0
4 additions, 0 deletions
.husky/pre-commit
.husky/pre-push
+4
-0
4 additions, 0 deletions
.husky/pre-push
packages/core/src/OutputParser.ts
+46
-6
46 additions, 6 deletions
packages/core/src/OutputParser.ts
with
54 additions
and
6 deletions
.husky/pre-commit
0 → 100755
+
4
−
0
View file @
26a90435
#!/usr/bin/env sh
.
"
$(
dirname
--
"
$0
"
)
/_/husky.sh"
pnpm lint
This diff is collapsed.
Click to expand it.
.husky/pre-push
0 → 100755
+
4
−
0
View file @
26a90435
#!/usr/bin/env sh
.
"
$(
dirname
--
"
$0
"
)
/_/husky.sh"
pnpm
test
This diff is collapsed.
Click to expand it.
packages/core/src/OutputParser.ts
+
46
−
6
View file @
26a90435
...
@@ -56,14 +56,54 @@ class OutputParserError extends Error {
...
@@ -56,14 +56,54 @@ class OutputParserError extends Error {
function
parseJsonMarkdown
(
text
:
string
)
{
function
parseJsonMarkdown
(
text
:
string
)
{
text
=
text
.
trim
();
text
=
text
.
trim
();
//This code is more general than the previous version, and should be faster.
const
beginDelimiter
=
"
```json
"
;
const
beginIndex
=
text
.
indexOf
(
"
[
"
);
const
endDelimiter
=
"
```
"
;
const
endIndex
=
text
.
lastIndexOf
(
"
]
"
);
const
jsonText
=
text
.
substring
(
beginIndex
,
endIndex
+
1
);
const
beginIndex
=
text
.
indexOf
(
beginDelimiter
);
try
{
const
endIndex
=
text
.
indexOf
(
endDelimiter
,
beginIndex
+
beginDelimiter
.
length
,
);
//Scenario 1: LLM follows instruction format. However, it doesn't always do this.
if
(
!
(
beginIndex
===
-
1
||
endIndex
===
-
1
))
{
const
jsonText
=
text
.
substring
(
beginIndex
+
beginDelimiter
.
length
,
endIndex
,
);
return
JSON
.
parse
(
jsonText
);
return
JSON
.
parse
(
jsonText
);
}
//Scenario 2: LLM follows instruction format roughly, but doesn't do this exactly.
// For example: [```json] part was not returned, or there are irregular \n spaces.
try
{
//This isn't a JSON markdown, but we should try again with something else.
//Try to get data_str to be a list of JSON objects
const
new_data_str
:
string
[]
=
text
.
replace
(
"
[
"
,
"
"
)
.
replace
(
"
]
"
,
"
"
)
.
replace
(
"
\n
"
,
"
"
)
.
trim
()
//Warning: This regex might be slow.
.
split
(
/
(?=
},
)
/g
);
const
arr_length
=
new_data_str
.
length
;
//String formatting
//First to penultimate element
for
(
let
i
=
0
;
i
<
arr_length
-
1
;
i
++
)
{
new_data_str
[
i
]
+=
"
}
"
;
}
//Second to final element
for
(
let
i
=
1
;
i
<
arr_length
;
i
++
)
{
new_data_str
[
i
]
=
new_data_str
[
i
].
replace
(
"
},
"
,
"
"
);
}
const
output
:
object
[]
=
new_data_str
.
map
((
item
)
=>
JSON
.
parse
(
item
));
return
output
;
}
catch
(
e
)
{
}
catch
(
e
)
{
throw
new
OutputParserError
(
"
Not a json markdown
"
,
{
output
:
text
});
//In the worst case scenario and our options are exhausted, throw error.
throw
new
OutputParserError
(
"
Not a valid json
"
,
{
cause
:
e
as
Error
,
output
:
text
,
});
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment