Refactor getChatInputField function and update API code
This commit is contained in:
parent
bb19581750
commit
fd2200af69
1 changed files with 35 additions and 35 deletions
|
|
@ -289,11 +289,10 @@ export function buildTweakObject(tweak: tweakType) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to get Chat Input Field
|
* Function to get Chat Input Field
|
||||||
* @param {FlowType} flow - The current flow.
|
|
||||||
* @param {FlowsState} tabsState - The current tabs state.
|
* @param {FlowsState} tabsState - The current tabs state.
|
||||||
* @returns {string} - The chat input field
|
* @returns {string} - The chat input field
|
||||||
*/
|
*/
|
||||||
export function getChatInputField(flow: FlowType, flowState?: FlowState) {
|
export function getChatInputField(flowState?: FlowState) {
|
||||||
let chat_input_field = "text";
|
let chat_input_field = "text";
|
||||||
|
|
||||||
if (flowState && flowState.input_keys) {
|
if (flowState && flowState.input_keys) {
|
||||||
|
|
@ -305,13 +304,14 @@ export function getChatInputField(flow: FlowType, flowState?: FlowState) {
|
||||||
/**
|
/**
|
||||||
* Function to get the python code for the API
|
* Function to get the python code for the API
|
||||||
* @param {string} flowId - The id of the flow
|
* @param {string} flowId - The id of the flow
|
||||||
|
* @param {boolean} isAuth - If the API is authenticated
|
||||||
|
* @param {any[]} tweak - The tweaks
|
||||||
* @returns {string} - The python code
|
* @returns {string} - The python code
|
||||||
*/
|
*/
|
||||||
export function getPythonApiCode(
|
export function getPythonApiCode(
|
||||||
flow: FlowType,
|
flow: FlowType,
|
||||||
isAuth: boolean,
|
isAuth: boolean,
|
||||||
tweak?: any[],
|
tweak?: any[]
|
||||||
flowState?: FlowState
|
|
||||||
): string {
|
): string {
|
||||||
const flowId = flow.id;
|
const flowId = flow.id;
|
||||||
|
|
||||||
|
|
@ -320,13 +320,10 @@ export function getPythonApiCode(
|
||||||
// node.data.id
|
// node.data.id
|
||||||
// }
|
// }
|
||||||
const tweaks = buildTweaks(flow);
|
const tweaks = buildTweaks(flow);
|
||||||
const inputs = buildInputs();
|
|
||||||
return `import requests
|
return `import requests
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
BASE_API_URL = "${window.location.protocol}//${
|
BASE_API_URL = "${window.location.protocol}//${window.location.host}/api/v1/run"
|
||||||
window.location.host
|
|
||||||
}/api/v1/process"
|
|
||||||
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"}}
|
||||||
|
|
@ -336,9 +333,12 @@ TWEAKS = ${
|
||||||
: JSON.stringify(tweaks, null, 2)
|
: JSON.stringify(tweaks, null, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
def run_flow(inputs: dict, flow_id: str, tweaks: Optional[dict] = None${
|
def run_flow(message: str,
|
||||||
!isAuth ? `, api_key: Optional[str] = None` : ""
|
flow_id: str,
|
||||||
}) -> dict:
|
output_type: str = "chat",
|
||||||
|
input_type: str = "chat",
|
||||||
|
tweaks: Optional[dict] = None,
|
||||||
|
api_key: Optional[str] = None) -> dict:
|
||||||
"""
|
"""
|
||||||
Run a flow with a given message and optional tweaks.
|
Run a flow with a given message and optional tweaks.
|
||||||
|
|
||||||
|
|
@ -349,7 +349,11 @@ def run_flow(inputs: dict, flow_id: str, tweaks: Optional[dict] = None${
|
||||||
"""
|
"""
|
||||||
api_url = f"{BASE_API_URL}/{flow_id}"
|
api_url = f"{BASE_API_URL}/{flow_id}"
|
||||||
|
|
||||||
payload = {"inputs": inputs}
|
payload = {
|
||||||
|
"input_value": message,
|
||||||
|
"output_type": output_type,
|
||||||
|
"input_type": input_type,
|
||||||
|
}
|
||||||
headers = None
|
headers = None
|
||||||
if tweaks:
|
if tweaks:
|
||||||
payload["tweaks"] = tweaks
|
payload["tweaks"] = tweaks
|
||||||
|
|
@ -359,9 +363,9 @@ def run_flow(inputs: dict, flow_id: str, tweaks: Optional[dict] = None${
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
# Setup any tweaks you want to apply to the flow
|
# Setup any tweaks you want to apply to the flow
|
||||||
inputs = ${inputs}
|
message = "message"
|
||||||
${!isAuth ? `api_key = "<your api key>"` : ""}
|
${!isAuth ? `api_key = "<your api key>"` : ""}
|
||||||
print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS${
|
print(run_flow(message=message, flow_id=FLOW_ID, tweaks=TWEAKS${
|
||||||
!isAuth ? `, api_key=api_key` : ""
|
!isAuth ? `, api_key=api_key` : ""
|
||||||
}))`;
|
}))`;
|
||||||
}
|
}
|
||||||
|
|
@ -369,28 +373,27 @@ print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS${
|
||||||
/**
|
/**
|
||||||
* Function to get the curl code for the API
|
* Function to get the curl code for the API
|
||||||
* @param {string} flowId - The id of the flow
|
* @param {string} flowId - The id of the flow
|
||||||
|
* @param {boolean} isAuth - If the API is authenticated
|
||||||
* @returns {string} - The curl code
|
* @returns {string} - The curl code
|
||||||
*/
|
*/
|
||||||
export function getCurlCode(
|
export function getCurlCode(
|
||||||
flow: FlowType,
|
flow: FlowType,
|
||||||
isAuth: boolean,
|
isAuth: boolean,
|
||||||
tweak?: any[],
|
tweak?: any[]
|
||||||
flowState?: FlowState
|
|
||||||
): string {
|
): string {
|
||||||
const flowId = flow.id;
|
const flowId = flow.id;
|
||||||
const tweaks = buildTweaks(flow);
|
const tweaks = buildTweaks(flow);
|
||||||
const inputs = buildInputs();
|
|
||||||
|
|
||||||
const arrayOfOutputs = getOutputIds(flow);
|
|
||||||
|
|
||||||
return `curl -X POST \\
|
return `curl -X POST \\
|
||||||
${window.location.protocol}//${window.location.host}/api/v1/run/${flowId} \\
|
${window.location.protocol}//${
|
||||||
|
window.location.host
|
||||||
|
}/api/v1/run/${flowId}?stream=false \\
|
||||||
-H 'Content-Type: application/json'\\${
|
-H 'Content-Type: application/json'\\${
|
||||||
!isAuth ? `\n -H 'x-api-key: <your api key>'\\` : ""
|
!isAuth ? `\n -H 'x-api-key: <your api key>'\\` : ""
|
||||||
}
|
}
|
||||||
-d '{"inputs": [${inputs}],
|
-d '{"input_value": "message",
|
||||||
"outputs": [${arrayOfOutputs}],
|
"output_type": "chat",
|
||||||
"stream": false,
|
"input_type": "chat",
|
||||||
"tweaks": ${
|
"tweaks": ${
|
||||||
tweak && tweak.length > 0
|
tweak && tweak.length > 0
|
||||||
? buildTweakObject(tweak)
|
? buildTweakObject(tweak)
|
||||||
|
|
@ -419,26 +422,23 @@ export function getOutputIds(flow) {
|
||||||
/**
|
/**
|
||||||
* Function to get the python code for the API
|
* Function to get the python code for the API
|
||||||
* @param {string} flow - The current flow
|
* @param {string} flow - The current flow
|
||||||
|
* @param {any[]} tweak - The tweaks
|
||||||
* @returns {string} - The python code
|
* @returns {string} - The python code
|
||||||
*/
|
*/
|
||||||
export function getPythonCode(
|
export function getPythonCode(flow: FlowType, tweak?: any[]): string {
|
||||||
flow: FlowType,
|
|
||||||
tweak?: any[],
|
|
||||||
flowState?: FlowState
|
|
||||||
): string {
|
|
||||||
const flowName = flow.name;
|
const flowName = flow.name;
|
||||||
const tweaks = buildTweaks(flow);
|
const tweaks = buildTweaks(flow);
|
||||||
const inputs = buildInputs();
|
|
||||||
return `from langflow.load import load_flow_from_json
|
return `from langflow.load import run_flow_from_json
|
||||||
TWEAKS = ${
|
TWEAKS = ${
|
||||||
tweak && tweak.length > 0
|
tweak && tweak.length > 0
|
||||||
? buildTweakObject(tweak)
|
? buildTweakObject(tweak)
|
||||||
: JSON.stringify(tweaks, null, 2)
|
: JSON.stringify(tweaks, null, 2)
|
||||||
}
|
}
|
||||||
flow = load_flow_from_json("${flowName}.json", tweaks=TWEAKS)
|
|
||||||
# Now you can use it like any chain
|
result = run_flow_from_json(flow="${flowName}.json",
|
||||||
inputs = ${inputs}
|
input_value="message",
|
||||||
flow(inputs)`;
|
tweaks=TWEAKS)`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -454,7 +454,7 @@ export function getWidgetCode(
|
||||||
const flowId = flow.id;
|
const flowId = flow.id;
|
||||||
const flowName = flow.name;
|
const flowName = flow.name;
|
||||||
const inputs = buildInputs();
|
const inputs = buildInputs();
|
||||||
let chat_input_field = getChatInputField(flow, flowState);
|
let chat_input_field = getChatInputField(flowState);
|
||||||
|
|
||||||
return `<script src="https://cdn.jsdelivr.net/gh/logspace-ai/langflow-embedded-chat@main/dist/build/static/js/bundle.min.js"></script>
|
return `<script src="https://cdn.jsdelivr.net/gh/logspace-ai/langflow-embedded-chat@main/dist/build/static/js/bundle.min.js"></script>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue