github-bot / server.py
robinroy03's picture
reqs added and server endpoint moved to /github
d1d2060
raw
history blame
1.74 kB
from flask import Flask, request
import requests
import json
import os
from jwt_setup import get_installation_access_token
app = Flask(__name__)
def get_llm_output(query: str):
LLM_output = requests.post('https://robinroy03-fury-engine.hf.space/api/google/generate', json={"query": query, "llm": "gemini-1.5-pro", "knn": 3, "stream": False})
LLM_output = json.loads(LLM_output.text)
LLM_output = LLM_output['response'] + '\n\n**References**\n' + LLM_output['references']
return LLM_output
def post_discussion_comment(discussion_id: str, body: str):
token = get_installation_access_token(
os.environ.get('CHATBOT_PUB_KEY'),
os.environ.get('APP_ID'),
os.environ.get('INSTALLATION_ID')
)
headers = {"Authorization": f"token {token}"}
query = "mutation AddDiscussionComment($discussionId: ID!, $body: String!) {addDiscussionComment(input: {discussionId: $discussionId, body: $body}) {clientMutationId}}"
variables = {"discussionId": discussion_id, "body": body}
requests.post('https://api.github.com/graphql', json={'query': query, 'variables': variables}, headers=headers)
@app.route("/github", methods=['POST'])
def main():
payload = request.get_json()
URL = "https://api.github.com/graphql"
if 'discussion' in payload and 'comment' not in payload:
discussion_id = payload['discussion']['node_id']
discussion_title = payload['discussion']['title']
discussion_body = payload['discussion']['body']
print(discussion_id)
post_discussion_comment(discussion_id=discussion_id, body=get_llm_output(f"{discussion_title}\n{discussion_body}"))
return {"status": True}
else:
return {"status": False}