mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
add chat testing client
This commit is contained in:
parent
849f02cf54
commit
6fd874c2cc
30
tests/chat.py
Normal file
30
tests/chat.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import anyio
|
||||||
|
import httpx
|
||||||
|
import orjson
|
||||||
|
|
||||||
|
async def chat_with_endpoint():
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
while True:
|
||||||
|
# Get user input
|
||||||
|
prompt = input("\nYou: ")
|
||||||
|
if prompt.lower() == "exit":
|
||||||
|
break
|
||||||
|
|
||||||
|
# Send request to the API
|
||||||
|
print("\nModel: ", end="", flush=True)
|
||||||
|
async with client.stream(
|
||||||
|
"POST",
|
||||||
|
"http://localhost:8000/chat/",
|
||||||
|
data={"prompt": prompt},
|
||||||
|
timeout=60
|
||||||
|
) as response:
|
||||||
|
async for chunk in response.aiter_lines():
|
||||||
|
if chunk:
|
||||||
|
try:
|
||||||
|
data = orjson.loads(chunk)
|
||||||
|
print(data["content"], end="", flush=True)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\nError parsing chunk: {e}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
anyio.run(chat_with_endpoint)
|
Loading…
x
Reference in New Issue
Block a user