mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-17 01:01:03 +02:00
Ported tg-invoke-agent to websockets
This commit is contained in:
parent
d7299aeba7
commit
6343072b7b
2 changed files with 67 additions and 27 deletions
|
|
@ -1,16 +1,18 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Uses the GraphRAG service to answer a query
|
Uses the GraphRAG service to answer a question
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import textwrap
|
import textwrap
|
||||||
|
import uuid
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
from websockets.asyncio.client import connect
|
||||||
|
|
||||||
from trustgraph.clients.agent_client import AgentClient
|
default_url = os.getenv("TRUSTGRAPH_URL", 'ws://localhost:8088/')
|
||||||
|
|
||||||
default_pulsar_host = os.getenv("PULSAR_HOST", 'pulsar://localhost:6650')
|
|
||||||
default_user = 'trustgraph'
|
default_user = 'trustgraph'
|
||||||
default_collection = 'default'
|
default_collection = 'default'
|
||||||
|
|
||||||
|
|
@ -27,15 +29,18 @@ def output(text, prefix="> ", width=78):
|
||||||
)
|
)
|
||||||
print(out)
|
print(out)
|
||||||
|
|
||||||
def query(
|
async def question(
|
||||||
pulsar_host, query, user, collection,
|
url, question, user, collection,
|
||||||
plan=None, state=None, verbose=False
|
plan=None, state=None, verbose=False
|
||||||
):
|
):
|
||||||
|
|
||||||
am = AgentClient(pulsar_host=pulsar_host)
|
if not url.endswith("/"):
|
||||||
|
url += "/"
|
||||||
|
|
||||||
|
url = url + "api/v1/socket"
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
output(wrap(query), "\U00002753 ")
|
output(wrap(question), "\U00002753 ")
|
||||||
print()
|
print()
|
||||||
|
|
||||||
def think(x):
|
def think(x):
|
||||||
|
|
@ -48,11 +53,43 @@ def query(
|
||||||
output(wrap(x), "\U0001f4a1 ")
|
output(wrap(x), "\U0001f4a1 ")
|
||||||
print()
|
print()
|
||||||
|
|
||||||
resp = am.request(
|
mid = str(uuid.uuid4())
|
||||||
question=query, think=think, observe=observe,
|
|
||||||
)
|
|
||||||
|
|
||||||
print(resp)
|
async with connect(url) as ws:
|
||||||
|
|
||||||
|
req = json.dumps({
|
||||||
|
"id": mid,
|
||||||
|
"service": "agent",
|
||||||
|
"request": {
|
||||||
|
"question": question,
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
await ws.send(req)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
msg = await ws.recv()
|
||||||
|
|
||||||
|
obj = json.loads(msg)
|
||||||
|
|
||||||
|
if obj["id"] != mid:
|
||||||
|
print("Ignore message")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if "thought" in obj["response"]:
|
||||||
|
think(obj["response"]["thought"])
|
||||||
|
|
||||||
|
if "observation" in obj["response"]:
|
||||||
|
observe(obj["response"]["observation"])
|
||||||
|
|
||||||
|
if "answer" in obj["response"]:
|
||||||
|
print(obj["response"]["answer"])
|
||||||
|
|
||||||
|
if obj["complete"]: break
|
||||||
|
|
||||||
|
await ws.close()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
|
|
@ -62,25 +99,25 @@ def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-p', '--pulsar-host',
|
'-u', '--url',
|
||||||
default=default_pulsar_host,
|
default=default_url,
|
||||||
help=f'Pulsar host (default: {default_pulsar_host})',
|
help=f'API URL (default: {default_url})',
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-q', '--query',
|
'-q', '--question',
|
||||||
required=True,
|
required=True,
|
||||||
help=f'Query to execute',
|
help=f'Question to answer',
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-u', '--user',
|
'-U', '--user',
|
||||||
default=default_user,
|
default=default_user,
|
||||||
help=f'User ID (default: {default_user})'
|
help=f'User ID (default: {default_user})'
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-c', '--collection',
|
'-C', '--collection',
|
||||||
default=default_collection,
|
default=default_collection,
|
||||||
help=f'Collection ID (default: {default_collection})'
|
help=f'Collection ID (default: {default_collection})'
|
||||||
)
|
)
|
||||||
|
|
@ -105,14 +142,16 @@ def main():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
query(
|
asyncio.run(
|
||||||
pulsar_host=args.pulsar_host,
|
question(
|
||||||
query=args.query,
|
url=args.url,
|
||||||
user=args.user,
|
question=args.question,
|
||||||
collection=args.collection,
|
user=args.user,
|
||||||
plan=args.plan,
|
collection=args.collection,
|
||||||
state=args.state,
|
plan=args.plan,
|
||||||
verbose=args.verbose,
|
state=args.state,
|
||||||
|
verbose=args.verbose,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ setuptools.setup(
|
||||||
"rdflib",
|
"rdflib",
|
||||||
"tabulate",
|
"tabulate",
|
||||||
"msgpack",
|
"msgpack",
|
||||||
|
"websockets",
|
||||||
],
|
],
|
||||||
scripts=[
|
scripts=[
|
||||||
"scripts/tg-graph-show",
|
"scripts/tg-graph-show",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue