From dc4e0e5ce19972eeac53fbab7d10ead8ae941f10 Mon Sep 17 00:00:00 2001 From: Mendon Kissling <59585235+mendonk@users.noreply.github.com> Date: Sat, 2 Nov 2024 09:18:52 -0400 Subject: [PATCH] Docs: update api configuration (#4354) --- docs/docs/Configuration/596474918.png | Bin 2947 -> 0 bytes .../Configuration/configuration-api-keys.md | 236 ++++++++---------- 2 files changed, 109 insertions(+), 127 deletions(-) delete mode 100644 docs/docs/Configuration/596474918.png diff --git a/docs/docs/Configuration/596474918.png b/docs/docs/Configuration/596474918.png deleted file mode 100644 index eb1c8de37171df0561fa5a5625ce6c18c8189d2d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2947 zcmcImc{CK>9-m=|VI+()_N)<=tt3lj$u`-yY*WToV+q+AA)*Yj80l)wYgByVAK_Mz*@BnFN=x%BXkYjK*00;;Ju>6rQN((6XU#<_7 z1u*|>o(TX*@B)DTqk%Kh0nx8P4axuweXF(fQ?*# zI(A#wZm5y+>1gnihx>=^eD#yzM=kgFL=a*?9x-4tD9oWw&r(u{Ns^nph(Hie1+d%6 z=pf#vWUv~dL9*u`Ff-OB*v;%JB}%U|3}B8K0DCGrh>4G%cFQXjbPL_yuEub?769VA zu3Eq(fjo?DApe(bxXv>1tR?9TG?}(hyNtEZ>j}=7iT;HVR5pSM>Sv_{^a#iF6o#9A z{8j8_x}kWN^bm6`(nPQzPFUQT2E_NQzK6+gv5ZQGjxOATR&<}eZZ%Oke`dvd0;2rh zvmv*$efAI6W2`a1s9Yk<)#%)6;8_xE$u(+qG>s za_|s$2g_l6eRe}&Sp{0EOk3Kru(Rt9f}{#XMSSHKsE?u5o`x09M1o8@xQj zq~4(MpZjrVm_hBu#XD$}onypz))pxLwg3m3jKfTsL{?$e}PJ4E2_J^ueZ|9GFAY*>?#5@Dvq_8{o zc^Pi5$bUH3Ii59J2i#btd!r`8nm(_$_DzdAAP2Hg!~n7)b+oyGQ~0V~cS?2kVh0yB zHX=9lV)xeJ$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(#d8QDnJ83#nA5}eU5wv>BJSUv@YwX(8_PmIIvUl9pZ^LUf;2s=RVteBVMM~|KXR5>k0xwebxzj54Vz^IuwGY)+X!<0BK5EoFn~R`C z+7p*XEi6Aa-;@#2PzxauTo#qV!tuA|AN-_dFU9)B2gj;l!*_8HsXuCec$78dp5m`6 zm8I(q1=W`p>5h^vA30`Vf8ntW0tq2b$3m<+DRmXiYm-}bzW;y3P63ZowlY@N(U zj%VY)CQKA+bO||WJ4(O20++s>C)N@c8qWL=%f=<4-b#WlRAfWrhO^VYtaN5dX8zr? z1)uL6Wp0_%D4WQ}U6+P|!Gd^<*B(`&;|V8Egp*>8xNUnn*$95ehw;Z5;uOL{jULa) zGVx`}%3afEl1xoPlZW1x$f@(b0I*+?&_Vb=$$DZa*2)N42UX!&SP=G95#h>B;||GG z7l!F*Lem(|G{|OVhkaFw;OV^p1RF=uUP{RTJs6I`;gO5%E7^c}sn(#O^cQGlMke#{ z^vVHk8Kc|N=bJqG3_l70!O6;XDke+^QaXq}-!HW*k_*omNlm~3CLMFch-xbh8}<)* v{PfZ7OPmUj7dlalsJC@z@4Wi|INrXf&J(cPjZ9SgpI~dK2iL8>;rjTW(p+UB diff --git a/docs/docs/Configuration/configuration-api-keys.md b/docs/docs/Configuration/configuration-api-keys.md index e7f79b84a..f321ee957 100644 --- a/docs/docs/Configuration/configuration-api-keys.md +++ b/docs/docs/Configuration/configuration-api-keys.md @@ -4,48 +4,26 @@ sidebar_position: 1 slug: /configuration-api-keys --- - +Langflow provides an API key functionality that allows users to access their individual components and flows without traditional login authentication. The API key is a user-specific token that can be included in the request header, query parameter, or as a command line argument to authenticate API calls. This documentation outlines how to generate, use, and manage API keys in Langflow. :::info -This page may contain outdated information. It will be updated as soon as possible. +The default user and password are set using the LANGFLOW_SUPERUSER and LANGFLOW_SUPERUSER_PASSWORD environment variables. The default values are `langflow` and `langflow`, respectively. ::: - - - -Langflow provides an API key functionality that allows users to access their individual components and flows without traditional login authentication. The API key is a user-specific token that can be included in the request header or query parameter to authenticate API calls. This documentation outlines how to generate, use, and manage API keys in Langflow. - - -:::info - -The default user and password are set using the LANGFLOW_SUPERUSER and LANGFLOW_SUPERUSER_PASSWORD environment variables. The default values are langflow and langflow, respectively. - -::: - - - - -## Generate an API key {#c29986a69cad4cdbbe7537e383ea7207} - +## Generate an API key Generate a user-specific token to use with Langflow. +### Generate an API key with the Langflow UI -### Generate an API key with the Langflow UI {#3d90098ddd7c44b6836c0273acf57123} - -1. Click on the "API Key" icon. - - ![](./596474918.png) - -2. Click on "Create new secret key". -3. Give it an optional name. -4. Click on "Create secret key". -5. Copy the API key and store it in a secure location. - -### Generate an API key with the Langflow CLI {#2368f62fc4b8477e8080c9c2d3659d76} +1. Click your user icon and select **Settings**. +2. Click **Langflow API**, and then click **Add New**. +3. Name your key, and then click **Create Secret Key**. +4. Copy the API key and store it in a secure location. +### Generate an API key with the Langflow CLI ```shell langflow api-key @@ -64,148 +42,152 @@ python -m langflow api-key ``` - -## Use the Langflow API key {#ae787e4b0d3846aa9094fac75e0ac04f} - +## Authenticate requests with the Langflow API key Include your API key in API requests to authenticate requests to Langflow. +### Include the API key in the HTTP header -### Use the `x-api-key` header {#70965b3ad24d467ca4f90e7c13a1f394} - - -Include the `x-api-key` in the HTTP header when making API requests: - +To use the API key when making API requests with cURL, include the API key in the HTTP header. ```shell -curl -X POST \\ - \\ - -H 'Content-Type: application/json'\\ - -H 'x-api-key: '\\ +curl -X POST \ + "http://127.0.0.1:7860/api/v1/run/*`YOUR_FLOW_ID`*?stream=false" \ + -H 'Content-Type: application/json' \ + -H 'x-api-key: *`YOUR_API_KEY`*' \ -d '{"inputs": {"text":""}, "tweaks": {}}' - ``` +To instead pass the API key as a query parameter, do the following: -With Python using `requests`: +```shell +curl -X POST \ + "http://127.0.0.1:7860/api/v1/run/*`YOUR_FLOW_ID`*?x-api-key=*`YOUR_API_KEY`*?stream=false" \ + -H 'Content-Type: application/json' \ + -d '{"inputs": {"text":""}, "tweaks": {}}' +``` +To use the API key when making API requests with the Python `requests` library, include the API key as a variable string. ```python +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 = "http://127.0.0.1:7860" +FLOW_ID = "*`YOUR_FLOW_ID`*" +ENDPOINT = "" # You can set a specific endpoint name in the flow settings -BASE_API_URL = "" -FLOW_ID = "4441b773-0724-434e-9cee-19d995d8f2df" # You can tweak the flow by adding a tweaks dictionary # e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} -TWEAKS = {} +TWEAKS = { + "ChatInput-8a86T": {}, + "Prompt-pKfl9": {}, + "ChatOutput-WcGpD": {}, + "OpenAIModel-5UyvQ": {} +} -def run_flow(inputs: dict, - flow_id: str, - tweaks: Optional[dict] = None, - apiKey: Optional[str] = None) -> dict: +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 flow_id: The ID of the flow to run + :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}/{flow_id}" - - payload = {"inputs": inputs} - headers = {} + api_url = f"{BASE_API_URL}/api/v1/run/{endpoint}" + payload = { + "input_value": message, + "output_type": output_type, + "input_type": input_type, + } + headers = None if tweaks: payload["tweaks"] = tweaks - if apiKey: - headers = {"x-api-key": apiKey} - + if api_key: + headers = {"x-api-key": api_key} response = requests.post(api_url, json=payload, headers=headers) return response.json() -# Setup any tweaks you want to apply to the flow -inputs = {"text":""} -api_key = "" -print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS, apiKey=api_key)) +def main(): + parser = argparse.ArgumentParser(description="""Run a flow with a given message and optional tweaks. +Run it like: python .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.") + tweaks = upload_file(file_path=args.upload_file, host=BASE_API_URL, flow_id=args.endpoint, components=[args.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() ``` - -### Use the query parameter {#febb797f3bb5403b9f070afc0fa4f453} - - -Include the API key as a query parameter in the URL: - +To pass the API key to your script with a command line argument, do the following: ```shell -curl -X POST \\ - ?x-api-key= \\ - -H 'Content-Type: application/json'\\ - -d '{"inputs": {"text":""}, "tweaks": {}}' - +python your_script.py "*`YOUR_INPUT_MESSAGE`*" --api_key "*`YOUR_API_KEY`*" ``` - -With Python using `requests`: - - -```python -import requests - -BASE_API_URL = "" -FLOW_ID = "4441b773-0724-434e-9cee-19d995d8f2df" -# You can tweak the flow by adding a tweaks dictionary -# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} -TWEAKS = {} - -def run_flow(inputs: dict, - flow_id: str, - tweaks: Optional[dict] = None, - apiKey: Optional[str] = None) -> dict: - """ - Run a flow with a given message and optional tweaks. - - :param message: The message to send to the flow - :param flow_id: The ID of the flow to run - :param tweaks: Optional tweaks to customize the flow - :return: The JSON response from the flow - """ - api_url = f"{BASE_API_URL}/{flow_id}" - - payload = {"inputs": inputs} - headers = {} - - if tweaks: - payload["tweaks"] = tweaks - if apiKey: - api_url += f"?x-api-key={apiKey}" - - response = requests.post(api_url, json=payload, headers=headers) - return response.json() - -# Setup any tweaks you want to apply to the flow -inputs = {"text":""} -api_key = "" -print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS, apiKey=api_key)) - -``` - - -## Security Considerations {#1273eb69a61344d19827b30dba46dfd5} +## Security considerations - **Visibility**: For security reasons, the API key cannot be retrieved again through the UI. - **Scope**: The key allows access only to the flows and components of the specific user to whom it was issued. -## Custom API endpoint {#da933a86690a4fdeac24024472caf8a9} +## Custom API endpoint +To choose a custom name for your API endpoint, select **Project Settings** > **Endpoint Name** and name your endpoint. -Under **Project Settings** > **Endpoint Name**, you can pick a custom name for the endpoint used to call your flow from the API. +## Revoke an API key +To revoke an API key, delete it from the the list of keys in the **Settings** menu. -## Revoke an API Key {#f0ea41ea167845cea91bb5e8f90d9df0} +1. Click your user icon and select **Settings**. +2. Click **Langflow API**. +3. Select the keys you want to delete and click the trash can icon. - -To revoke an API key, delete it from the UI. This action immediately invalidates the key and prevents it from being used again. +This action immediately invalidates the key and prevents it from being used again.