internet_ml/web/backend/api/nlp/nocontext/views.py

52 lines
1.3 KiB
Python
Raw Normal View History

2023-01-11 15:59:46 +00:00
# type: ignore
2022-12-30 06:50:36 +00:00
import os
2022-12-28 16:37:10 +00:00
2023-01-01 13:01:12 +00:00
import dotenv
2022-12-28 16:37:10 +00:00
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
2022-12-30 06:50:36 +00:00
import internet_ml.NLP.no_context.QA
2022-12-28 10:41:27 +00:00
2023-01-01 13:01:12 +00:00
dotenv_path = os.path.join(os.path.dirname(__file__), ".env")
dotenv.load_dotenv(dotenv_path)
2022-12-28 10:41:27 +00:00
2022-12-28 16:37:10 +00:00
class QAView(APIView):
2023-01-01 13:01:12 +00:00
def post(self, request):
2022-12-28 16:37:10 +00:00
"""
{"question": "Who is Elon Musk?"}
{
"error": "",
"response": {
'score': VAL,
'start': VAL,
'end': VAL,
'answer': 'THE_ANSWER'
},
"resources": [
'SOME_LINKS_HERE'
]
}
or
{
"error": "",
"status": "",
"detail": "",
}
so check error if it exists first and then for other stuff
"""
2022-12-30 06:50:36 +00:00
answer = internet_ml.NLP.no_context.QA.answer(
2023-01-01 13:01:12 +00:00
str(request.data["question"]),
2023-01-10 12:50:43 +00:00
str(os.environ.get("GOOGLE_SEARCH_API_KEY")),
str(os.environ.get("GOOGLE_SEARCH_ENGINE_ID")),
str(os.environ.get("OPENAI_API_KEY")),
2022-12-30 06:50:36 +00:00
)
2023-01-01 13:01:12 +00:00
content = {
"error": "",
"question": str(request.data["question"]),
"response": answer[0],
"resources": answer[1],
}
return Response(content)