Skip to content

Instantly share code, notes, and snippets.

@smellslikeml
Created March 11, 2024 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smellslikeml/1bca140c643383a918e5b5610a8d2728 to your computer and use it in GitHub Desktop.
Save smellslikeml/1bca140c643383a918e5b5610a8d2728 to your computer and use it in GitHub Desktop.
Uses NATS to aggregate responses from workers publishing LLM inference to the subject inference.requests
import asyncio
import nats
import uuid
async def aggregate_inferences(nats_url, request_subject, data, timeout=10):
nc = await nats.connect(nats_url)
responses = []
async def response_handler(msg):
responses.append(msg.data.decode())
inbox = f"_INBOX.{uuid.uuid4()}"
subscription = await nc.subscribe(inbox, cb=response_handler)
await nc.publish(request_subject, data.encode(), reply=inbox)
await asyncio.sleep(timeout)
await subscription.drain()
await nc.close()
return responses
if __name__ == "__main__":
prompt = "Let's discuss the best way to run machine learning on the edge"
results = asyncio.run(
aggregate_inferences("nats://localhost:4222", "inference.requests", prompt)
)
print("Aggregated Results:", results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment