2023-09-13 07:27:34 +00:00
|
|
|
import httpx
|
2023-06-05 03:27:37 +00:00
|
|
|
|
2023-09-13 06:36:35 +00:00
|
|
|
|
|
|
|
async def flowise_query(
|
2023-09-13 07:27:34 +00:00
|
|
|
api_url: str, prompt: str, session: httpx.AsyncClient, headers: dict = None
|
2023-09-13 06:36:35 +00:00
|
|
|
) -> str:
|
2023-06-05 03:27:37 +00:00
|
|
|
"""
|
|
|
|
Sends a query to the Flowise API and returns the response.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
api_url (str): The URL of the Flowise API.
|
|
|
|
prompt (str): The question to ask the API.
|
|
|
|
session (aiohttp.ClientSession): The aiohttp session to use.
|
|
|
|
headers (dict, optional): The headers to use. Defaults to None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The response from the API.
|
|
|
|
"""
|
|
|
|
if headers:
|
|
|
|
response = await session.post(
|
2023-09-13 06:36:35 +00:00
|
|
|
api_url,
|
|
|
|
json={"question": prompt},
|
|
|
|
headers=headers,
|
2023-06-05 03:27:37 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
response = await session.post(api_url, json={"question": prompt})
|
2023-09-13 07:27:34 +00:00
|
|
|
return await response.text()
|
2023-06-05 03:27:37 +00:00
|
|
|
|
2023-09-13 06:36:35 +00:00
|
|
|
|
2023-06-05 03:27:37 +00:00
|
|
|
async def test():
|
2023-09-13 07:27:34 +00:00
|
|
|
async with httpx.AsyncClient() as session:
|
|
|
|
api_url = "http://127.0.0.1:3000/api/v1/prediction/683f9ea8-e670-4d51-b657-0886eab9cea1"
|
|
|
|
prompt = "What is the capital of France?"
|
|
|
|
response = await flowise_query(api_url, prompt, session)
|
|
|
|
print(response)
|
2023-06-05 03:27:37 +00:00
|
|
|
|
2023-09-13 06:36:35 +00:00
|
|
|
|
2023-06-05 03:27:37 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
asyncio.run(test())
|