add chroma chat

This commit is contained in:
ACBBZ
2024-05-07 02:17:46 +00:00
parent 6949c2988a
commit 95d6c3785c

52
src/blackbox/chroma_chat.py Executable file
View File

@ -0,0 +1,52 @@
from typing import Any, Coroutine
from fastapi import Request, Response, status
from fastapi.responses import JSONResponse
from .blackbox import Blackbox
from injector import singleton
@singleton
class ChromaChat(Blackbox):
def __init__(self, fastchat, chroma_query):
self.fastchat = fastchat
self.chroma_query = chroma_query
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def valid(self, *args, **kwargs) -> bool:
data = args[0]
return isinstance(data, list)
def processing(self, question, context) -> str:
# load or create collection
if context is None:
collection_id = "123"
else:
collection_id = context["collections"][0]
# query it
chroma_result = self.chroma_query(question, collection_id)
fast_question = "问题: "+ question + "。根据问题,总结以下内容:" + chroma_result
response = self.fastchat(fast_question)
return response
async def fast_api_handler(self, request: Request) -> Response:
try:
data = await request.json()
except:
return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST)
user_question = data.get("question")
user_context = data.get("context")
if user_question is None:
return JSONResponse(content={"error": "question is required"}, status_code=status.HTTP_400_BAD_REQUEST)
return JSONResponse(
content={"response": self.processing(user_question, user_context)},
status_code=status.HTTP_200_OK)