🎨 style(constants.tsx): refactor getPythonApiCode, getCurlCode, and getPythonCode to separate functions
✨ feat(ApiModal): use getPythonApiCode and getCurlCode functions to generate python and curl code ✨ feat(cardComponent): add RenameLabel component to allow renaming of flow name and description 🐛 fix(cardComponent): fix CardDescription height to prevent overflow The getPythonApiCode, getCurlCode, and getPythonCode functions were refactored into separate functions to improve code readability and maintainability. The ApiModal component now uses the getPythonApiCode and getCurlCode functions to generate the python and curl code. The CardComponent now has a RenameLabel component that allows renaming of the flow name and description. The CardDescription height was also fixed to prevent overflow.
This commit is contained in:
parent
445f4013c1
commit
09bd1093dd
3 changed files with 95 additions and 46 deletions
|
|
@ -37,3 +37,47 @@ export const PROMPT_DIALOG_SUBTITLE = "Edit you prompt.";
|
||||||
* @constant
|
* @constant
|
||||||
*/
|
*/
|
||||||
export const TEXT_DIALOG_SUBTITLE = "Edit you text.";
|
export const TEXT_DIALOG_SUBTITLE = "Edit you text.";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to get the python code for the API
|
||||||
|
* @param {string} flowId - The id of the flow
|
||||||
|
* @returns {string} - The python code
|
||||||
|
*/
|
||||||
|
export const getPythonApiCode = (flowId: string): string => {
|
||||||
|
return `import requests
|
||||||
|
|
||||||
|
FLOW_ID = "${flowId}"
|
||||||
|
API_URL = f"${window.location.protocol}//${window.location.host}/predict/{FLOW_ID}"
|
||||||
|
|
||||||
|
def predict(message):
|
||||||
|
payload = {'message': message}
|
||||||
|
response = requests.post(API_URL, json=payload)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
print(predict("Your message"))`;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to get the curl code for the API
|
||||||
|
* @param {string} flowId - The id of the flow
|
||||||
|
* @returns {string} - The curl code
|
||||||
|
*/
|
||||||
|
export const getCurlCode = (flowId: string): string => {
|
||||||
|
return `curl -X POST \\
|
||||||
|
-H "Content-Type: application/json" \\
|
||||||
|
-d '{"message": "Your message"}' \\
|
||||||
|
${window.location.protocol}//${window.location.host}/predict/${flowId}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to get the python code for the API
|
||||||
|
* @param {string} flowName - The name of the flow
|
||||||
|
* @returns {string} - The python code
|
||||||
|
*/
|
||||||
|
export const getPythonCode = (flowName: string): string => {
|
||||||
|
return `from langflow import load_flow_from_json
|
||||||
|
|
||||||
|
flow = load_flow_from_json("${flowName}.json")
|
||||||
|
# Now you can use it like any chain
|
||||||
|
flow("Hey, have you heard of LangFlow?")`;
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import {
|
||||||
} from "../../components/ui/dialog";
|
} from "../../components/ui/dialog";
|
||||||
import { Button } from "../../components/ui/button";
|
import { Button } from "../../components/ui/button";
|
||||||
import { FlowType } from "src/types/flow";
|
import { FlowType } from "src/types/flow";
|
||||||
|
import { getCurlCode, getPythonApiCode, getPythonCode } from "../../constants";
|
||||||
|
|
||||||
export default function ApiModal({ flow }: { flow: FlowType }) {
|
export default function ApiModal({ flow }: { flow: FlowType }) {
|
||||||
const [open, setOpen] = useState(true);
|
const [open, setOpen] = useState(true);
|
||||||
|
|
@ -53,28 +54,10 @@ export default function ApiModal({ flow }: { flow: FlowType }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pythonApiCode = `import requests
|
const pythonApiCode = getPythonApiCode(flow.id);
|
||||||
|
|
||||||
FLOW_ID = "${flow.id}"
|
const curl_code = getCurlCode(flow.id);
|
||||||
API_URL = f"${window.location.protocol}//${window.location.host}/predict/{FLOW_ID}"
|
const pythonCode = getPythonCode(flow.name);
|
||||||
|
|
||||||
def predict(message):
|
|
||||||
payload = {'message': message}
|
|
||||||
response = requests.post(API_URL, json=payload)
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
print(predict("Your message"))`;
|
|
||||||
|
|
||||||
const curl_code = `curl -X POST \\
|
|
||||||
-H "Content-Type: application/json" \\
|
|
||||||
-d '{"message": "Your message"}' \\
|
|
||||||
${window.location.protocol}//${window.location.host}/predict/${flow.id}`;
|
|
||||||
|
|
||||||
const pythonCode = `from langflow import load_flow_from_json
|
|
||||||
|
|
||||||
flow = load_flow_from_json("${flow.name}.json")
|
|
||||||
# Now you can use it like any chain
|
|
||||||
flow("Hey, have you heard of LangFlow?")`;
|
|
||||||
|
|
||||||
const tabs = [
|
const tabs = [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import React from "react";
|
import React, { useContext, useState } from "react";
|
||||||
import {
|
import {
|
||||||
ArrowTopRightOnSquareIcon,
|
ArrowTopRightOnSquareIcon,
|
||||||
TrashIcon,
|
TrashIcon,
|
||||||
|
|
@ -15,6 +15,11 @@ import {
|
||||||
CardTitle,
|
CardTitle,
|
||||||
} from "../../../../components/ui/card";
|
} from "../../../../components/ui/card";
|
||||||
import { FlowType } from "../../../../types/flow";
|
import { FlowType } from "../../../../types/flow";
|
||||||
|
import RenameLabel from "../../../../components/ui/rename-label";
|
||||||
|
import _ from "lodash";
|
||||||
|
import { TabsContext } from "../../../../contexts/tabsContext";
|
||||||
|
import { alertContext } from "../../../../contexts/alertContext";
|
||||||
|
import { updateFlowInDatabase } from "../../../../controllers/API";
|
||||||
export const CardComponent = ({
|
export const CardComponent = ({
|
||||||
flow,
|
flow,
|
||||||
idx,
|
idx,
|
||||||
|
|
@ -28,31 +33,38 @@ export const CardComponent = ({
|
||||||
setTabIndex: (idx: number) => void;
|
setTabIndex: (idx: number) => void;
|
||||||
setActiveTab: (tab: string) => void;
|
setActiveTab: (tab: string) => void;
|
||||||
}) => {
|
}) => {
|
||||||
// flow has a style attribute
|
const { setErrorData } = useContext(alertContext);
|
||||||
// if it is empty just get a random style
|
const { updateFlow } = useContext(TabsContext);
|
||||||
// if it is not empty use that style
|
function handleSaveFlow(flow) {
|
||||||
// if it is not empty and it is not a valid style get a random style
|
try {
|
||||||
|
updateFlowInDatabase(flow);
|
||||||
|
updateFlow(flow);
|
||||||
|
// updateFlowStyleInDataBase(flow);
|
||||||
|
} catch (err) {
|
||||||
|
setErrorData(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const [rename, setRename] = useState(false);
|
||||||
|
|
||||||
let emoji = flow.style?.emoji || "🤖";
|
|
||||||
// get random tailwind color
|
|
||||||
let color = flow.style?.color || "bg-blue-200";
|
|
||||||
return (
|
return (
|
||||||
<Card className="group">
|
<Card className="group">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
<CardTitle className="flex justify-between items-start">
|
<CardTitle className="flex justify-between items-start">
|
||||||
<div className="flex gap-4 items-center">
|
<div className="flex gap-4 items-center">
|
||||||
{/* <span
|
<RenameLabel
|
||||||
className={
|
value={flow.name}
|
||||||
"rounded-md w-10 h-10 flex items-center justify-center text-2xl " +
|
setValue={(value) => {
|
||||||
color
|
if (value !== "") {
|
||||||
|
let newFlow = _.cloneDeep(flow);
|
||||||
|
newFlow.name = value;
|
||||||
|
handleSaveFlow(newFlow);
|
||||||
}
|
}
|
||||||
>
|
}}
|
||||||
{emoji}
|
rename={rename}
|
||||||
</span> */}
|
setRename={setRename}
|
||||||
{flow.name}
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex gap-5">
|
<div className="flex gap-5">
|
||||||
{/* make the icons shake a bit on hover */}
|
|
||||||
<Edit
|
<Edit
|
||||||
className="w-4"
|
className="w-4"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
|
@ -68,13 +80,23 @@ export const CardComponent = ({
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
<CardDescription className="pt-2 pb-2">
|
<CardDescription className="pt-2 pb-2 h-10">
|
||||||
<div className="truncate-doubleline">
|
{/* <div className="flex gap-2"> */}
|
||||||
{idx === 0
|
<RenameLabel
|
||||||
? "This flow creates an agent that accesses a department store database and APIs to monitor customer activity and overall storage."
|
className="truncate-doubleline w-full h-full"
|
||||||
: "This is a new Flow"}
|
placeholder="Description"
|
||||||
{/* {flow.description} */}
|
value={flow.description || "Description"}
|
||||||
</div>
|
setValue={(value) => {
|
||||||
|
if (value !== "") {
|
||||||
|
let newFlow = _.cloneDeep(flow);
|
||||||
|
newFlow.description = value;
|
||||||
|
handleSaveFlow(newFlow);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
rename={rename}
|
||||||
|
setRename={setRename}
|
||||||
|
/>
|
||||||
|
{/* </div> */}
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue