-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multiple JSON blocks bug fixes Sometimes two JSON blocks of different types would not be found properly. This has been fixed.
- Loading branch information
1 parent
b264096
commit 88eb937
Showing
7 changed files
with
183 additions
and
25 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,7 @@ | ||
--- | ||
"@llm-ui/json": patch | ||
--- | ||
|
||
Multiple JSON blocks bug fixes | ||
|
||
Sometimes two JSON blocks of different types would not be found properly. This has been fixed. |
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
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
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { regexMatcher } from "./regexMatcher"; | ||
export { regexMatcher, regexMatcherGlobal } from "./regexMatcher"; | ||
export { removeStartEndChars } from "./removeStartEndChars"; |
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
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 |
---|---|---|
@@ -1,18 +1,41 @@ | ||
import { MaybeLLMOutputMatch } from "@llm-ui/react"; | ||
import { LLMOutputMatch, MaybeLLMOutputMatch } from "@llm-ui/react"; | ||
|
||
const regexMatchToLLmOutputMatch = ( | ||
regexMatch: RegExpMatchArray | null, | ||
): MaybeLLMOutputMatch => { | ||
if (regexMatch) { | ||
const matchString = regexMatch[0]; | ||
const startIndex = regexMatch.index!; | ||
const endIndex = startIndex + matchString.length; | ||
return { | ||
startIndex, | ||
endIndex, | ||
outputRaw: matchString, | ||
}; | ||
} | ||
return undefined; | ||
}; | ||
|
||
export const regexMatcher = | ||
(regex: RegExp) => | ||
(llmOutput: string): MaybeLLMOutputMatch => { | ||
const regexMatch = llmOutput.match(regex); | ||
if (regexMatch) { | ||
const matchString = regexMatch[0]; | ||
const startIndex = regexMatch.index!; | ||
const endIndex = startIndex + matchString.length; | ||
return { | ||
startIndex, | ||
endIndex, | ||
outputRaw: matchString, | ||
}; | ||
if (regex.global) { | ||
throw new Error("regexMatcher does not support global regexes"); | ||
} | ||
return regexMatchToLLmOutputMatch(llmOutput.match(regex)); | ||
}; | ||
|
||
export const regexMatcherGlobal = | ||
(regex: RegExp) => | ||
(llmOutput: string): LLMOutputMatch[] => { | ||
if (!regex.global) { | ||
throw new Error("regexMatcherGlobal does not support non-global regexes"); | ||
} | ||
const matches = Array.from(llmOutput.matchAll(regex)); | ||
if (!matches) { | ||
return []; | ||
} | ||
return undefined; | ||
return matches | ||
.map((m) => regexMatchToLLmOutputMatch(m)) | ||
.filter((m) => m !== undefined) as LLMOutputMatch[]; | ||
}; |