-
Notifications
You must be signed in to change notification settings - Fork 61
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
Support combining multiple output variables. #737
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
https://localhost:* | ||
http://localhost:* | ||
http://webhook:8080 | ||
https://api.openai.com/v1/chat/completions |
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
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,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { split } from "../helpers" | ||
|
||
export class OutputReference { | ||
chapter_id: string | ||
output_name: string | ||
|
||
constructor(chapter_id: string, output_name: string) { | ||
this.chapter_id = chapter_id | ||
this.output_name = output_name | ||
} | ||
|
||
/** | ||
* Parses a string and returns a collection of output references that it may contain. | ||
* | ||
* @param str a string | ||
* @returns an array of output references | ||
*/ | ||
static parse(str: string): OutputReference[] { | ||
const pattern = /\$\{([^}]+)\}/g | ||
let match | ||
var result = [] | ||
while ((match = pattern.exec(str)) !== null) { | ||
const spl = split(match[1], '.', 2) | ||
result.push(new OutputReference(spl[0], spl[1])) | ||
} | ||
return result | ||
} | ||
|
||
/** | ||
* Replaces occurrences of output references. | ||
* Takes special care of preserving the type of the reference value if the entire string is a reference. | ||
* | ||
* @param str a string that may contain output references | ||
* @param callback a callback for fetching reference values | ||
* @returns a string with output references replaced by their values | ||
*/ | ||
static replace(str: string, callback: (chapter_id: any, variable: any) => string): any { | ||
let full_match = str.match(/^\$\{([^}]+)\}$/) | ||
if (full_match) { | ||
// the entire string is a reference, preserve the type of the returned value | ||
const spl = split(full_match[1], '.', 2) | ||
return callback(spl[0], spl[1]) | ||
} else return str.replace(/\$\{([^}]+)\}/g, (_, k) => { | ||
const spl = split(k, '.', 2) | ||
return callback(spl[0], spl[1]) | ||
}) | ||
} | ||
} |
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
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,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { OutputReference } from "tester/OutputReference"; | ||
|
||
describe('OutputReference', () => { | ||
let f = (id: any, k: any): string => `[${id}:${k}]` | ||
|
||
describe('parse', () => { | ||
it('replaces', () => { | ||
expect(OutputReference.parse('string')).toEqual([]) | ||
expect(OutputReference.parse('${k.v}')).toEqual([new OutputReference('k', 'v')]) | ||
expect(OutputReference.parse('${k.value}')).toEqual([new OutputReference('k', 'value')]) | ||
expect(OutputReference.parse('${k.v.m}')).toEqual([new OutputReference('k', 'v.m')]) | ||
expect(OutputReference.parse('A reference to ${k.v.m} and ${x.y}.')).toEqual([ | ||
new OutputReference('k', 'v.m'), | ||
new OutputReference('x', 'y') | ||
]) | ||
}) | ||
}) | ||
|
||
describe('replace', () => { | ||
it('replaces', () => { | ||
expect(OutputReference.replace('string', f)).toEqual('string') | ||
expect(OutputReference.replace('${k.v}', f)).toEqual('[k:v]') | ||
expect(OutputReference.replace('${k.value}', f)).toEqual('[k:value]') | ||
expect(OutputReference.replace('${k.v.m}', f)).toEqual('[k:v.m]') | ||
expect(OutputReference.replace('A reference to ${k.v.m} and ${x} and ${x.y}.', f)).toEqual( | ||
'A reference to [k:v.m] and [x:undefined] and [x:y].' | ||
) | ||
}) | ||
}) | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is the only place the
replace
function is used, then having a generic replace function that can handle any callback is a bit over-engineered, IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's best we keep the parsing regex values/logic in one place so it doesn't leak into the usage of it.