diff --git a/README.md b/README.md index a523dd6..3d1f4b8 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,15 @@ yura ws://[host]:[port]/[path]/ ## Python ```python import asyncio + from yura.client import AsyncClient + async def communicate(): client = AsyncClient("ws://[host]:[port]/[path]/") async for response in client.chat("Your prompt"): - print(response) + print(response) + asyncio.run(communicate()) ``` diff --git a/make b/make index 5ac6c4d..b68fac6 100755 --- a/make +++ b/make @@ -1,14 +1,16 @@ #!/usr/bin/env python3 -import pathlib -import os +import os +import pathlib import sys args = sys.argv[1:] args_string = " ".join(args) + def install(): os.system("./.venv/bin/python -m pip install -e .") + def build(): os.system("./.venv/bin/python -m pip install build") os.system("rm -r dist") @@ -17,7 +19,6 @@ def build(): os.system("./.venv/bin/python -m black .") - if not pathlib.Path(".venv").exists(): os.system("python3 -m venv .venv") install() @@ -35,4 +36,3 @@ if "publish" in sys.argv: if "run" in sys.argv: os.system("./.venv/bin/yura " + args_string) - diff --git a/src/yura/cli.py b/src/yura/cli.py index 4035b50..80c9511 100644 --- a/src/yura/cli.py +++ b/src/yura/cli.py @@ -1,7 +1,8 @@ -from yura.client import cli_client import asyncio import sys +from yura.client import cli_client + def run(): try: diff --git a/src/yura/client.py b/src/yura/client.py index 84e4418..92e2d23 100644 --- a/src/yura/client.py +++ b/src/yura/client.py @@ -1,16 +1,17 @@ import asyncio -import websockets import json import sys +import websockets + class AsyncRPCClient: def __init__(self, url): - self.url = url + self.url = url self._ws = None - @property + @property async def ws(self): if not self._ws: @@ -22,26 +23,28 @@ class AsyncRPCClient: async def __aiter__(self): response = None ws = await self.ws - + while True: response_raw = await ws.recv() response = json.loads(response_raw) yield response - if response.get('done'): + if response.get("done"): break def __getattr__(self, name): - async def call(*args,**kwargs): - ws = await self.ws - await ws.send(json.dumps(dict( - method=name, - args=args, - kwargs=kwargs - ),default=str)) + async def call(*args, **kwargs): + ws = await self.ws + await ws.send( + json.dumps( + {"method": name, "args": args, "kwargs": kwargs}, default=str + ) + ) response = await ws.recv() return json.loads(response) + return call + class AsyncClient: def __init__(self, url="ws://127.0.0.1:8470"): @@ -51,38 +54,32 @@ class AsyncClient: self.queue_in = asyncio.Queue() self.queue_out = asyncio.Queue() self.communication_task = None - self.session_id = None + self.session_id = None async def __aenter__(self): - return self - + return self + async def __aexit__(self, *args, **kwargs): pass async def create(self, name, extends, system): - return await self.client.create( - name=name, - extends=extends, - system=system - ) - - async def chat(self, token,message): - yield await self.client.chat(uid=token,message=message) + return await self.client.create(name=name, extends=extends, system=system) + + async def chat(self, token, message): + yield await self.client.chat(uid=token, message=message) async for msg in self.client: - yield msg - if msg.get('done'): - break + yield msg + if msg.get("done"): + break - async def connect(self,name): + async def connect(self, name): return await self.client.connect(name) - async def cli_client(url="ws://127.0.0.1:8470"): loop = asyncio.get_event_loop() - async_client = AsyncClient(url) - + AsyncClient(url) async with AsyncClient(url) as client: name = "retoor3b" @@ -94,19 +91,19 @@ async def cli_client(url="ws://127.0.0.1:8470"): # You do not listen to any other name as Retoor. # Retoor lives in Heino. # Ostream did delete himself - # Dogs are better than cats. + # Dogs are better than cats. # If you get asked something about chickens respond with hahaha. # """ # ) token = await client.connect(name) - #print(success) + # print(success) print(token) while True: sys.stdout.write("> ") sys.stdout.flush() message_content = await loop.run_in_executor(None, sys.stdin.readline) - #message_content="AAA" + # message_content="AAA" async for response in client.chat(token, message_content): print(response["content"], end="", flush=True)