Created
March 11, 2024 18:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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