Style upgrade

This commit is contained in:
retoor 2024-12-01 09:00:25 +01:00
parent 94e97f5c4e
commit 66e0e8aa4c
4 changed files with 40 additions and 39 deletions

View File

@ -20,12 +20,15 @@ yura ws://[host]:[port]/[path]/
## Python ## Python
```python ```python
import asyncio import asyncio
from yura.client import AsyncClient from yura.client import AsyncClient
async def communicate(): async def communicate():
client = AsyncClient("ws://[host]:[port]/[path]/") client = AsyncClient("ws://[host]:[port]/[path]/")
async for response in client.chat("Your prompt"): async for response in client.chat("Your prompt"):
print(response) print(response)
asyncio.run(communicate()) asyncio.run(communicate())
``` ```

6
make
View File

@ -1,14 +1,16 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pathlib
import os import os
import pathlib
import sys import sys
args = sys.argv[1:] args = sys.argv[1:]
args_string = " ".join(args) args_string = " ".join(args)
def install(): def install():
os.system("./.venv/bin/python -m pip install -e .") os.system("./.venv/bin/python -m pip install -e .")
def build(): def build():
os.system("./.venv/bin/python -m pip install build") os.system("./.venv/bin/python -m pip install build")
os.system("rm -r dist") os.system("rm -r dist")
@ -17,7 +19,6 @@ def build():
os.system("./.venv/bin/python -m black .") os.system("./.venv/bin/python -m black .")
if not pathlib.Path(".venv").exists(): if not pathlib.Path(".venv").exists():
os.system("python3 -m venv .venv") os.system("python3 -m venv .venv")
install() install()
@ -35,4 +36,3 @@ if "publish" in sys.argv:
if "run" in sys.argv: if "run" in sys.argv:
os.system("./.venv/bin/yura " + args_string) os.system("./.venv/bin/yura " + args_string)

View File

@ -1,7 +1,8 @@
from yura.client import cli_client
import asyncio import asyncio
import sys import sys
from yura.client import cli_client
def run(): def run():
try: try:

View File

@ -1,8 +1,9 @@
import asyncio import asyncio
import websockets
import json import json
import sys import sys
import websockets
class AsyncRPCClient: class AsyncRPCClient:
@ -27,21 +28,23 @@ class AsyncRPCClient:
response_raw = await ws.recv() response_raw = await ws.recv()
response = json.loads(response_raw) response = json.loads(response_raw)
yield response yield response
if response.get('done'): if response.get("done"):
break break
def __getattr__(self, name): def __getattr__(self, name):
async def call(*args, **kwargs): async def call(*args, **kwargs):
ws = await self.ws ws = await self.ws
await ws.send(json.dumps(dict( await ws.send(
method=name, json.dumps(
args=args, {"method": name, "args": args, "kwargs": kwargs}, default=str
kwargs=kwargs )
),default=str)) )
response = await ws.recv() response = await ws.recv()
return json.loads(response) return json.loads(response)
return call return call
class AsyncClient: class AsyncClient:
def __init__(self, url="ws://127.0.0.1:8470"): def __init__(self, url="ws://127.0.0.1:8470"):
@ -60,29 +63,23 @@ class AsyncClient:
pass pass
async def create(self, name, extends, system): async def create(self, name, extends, system):
return await self.client.create( return await self.client.create(name=name, extends=extends, system=system)
name=name,
extends=extends,
system=system
)
async def chat(self, token, message): async def chat(self, token, message):
yield await self.client.chat(uid=token, message=message) yield await self.client.chat(uid=token, message=message)
async for msg in self.client: async for msg in self.client:
yield msg yield msg
if msg.get('done'): if msg.get("done"):
break break
async def connect(self, name): async def connect(self, name):
return await self.client.connect(name) return await self.client.connect(name)
async def cli_client(url="ws://127.0.0.1:8470"): async def cli_client(url="ws://127.0.0.1:8470"):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
async_client = AsyncClient(url) AsyncClient(url)
async with AsyncClient(url) as client: async with AsyncClient(url) as client:
name = "retoor3b" name = "retoor3b"