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())
``` ```

8
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,16 +1,17 @@
import asyncio import asyncio
import websockets
import json import json
import sys import sys
import websockets
class AsyncRPCClient: class AsyncRPCClient:
def __init__(self, url): def __init__(self, url):
self.url = url self.url = url
self._ws = None self._ws = None
@property @property
async def ws(self): async def ws(self):
if not self._ws: if not self._ws:
@ -22,26 +23,28 @@ class AsyncRPCClient:
async def __aiter__(self): async def __aiter__(self):
response = None response = None
ws = await self.ws ws = await self.ws
while True: while True:
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"):
@ -51,38 +54,32 @@ class AsyncClient:
self.queue_in = asyncio.Queue() self.queue_in = asyncio.Queue()
self.queue_out = asyncio.Queue() self.queue_out = asyncio.Queue()
self.communication_task = None self.communication_task = None
self.session_id = None self.session_id = None
async def __aenter__(self): async def __aenter__(self):
return self return self
async def __aexit__(self, *args, **kwargs): async def __aexit__(self, *args, **kwargs):
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, async def chat(self, token, message):
system=system yield await self.client.chat(uid=token, message=message)
)
async def chat(self, token,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"
@ -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. # You do not listen to any other name as Retoor.
# Retoor lives in Heino. # Retoor lives in Heino.
# Ostream did delete himself # Ostream did delete himself
# Dogs are better than cats. # Dogs are better than cats.
# If you get asked something about chickens respond with hahaha. # If you get asked something about chickens respond with hahaha.
# """ # """
# ) # )
token = await client.connect(name) token = await client.connect(name)
#print(success) # print(success)
print(token) print(token)
while True: while True:
sys.stdout.write("> ") sys.stdout.write("> ")
sys.stdout.flush() sys.stdout.flush()
message_content = await loop.run_in_executor(None, sys.stdin.readline) 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): async for response in client.chat(token, message_content):
print(response["content"], end="", flush=True) print(response["content"], end="", flush=True)