mirror of
https://github.com/aljazceru/sciencestr.git
synced 2025-12-17 05:54:24 +01:00
proof of concept - horrible code
This commit is contained in:
1
example_settings.py
Normal file
1
example_settings.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
NOSTR_KEY="<hex priv key"
|
||||||
60
science-bot.py
Normal file
60
science-bot.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
from nostr_sdk import Keys, Client, Event, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt, SecretKey, init_logger, LogLevel
|
||||||
|
import time
|
||||||
|
import requests
|
||||||
|
from settings import *
|
||||||
|
|
||||||
|
|
||||||
|
init_logger(LogLevel.DEBUG)
|
||||||
|
|
||||||
|
sk = SecretKey.from_hex(NOSTR_KEY)
|
||||||
|
keys = Keys(sk)
|
||||||
|
sk = keys.secret_key()
|
||||||
|
pk = keys.public_key()
|
||||||
|
print(f"Bot public key: {pk.to_bech32()}")
|
||||||
|
|
||||||
|
client = Client(keys)
|
||||||
|
|
||||||
|
client.add_relay("wss://relay.damus.io")
|
||||||
|
client.add_relay("wss://nostr.mom")
|
||||||
|
client.add_relay("wss://nostr.oxtr.dev")
|
||||||
|
client.connect()
|
||||||
|
|
||||||
|
filter = Filter().pubkey(pk).kind(4).since(Timestamp.now())
|
||||||
|
client.subscribe([filter])
|
||||||
|
|
||||||
|
# function to format results into title and url
|
||||||
|
|
||||||
|
def format_answer(response):
|
||||||
|
# Extract the answer
|
||||||
|
formatted_response = "Short answer:\n" + response["short_answer"] + "\n\nExcerpts from articles:\n" + response["chunks"]
|
||||||
|
return formatted_response
|
||||||
|
|
||||||
|
class NotificationHandler(HandleNotification):
|
||||||
|
def handle(self, relay_url, event):
|
||||||
|
print(f"Received new event from {relay_url}: {event.as_json()}")
|
||||||
|
if event.kind() == 4:
|
||||||
|
print("Decrypting event")
|
||||||
|
try:
|
||||||
|
msg = nip04_decrypt(sk, event.pubkey(), event.content())
|
||||||
|
print(f"Received new msg: {msg}")
|
||||||
|
params = {'query': str(msg)}
|
||||||
|
try:
|
||||||
|
resp = requests.get('http://127.0.0.1:6000/ask', params=params)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error during request: {e}")
|
||||||
|
if resp.status_code == 200:
|
||||||
|
print("Sending answer")
|
||||||
|
event = EventBuilder.new_encrypted_direct_msg(keys, event.pubkey(), f"{resp.text}", event.id()).to_event(keys)
|
||||||
|
client.send_event(event)
|
||||||
|
else:
|
||||||
|
print(f"Error during content decryption: {resp.text}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error during content decryption: {e}")
|
||||||
|
|
||||||
|
def handle_msg(self, relay_url, msg):
|
||||||
|
None
|
||||||
|
|
||||||
|
client.handle_notifications(NotificationHandler())
|
||||||
|
|
||||||
|
while True:
|
||||||
|
time.sleep(5.0)
|
||||||
43
science.py
Normal file
43
science.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
from cybrex.cybrex_ai import CybrexAI
|
||||||
|
from quart import Quart, request, jsonify
|
||||||
|
|
||||||
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
app = Quart(__name__)
|
||||||
|
|
||||||
|
@app.route('/ask')
|
||||||
|
async def ask():
|
||||||
|
cybrex = CybrexAI()
|
||||||
|
query = request.args.get('query')
|
||||||
|
if not query:
|
||||||
|
return {'error': 'Please provide a query parameter'}, 400
|
||||||
|
|
||||||
|
# Only await if cybrex.start() needs to be called for each request
|
||||||
|
# Otherwise, consider initializing cybrex outside the route
|
||||||
|
await cybrex.start()
|
||||||
|
|
||||||
|
# Assuming chat_science is an asynchronous method, it should be awaited
|
||||||
|
answer = await cybrex.chat_science(query=query, n_chunks=4, n_documents=10)
|
||||||
|
|
||||||
|
payload = format_cybrex_response(answer)
|
||||||
|
return payload
|
||||||
|
|
||||||
|
def format_cybrex_response(answer):
|
||||||
|
# Extract the answer
|
||||||
|
chunks = answer.chunks
|
||||||
|
reply = answer.answer
|
||||||
|
formatted_response = "Short answer:\n" + reply + "\n\nExcerpts from articles:\n"
|
||||||
|
|
||||||
|
# Extract and format each chunk
|
||||||
|
for chunk in chunks:
|
||||||
|
formatted_response += "\n" + chunk.title + "\n" + chunk.text + "\n"
|
||||||
|
|
||||||
|
return formatted_response
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(debug=True, port=6000)
|
||||||
Reference in New Issue
Block a user