Working with API tests

This commit is contained in:
Cyber MacGeddon 2024-12-06 18:55:37 +00:00
parent f82d96c3e1
commit 8fbf3cc07d
3 changed files with 16 additions and 20 deletions

View file

@ -24,16 +24,26 @@ class AgentRequestor(MultiResponseServiceRequestor):
) )
def from_response(self, message): def from_response(self, message):
resp = {
}
if message.answer: if message.answer:
return { "answer": message.answer }, True resp["answer"] = message.answer
else:
return {}, False if message.thought:
resp["thought"] = message.thought
if message.observation:
resp["observation"] = message.observation
# The 2nd boolean expression indicates whether we're done responding
return resp, (message.answer is not None)
class AgentEndpoint(MultiResponseServiceEndpoint): class AgentEndpoint(MultiResponseServiceEndpoint):
def __init__(self, pulsar_host, timeout, auth): def __init__(self, pulsar_host, timeout, auth):
super(AgentEndpoint, self).__init__( super(AgentEndpoint, self).__init__(
endpoint_path="/api/v1/text-completion", endpoint_path="/api/v1/agent",
auth=auth, auth=auth,
requestor = AgentRequestor( requestor = AgentRequestor(
pulsar_host=pulsar_host, timeout=timeout, auth=auth pulsar_host=pulsar_host, timeout=timeout, auth=auth

View file

@ -68,20 +68,13 @@ class MultiResponseServiceEndpoint(ServiceEndpoint):
async def handle(self, request): async def handle(self, request):
id = str(uuid.uuid4())
try: try:
data = await request.json() data = await request.json()
print(data)
q = self.sub.subscribe(id)
await asyncio.to_thread(
self.pub.send, id, self.to_request(data)
)
def responder(x): def responder(x):
print("Resp:", x) print(x)
resp = await self.requestor.process(data, responder) resp = await self.requestor.process(data, responder)
@ -94,5 +87,3 @@ class MultiResponseServiceEndpoint(ServiceEndpoint):
{ "error": str(e) } { "error": str(e) }
) )
finally:
self.sub.unsubscribe(id)

View file

@ -48,25 +48,20 @@ class ServiceRequestor:
async def process(self, request): async def process(self, request):
id = str(uuid.uuid4()) id = str(uuid.uuid4())
print(id)
try: try:
q = self.sub.subscribe(id) q = self.sub.subscribe(id)
print("Pub...")
await asyncio.to_thread( await asyncio.to_thread(
self.pub.send, id, self.to_request(request) self.pub.send, id, self.to_request(request)
) )
print("Pubd")
try: try:
resp = await asyncio.to_thread(q.get, timeout=self.timeout) resp = await asyncio.to_thread(q.get, timeout=self.timeout)
except Exception as e: except Exception as e:
raise RuntimeError("Timeout") raise RuntimeError("Timeout")
print("Recvd...")
if resp.error: if resp.error:
return { "error": resp.error.message } return { "error": resp.error.message }