Stream kinda working

This commit is contained in:
Cyber MacGeddon 2025-05-02 16:19:43 +01:00
parent bba84dbe39
commit 3d307cfb89
5 changed files with 87 additions and 30 deletions

View file

@ -1,7 +1,6 @@
from pulsar.schema import JsonSchema from pulsar.schema import JsonSchema
from .. schema import EmbeddingsRequest, EmbeddingsResponse from .. schema import EmbeddingsRequest, EmbeddingsResponse
from .. schema import embeddings_request_queue, embeddings_response_queue
from . base import BaseClient from . base import BaseClient
import _pulsar import _pulsar
@ -23,12 +22,6 @@ class EmbeddingsClient(BaseClient):
pulsar_api_key=None, pulsar_api_key=None,
): ):
if input_queue == None:
input_queue=embeddings_request_queue
if output_queue == None:
output_queue=embeddings_response_queue
super(EmbeddingsClient, self).__init__( super(EmbeddingsClient, self).__init__(
log_level=log_level, log_level=log_level,
subscriber=subscriber, subscriber=subscriber,
@ -43,4 +36,3 @@ class EmbeddingsClient(BaseClient):
def request(self, text, timeout=300): def request(self, text, timeout=300):
return self.call(text=text, timeout=timeout).vectors return self.call(text=text, timeout=timeout).vectors

View file

@ -11,6 +11,7 @@ from . triples_query import TriplesQueryRequestor
from . embeddings import EmbeddingsRequestor from . embeddings import EmbeddingsRequestor
from . graph_embeddings_query import GraphEmbeddingsQueryRequestor from . graph_embeddings_query import GraphEmbeddingsQueryRequestor
from . prompt import PromptRequestor from . prompt import PromptRequestor
from . triples_stream import TriplesStream
request_response_dispatchers = { request_response_dispatchers = {
"agent": AgentRequestor, "agent": AgentRequestor,
@ -23,6 +24,10 @@ request_response_dispatchers = {
"triples-query": TriplesQueryRequestor, "triples-query": TriplesQueryRequestor,
} }
receive_dispatchers = {
"embeddings": TriplesStream,
}
class TestDispatcher: class TestDispatcher:
def __init__(self, pulsar_client, timeout=120): def __init__(self, pulsar_client, timeout=120):
self.pulsar_client = pulsar_client self.pulsar_client = pulsar_client
@ -113,8 +118,46 @@ class DispatcherManager:
def dispatch_flow_service(self): def dispatch_flow_service(self):
return self return self
def dispatch_socket_service(self): def dispatch_flow_receive(self):
return TestDispatcher3(pulsar_client = self.pulsar_client).dispatch return self.dispatch_receive
async def dispatch_receive(self, ws, running, params):
print("HERE")
flow = params.get("flow")
kind = params.get("kind")
if flow not in self.flows:
raise RuntimeError("Invalid flow")
if kind not in receive_dispatchers:
raise RuntimeError("Invalid kind")
key = (flow, kind)
if key in self.dispatchers:
return self.dispatchers[key]
intf_defs = self.flows[flow]["interfaces"]
if kind not in intf_defs:
raise RuntimeError("This kind not supported by flow")
qconfig = intf_defs[kind]
dispatcher = receive_dispatchers[kind](
pulsar_client = self.pulsar_client,
ws = ws,
running = running,
# FIXME!
queue = qconfig["response"],
consumer = f"api-gateway-{flow}-{kind}-request",
subscriber = f"api-gateway-{flow}-{kind}-request",
)
self.dispatchers[key] = dispatcher
return dispatcher
async def process(self, data, responder, params): async def process(self, data, responder, params):

View file

@ -3,44 +3,59 @@ import asyncio
import queue import queue
import uuid import uuid
from ... schema import Triples from ... schema import Triples, EmbeddingsResponse
from ... base import Subscriber from ... base import Subscriber
from . serialize import serialize_triples from . serialize import serialize_triples
class TriplesStream: class TriplesStream:
def __init__(self, ws, running, pulsar_client, queue): def __init__(
self, ws, running, pulsar_client, queue, consumer, subscriber
):
self.ws = ws self.ws = ws
self.running = runnning self.running = running
self.pulsar_client = pulsar_client self.pulsar_client = pulsar_client
self.queue = queue self.queue = queue
self.consumer = consumer
self.subscriber = subscriber
async def destroy(self): async def destroy(self):
self.running.stop() self.running.stop()
self.ws.close() await self.ws.close()
async def receive(self, msg): async def receive(self, msg):
print(msg.data) print(msg.data)
async def run(self, ws, running): async def run(self):
self.subscriber = Subscriber( print("c", self.consumer)
pulsar_client, queue, print("s", self.subscriber)
"api-gateway", "api-gateway", print("q", self.queue)
schema=Triples
subs = Subscriber(
client = self.pulsar_client, topic = self.queue,
consumer_name = self.consumer, subscription = self.subscriber,
# schema = Triples
schema = EmbeddingsResponse
) )
await self.subscriber.start() await subs.start()
id = str(uuid.uuid4()) id = str(uuid.uuid4())
q = self.subscriber.subscribe_all(id) q = await subs.subscribe_all(id)
while running.get(): while self.running.get():
try: try:
resp = await asyncio.to_thread(q.get, timeout=0.5)
await ws.send_json(serialize_triples(resp)) resp = await asyncio.wait_for(q.get(), timeout=0.5)
# await self.ws.send_json(serialize_triples(resp))
print("GOT MESSAGE!!!")
await self.ws.send_json(str(resp))
except TimeoutError: except TimeoutError:
continue continue
@ -52,8 +67,10 @@ class TriplesStream:
print(f"Exception: {str(e)}", flush=True) print(f"Exception: {str(e)}", flush=True)
break break
self.subscriber.unsubscribe_all(id) await subs.unsubscribe_all(id)
await ws.close() await subs.stop()
running.stop()
await self.ws.close()
self.running.stop()

View file

@ -33,9 +33,9 @@ class FlowEndpointManager:
dispatcher = dispatcher_manager.dispatch_flow_service(), dispatcher = dispatcher_manager.dispatch_flow_service(),
), ),
SocketEndpoint( SocketEndpoint(
endpoint_path = "/api/v1/test2", endpoint_path = "/api/v1/flow/{flow}/receive/{kind}",
auth = auth, auth = auth,
dispatcher = dispatcher_manager.dispatch_socket_service() dispatcher = dispatcher_manager.dispatch_flow_receive()
), ),
] ]

View file

@ -26,10 +26,13 @@ class SocketEndpoint:
async def listener(self, ws, dispatcher, running): async def listener(self, ws, dispatcher, running):
print("In listener...!!!!!!!!!!!!!!!!!!!!")
async for msg in ws: async for msg in ws:
# On error, finish # On error, finish
print ("mt:", msg.type)
if msg.type == WSMsgType.TEXT: if msg.type == WSMsgType.TEXT:
print("YER!!!!!")
await dispatcher.receive(msg) await dispatcher.receive(msg)
continue continue
elif msg.type == WSMsgType.BINARY: elif msg.type == WSMsgType.BINARY:
@ -63,7 +66,9 @@ class SocketEndpoint:
running = Running() running = Running()
print("Create...") print("Create...")
dispatcher = await self.dispatcher(ws, running, request) dispatcher = await self.dispatcher(
ws, running, request.match_info
)
print("Create worker...") print("Create worker...")
worker_task = tg.create_task( worker_task = tg.create_task(