matrix_chatgpt_bot/src/flowise.py

44 lines
1.1 KiB
Python
Raw Normal View History

2023-06-05 03:27:37 +00:00
import aiohttp
2023-09-13 06:36:35 +00:00
async def flowise_query(
api_url: str, prompt: str, session: aiohttp.ClientSession, headers: dict = None
) -> 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})
return await response.json()
2023-09-13 06:36:35 +00:00
2023-06-05 03:27:37 +00:00
async def test():
session = aiohttp.ClientSession()
2023-09-13 06:36:35 +00:00
api_url = (
"http://127.0.0.1:3000/api/v1/prediction/683f9ea8-e670-4d51-b657-0886eab9cea1"
)
2023-06-05 03:27:37 +00:00
prompt = "What is the capital of France?"
response = await flowise_query(api_url, prompt, session)
print(response)
2023-09-13 06:36:35 +00:00
2023-06-05 03:27:37 +00:00
if __name__ == "__main__":
import asyncio
asyncio.run(test())