fix tweaks been displayed as an array (#1929)

🐛 (ApiModal): remove workaround for the issue with the tweak object
being an array as it has been fixed
🐛 (utils.ts): fix the issue with the tweaksBuildedObject being an array
instead of an object by accessing the first element of the array
This commit is contained in:
Cristhian Zanforlin Lousa 2024-05-20 16:36:22 -03:00 • committed by GitHub
commit 15b6b151dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 33 deletions

View file

@ -37,17 +37,12 @@ const ApiModal = forwardRef(
flow: FlowType; flow: FlowType;
children: ReactNode; children: ReactNode;
}, },
ref ref,
) => { ) => {
let tweak = useTweaksStore((state) => state.tweak); const tweak = useTweaksStore((state) => state.tweak);
const addTweaks = useTweaksStore((state) => state.setTweak); const addTweaks = useTweaksStore((state) => state.setTweak);
const setTweaksList = useTweaksStore((state) => state.setTweaksList); const setTweaksList = useTweaksStore((state) => state.setTweaksList);
const tweaksList = useTweaksStore((state) => state.tweaksList); const tweaksList = useTweaksStore((state) => state.tweaksList);
// ! Workdaround to fix the issue with the tweak object being an array
// TODO: Fix the issue with the tweak object being an array
if (Array.isArray(tweak) && tweak.length === 1) {
tweak = tweak[0];
}
const [activeTweaks, setActiveTweaks] = useState(false); const [activeTweaks, setActiveTweaks] = useState(false);
const { autoLogin } = useContext(AuthContext); const { autoLogin } = useContext(AuthContext);
@ -113,7 +108,7 @@ const ApiModal = forwardRef(
buildTweakObject( buildTweakObject(
nodeId, nodeId,
element.data.node.template[templateField].value, element.data.node.template[templateField].value,
element.data.node.template[templateField] element.data.node.template[templateField],
); );
} }
}); });
@ -130,7 +125,7 @@ const ApiModal = forwardRef(
async function buildTweakObject( async function buildTweakObject(
tw: string, tw: string,
changes: string | string[] | boolean | number | Object[] | Object, changes: string | string[] | boolean | number | Object[] | Object,
template: TemplateVariableType template: TemplateVariableType,
) { ) {
changes = getChangesType(changes, template); changes = getChangesType(changes, template);
@ -167,12 +162,6 @@ const ApiModal = forwardRef(
} }
const addCodes = (cloneTweak) => { const addCodes = (cloneTweak) => {
// if cloneTweak is an array and it's lenght is 1, then it's a single tweak
// so just get the first element
if (Array.isArray(cloneTweak) && cloneTweak.length === 1) {
cloneTweak = cloneTweak[0];
}
const pythonApiCode = getPythonApiCode(flow?.id, autoLogin, cloneTweak); const pythonApiCode = getPythonApiCode(flow?.id, autoLogin, cloneTweak);
const curl_code = getCurlCode(flow?.id, autoLogin, cloneTweak); const curl_code = getCurlCode(flow?.id, autoLogin, cloneTweak);
const pythonCode = getPythonCode(flow?.name, cloneTweak); const pythonCode = getPythonCode(flow?.name, cloneTweak);
@ -217,7 +206,7 @@ const ApiModal = forwardRef(
</BaseModal.Content> </BaseModal.Content>
</BaseModal> </BaseModal>
); );
} },
); );
export default ApiModal; export default ApiModal;

View file

@ -60,7 +60,7 @@ export function normalCaseToSnakeCase(str: string): string {
export function toTitleCase( export function toTitleCase(
str: string | undefined, str: string | undefined,
isNodeField?: boolean isNodeField?: boolean,
): string { ): string {
if (!str) return ""; if (!str) return "";
let result = str let result = str
@ -69,7 +69,7 @@ export function toTitleCase(
if (isNodeField) return word; if (isNodeField) return word;
if (index === 0) { if (index === 0) {
return checkUpperWords( return checkUpperWords(
word[0].toUpperCase() + word.slice(1).toLowerCase() word[0].toUpperCase() + word.slice(1).toLowerCase(),
); );
} }
return checkUpperWords(word.toLowerCase()); return checkUpperWords(word.toLowerCase());
@ -82,7 +82,7 @@ export function toTitleCase(
if (isNodeField) return word; if (isNodeField) return word;
if (index === 0) { if (index === 0) {
return checkUpperWords( return checkUpperWords(
word[0].toUpperCase() + word.slice(1).toLowerCase() word[0].toUpperCase() + word.slice(1).toLowerCase(),
); );
} }
return checkUpperWords(word.toLowerCase()); return checkUpperWords(word.toLowerCase());
@ -122,7 +122,7 @@ export function groupByFamily(
data: APIDataType, data: APIDataType,
baseClasses: string, baseClasses: string,
left: boolean, left: boolean,
flow?: NodeType[] flow?: NodeType[],
): groupedObjType[] { ): groupedObjType[] {
const baseClassesSet = new Set(baseClasses.split("\n")); const baseClassesSet = new Set(baseClasses.split("\n"));
let arrOfPossibleInputs: Array<{ let arrOfPossibleInputs: Array<{
@ -148,7 +148,7 @@ export function groupByFamily(
baseClassesSet.has(template.type)) || baseClassesSet.has(template.type)) ||
(template.input_types && (template.input_types &&
template.input_types.some((inputType) => template.input_types.some((inputType) =>
baseClassesSet.has(inputType) baseClassesSet.has(inputType),
))) )))
); );
}; };
@ -168,7 +168,7 @@ export function groupByFamily(
hasBaseClassInBaseClasses: hasBaseClassInBaseClasses:
foundNode?.hasBaseClassInBaseClasses || foundNode?.hasBaseClassInBaseClasses ||
nodeData.node!.base_classes.some((baseClass) => nodeData.node!.base_classes.some((baseClass) =>
baseClassesSet.has(baseClass) baseClassesSet.has(baseClass),
), //seta como anterior ou verifica se o node tem base class ), //seta como anterior ou verifica se o node tem base class
displayName: nodeData.node?.display_name, displayName: nodeData.node?.display_name,
}); });
@ -185,10 +185,10 @@ export function groupByFamily(
if (!foundNode) { if (!foundNode) {
foundNode = { foundNode = {
hasBaseClassInTemplate: Object.values(node!.template).some( hasBaseClassInTemplate: Object.values(node!.template).some(
checkBaseClass checkBaseClass,
), ),
hasBaseClassInBaseClasses: node!.base_classes.some((baseClass) => hasBaseClassInBaseClasses: node!.base_classes.some((baseClass) =>
baseClassesSet.has(baseClass) baseClassesSet.has(baseClass),
), ),
displayName: node?.display_name, displayName: node?.display_name,
}; };
@ -245,7 +245,7 @@ export function getRandomDescription(): string {
export function getRandomName( export function getRandomName(
retry: number = 0, retry: number = 0,
noSpace: boolean = false, noSpace: boolean = false,
maxRetries: number = 3 maxRetries: number = 3,
): string { ): string {
const left: string[] = ADJECTIVES; const left: string[] = ADJECTIVES;
const right: string[] = NOUNS; const right: string[] = NOUNS;
@ -324,8 +324,9 @@ export function getChatInputField(flowState?: FlowState) {
export function getPythonApiCode( export function getPythonApiCode(
flowId: string, flowId: string,
isAuth: boolean, isAuth: boolean,
tweaksBuildedObject tweaksBuildedObject,
): string { ): string {
const tweaksObject = tweaksBuildedObject[0];
return `import requests return `import requests
from typing import Optional from typing import Optional
@ -333,7 +334,7 @@ BASE_API_URL = "${window.location.protocol}//${window.location.host}/api/v1/run"
FLOW_ID = "${flowId}" FLOW_ID = "${flowId}"
# You can tweak the flow by adding a tweaks dictionary # You can tweak the flow by adding a tweaks dictionary
# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} # e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}}
TWEAKS = ${JSON.stringify(tweaksBuildedObject, null, 2)} TWEAKS = ${JSON.stringify(tweaksObject, null, 2)}
def run_flow(message: str, def run_flow(message: str,
flow_id: str, flow_id: str,
@ -381,8 +382,10 @@ print(run_flow(message=message, flow_id=FLOW_ID, tweaks=TWEAKS${
export function getCurlCode( export function getCurlCode(
flowId: string, flowId: string,
isAuth: boolean, isAuth: boolean,
tweaksBuildedObject tweaksBuildedObject,
): string { ): string {
const tweaksObject = tweaksBuildedObject[0];
return `curl -X POST \\ return `curl -X POST \\
${window.location.protocol}//${ ${window.location.protocol}//${
window.location.host window.location.host
@ -393,7 +396,7 @@ export function getCurlCode(
-d '{"input_value": "message", -d '{"input_value": "message",
"output_type": "chat", "output_type": "chat",
"input_type": "chat", "input_type": "chat",
"tweaks": ${JSON.stringify(tweaksBuildedObject, null, 2)}' "tweaks": ${JSON.stringify(tweaksObject, null, 2)}'
`; `;
} }
@ -421,8 +424,10 @@ export function getOutputIds(flow) {
* @returns {string} - The python code * @returns {string} - The python code
*/ */
export function getPythonCode(flowName: string, tweaksBuildedObject): string { export function getPythonCode(flowName: string, tweaksBuildedObject): string {
const tweaksObject = tweaksBuildedObject[0];
return `from langflow.load import run_flow_from_json return `from langflow.load import run_flow_from_json
TWEAKS = ${JSON.stringify(tweaksBuildedObject, null, 2)} TWEAKS = ${JSON.stringify(tweaksObject, null, 2)}
result = run_flow_from_json(flow="${flowName}.json", result = run_flow_from_json(flow="${flowName}.json",
input_value="message", input_value="message",
@ -438,7 +443,7 @@ result = run_flow_from_json(flow="${flowName}.json",
export function getWidgetCode( export function getWidgetCode(
flowId: string, flowId: string,
flowName: string, flowName: string,
isAuth: boolean isAuth: boolean,
): string { ): string {
return `<script src="https://cdn.jsdelivr.net/gh/langflow-ai/langflow-embedded-chat@1.0_alpha/dist/build/static/js/bundle.min.js"></script> return `<script src="https://cdn.jsdelivr.net/gh/langflow-ai/langflow-embedded-chat@1.0_alpha/dist/build/static/js/bundle.min.js"></script>
@ -563,7 +568,7 @@ export function checkLocalStorageKey(key: string): boolean {
export function IncrementObjectKey( export function IncrementObjectKey(
object: object, object: object,
key: string key: string,
): { newKey: string; increment: number } { ): { newKey: string; increment: number } {
let count = 1; let count = 1;
const type = removeCountFromString(key); const type = removeCountFromString(key);
@ -635,7 +640,7 @@ export function getSetFromObject(obj: object, key?: string): Set<string> {
export function getFieldTitle( export function getFieldTitle(
template: APITemplateType, template: APITemplateType,
templateField: string templateField: string,
): string { ): string {
return template[templateField].display_name return template[templateField].display_name
? template[templateField].display_name! ? template[templateField].display_name!