-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented rag producer and consumer
- Loading branch information
root
committed
Jan 9, 2024
1 parent
011751b
commit eebb3b5
Showing
4 changed files
with
100 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
.env | ||
venv/ | ||
__pycache__ | ||
.venv/ | ||
kafkaconsumer.py | ||
kafkaproducer.py | ||
**/__pycache__ |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,80 @@ | ||
def main(): | ||
print("I am in RAG") | ||
from confluent_kafka import Consumer, KafkaException, Producer | ||
import json | ||
import os | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
username = os.getenv('USERNAME') | ||
kafka_broker = os.getenv('KAFKA_BROKER') | ||
producerpassword = os.getenv('PRODUCERPASSWORD') | ||
consumerpassword = os.getenv('CONSUMERPASSWORD') | ||
|
||
consumer_conf = { | ||
'bootstrap.servers': kafka_broker, # replace with your EventHub namespace | ||
'security.protocol': 'SASL_SSL', | ||
'sasl.mechanism': 'PLAIN', | ||
'sasl.username': username, | ||
'sasl.password': consumerpassword, # replace with your EventHub connection string | ||
'group.id': 'python-example-consumer', | ||
'auto.offset.reset': 'earliest' | ||
} | ||
|
||
producer_conf = { | ||
'bootstrap.servers': kafka_broker, # replace with your EventHub namespace | ||
'security.protocol': 'SASL_SSL', | ||
'sasl.mechanism': 'PLAIN', | ||
'sasl.username': username, | ||
'sasl.password': producerpassword, # replace with your EventHub connection string | ||
# 'group.id': 'python-example-consumer', | ||
# 'auto.offset.reset': 'earliest' | ||
} | ||
|
||
producer = Producer(producer_conf) | ||
|
||
consumer = Consumer(consumer_conf) | ||
consumer.subscribe(['topic3']) | ||
|
||
def process_message(msg): | ||
return {"data":msg, "message": "result from RAG", "type":"rag"} | ||
|
||
def produce_message(msg): | ||
producer.produce('topic1', msg) | ||
producer.flush() | ||
|
||
|
||
def listen_and_produce(): | ||
try: | ||
while True: | ||
# Poll for a message | ||
msg = consumer.poll(1.0) | ||
|
||
if msg is not None: | ||
if not msg.error(): | ||
event = json.loads(msg.value().decode('utf-8')) | ||
# Print the event type | ||
print("Recieved Event:", event) | ||
|
||
result = process_message(event) | ||
print("Result:", result) | ||
|
||
producer.produce('topic1', json.dumps(result)) | ||
producer.flush() | ||
|
||
elif msg.error().code() != KafkaException._PARTITION_EOF: | ||
print(msg.error()) | ||
else: | ||
continue | ||
|
||
except KeyboardInterrupt: | ||
pass | ||
|
||
finally: | ||
# Close down consumer to commit final offsets. | ||
print('Stopping RAG consumer') | ||
consumer.close() | ||
|
||
if __name__ == '__main__': | ||
result = {"event":"rag", "message": "result from RAG", "data":"result"} | ||
produce_message(json.dumps(result)) | ||
print('Starting RAG Listening') | ||
listen_and_produce() |