-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Python API code for uploading file is incorrect #2799
Comments
To address the issues with the Python API code for uploading a file generated by
Here is the corrected version of the code: import argparse
import json
from argparse import RawTextHelpFormatter
import requests
from typing import Optional
import warnings
try:
from langflow.load import upload_file
except ImportError:
warnings.warn("Langflow provides a function to help you upload files to the flow. Please install langflow to use it.")
upload_file = None
BASE_API_URL = f"{window.location.protocol}//{window.location.host}/api/v1"
FLOW_ID = "{flowId}"
ENDPOINT = "{endpointName or ''}" # The endpoint name of the flow
# You can tweak the flow by adding a tweaks dictionary
# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}}
TWEAKS = {tweaksString}
def run_flow(message: str,
endpoint: str,
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.
:param message: The message to send to the flow
:param endpoint: The ID or the endpoint name of the flow
:param tweaks: Optional tweaks to customize the flow
:return: The JSON response from the flow
"""
api_url = f"{BASE_API_URL}/run/{endpoint}"
payload = {
"input_value": message,
"output_type": output_type,
"input_type": input_type,
}
headers = None
if tweaks:
payload["tweaks"] = tweaks
if api_key:
headers = {"x-api-key": api_key}
response = requests.post(api_url, json=payload, headers=headers)
return response.json()
def main():
parser = argparse.ArgumentParser(description="""Run a flow with a given message and optional tweaks.\nRun it like: python <your file>.py "your message here" --endpoint "your_endpoint" --tweaks '{"key": "value"}'""",
formatter_class=RawTextHelpFormatter)
parser.add_argument("message", type=str, help="The message to send to the flow")
parser.add_argument("--endpoint", type=str, default=ENDPOINT or FLOW_ID, help="The ID or the endpoint name of the flow")
parser.add_argument("--tweaks", type=str, help="JSON string representing the tweaks to customize the flow", default=json.dumps(TWEAKS))
parser.add_argument("--api_key", type=str, help="API key for authentication", default=None)
parser.add_argument("--output_type", type=str, default="chat", help="The output type")
parser.add_argument("--input_type", type=str, default="chat", help="The input type")
parser.add_argument("--upload_file", type=str, help="Path to the file to upload", default=None)
parser.add_argument("--components", type=str, help="Components to upload the file to", default=None)
args = parser.parse_args()
try:
tweaks = json.loads(args.tweaks)
except json.JSONDecodeError:
raise ValueError("Invalid tweaks JSON string")
if args.upload_file:
if not upload_file:
raise ImportError("Langflow is not installed. Please install it to use the upload_file function.")
elif not args.components:
raise ValueError("You need to provide the components to upload the file to.")
components = args.components.split(",") # Ensure components are treated as a list
tweaks = upload_file(file_path=args.upload_file, host=BASE_API_URL, flow_id=ENDPOINT, components=components, tweaks=tweaks)
response = run_flow(
message=args.message,
endpoint=args.endpoint,
output_type=args.output_type,
input_type=args.input_type,
tweaks=tweaks,
api_key=args.api_key
)
print(json.dumps(response, indent=2))
if __name__ == "__main__":
main() Changes Made:
This should resolve the issues you encountered with the Python API code for uploading a file [1]. |
Hey @vertgo Good catch. I'll fix that ASAP. Thank you! |
@ogabrielluiz sorry I wouldn't consider this closed:
(i think, unless you're fixing it in a different commit) |
@ogabrielluiz made a PR here: |
…n issue #2799 (#2815) fixing errors in a file uploading flow, as in issue #2799 Co-authored-by: Gabriel Luiz Freitas Almeida <[email protected]>
Bug Description
When getting the API code for a flow, the Python window shows code to execute. It doesn't work if you want to upload a file
There are several problems:
the code is templated in get-python-api-code.tsx and it is not correct
it specifies the base_api_url to include /api/v1/run
BASE_API_URL = "${window.location.protocol}//${window.location.host}/api/v1/run"
and then it calls upload_file (in load/utils.py) which appends /api/v1/upload so the full path to the upload endpoint is wrong, which results in a 405 error when you try to upload
In upload it checks if the response from the server is 200, and throws an error if it isn't. If error 1 is fixed, the server actually responds with code 201, which is fine, but the code still thinks it's an error and throws an exception
if that is corrected to allow code 201:
3.the component is passed in as a string from command line argument, and yet when it assigns the returned file path from the json response, it assigns it to each letter in the component (so instead of adding a tweaks key of "FILE-ABCD" it does "F", "I", "L", "E" each with its own tweak, and the tweaks for FILE-ABCD is untouched)
if that is fixed by turning the components into a list before calling upload_file
4. it assigns the returned path from the response to the key "file_path" in the tweaks, but the key is now just "path" so it does not load the correct file still
5.Also if endpoint is "" it weirdly doesn't evaluate to false, so you have to specify the endpoint and the flowId or make the endpoint None. (or just hardcode the flowid in)
Reproduction
Expected behavior
It should upload a file and set that file to the path of the component passed in via args
Who can help?
@anovazzi1
@ogabrielluiz
Operating System
macOS
Langflow Version
1.0.7, 1.0.9, 1.0.10
Python Version
The text was updated successfully, but these errors were encountered: