refactor langflow calls
This commit is contained in:
parent
50d62c2cc0
commit
d33cf7ecc8
1 changed files with 66 additions and 88 deletions
|
|
@ -568,25 +568,17 @@ class AppClients:
|
||||||
self, name: str, value: str, modify: bool = False
|
self, name: str, value: str, modify: bool = False
|
||||||
):
|
):
|
||||||
"""Create a global variable in Langflow via API"""
|
"""Create a global variable in Langflow via API"""
|
||||||
api_key = await get_langflow_api_key()
|
|
||||||
if not api_key:
|
|
||||||
logger.warning(
|
|
||||||
"Cannot create Langflow global variable: No API key", variable_name=name
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
url = f"{LANGFLOW_URL}/api/v1/variables/"
|
|
||||||
payload = {
|
payload = {
|
||||||
"name": name,
|
"name": name,
|
||||||
"value": value,
|
"value": value,
|
||||||
"default_fields": [],
|
"default_fields": [],
|
||||||
"type": "Credential",
|
"type": "Credential",
|
||||||
}
|
}
|
||||||
headers = {"x-api-key": api_key, "Content-Type": "application/json"}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient() as client:
|
response = await self.langflow_request(
|
||||||
response = await client.post(url, headers=headers, json=payload)
|
"POST", "/api/v1/variables/", json=payload
|
||||||
|
)
|
||||||
|
|
||||||
if response.status_code in [200, 201]:
|
if response.status_code in [200, 201]:
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|
@ -620,21 +612,9 @@ class AppClients:
|
||||||
|
|
||||||
async def _update_langflow_global_variable(self, name: str, value: str):
|
async def _update_langflow_global_variable(self, name: str, value: str):
|
||||||
"""Update an existing global variable in Langflow via API"""
|
"""Update an existing global variable in Langflow via API"""
|
||||||
api_key = await get_langflow_api_key()
|
|
||||||
if not api_key:
|
|
||||||
logger.warning(
|
|
||||||
"Cannot update Langflow global variable: No API key", variable_name=name
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
headers = {"x-api-key": api_key, "Content-Type": "application/json"}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
# First, get all variables to find the one with the matching name
|
# First, get all variables to find the one with the matching name
|
||||||
get_response = await client.get(
|
get_response = await self.langflow_request("GET", "/api/v1/variables/")
|
||||||
f"{LANGFLOW_URL}/api/v1/variables/", headers=headers
|
|
||||||
)
|
|
||||||
|
|
||||||
if get_response.status_code != 200:
|
if get_response.status_code != 200:
|
||||||
logger.error(
|
logger.error(
|
||||||
|
|
@ -670,10 +650,8 @@ class AppClients:
|
||||||
"default_fields": target_variable.get("default_fields", []),
|
"default_fields": target_variable.get("default_fields", []),
|
||||||
}
|
}
|
||||||
|
|
||||||
patch_response = await client.patch(
|
patch_response = await self.langflow_request(
|
||||||
f"{LANGFLOW_URL}/api/v1/variables/{variable_id}",
|
"PATCH", f"/api/v1/variables/{variable_id}", json=update_payload
|
||||||
headers=headers,
|
|
||||||
json=update_payload,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if patch_response.status_code == 200:
|
if patch_response.status_code == 200:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue