mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-17 09:11:03 +02:00
Working with API tests
This commit is contained in:
parent
f82d96c3e1
commit
8fbf3cc07d
3 changed files with 16 additions and 20 deletions
|
|
@ -24,16 +24,26 @@ class AgentRequestor(MultiResponseServiceRequestor):
|
|||
)
|
||||
|
||||
def from_response(self, message):
|
||||
resp = {
|
||||
}
|
||||
|
||||
if message.answer:
|
||||
return { "answer": message.answer }, True
|
||||
else:
|
||||
return {}, False
|
||||
resp["answer"] = message.answer
|
||||
|
||||
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):
|
||||
def __init__(self, pulsar_host, timeout, auth):
|
||||
|
||||
super(AgentEndpoint, self).__init__(
|
||||
endpoint_path="/api/v1/text-completion",
|
||||
endpoint_path="/api/v1/agent",
|
||||
auth=auth,
|
||||
requestor = AgentRequestor(
|
||||
pulsar_host=pulsar_host, timeout=timeout, auth=auth
|
||||
|
|
|
|||
|
|
@ -68,20 +68,13 @@ class MultiResponseServiceEndpoint(ServiceEndpoint):
|
|||
|
||||
async def handle(self, request):
|
||||
|
||||
id = str(uuid.uuid4())
|
||||
|
||||
try:
|
||||
|
||||
data = await request.json()
|
||||
|
||||
q = self.sub.subscribe(id)
|
||||
|
||||
await asyncio.to_thread(
|
||||
self.pub.send, id, self.to_request(data)
|
||||
)
|
||||
print(data)
|
||||
|
||||
def responder(x):
|
||||
print("Resp:", x)
|
||||
print(x)
|
||||
|
||||
resp = await self.requestor.process(data, responder)
|
||||
|
||||
|
|
@ -94,5 +87,3 @@ class MultiResponseServiceEndpoint(ServiceEndpoint):
|
|||
{ "error": str(e) }
|
||||
)
|
||||
|
||||
finally:
|
||||
self.sub.unsubscribe(id)
|
||||
|
|
|
|||
|
|
@ -48,25 +48,20 @@ class ServiceRequestor:
|
|||
async def process(self, request):
|
||||
|
||||
id = str(uuid.uuid4())
|
||||
print(id)
|
||||
|
||||
try:
|
||||
|
||||
q = self.sub.subscribe(id)
|
||||
|
||||
print("Pub...")
|
||||
await asyncio.to_thread(
|
||||
self.pub.send, id, self.to_request(request)
|
||||
)
|
||||
|
||||
print("Pubd")
|
||||
try:
|
||||
resp = await asyncio.to_thread(q.get, timeout=self.timeout)
|
||||
except Exception as e:
|
||||
raise RuntimeError("Timeout")
|
||||
|
||||
print("Recvd...")
|
||||
|
||||
if resp.error:
|
||||
return { "error": resp.error.message }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue